bw2io.strategies.simapro#
Attributes#
Functions#
Change datasets with the string "electricity" in their name from units of MJ to kilowatt hour. |
|
|
Replace SimaPro 'iff' formula with a Python equivalent 'if-else' expression. |
Change water flows with location information to generic water flows. |
|
Fix datasets with a single production exchange and zero allocation factors. |
|
|
Flip the sign on waste exchanges in the imported database based on the waste convention. |
|
Determine if an exchange is functional by looking at type and functional attributes. |
|
Link technosphere exchanges based on name, unit, and location. |
Normalize biosphere categories in a dataset to the ecoinvent standard. |
|
Normalize biosphere flow names in a dataset to the ecoinvent standard. |
|
|
Convert SimaPro formulae to Python expressions. |
Normalize unlinked exchange context and identifier labels to Brightway standards. |
|
Set process dataset name from the single functional exchange. |
|
|
If a biosphere flow is SimaPro-regionalized, like 'Ammonia, AR', and the process location is |
Ensure the 'loc' value is correct for lognormal uncertainty distributions in the given database. |
|
|
Set name, unit, production amount, and reference product from the functional exchange. |
Allocate products in a SimaPro dataset by creating a separate dataset for each product. |
|
Allocate products in a SimaPro dataset by creating a separate dataset for each product. |
|
|
Split a name like 'foo/CH U' into name and geo components in a dataset. |
|
Split a name like 'Wheat straw, at farm {NL} Energy, U' into name and geo components in a dataset. |
Module Contents#
- bw2io.strategies.simapro.change_electricity_unit_mj_to_kwh(db)[source]#
Change datasets with the string âelectricityâ in their name from units of MJ to kilowatt hour.
Iterates through a given database (list of datasets) and modifies the unit of exchanges containing the string âelectricityâ or âmarket for electricityâ in their name from âmegajouleâ (MJ) to âkilowatt hourâ (kWh). It also rescales the exchange accordingly.
- Parameters:
db (list) â A list of datasets containing exchanges with the unit âmegajouleâ (MJ).
- Returns:
A modified list of datasets with exchanges containing the string âelectricityâ or âmarket for electricityâ in their name updated to have the unit âkilowatt hourâ (kWh).
- Return type:
list
Examples
>>> db = [ { "exchanges": [ {"name": "Electricity", "unit": "megajoule", "amount": 3.6} ] } ] >>> change_electricity_unit_mj_to_kwh(db) [{'exchanges': [{'name': 'Electricity', 'unit': 'kilowatt hour', 'amount': 1.0}]}]
- bw2io.strategies.simapro.fix_iff_formula(string)[source]#
Replace SimaPro âiffâ formula with a Python equivalent âif-elseâ expression.
Processes a given string containing SimaPro âiffâ formulae and replaces them with Python equivalent âif-elseâ expressions. The conversion is done using regular expressions.
- Parameters:
string (str) â A string containing SimaPro âiffâ formulae.
- Returns:
string â A string with SimaPro âiffâ formulae replaced by Python âif-elseâ expressions.
- Return type:
str
Examples
>>> string = "iff(A > 0, A, 0)" >>> fix_iff_formula(string) "((A) if (A > 0) else (0))"
- bw2io.strategies.simapro.fix_localized_water_flows(db)[source]#
Change water flows with location information to generic water flows.
Biosphere flows cannot have locations; locations are defined by the activity dataset. Iterates through a given database (list of datasets) and modifies the name of exchanges containing water flows with location information by removing the location details.
- Parameters:
db (list) â A list of datasets containing exchanges with water flows including location information.
- Returns:
A modified list of datasets with exchanges containing water flows updated to have generic names, without location information.
- Return type:
list
Examples
>>> db = [ { "exchanges": [ {"name": "Water, river, BR", "type": "biosphere"} ] } ] >>> fix_localized_water_flows(db) [{'exchanges': [{'name': 'Water, river', 'type': 'biosphere', 'simapro location': 'BR'}]}]
- bw2io.strategies.simapro.fix_zero_allocation_products(db)[source]#
Fix datasets with a single production exchange and zero allocation factors.
For datasets with a single production exchange and zero allocation factors, sets the production amount to one and removes all inputs. This prevents the creation of a singular technosphere matrix.
- Parameters:
db (list) â A list of dictionaries representing datasets with production exchanges.
- Returns:
db â A list of dictionaries representing modified datasets with fixed zero allocation factors.
- Return type:
list
Examples
>>> db = [ ... { ... "name": "Dataset 1", ... "exchanges": [ ... {"type": "production", "name": "Product A", "unit": "kg", "amount": 0}, ... {"type": "input", "name": "Resource 1", "unit": "kg", "amount": 5}, ... ], ... } ... ] >>> fix_zero_allocation_products(db) [ { "name": "Dataset 1", "exchanges": [ {"type": "production", "name": "Product A", "unit": "kg", "amount": 1, "uncertainty type": 0}, ], }, ]
- bw2io.strategies.simapro.flip_sign_on_waste(db, other)[source]#
Flip the sign on waste exchanges in the imported database based on the waste convention.
Adjusts the sign of waste exchanges in the imported database to match the waste exchange convention in SimaPro.
- Parameters:
db (list) â A list of datasets containing waste exchanges to be adjusted.
other (str) â The name of the external database (e.g., ecoinvent) that is linked to the imported database.
- Returns:
A modified list of datasets with the sign of waste exchanges updated.
- Return type:
list
Notes
This strategy needs to be run after matching with ecoinvent. The strategy should be run as follows: sp_imported.apply_strategy(functools.partial(flip_sign_on_waste, other=âname_of_otherâ))
Examples
>>> db = [ { "exchanges": [ { "amount": -10, "input": ("key",), "uncertainty type": 0, "loc": -10 } ] } ] >>> other_db_name = "name_of_other" >>> flip_sign_on_waste(db, other_db_name) [{'exchanges': [{'amount': 10, 'input': ('key',), 'uncertainty type': 0, 'loc': 10}]}]
- bw2io.strategies.simapro.functional(exc: dict) bool [source]#
Determine if an exchange is functional by looking at type and functional attributes.
- bw2io.strategies.simapro.link_technosphere_based_on_name_unit_location(db, external_db_name=None)[source]#
Link technosphere exchanges based on name, unit, and location.
Links technosphere exchanges internally or against an external database based on their name, unit, and location. It doesnât use categories because categories cannot be reliably extracted from SimaPro exports.
- Parameters:
db (list) â A list of dictionaries representing datasets with technosphere exchanges.
external_db_name (str, optional) â The name of the external database to link against, by default None. If None, link technosphere exchanges internally within the given database.
- Returns:
db â A list of dictionaries representing modified datasets with linked technosphere exchanges.
- Return type:
list
Examples
>>> db = [ ... { ... "name": "Dataset 1", ... "exchanges": [ ... {"type": "technosphere", "name": "Product A", "unit": "kg", "location": "GLO"}, ... ], ... } ... ] >>> link_technosphere_based_on_name_unit_location(db) [ { "name": "Dataset 1", "exchanges": [ {"type": "technosphere", "name": "Product A", "unit": "kg", "location": "GLO"}, ], }, ]
- bw2io.strategies.simapro.normalize_simapro_biosphere_categories(db)[source]#
Normalize biosphere categories in a dataset to the ecoinvent standard.
Processes datasets and their exchanges by normalizing biosphere categories and subcategories to match the ecoinvent standard. It uses predefined mappings for SimaPro and ecoinvent categories.
- Parameters:
db (list) â A list of dictionaries representing datasets with biosphere exchanges.
- Returns:
db â A list of dictionaries representing modified datasets with normalized biosphere categories.
- Return type:
list
Examples
>>> db = [ ... { ... "exchanges": [ ... { ... "type": "biosphere", ... "categories": ["emission", "air"], ... }, ... ], ... } ... ] >>> normalize_simapro_biosphere_categories(db) [ { "exchanges": [ { "type": "biosphere", "categories": ("Emissions", "Air"), }, ], }, ]
- bw2io.strategies.simapro.normalize_simapro_biosphere_names(db)[source]#
Normalize biosphere flow names in a dataset to the ecoinvent standard.
Processes datasets and their exchanges by normalizing biosphere flow names to match the ecoinvent standard. It uses a predefined mapping for SimaPro and ecoinvent flow names.
- Parameters:
db (list) â A list of dictionaries representing datasets with biosphere exchanges.
- Returns:
db â A list of dictionaries representing modified datasets with normalized biosphere flow names.
- Return type:
list
Examples
>>> db = [ ... { ... "exchanges": [ ... { ... "type": "biosphere", ... "categories": ["Emissions", "Air"], ... "name": "Example emission", ... }, ... ], ... } ... ] >>> normalize_simapro_biosphere_names(db) [ { "exchanges": [ { "type": "biosphere", "categories": ["Emissions", "Air"], "name": "Normalized emission", }, ], }, ]
- bw2io.strategies.simapro.normalize_simapro_formulae(formula, settings)[source]#
Convert SimaPro formulae to Python expressions.
Processes a given formula string containing SimaPro formulae and converts them to Python expressions. The conversion is done using string manipulation and by calling the fix_iff_formula function.
- Parameters:
formula (str) â A string containing SimaPro formulae.
settings (dict) â A dictionary containing settings that affect the formula conversion, e.g., decimal separator.
- Returns:
A string with SimaPro formulae replaced by equivalent Python expressions.
- Return type:
str
Examples
>>> formula = "A^2" >>> settings = {"Decimal separator": ","} >>> normalize_simapro_formulae(formula, settings) "A**2"
- bw2io.strategies.simapro.normalize_simapro_labels_to_brightway_standard(db: List[dict]) List[dict] [source]#
Normalize unlinked exchange context and identifier labels to Brightway standards.
context -> categories
identifier -> code
Changes data in-place.
Needed because some randonneur transformations use more standard (i.e. not Brightway-specific) labels.
- bw2io.strategies.simapro.override_process_name_using_single_functional_exchange(db: List[dict], missing_value: str = '(unknown)') List[dict] [source]#
Set process dataset name from the single functional exchange.
SimaPro exports can include process names, but as the manual states:
âUnder the Documentation tab, you can enter the process name. Please note that this is only for your own reference and this name is not used anywhere. Processes are identified by the name defined under the Input/Output tab in the product section. Therefore, if you want to search for a certain process, you should use the product name defined in the Input/Output as the keyword.â
We therefore need to set the name to the same term being used as inputs elsewhere.
- Parameters:
db (list) â An list of dataset dictionaries.
- Return type:
The modified database list of dataset dictionaries.
- bw2io.strategies.simapro.remove_biosphere_location_prefix_if_flow_in_same_location(db: List[dict]) List[dict] [source]#
If a biosphere flow is SimaPro-regionalized, like âAmmonia, ARâ, and the process location is âARâ, then remove that suffix.
- bw2io.strategies.simapro.set_lognormal_loc_value_uncertainty_safe(db)[source]#
Ensure the âlocâ value is correct for lognormal uncertainty distributions in the given database.
Iterates through a given database (list of datasets) and updates the âlocâ value of exchanges with lognormal uncertainty distributions, setting it to the natural logarithm of the absolute value of the exchange amount.
- Parameters:
db (list) â A list of datasets containing exchanges with lognormal uncertainty distributions.
- Returns:
A modified list of datasets with the âlocâ value updated for exchanges with lognormal uncertainty distributions.
- Return type:
list
Examples
>>> db = [ { "exchanges": [ { "amount": 10, "uncertainty type": LognormalUncertainty.id, "loc": 0 } ] } ] >>> set_lognormal_loc_value_uncertainty_safe(db) [{'exchanges': [{'amount': 10, 'uncertainty type': 2, 'loc': 2.302585092994046}]}]
- bw2io.strategies.simapro.set_metadata_using_single_functional_exchange(db: List[dict], missing_value: str = '(unknown)') List[dict] [source]#
Set name, unit, production amount, and reference product from the functional exchange.
Does not do anything unless these conditions are met:
There is only one functional exchange
None of name⌠are present, or are set to missing_value
- Parameters:
db (list) â An list of dataset dictionaries.
- Return type:
The modified database list of dataset dictionaries.
- bw2io.strategies.simapro.sp_allocate_functional_products(db)[source]#
Allocate products in a SimaPro dataset by creating a separate dataset for each product.
For raw SimaPro datasets, creates a separate dataset for each product, taking into account the allocation factor if provided. Also handles waste treatment datasets with a single product.
- Parameters:
db (list) â A list of dataset dictionaries
- Returns:
db â A list of dictionaries, including all of the original db, but also a separate process dataset for each product from multifunctional SimaPro datasets.
- Return type:
list
Examples
>>> db = [ ... { ... "name": "Dataset 1", ... "type": "multifunctional", ... "exchanges": [ ... {"type": "production", "name": "Product A", "unit": "kg", "amount": 10, "allocation": 80}, ... {"type": "production", "name": "Product B", "unit": "kg", "amount": 20, "allocation": 20}, ... {"type": "biosphere", "name": "Burden", "unit": "kg", "amount": 100}, ... ], ... } ... ] >>> sp_allocate_products(db) [ { "name": "Dataset 1", "type": "multifunctional", "exchanges": [ {"type": "production", "name": "Product A", "unit": "kg", "amount": 10, "allocation": 80}, {"type": "production", "name": "Product B", "unit": "kg", "amount": 20, "allocation": 20}, ], }, { "name": "Product A", "reference product": "Product A", "unit": "kg", "production amount": 10, "exchanges": [ {"type": "production", "name": "Product A", "unit": "kg", "amount": 10}, {"type": "biosphere", "name": "Burden", "unit": "kg", "amount": 80}, ], }, { "name": "Product B", "reference product": "Product B", "unit": "kg", "production amount": 5, "exchanges": [ {"type": "production", "name": "Product B", "unit": "kg", "amount": 5}, {"type": "biosphere", "name": "Burden", "unit": "kg", "amount": 20}, ], }, ]
- bw2io.strategies.simapro.sp_allocate_products(db)[source]#
Allocate products in a SimaPro dataset by creating a separate dataset for each product.
For raw SimaPro datasets, creates a separate dataset for each product, taking into account the allocation factor if provided. Also handles waste treatment datasets with a single product.
- Parameters:
db (list) â A list of dictionaries representing raw SimaPro datasets.
- Returns:
new_db â A list of dictionaries representing the allocated datasets with separate entries for each product.
- Return type:
list
Examples
>>> db = [ ... { ... "name": "Dataset 1", ... "exchanges": [ ... {"type": "production", "name": "Product A", "unit": "kg", "amount": 10, "allocation": 80}, ... {"type": "production", "name": "Product B", "unit": "kg", "amount": 20, "allocation": 20}, ... ], ... } ... ] >>> sp_allocate_products(db) [ { "name": "Product A", "reference product": "Product A", "unit": "kg", "production amount": 10, "exchanges": [ {"type": "production", "name": "Product A", "unit": "kg", "amount": 10, "allocation": 80}, {"type": "production", "name": "Product B", "unit": "kg", "amount": 5, "allocation": 20}, ], }, { "name": "Product B", "reference product": "Product B", "unit": "kg", "production amount": 5, "exchanges": [ {"type": "production", "name": "Product A", "unit": "kg", "amount": 2.5, "allocation": 80}, {"type": "production", "name": "Product B", "unit": "kg", "amount": 5, "allocation": 20}, ], }, ]
- bw2io.strategies.simapro.split_simapro_name_geo(db, regex_string=detoxify_pattern)[source]#
Split a name like âfoo/CH Uâ into name and geo components in a dataset.
Processes datasets and their exchanges by splitting their names into name and geo components (e.g., âfoo/CH Uâ into âfooâ and âCH Uâ). The original name is stored in a new field called âsimapro nameâ.
- Parameters:
db (list) â A list of dictionaries representing datasets with names to be split.
regex_string (str) â A regex pattern to split names. Defaults to a routine to split ecoinvent names.
- Returns:
db â A list of dictionaries representing modified datasets with split names and geo components.
- Return type:
list
Examples
>>> db = [ ... { ... "name": "foo/CH U", ... "exchanges": [ ... {"name": "bar/US U", "type": "technosphere"}, ... ], ... } ... ] >>> split_simapro_name_geo(db) [ { "name": "foo", "simapro name": "foo/CH U", "location": "CH U", "exchanges": [ {"name": "bar", "simapro name": "bar/US U", "location": "US U", "type": "technosphere"}, ], }, ]
- bw2io.strategies.simapro.split_simapro_name_geo_curly_brackets(db: List[dict], suffix: str = '') List[dict] [source]#
Split a name like âWheat straw, at farm {NL} Energy, Uâ into name and geo components in a dataset.
The original name is stored in a new field called âsimapro nameâ if that field is not yet present.
White space around the suffix and process name are stripped.
- Parameters:
db (list) â A list of dictionaries representing datasets with names to be split.
suffix (str) â Suffix expected to be added to the end of process names, like âfooâ in âEnergy {CO} fooâ.
- Returns:
db â A list of dictionaries representing modified datasets with split names and geo components.
- Return type:
list
Examples
>>> db = [ ... { ... "name": "Wheat straw, at farm {NL} Energy, U", ... "exchanges": [ ... {"name": "Dairy cows ration, at farm {ES} Energy, U"}, ... ], ... } ... ] >>> split_simapro_name_geo_curly_brackets(db, "Energy, U") [ { "name": "Wheat straw, at farm", "simapro name": "Wheat straw, at farm {NL} Energy, U", "location": "NL", "exchanges": [ { "name": "Dairy cows ration, at farm", "simapro name": "Dairy cows ration, at farm {ES} Energy, U", "location": "ES", }, ], }, ]