Skip to content

Commit

Permalink
add test #78
Browse files Browse the repository at this point in the history
  • Loading branch information
muelleram committed Aug 2, 2024
1 parent 5eb79bd commit 74bb28e
Show file tree
Hide file tree
Showing 3 changed files with 167 additions and 0 deletions.
1 change: 1 addition & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -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
120 changes: 120 additions & 0 deletions tests/fixtures/dynamic_biomatrix_db_fixture.py
Original file line number Diff line number Diff line change
@@ -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),
]
)
46 changes: 46 additions & 0 deletions tests/test_dynamic_biomatrix_construction.py
Original file line number Diff line number Diff line change
@@ -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"

0 comments on commit 74bb28e

Please sign in to comment.