Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update docstrings to NumPy standard (importers) #166

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions bw2io/importers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,27 @@
from .exiobase3_monetary import Exiobase3MonetaryImporter
from .simapro_csv import SimaProCSVImporter
from .simapro_lcia_csv import SimaProLCIACSVImporter

"""
This module provides classes for importing Life Cycle Impact Assessment (LCIA) data
from different file formats. The following importers are provided:

- EcoinventLCIAImporter
michaelweinold marked this conversation as resolved.
Show resolved Hide resolved
- MultiOutputEcospold1Importer
- NoIntegerCodesEcospold1Importer
- SingleOutputEcospold1Importer
- Ecospold1LCIAImporter
- SingleOutputEcospold2Importer
- Ecospold2BiosphereImporter
- CSVImporter
- ExcelImporter
- CSVLCIAImporter
- ExcelLCIAImporter
- Exiobase3HybridImporter
- Exiobase3MonetaryImporter
- SimaProCSVImporter
- SimaProLCIACSVImporter

"""

# Import classes for LCIA data import
30 changes: 29 additions & 1 deletion bw2io/importers/ecospold1_lcia.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,41 @@
from time import time

from ..extractors import Ecospold1LCIAExtractor
from .base_lcia import LCIAImporter


class Ecospold1LCIAImporter(LCIAImporter):
"""
Importer for Ecospold1 LCIA format.

Parameters
----------
filepath: str
Path to the Ecospold1 LCIA file.
biosphere: bw2data.BiosphereDatabase, optional
Biosphere database to use. If None, the default biosphere database will be used.

Attributes
----------
format : str
The format of the LCIA data, which is "Ecospold1 LCIA".
data : dict
The LCIA data extracted from the Ecospold1 LCIA file.

"""
format = "Ecospold1 LCIA"

def __init__(self, filepath, biosphere=None):
"""
Initialize the Ecospold1LCIAImporter instance.

Parameters
----------
filepath : str
Path to the Ecospold1 LCIA file.
biosphere : bw2data.BiosphereDatabase, optional
Biosphere database to use. If None, the default biosphere database will be used.

"""
super(Ecospold1LCIAImporter, self).__init__(filepath, biosphere)
start = time()
self.data = Ecospold1LCIAExtractor.extract(filepath)
Expand Down
44 changes: 44 additions & 0 deletions bw2io/importers/ecospold2_biosphere.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
)
from .base_lci import LCIImporter


