Skip to content

Commit

Permalink
Fix #109
Browse files Browse the repository at this point in the history
  • Loading branch information
cmutel committed Sep 18, 2024
1 parent c33a639 commit f13ba6e
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 5 deletions.
10 changes: 7 additions & 3 deletions bw2calc/multi_lca.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ class MultiLCA(LCABase):
----------
demands : dict[str, dict[int, float]]
The demands for which the LCA will be calculated. The keys identify functional unit sets.
method_config : dict
Dictionary satisfying the `MethodConfig` specification.
method_config : dict | MethodConfig
Dictionary satisfying the `MethodConfig` specification or `MethodConfig` instance.
data_objs : list[bw_processing.Datapackage]
List of `bw_processing.Datapackage` objects. Should include data for all needed matrices.
remapping_dicts : dict[str, dict]
Expand Down Expand Up @@ -80,7 +80,7 @@ class MultiLCA(LCABase):
def __init__(
self,
demands: dict[str, dict[int, float]],
method_config: dict,
method_config: Union[dict, MethodConfig],
data_objs: Iterable[Union[Path, AbstractFileSystem, bwp.DatapackageBase]],
remapping_dicts: Optional[Iterable[dict]] = None,
log_config: Optional[dict] = None,
Expand All @@ -91,6 +91,10 @@ def __init__(
):
# Validation checks
DemandsValidator(demands=demands)
if isinstance(method_config, MethodConfig):
method_config = {
key: value for key, value in method_config.model_dump().items() if value is not None
}
MethodConfig(**method_config)

self.demands = demands
Expand Down
60 changes: 58 additions & 2 deletions tests/multi_lca.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import numpy as np
import pytest

from bw2calc import MultiLCA
from bw2calc import MethodConfig, MultiLCA
from bw2calc.utils import get_datapackage

# Technosphere
Expand Down Expand Up @@ -47,6 +47,16 @@ def config():
}


@pytest.fixture
def method_config():
return MethodConfig(
impact_categories=[
("first", "category"),
("second", "category"),
]
)


@pytest.fixture
def func_units():
return {
Expand Down Expand Up @@ -127,6 +137,16 @@ def test_single_demand(dps, config):
assert mlca.scores[(("second", "category"), "γ")] == 3 * 10


def test_single_demand_method_config(dps, method_config):
single_func_unit = {"γ": {100: 1}}
mlca = MultiLCA(demands=single_func_unit, method_config=method_config, data_objs=dps)
mlca.lci()
mlca.lcia()

assert mlca.scores[(("first", "category"), "γ")] == 8 + 3
assert mlca.scores[(("second", "category"), "γ")] == 3 * 10


def test_normalization(dps, func_units):
config = {
"impact_categories": [
Expand Down Expand Up @@ -528,7 +548,7 @@ def test_monte_carlo_multiple_iterations_selective_use_in_list_comprehension(dps
assert np.unique(lst).shape == (10,)


def test_bug_108(dps, config, func_units):
def test_bug_108(dps, func_units):
# https://github.com/brightway-lca/brightway2-calc/issues/108
config = {
"impact_categories": [
Expand Down Expand Up @@ -574,3 +594,39 @@ def test_bug_108(dps, config, func_units):
== 3 * (3 * 10 + 1 * 10) * 84
)
assert mlca.scores[(("w", "1"), ("n", "1"), ("first", "category"), "γ")] == 3 * 42


def test_pass_full_methodconfig(dps, func_units):
method_config = MethodConfig(
impact_categories=[
("first", "category"),
("second", "category"),
],
normalizations={
("n", "1"): [("first", "category")],
("n", "2"): [("second", "category")],
},
weightings={
("w", "1"): [("n", "1")],
("w", "2"): [("n", "2")],
},
)

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=method_config, data_objs=dps)
mlca.lci()
mlca.lcia()
mlca.normalize()
mlca.weight()

assert (
mlca.scores[(("w", "2"), ("n", "2"), ("second", "category"), "ζ")]
== 3 * (3 * 10 + 1 * 10) * 84
)
assert mlca.scores[(("w", "1"), ("n", "1"), ("first", "category"), "γ")] == 3 * 42

0 comments on commit f13ba6e

Please sign in to comment.