Skip to content

Commit

Permalink
MultiLCA progress, now with tests
Browse files Browse the repository at this point in the history
  • Loading branch information
cmutel committed Nov 26, 2023
1 parent ba83289 commit e464b48
Show file tree
Hide file tree
Showing 9 changed files with 283 additions and 207 deletions.
4 changes: 2 additions & 2 deletions bw2calc/lca.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def __init__(
List of `bw_processing.Datapackage` objects. Can be loaded via
`bw2data.prepare_lca_inputs` or constructed manually. Should include data for all needed
matrices.
remapping_dicts : dict[str : dict]
remapping_dicts : dict[str, dict]
Dict of remapping dictionaries that link Brightway `Node` ids to `(database, code)`
tuples. `remapping_dicts` can provide such remapping for any of `activity`, `product`,
`biosphere`.
Expand All @@ -98,7 +98,7 @@ def __init__(
Use arrays instead of vectors from the given `data_objs`
use_distributions : bool
Use probability distributions from the given `data_objs`
selective_use : dict[str : dict]
selective_use : dict[str, dict]
Dictionary that gives more control on whether `use_arrays` or `use_distributions` should
be used. Has the form `{matrix_label: {"use_arrays"|"use_distributions": bool}`.
Standard matrix labels are `technosphere_matrix`, `biosphere_matrix`, and
Expand Down
2 changes: 1 addition & 1 deletion bw2calc/lca_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ def lcia(self, demand: Optional[dict] = None) -> None:
Doesn't return anything, but creates ``self.characterized_inventory``.
"""
assert hasattr(self, "inventory"), "Must do lci first"
assert hasattr(self, "inventory") or (self, "inventories"), "Must do lci first"
if not self.dicts.biosphere:
raise EmptyBiosphere

Expand Down
385 changes: 181 additions & 204 deletions bw2calc/multi_lca.py

Large diffs are not rendered by default.

Binary file added tests/fixtures/multi_lca_simple_1.zip
Binary file not shown.
Binary file added tests/fixtures/multi_lca_simple_2.zip
Binary file not shown.
Binary file added tests/fixtures/multi_lca_simple_3.zip
Binary file not shown.
Binary file added tests/fixtures/multi_lca_simple_4.zip
Binary file not shown.
Binary file added tests/fixtures/multi_lca_simple_5.zip
Binary file not shown.
99 changes: 99 additions & 0 deletions tests/multi_lca.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
from pathlib import Path

import numpy as np
import pytest

from bw2calc import MultiLCA
from bw2calc.utils import get_datapackage

# Technosphere
# α: 1 β: 2 γ: 3 δ: 4 ε: 5 ζ: 6
# L: 100 -0.2 -0.5 1 -0.1 -0.1
# M: 101 1 -0.2 -0.1
# N: 102 -0.5 1 -0.2
# O: 103 -0.4 1
# P: 104 1
# Q: 105 1

# Biosphere
# α: 1 β: 2 γ: 3 δ: 4 ε: 5 ζ: 6
# 200 2 4 8 1 2 1
# 201 1 2 3
# 202 1 2
# 203 3

fixture_dir = Path(__file__).resolve().parent / "fixtures"


@pytest.fixture
def dps():
return [
get_datapackage(fixture_dir / "multi_lca_simple_1.zip"),
get_datapackage(fixture_dir / "multi_lca_simple_2.zip"),
get_datapackage(fixture_dir / "multi_lca_simple_3.zip"),
get_datapackage(fixture_dir / "multi_lca_simple_4.zip"),
get_datapackage(fixture_dir / "multi_lca_simple_5.zip"),
]


@pytest.fixture
def config():
return {
"impact_categories": [
("first", "category"),
("second", "category"),
]
}


@pytest.fixture
def func_units():
return {
"γ": {100: 1},
"ε": {103: 2},
"ζ": {105: 3},
}


def test_inventory_matrix_construction(dps, config, func_units):
mlca = MultiLCA(demands=func_units, method_config=config, data_objs=dps)
mlca.lci()
mlca.lcia()

print(mlca.scores)
print(mlca.technosphere_matrix.todense())
print(mlca.biosphere_matrix.todense())

for name, mat in mlca.characterization_matrices.items():
print(name)
print(mat.todense())

tm = [
(100, 1, -0.2),
(100, 2, -0.5),
(100, 3, 1),
(100, 4, -0.1),
(100, 6, -0.1),
]
for a, b, c in tm:
assert mlca.technosphere_matrix[mlca.dicts.product[a], mlca.dicts.activity[b]] == c

assert (
mlca.technosphere_matrix.sum()
== np.array([-0.2, -0.5, 1, -0.1, -0.1, 1, -0.2, -0.1, -0.2, -0.5, 1, -0.4, 1, 1, 1]).sum()
)

bm = [
(200, 1, 2),
(200, 2, 4),
(200, 3, 8),
(200, 4, 1),
(200, 5, 2),
(200, 6, 1),
]
for a, b, c in bm:
assert mlca.biosphere_matrix[mlca.dicts.biosphere[a], mlca.dicts.activity[b]] == c

assert mlca.biosphere_matrix.sum() == np.array([2, 4, 8, 1, 2, 1, 1, 2, 3, 1, 2, 3]).sum()

assert 0

0 comments on commit e464b48

Please sign in to comment.