diff --git a/CHANGES.md b/CHANGES.md index 57194a7..92573ab 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,5 +1,9 @@ # `bw2calc` Changelog +## 2.0.DEV23 (2024-09-18) + +* Allow `MethodConfig` as an input to `MultiLCA` + ## 2.0.DEV22 (2024-09-18) * Add `MethodConfig` to `__init__.py` for better UX diff --git a/bw2calc/__init__.py b/bw2calc/__init__.py index 98558fb..d942969 100644 --- a/bw2calc/__init__.py +++ b/bw2calc/__init__.py @@ -9,7 +9,7 @@ "MultiLCA", ] -__version__ = "2.0.DEV22" +__version__ = "2.0.DEV23" import platform diff --git a/bw2calc/multi_lca.py b/bw2calc/multi_lca.py index cf4de16..b249092 100644 --- a/bw2calc/multi_lca.py +++ b/bw2calc/multi_lca.py @@ -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] @@ -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, @@ -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 diff --git a/tests/multi_lca.py b/tests/multi_lca.py index b2ad0a7..3c314c1 100644 --- a/tests/multi_lca.py +++ b/tests/multi_lca.py @@ -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 @@ -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 { @@ -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": [ @@ -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": [ @@ -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