-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
194 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,56 +1,75 @@ | ||
from typing import Iterable, Optional | ||
from typing import Optional, Sequence | ||
|
||
from pydantic import BaseModel, model_validator | ||
|
||
|
||
class MethodConfig(BaseModel): | ||
impact_categories: Iterable[tuple[str, ...]] | ||
normalizations: Optional[dict[tuple[str, ...], tuple[str, ...]]] = None | ||
weightings: Optional[dict[tuple[str, ...], tuple[str, ...]]] = None | ||
impact_categories: Sequence[tuple[str, ...]] | ||
normalizations: Optional[dict[tuple[str, ...], list[tuple[str, ...]]]] = None | ||
weightings: Optional[dict[tuple[str, ...], list[tuple[str, ...]]]] = None | ||
|
||
@model_validator(mode='after') | ||
@model_validator(mode="after") | ||
def normalizations_reference_impact_categories(self): | ||
if not self.normalizations: | ||
return self | ||
difference = set(self.normalizations).difference(set(self.impact_categories)) | ||
references = set.union(*[set(lst) for lst in self.normalizations.values()]) | ||
difference = references.difference(set(self.impact_categories)) | ||
if difference: | ||
raise ValueError(f"Impact categories in `normalizations` not present in `impact_categories`: {difference}") | ||
raise ValueError( | ||
f"Impact categories in `normalizations` not present in `impact_categories`: {difference}" | ||
) | ||
return self | ||
|
||
@model_validator(mode='after') | ||
def unique_normalizations(self): | ||
if self.normalizations: | ||
overlap = set(self.normalizations.values()).intersection(set(self.impact_categories)) | ||
if overlap: | ||
raise ValueError(f"Normalization identifiers overlap impact category identifiers: {overlap}") | ||
@model_validator(mode="after") | ||
def normalizations_unique_from_impact_categories(self): | ||
if not self.normalizations: | ||
return self | ||
|
||
references = set.union(*[set(lst) for lst in self.normalizations.values()]) | ||
overlap = set(self.normalizations).intersection(references) | ||
if overlap: | ||
raise ValueError( | ||
f"Normalization identifiers overlap impact category identifiers: {overlap}" | ||
) | ||
return self | ||
|
||
@model_validator(mode='after') | ||
@model_validator(mode="after") | ||
def weightings_reference_impact_categories_or_normalizations(self): | ||
if not self.weightings: | ||
return self | ||
possibles = set(self.impact_categories) | ||
|
||
if self.normalizations: | ||
possibles = possibles.union(set(self.normalizations.values())) | ||
difference = set(self.weightings).difference(possibles) | ||
possibles = set(self.normalizations).union(set(self.impact_categories)) | ||
else: | ||
possibles = set(self.impact_categories) | ||
|
||
references = set.union(*[set(lst) for lst in self.weightings.values()]) | ||
difference = set(references).difference(possibles) | ||
if difference: | ||
raise ValueError(f"`weightings` refers to missing impact categories or normalizations: {difference}") | ||
raise ValueError( | ||
f"`weightings` refers to missing impact categories or normalizations: {difference}" | ||
) | ||
return self | ||
|
||
@model_validator(mode='after') | ||
def unique_weightings_to_impact_categories(self): | ||
@model_validator(mode="after") | ||
def weightings_unique_from_impact_categories(self): | ||
if not self.weightings: | ||
return self | ||
overlap = set(self.weightings.values()).intersection(set(self.impact_categories)) | ||
overlap = set(self.weightings).intersection(set(self.impact_categories)) | ||
if overlap: | ||
raise ValueError(f"Weighting identifiers overlap impact category identifiers: {overlap}") | ||
raise ValueError( | ||
f"Weighting identifiers overlap impact category identifiers: {overlap}" | ||
) | ||
return self | ||
|
||
@model_validator(mode='after') | ||
def unique_weightings_to_normalizations(self): | ||
@model_validator(mode="after") | ||
def weightings_unique_from_normalizations(self): | ||
if not self.weightings: | ||
return self | ||
if self.normalizations: | ||
overlap = set(self.weightings.values()).intersection(set(self.normalizations)) | ||
overlap = set(self.weightings).intersection(set(self.normalizations)) | ||
if overlap: | ||
raise ValueError(f"Weighting identifiers overlap normalization identifiers: {overlap}") | ||
raise ValueError( | ||
f"Weighting identifiers overlap normalization identifiers: {overlap}" | ||
) | ||
return self |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,154 @@ | ||
import pytest | ||
from pydantic import ValidationError | ||
|
||
from bw2calc.method_config import MethodConfig | ||
|
||
|
||
def test_method_config_valid(): | ||
data = { | ||
'impact_categories': [('foo', 'a'), ('foo', 'b')], | ||
"impact_categories": [("foo", "a"), ("foo", "b")], | ||
} | ||
assert MethodConfig(**data) | ||
|
||
data = { | ||
"impact_categories": [("foo", "a"), ("foo", "b")], | ||
"normalizations": {("norm", "standard"): [("foo", "a"), ("foo", "b")]}, | ||
} | ||
assert MethodConfig(**data) | ||
|
||
data = { | ||
"impact_categories": [("foo", "a"), ("foo", "b")], | ||
"normalizations": {("norm", "standard"): [("foo", "a"), ("foo", "b")]}, | ||
"weightings": {("weighting",): [("norm", "standard")]}, | ||
} | ||
assert MethodConfig(**data) | ||
|
||
|
||
def test_method_config_len_one_tuples_valid(): | ||
data = { | ||
"impact_categories": [("a",), ("b",)], | ||
} | ||
assert MethodConfig(**data) | ||
|
||
data = { | ||
"impact_categories": [("a",), ("b",)], | ||
"normalizations": {("norm",): [("a",), ("b",)]}, | ||
} | ||
assert MethodConfig(**data) | ||
|
||
data = { | ||
"impact_categories": [("a",), ("b",)], | ||
"normalizations": {("norm",): [("a",), ("b",)]}, | ||
"weightings": {("weighting",): [("norm",)]}, | ||
} | ||
assert MethodConfig(**data) | ||
|
||
|
||
def test_method_config_weighting_can_refer_impact_category(): | ||
data = { | ||
'impact_categories': [('foo', 'a'), ('foo', 'b')], | ||
'normalizations': {('foo', 'a'): ('norm', 'standard'), ('foo', 'b'): ('norm', 'standard')} | ||
"impact_categories": [("a",), ("b",)], | ||
"normalizations": {("norm",): [("a",), ("b",)]}, | ||
"weightings": {("weighting",): [("a",)]}, | ||
} | ||
assert MethodConfig(**data) | ||
|
||
|
||
def test_method_config_weighting_can_refer_normalization(): | ||
data = { | ||
'impact_categories': [('foo', 'a'), ('foo', 'b')], | ||
'normalizations': {('foo', 'a'): ('norm', 'standard'), ('foo', 'b'): ('norm', 'standard')}, | ||
'weightings': {('norm', 'standard'): ('weighting',)} | ||
"impact_categories": [("a",), ("b",)], | ||
"normalizations": {("norm",): [("a",), ("b",)]}, | ||
"weightings": {("weighting",): [("norm",)]}, | ||
} | ||
assert MethodConfig(**data) | ||
|
||
|
||
def test_method_config_wrong_tuple_types(): | ||
data = { | ||
"impact_categories": [("a",), (1,)], | ||
} | ||
with pytest.raises(ValidationError): | ||
assert MethodConfig(**data) | ||
|
||
data = { | ||
"impact_categories": [("a",), 1], | ||
} | ||
with pytest.raises(ValidationError): | ||
assert MethodConfig(**data) | ||
|
||
data = { | ||
"impact_categories": [("a",), ("b",)], | ||
"normalizations": {("norm",): [("a",), (1,)]}, | ||
} | ||
with pytest.raises(ValidationError): | ||
assert MethodConfig(**data) | ||
|
||
data = { | ||
"impact_categories": [("a",), ("b",)], | ||
"normalizations": {("norm",): [(1,), ("b",)]}, | ||
} | ||
with pytest.raises(ValidationError): | ||
assert MethodConfig(**data) | ||
|
||
data = { | ||
"impact_categories": [("a",), ("b",)], | ||
"normalizations": {("norm",): [("a",), ("b",)]}, | ||
"weightings": {("norm",): (1,)}, | ||
} | ||
with pytest.raises(ValidationError): | ||
assert MethodConfig(**data) | ||
|
||
data = { | ||
"impact_categories": [("a",), ("b",)], | ||
"normalizations": {("norm",): [("a",), ("b",)]}, | ||
"weightings": {("norm",): 1}, | ||
} | ||
with pytest.raises(ValidationError): | ||
assert MethodConfig(**data) | ||
|
||
|
||
def test_method_config_missing_normalization_reference(): | ||
data = { | ||
"impact_categories": [("foo", "a"), ("foo", "b")], | ||
"normalizations": {("norm", "standard"): [("foo", "c")]}, | ||
} | ||
with pytest.raises(ValueError): | ||
assert MethodConfig(**data) | ||
|
||
|
||
def test_method_config_normalization_overlaps_impact_categories(): | ||
data = { | ||
"impact_categories": [("foo", "a"), ("foo", "b")], | ||
"normalizations": {("foo", "a"): [("foo", "a")]}, | ||
} | ||
with pytest.raises(ValueError): | ||
assert MethodConfig(**data) | ||
|
||
|
||
def test_method_config_weighting_overlaps_impact_categories(): | ||
data = { | ||
"impact_categories": [("foo", "a"), ("foo", "b")], | ||
"normalizations": {("normalization",): [("foo", "a")]}, | ||
"weightings": {("foo", "a"): [("foo", "a")]}, | ||
} | ||
with pytest.raises(ValueError): | ||
assert MethodConfig(**data) | ||
|
||
|
||
def test_method_config_weighting_overlaps_normalizations(): | ||
data = { | ||
"impact_categories": [("foo", "a"), ("foo", "b")], | ||
"normalizations": {("normalization",): [("foo", "a")]}, | ||
"weightings": {("normalization",): [("normalization",)]}, | ||
} | ||
with pytest.raises(ValueError): | ||
assert MethodConfig(**data) | ||
|
||
|
||
def test_method_config_weighting_missing_reference(): | ||
data = { | ||
"impact_categories": [("foo", "a"), ("foo", "b")], | ||
"normalizations": {("normalization",): [("foo", "a")]}, | ||
"weightings": {("normalization",): [("foo", "c")]}, | ||
} | ||
with pytest.raises(ValueError): | ||
assert MethodConfig(**data) |