diff --git a/tests/conftest.py b/tests/conftest.py index 92cf76a..f33e098 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,3 +1,4 @@ from .fixtures.nonunitary_db_fixture import nonunitary_db from .fixtures.substitution_db_fixture import substitution_db from .fixtures.vehicle_db_fixture import vehicle_db +from .fixtures.dynamic_biomatrix_db_fixture import dynamic_biomatrix_db diff --git a/tests/fixtures/dynamic_biomatrix_db_fixture.py b/tests/fixtures/dynamic_biomatrix_db_fixture.py new file mode 100644 index 0000000..0218c71 --- /dev/null +++ b/tests/fixtures/dynamic_biomatrix_db_fixture.py @@ -0,0 +1,120 @@ +import pytest +import bw2data as bd +import numpy as np + +from bw2data.tests import bw2test +from bw_temporalis import TemporalDistribution + + +@pytest.fixture +@bw2test +def dynamic_biomatrix_db(): + bd.projects.set_current("__test_dynamic_biomatrix__") + bd.Database("bio").write( + { + ("bio", "CO2"): { + "type": "emission", + "name": "carbon dioxide", + }, + }, + ) + + bd.Database("db_2020").write( + { + ("db_2020", "C"): { + "name": "node c", + "location": "somewhere", + "reference product": "C", + "exchanges": [ + { + "amount": 1, + "type": "production", + "input": ("db_2020", "C"), + }, + { + "amount": 1.5, + "type": "biosphere", + "input": ("bio", "CO2"), + }, + ], + }, + } + ) + + bd.Database("foreground").write( + { + ("foreground", "A"): { + "name": "node a", + "location": "somewhere", + "reference product": "A", + "exchanges": [ + { + "amount": 1, + "type": "production", + "input": ("foreground", "A"), + }, + { + "amount": 8, + "type": "technosphere", + "input": ("foreground", "B"), + "temporal_distribution": TemporalDistribution( + date=np.array([4], dtype="timedelta64[Y]"), + amount=np.array([1]), + ), + }, + { + "amount": 8, + "type": "technosphere", + "input": ( + "foreground", + "B_1", + ), # occuring at exactly the same time as B + "temporal_distribution": TemporalDistribution( + date=np.array([4], dtype="timedelta64[Y]"), + amount=np.array([1]), + ), + }, + ], + }, + ("foreground", "B"): { + "name": "node b", + "location": "somewhere", + "reference product": "B", + "exchanges": [ + { + "amount": 1, + "type": "production", + "input": ("foreground", "B"), + }, + { + "amount": 2, + "type": "technosphere", + "input": ("db_2020", "C"), + }, + ], + }, + ("foreground", "B_1"): { # identical to B + "name": "node b_1", + "location": "somewhere", + "reference product": "B_1", + "exchanges": [ + { + "amount": 1, + "type": "production", + "input": ("foreground", "B_1"), + }, + { + "amount": 2, + "type": "technosphere", + "input": ("db_2020", "C"), + }, + ], + }, + } + ) + + bd.Method(("GWP", "example")).write( + [ + (("bio", "CO2"), 1), + ] + ) diff --git a/tests/test_dynamic_biomatrix_construction.py b/tests/test_dynamic_biomatrix_construction.py new file mode 100644 index 0000000..847498b --- /dev/null +++ b/tests/test_dynamic_biomatrix_construction.py @@ -0,0 +1,46 @@ +from datetime import datetime +import numpy as np +import pandas as pd +import bw2data as bd +import pytest + +from bw_timex import TimexLCA + +# make sure the test db is loaded +def test_dynamic_biomatrix_fixture(dynamic_biomatrix_db): + assert len(bd.databases) == 3 + + +@pytest.mark.usefixtures("dynamic_biomatrix_db") +class TestClass_dynamic_biomatrix: + + @pytest.fixture(autouse=True) + def setup_method(self): + self.node_a = bd.get_node(database="foreground", code="A") + + database_date_dict = { + "db_2020": datetime.strptime("2020", "%Y"), + "foreground": "dynamic", + } + + self.tlca = TimexLCA( + demand={self.node_a: 1}, + method=("GWP", "example"), + database_date_dict=database_date_dict, + ) + + self.tlca.build_timeline() + self.tlca.lci(expand_technosphere=True, build_dynamic_biosphere=True) + self.tlca.static_lcia() + + def test_identical_time_mapped_producers_exist_in_timeline(self): + + duplicates = self.tlca.timeline["time_mapped_producer"].duplicated().any() + assert duplicates, f"No duplicates found in column {"time_mapped_producer"}" + + def test_dynamic_biomatrix_for_multiple_identical_time_mapped_producers_in_timeline(self): + + expected_dynamic_biomatrix_as_array = np.array([[0. , 0. , 0. , 0. , 0. , 0. , 0. , 1.5]]) #1.5 CO2 is only added once for the time_mapped producer, depsite it occuring twice in the timeline! Before #78, it was added twice , resulting in 3.0 + + assert np.array_equal(self.tlca.dynamic_biomatrix.toarray(), expected_dynamic_biomatrix_as_array), "Arrays are not equal" +