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

forward-port of #160 #161

Merged
merged 6 commits into from
Mar 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
38 changes: 31 additions & 7 deletions bw2io/strategies/simapro.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import numpy as np
from bw2data import Database
import bw2parameters
from stats_arrays import LognormalUncertainty

from ..compatibility import (
Expand Down Expand Up @@ -46,9 +47,30 @@ def sp_allocate_products(db):
for product in products:
product = copy.deepcopy(product)
if product["allocation"]:
product["amount"] = (
product["amount"] * 1 / (product["allocation"] / 100)
)
allocation = product["allocation"]
if type(product["allocation"]) is str and "parameters" in ds:
ds["parameters"] = {
k.lower(): v for k, v in ds["parameters"].items()
}
interp = bw2parameters.DefaultParameterSet(
ds["parameters"]
).get_interpreter()
interp.add_symbols(
bw2parameters.DefaultParameterSet(
ds["parameters"]
).evaluate_and_set_amount_field()
)
allocation = interp(
normalize_simapro_formulae(
product["allocation"].lower(),
settings={"Decimal separator": ","},
)
)

if allocation != 0:
product["amount"] = product["amount"] * 1 / (allocation / 100)
else:
product["amount"] = 0 # Infinity as zero? :-/
else:
product["amount"] = 0
copied = copy.deepcopy(ds)
Expand Down Expand Up @@ -159,17 +181,19 @@ def normalize_simapro_biosphere_names(db):
"(?P<when_false>[^,]+)" # Value if condition is false
"\s*" # Whitespace
"\)", # End parentheses
re.IGNORECASE
re.IGNORECASE,
)


def fix_iff_formula(string):
while iff_exp.findall(string):
match = next(iff_exp.finditer(string))
string = (
string[:match.start()] \
+ "(({when_true}) if ({condition}) else ({when_false}))".format(**match.groupdict()) \
+ string[match.end():]
string[: match.start()]
+ "(({when_true}) if ({condition}) else ({when_false}))".format(
**match.groupdict()
)
+ string[match.end() :]
)
return string

Expand Down
10 changes: 7 additions & 3 deletions tests/simapro.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,18 @@ def test_sp_wrong_field_ordering():

@bw2test
def test_sp_python_builtin_as_unit_name():
sp = SimaProCSVImporter(os.path.join(SP_FIXTURES_DIR, "python_builtin_as_unit_name.csv"))
sp = SimaProCSVImporter(
os.path.join(SP_FIXTURES_DIR, "python_builtin_as_unit_name.csv")
)
assert len(sp.data)


@bw2test
def test_sp_invalid_lognormal_scale():
sp = SimaProCSVImporter(os.path.join(SP_FIXTURES_DIR, "process_with_invalid_lognormal_scale.csv"))
assert sp.data[0]['exchanges'][1]['uncertainty type'] == 0
sp = SimaProCSVImporter(
os.path.join(SP_FIXTURES_DIR, "process_with_invalid_lognormal_scale.csv")
)
assert sp.data[0]["exchanges"][1]["uncertainty type"] == 0


@bw2test
Expand Down