Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
cmutel committed Oct 2, 2024
2 parents 116cbb8 + 03fba0a commit 5a26969
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 6 deletions.
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion bw2calc/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"MultiLCA",
]

__version__ = "2.0.DEV22"
__version__ = "2.0.DEV23"


import platform
Expand Down
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 5a26969

Please sign in to comment.