Skip to content

Commit

Permalink
Merge pull request #166 from ShanikaNishadhi/swdocumentation_improvem…
Browse files Browse the repository at this point in the history
…ents

Update docstrings to NumPy standard
  • Loading branch information
michaelweinold authored Mar 19, 2023
2 parents 001b427 + a3d8ef4 commit 551431e
Show file tree
Hide file tree
Showing 6 changed files with 191 additions and 4 deletions.
8 changes: 8 additions & 0 deletions bw2io/importers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,11 @@
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.
"""

# 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
48 changes: 48 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,43 @@


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.
See Also
--------
https://github.com/brightway-lca/brightway2-io/tree/main/bw2io/strategies
'''

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 +66,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>`__.
Attributes:
format (str): The file format. The default format is CSV.
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.
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
30 changes: 29 additions & 1 deletion bw2io/importers/simapro_lcia_csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,36 @@
)
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.
If ``normalize_biosphere=True``, the following strategies are applied:
* ``normalize_units``
* ``set_biosphere_type``
* ``normalize_simapro_biosphere_categories``
* ``normalize_simapro_biosphere_names``
"""
format = u"SimaPro CSV LCIA"

def __init__(
Expand Down

0 comments on commit 551431e

Please sign in to comment.