Skip to content

Commit 5a26969

Browse files
committed
2 parents 116cbb8 + 03fba0a commit 5a26969

File tree

4 files changed

+70
-6
lines changed

4 files changed

+70
-6
lines changed

CHANGES.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# `bw2calc` Changelog
22

3+
## 2.0.DEV23 (2024-09-18)
4+
5+
* Allow `MethodConfig` as an input to `MultiLCA`
6+
37
## 2.0.DEV22 (2024-09-18)
48

59
* Add `MethodConfig` to `__init__.py` for better UX

bw2calc/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"MultiLCA",
1010
]
1111

12-
__version__ = "2.0.DEV22"
12+
__version__ = "2.0.DEV23"
1313

1414

1515
import platform

bw2calc/multi_lca.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ class MultiLCA(LCABase):
4646
----------
4747
demands : dict[str, dict[int, float]]
4848
The demands for which the LCA will be calculated. The keys identify functional unit sets.
49-
method_config : dict
50-
Dictionary satisfying the `MethodConfig` specification.
49+
method_config : dict | MethodConfig
50+
Dictionary satisfying the `MethodConfig` specification or `MethodConfig` instance.
5151
data_objs : list[bw_processing.Datapackage]
5252
List of `bw_processing.Datapackage` objects. Should include data for all needed matrices.
5353
remapping_dicts : dict[str, dict]
@@ -80,7 +80,7 @@ class MultiLCA(LCABase):
8080
def __init__(
8181
self,
8282
demands: dict[str, dict[int, float]],
83-
method_config: dict,
83+
method_config: Union[dict, MethodConfig],
8484
data_objs: Iterable[Union[Path, AbstractFileSystem, bwp.DatapackageBase]],
8585
remapping_dicts: Optional[Iterable[dict]] = None,
8686
log_config: Optional[dict] = None,
@@ -91,6 +91,10 @@ def __init__(
9191
):
9292
# Validation checks
9393
DemandsValidator(demands=demands)
94+
if isinstance(method_config, MethodConfig):
95+
method_config = {
96+
key: value for key, value in method_config.model_dump().items() if value is not None
97+
}
9498
MethodConfig(**method_config)
9599

96100
self.demands = demands

tests/multi_lca.py

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import numpy as np
55
import pytest
66

7-
from bw2calc import MultiLCA
7+
from bw2calc import MethodConfig, MultiLCA
88
from bw2calc.utils import get_datapackage
99

1010
# Technosphere
@@ -47,6 +47,16 @@ def config():
4747
}
4848

4949

50+
@pytest.fixture
51+
def method_config():
52+
return MethodConfig(
53+
impact_categories=[
54+
("first", "category"),
55+
("second", "category"),
56+
]
57+
)
58+
59+
5060
@pytest.fixture
5161
def func_units():
5262
return {
@@ -127,6 +137,16 @@ def test_single_demand(dps, config):
127137
assert mlca.scores[(("second", "category"), "γ")] == 3 * 10
128138

129139

140+
def test_single_demand_method_config(dps, method_config):
141+
single_func_unit = {"γ": {100: 1}}
142+
mlca = MultiLCA(demands=single_func_unit, method_config=method_config, data_objs=dps)
143+
mlca.lci()
144+
mlca.lcia()
145+
146+
assert mlca.scores[(("first", "category"), "γ")] == 8 + 3
147+
assert mlca.scores[(("second", "category"), "γ")] == 3 * 10
148+
149+
130150
def test_normalization(dps, func_units):
131151
config = {
132152
"impact_categories": [
@@ -528,7 +548,7 @@ def test_monte_carlo_multiple_iterations_selective_use_in_list_comprehension(dps
528548
assert np.unique(lst).shape == (10,)
529549

530550

531-
def test_bug_108(dps, config, func_units):
551+
def test_bug_108(dps, func_units):
532552
# https://github.com/brightway-lca/brightway2-calc/issues/108
533553
config = {
534554
"impact_categories": [
@@ -574,3 +594,39 @@ def test_bug_108(dps, config, func_units):
574594
== 3 * (3 * 10 + 1 * 10) * 84
575595
)
576596
assert mlca.scores[(("w", "1"), ("n", "1"), ("first", "category"), "γ")] == 3 * 42
597+
598+
599+
def test_pass_full_methodconfig(dps, func_units):
600+
method_config = MethodConfig(
601+
impact_categories=[
602+
("first", "category"),
603+
("second", "category"),
604+
],
605+
normalizations={
606+
("n", "1"): [("first", "category")],
607+
("n", "2"): [("second", "category")],
608+
},
609+
weightings={
610+
("w", "1"): [("n", "1")],
611+
("w", "2"): [("n", "2")],
612+
},
613+
)
614+
615+
dps.append(
616+
get_datapackage(fixture_dir / "multi_lca_simple_normalization.zip"),
617+
)
618+
dps.append(
619+
get_datapackage(fixture_dir / "multi_lca_simple_weighting.zip"),
620+
)
621+
622+
mlca = MultiLCA(demands=func_units, method_config=method_config, data_objs=dps)
623+
mlca.lci()
624+
mlca.lcia()
625+
mlca.normalize()
626+
mlca.weight()
627+
628+
assert (
629+
mlca.scores[(("w", "2"), ("n", "2"), ("second", "category"), "ζ")]
630+
== 3 * (3 * 10 + 1 * 10) * 84
631+
)
632+
assert mlca.scores[(("w", "1"), ("n", "1"), ("first", "category"), "γ")] == 3 * 42

0 commit comments

Comments
 (0)