Skip to content

Commit

Permalink
Remove composite "MFP:" names where possible
Browse files Browse the repository at this point in the history
  • Loading branch information
cmutel committed Nov 25, 2024
1 parent 0f7cda2 commit 5cbeb9a
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 6 deletions.
4 changes: 4 additions & 0 deletions multifunctional/allocation.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,10 @@ def generic_allocation(
del allocated_process["id"]
if strategy_label:
allocated_process["mf_strategy_label"] = act["mf_strategy_label"] = strategy_label
if "name" in allocated_process and allocated_process["name"].startswith("MFP: "):
allocated_process["name"] = (product or new_exc).get(
"name", allocated_process["name"]
) + " (read-only process)"
allocated_process["code"] = process_code
allocated_process["mf_parent_key"] = (act["database"], act["code"])
# Used to filter out previous read only processes
Expand Down
12 changes: 11 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
from fixtures.basic import DATA as BASIC_DATA
from fixtures.errors import DATA as ERRORS_DATA
from fixtures.internal_linking import DATA as INTERNAL_LINKING_DATA
from fixtures.many_products import DATA as MANY_PRODUCTS_DATA
from fixtures.name_change import DATA as NAME_CHANGE_DATA
from fixtures.product_properties import DATA as PP_DATA
from fixtures.products import DATA as PRODUCT_DATA
from fixtures.many_products import DATA as MANY_PRODUCTS_DATA

from multifunctional import MultifunctionalDatabase, allocation_before_writing

Expand All @@ -26,6 +27,15 @@ def basic():
return db


@pytest.fixture
@bw2test
def name_change():
db = MultifunctionalDatabase("name_change")
db.write(deepcopy(NAME_CHANGE_DATA), process=False)
db.metadata["dirty"] = True
return db


@pytest.fixture
@bw2test
def products():
Expand Down
45 changes: 45 additions & 0 deletions tests/fixtures/name_change.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
DATA = {
("name_change", "a"): {
"name": "flow - a",
"code": "a",
"unit": "kg",
"type": "emission",
"categories": ("air",),
},
("name_change", "1"): {
"name": "MFP: Longer name because like⧺Long name look here wut",
"code": "1",
"location": "first",
"type": "multifunctional",
"exchanges": [
{
"functional": True,
"type": "production",
"name": "Longer name because like reasons",
"desired_code": "first - generated",
"amount": 4,
"properties": {
"price": 7,
"mass": 6,
},
},
{
"functional": True,
"type": "production",
"name": "Long name look here wut, wut",
"desired_code": "second - generated",
"amount": 4,
"properties": {
"price": 7,
"mass": 6,
},
},
{
"type": "biosphere",
"name": "flow - a",
"amount": 10,
"input": ("name_change", "a"),
},
],
},
}
21 changes: 21 additions & 0 deletions tests/test_allocation.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,3 +247,24 @@ def test_allocation_zero_factor_still_gives_process():
(exc.input, exc["amount"])
for exc in bd.get_node(name="process - 1", unit="megajoule").edges()
]


def test_name_replacement_mfp(name_change):
name_change.metadata["default_allocation"] = "price"
bd.get_node(code="1").allocate()
assert {ds["name"] for ds in name_change} == {
"flow - a",
"MFP: Longer name because like⧺Long name look here wut",
"Longer name because like reasons (read-only process)",
"Long name look here wut, wut (read-only process)",
}


def test_name_replacement_not_mfp(name_change):
name_change.metadata["default_allocation"] = "price"

node = bd.get_node(code="1")
node['name'] = "Replace me"
node.save()
node.allocate()
assert sorted(ds["name"] for ds in name_change) == ["Replace me"] * 3 + ["flow - a"]
10 changes: 5 additions & 5 deletions tests/test_readonly_process_creation_deletion.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,30 +99,30 @@ def test_change_multifunctional_reduce_num_still_multifunctional(many_products):
assert bd.get_node(code="1")["type"] == "multifunctional"
assert len(many_products) == 8

exc = [exc for exc in bd.get_node(code="1").exchanges() if exc.input['code'] == 'p1'][0]
exc = [exc for exc in bd.get_node(code="1").exchanges() if exc.input["code"] == "p1"][0]
assert exc["functional"]
exc["functional"] = False
exc.save()
bd.get_node(code="1").allocate()
assert bd.get_node(code="1")["mf_was_once_allocated"]
assert bd.get_node(code="1")["type"] == 'multifunctional'
assert bd.get_node(code="1")["type"] == "multifunctional"
assert len(many_products) == 7

exc = [exc for exc in bd.get_node(code="1").exchanges() if exc.input['code'] == 'p1'][0]
exc = [exc for exc in bd.get_node(code="1").exchanges() if exc.input["code"] == "p1"][0]
exc["functional"] = True
exc.save()
bd.get_node(code="1").allocate()
assert bd.get_node(code="1")["type"] == "multifunctional"
assert len(many_products) == 8

exc = [exc for exc in bd.get_node(code="1").exchanges() if exc.input['code'] == 'p1'][0]
exc = [exc for exc in bd.get_node(code="1").exchanges() if exc.input["code"] == "p1"][0]
exc["functional"] = False
exc.save()
bd.get_node(code="1").allocate()
assert bd.get_node(code="1")["type"] == "multifunctional"
assert len(many_products) == 7

exc = [exc for exc in bd.get_node(code="1").exchanges() if exc.input['code'] == 'p1'][0]
exc = [exc for exc in bd.get_node(code="1").exchanges() if exc.input["code"] == "p1"][0]
exc["functional"] = True
exc.save()
bd.get_node(code="1").allocate()
Expand Down

0 comments on commit 5cbeb9a

Please sign in to comment.