EMISSIONS_CATEGORIES = {
"air": "emission",
"soil": "emission",
Expand All @@ -19,9 +20,39 @@


class Ecospold2BiosphereImporter(LCIImporter):
'''Import elementary flows from ecoinvent xml format.

Parameters
----------
name : str, optional
Name of the database, by default "biosphere3".
version : str, optional
Version of the database, by default "3.9".

Attributes
----------
format : str
Format of the data: "Ecoinvent XML".
db_name : str
Name of the database.
data : list
Extracted data from the xml file.
strategies : list
List of functions to apply to the extracted data.
michaelweinold marked this conversation as resolved.
Show resolved Hide resolved
'''

format = "Ecoinvent XML"

def __init__(self, name="biosphere3", version="3.9"):
'''Initialize the importer.

Parameters
----------
name : str, optional
Name of the database, by default "biosphere3".
version : str, optional
Version of the database, by default "3.9".
'''
self.db_name = name
self.data = self.extract(version)
self.strategies = [
Expand All @@ -31,6 +62,19 @@ def __init__(self, name="biosphere3", version="3.9"):
]

def extract(self, version):
'''Extract elementary flows from the xml file.

Parameters
----------
version : str
Version of the database.

Returns
-------
list
Extracted data from the xml file.
'''

def extract_flow_data(o):
ds = {
"categories": (
Expand Down
27 changes: 26 additions & 1 deletion bw2io/importers/excel_lcia.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@


def as_dicts(obj):
"""Converts a 2D list to a list of dictionaries.

Args:
obj (list): The 2D list to be converted.

Returns:
list: The list of dictionaries.
"""
if len(obj) == 1:
obj = obj[0]
assert isinstance(obj[0], str)
Expand All @@ -30,12 +38,24 @@ class ExcelLCIAImporter(LCIAImporter):

See the `documentation <https://2.docs.brightway.dev/intro.html#importing-lcia-methods-from-the-standard-excel-template>`__.
michaelweinold marked this conversation as resolved.
Show resolved Hide resolved

Attributes:
format (str): The file format.
extractor (class): The file extractor class.
"""

format = "Excel"
extractor = ExcelExtractor

def __init__(self, filepath, name, description, unit, **metadata):
"""Initializes the ExcelLCIAImporter object.

Args:
filepath (str): The path to the Excel file.
name (tuple): The name of the LCIA method.
description (str): The description of the LCIA method.
unit (str): The unit of the LCIA method.
**metadata: The metadata associated with the LCIA method.
"""
assert isinstance(name, tuple)
self.strategies = [
csv_restore_tuples,
Expand Down Expand Up @@ -66,7 +86,12 @@ def __init__(self, filepath, name, description, unit, **metadata):


class CSVLCIAImporter(ExcelLCIAImporter):
"""Generic CSV LCIA importer"""
"""Generic CSV LCIA importer.

Attributes:
format (str): The file format.
michaelweinold marked this conversation as resolved.
Show resolved Hide resolved
extractor (class): The file extractor class.
"""

format = "CSV"
extractor = CSVExtractor
52 changes: 51 additions & 1 deletion bw2io/importers/json_ld_lcia.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,49 @@


class JSONLDLCIAImporter(LCIAImporter):
"""Importer for the `OLCD JSON-LD LCIA data format <https://github.com/GreenDelta/olca-schema>`__."""
"""
Importer for the `OLCD JSON-LD LCIA data format <https://github.com/GreenDelta/olca-schema>`__.

Parameters
----------
dirpath: str
Directory path for the JSON-LD data.

Attributes
----------
format: str
Data format description.
extractor: :class:`JSONLDExtractor`
Extractor class for the JSON-LD data.
data: dict
Extracted LCIA data.
strategies: list
List of strategies to apply to the LCIA data.

Methods
-------
match_biosphere_by_id(database_name)
Matches biosphere flows to a specified database by ID.

"""

format = "OLCA JSON-LD"
extractor = JSONLDExtractor

def __init__(self, dirpath):
"""
Initialize the JSONLDLCIAImporter object.

Parameters
----------
dirpath: str
Directory path for the JSON-LD data.

Returns
-------
None

"""
self.data = self.extractor.extract(dirpath)
KEEP = ("lcia_categories", "lcia_methods", "flows")
for key in list(self.data.keys()):
Expand All @@ -33,6 +70,19 @@ def __init__(self, dirpath):
]

def match_biosphere_by_id(self, database_name):
"""
Matches biosphere flows to a specified database by ID.

Parameters
----------
database_name: str
Name of the biosphere database.

Returns
-------
None

"""
assert database_name in databases

codes = {o["code"] for o in Database(database_name)}
Expand Down
31 changes: 30 additions & 1 deletion bw2io/importers/simapro_lcia_csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,37 @@
)
from .base_lcia import LCIAImporter


class SimaProLCIACSVImporter(LCIAImporter):
"""Importer for SimaPro CSV LCIA data format.

Parameters
----------
filepath : str
Path to the SimaPro CSV LCIA file.
biosphere : str, optional
Name of the biosphere database to use. Default is None, which uses the current
project's default biosphere.
delimiter : str, optional
Delimiter used in the CSV file. Default is ';'.
encoding : str, optional
Character encoding used in the CSV file. Default is 'latin-1'.
normalize_biosphere : bool, optional
Whether to normalize biosphere flows using the included strategies.
Default is True.

Notes
-----
This importer extracts SimaPro CSV LCIA data using the
michaelweinold marked this conversation as resolved.
Show resolved Hide resolved
``SimaProLCIACSVExtractor`` extractor.

If ``normalize_biosphere=True``, the following strategies are applied:

* ``normalize_units``
* ``set_biosphere_type``
* ``normalize_simapro_biosphere_categories``
* ``normalize_simapro_biosphere_names``

"""
michaelweinold marked this conversation as resolved.
Show resolved Hide resolved
format = u"SimaPro CSV LCIA"

def __init__(
Expand Down