Skip to content

Commit

Permalink
Merge pull request #187 from Cerebrovinny/documentation-improvements-…
Browse files Browse the repository at this point in the history
…json_ld_lcia

Update docstrings to NumPy standard `(strategies/json_ld_lcia)`
  • Loading branch information
michaelweinold committed Jul 19, 2023
2 parents 995c2ec + e9faf55 commit 815eca7
Showing 1 changed file with 182 additions and 0 deletions.
182 changes: 182 additions & 0 deletions bw2io/strategies/json_ld_lcia.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,74 @@
def json_ld_lcia_add_method_metadata(data):
"""
Add metadata of the Life Cycle Impact Assessment (LCIA) method to the corresponding impact categories.
Iterates over the LCIA methods and adds metadata from the method to each of its impact
categories. The metadata includes the method's name, description, version, and lastChange.
Parameters
----------
data : dict
A dictionary containing LCIA methods and their impact categories.
Returns
-------
dict
A dictionary with the updated LCIA impact categories containing the parent method metadata.
Examples
--------
>>> data = {
... "lcia_methods": {
... "method_1": {
... "name": "LCIA Method 1",
... "description": "Sample LCIA Method 1",
... "version": "1.0",
... "lastChange": "2021-01-01",
... "impactCategories": [
... {"@id": "category_1"},
... {"@id": "category_2"},
... ],
... },
... },
... "lcia_categories": {
... "category_1": {},
... "category_2": {},
... },
... }
>>> json_ld_lcia_add_method_metadata(data)
{
'lcia_methods': {
'method_1': {
'name': 'LCIA Method 1',
'description': 'Sample LCIA Method 1',
'version': '1.0',
'lastChange': '2021-01-01',
'impactCategories': [
{'@id': 'category_1'},
{'@id': 'category_2'},
],
},
},
'lcia_categories': {
'category_1': {
'parent': {
'name': 'LCIA Method 1',
'description': 'Sample LCIA Method 1',
'version': '1.0',
'lastChange': '2021-01-01',
},
},
'category_2': {
'parent': {
'name': 'LCIA Method 1',
'description': 'Sample LCIA Method 1',
'version': '1.0',
'lastChange': '2021-01-01',
},
},
},
}
"""
for key, value in data["lcia_methods"].items():
for category in value["impactCategories"]:
obj = data["lcia_categories"][category["@id"]]
Expand All @@ -9,6 +79,52 @@ def json_ld_lcia_add_method_metadata(data):


def json_ld_lcia_set_method_metadata(data):
"""
Update the metadata of Life Cycle Impact Assessment (LCIA) methods in the given data.
Processes the metadata of the LCIA methods in the given data, removing unnecessary fields,
renaming fields, setting units, and updating the name and description.
Parameters
----------
data : list
A list of dictionaries representing LCIA methods with metadata.
Returns
-------
list
A list of dictionaries representing the updated LCIA methods with modified metadata.
Examples
--------
>>> data = [
... {
... "@context": "http://www.example.com",
... "@type": "LCIA",
... "referenceUnitName": "kg",
... "@id": "method_1",
... "name": "LCIA Method 1",
... "description": "Sample LCIA Method 1",
... "parent": {
... "name": "Parent Method",
... "description": "Sample parent method",
... },
... }
... ]
>>> json_ld_lcia_set_method_metadata(data)
[
{
'unit': 'kg',
'id': 'method_1',
'name': ('Parent Method', 'LCIA Method 1'),
'description': 'Sample LCIA Method 1\nSample parent method',
'parent': {
'name': 'Parent Method',
'description': 'Sample parent method',
},
}
]
"""
TO_DELETE = ("@context", "@type")
for method in data:
for field in TO_DELETE:
Expand All @@ -30,10 +146,76 @@ def json_ld_lcia_set_method_metadata(data):


def json_ld_lcia_convert_to_list(data):
"""
Convert the Life Cycle Impact Assessment (LCIA) categories in the given data to a list.
Takes the LCIA categories from the input data dictionary and returns them as a list.
Parameters
----------
data : dict
A dictionary containing the LCIA categories with their respective keys.
Returns
-------
list
A list of dictionaries representing the LCIA categories.
Examples
--------
>>> data = {
... "lcia_categories": {
... "category_1": {"name": "LCIA Category 1"},
... "category_2": {"name": "LCIA Category 2"},
... }
... }
>>> json_ld_lcia_convert_to_list(data)
[{'name': 'LCIA Category 1'}, {'name': 'LCIA Category 2'}]
"""
return data["lcia_categories"].values()


def json_ld_lcia_reformat_cfs_as_exchanges(data):
"""
Reformat the impact factors of Life Cycle Impact Assessment (LCIA) methods as exchanges.
Modifies the given LCIA methods data by renaming the 'impactFactors' field to 'exchanges' and
updating the fields within each exchange.
Parameters
----------
data : list
A list of dictionaries representing LCIA methods with impact factors.
Returns
-------
list
A list of dictionaries representing the updated LCIA methods with reformatted exchanges.
Examples
--------
>>> data = [
... {
... "impactFactors": [
... {
... "value": 1.0,
... "unit": {"name": "kg"},
... }
... ],
... }
... ]
>>> json_ld_lcia_reformat_cfs_as_exchanges(data)
[
{
'exchanges': [
{
'amount': 1.0,
'unit': 'kg',
}
],
}
]
"""
for method in data:
method["exchanges"] = method.pop("impactFactors")
for exc in method["exchanges"]:
Expand Down

0 comments on commit 815eca7

Please sign in to comment.