Skip to content

Commit

Permalink
Add tests for multiple iterations on MultiLCA
Browse files Browse the repository at this point in the history
  • Loading branch information
cmutel committed Aug 27, 2024
1 parent c3200b3 commit 401b10f
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 112 deletions.
112 changes: 0 additions & 112 deletions tests/monte_carlo.py

This file was deleted.

93 changes: 93 additions & 0 deletions tests/multi_lca.py
Original file line number Diff line number Diff line change
Expand Up @@ -354,3 +354,96 @@ def test_selective_use_keep_first(dps, func_units):
)
assert np.allclose(b, mlca.biosphere_matrix.sum())
assert np.allclose(t, mlca.technosphere_matrix.sum())


def test_monte_carlo_multiple_iterations_use_distributions(dps, func_units):
config = {
"impact_categories": [
("first", "category"),
("second", "category"),
],
"normalizations": {
("n", "1"): [
("first", "category"),
("second", "category"),
]
},
"weightings": {("w", "1"): [("n", "1")]},
}

dps.append(
get_datapackage(fixture_dir / "multi_lca_simple_normalization.zip"),
)
dps.append(
get_datapackage(fixture_dir / "multi_lca_simple_weighting.zip"),
)

mlca = MultiLCA(demands=func_units, method_config=config, data_objs=dps, use_distributions=True)
mlca.lci()
mlca.lcia()
mlca.normalize()
mlca.weight()

results_manual = {key: [mat.sum()] for key, mat in mlca.weighted_inventories.items()}
results_scores = {k: [v] for k, v in mlca.scores.items()}

for _ in range(9):
next(mlca)
for key, mat in mlca.weighted_inventories.items():
results_manual[key].append(mat.sum())
for key, val in mlca.scores.items():
results_scores[key].append(val)

for key, lst in results_manual.items():
assert np.unique(lst).shape == (10,)
for key, lst in results_scores.items():
assert np.unique(lst).shape == (10,)


def test_monte_carlo_multiple_iterations_selective_use(dps, func_units):
config = {
"impact_categories": [
("first", "category"),
("second", "category"),
],
"normalizations": {
("n", "1"): [
("first", "category"),
("second", "category"),
]
},
"weightings": {("w", "1"): [("n", "1")]},
}

dps.append(
get_datapackage(fixture_dir / "multi_lca_simple_normalization.zip"),
)
dps.append(
get_datapackage(fixture_dir / "multi_lca_simple_weighting.zip"),
)

su = {
"characterization_matrix": {"use_distributions": True},
"weighting_matrix": {"use_distributions": True},
}

mlca = MultiLCA(demands=func_units, method_config=config, data_objs=dps, selective_use=su)
mlca.lci()
mlca.lcia()
mlca.normalize()
mlca.weight()

results_manual = {key: [mat.sum()] for key, mat in mlca.weighted_inventories.items()}
results_scores = {k: [v] for k, v in mlca.scores.items()}

for _ in range(9):
next(mlca)
for key, mat in mlca.weighted_inventories.items():
results_manual[key].append(mat.sum())
for key, val in mlca.scores.items():
results_scores[key].append(val)

for key, lst in results_manual.items():
assert np.unique(lst).shape == (10,)
for key, lst in results_scores.items():
assert np.unique(lst).shape == (10,)

0 comments on commit 401b10f

Please sign in to comment.