Skip to content

Commit

Permalink
Merge branch 'master' into activity-links
Browse files Browse the repository at this point in the history
# Conflicts:
#	ocelot/errors.py
  • Loading branch information
cmutel committed Aug 23, 2016
2 parents 5e58ee2 + b4d3279 commit daf43e4
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 3 deletions.
4 changes: 4 additions & 0 deletions ocelot/configuration.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
# -*- coding: utf-8 -*-
from .transformations import (
drop_zero_pv_row_datasets,
ensure_mandatory_properties,
fix_ecoinvent_parameters,
relabel_global_to_row,
validate_markets,
variable_names_are_unique,
)
from .transformations.cutoff import cleanup_activity_links
Expand All @@ -20,6 +22,8 @@ def __iter__(self):
# Default config for now is cutoff
default_configuration = [
variable_names_are_unique,
# ensure_mandatory_properties,
validate_markets,
fix_ecoinvent_parameters,
cleanup_activity_links,
relabel_global_to_row,
Expand Down
5 changes: 5 additions & 0 deletions ocelot/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,8 @@ class InvalidMarket(Exception):
class UnresolvableActivityLink(OcelotError):
"""Activity link can't be uniquely resolved to an exchange"""
pass


class MissingMandatoryProperty(Exception):
"""Exchange is missing a mandatory property"""
pass
4 changes: 2 additions & 2 deletions ocelot/transformations/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@
variable_names_are_unique,
)
from .validation import (
ensure_markets_dont_consume_their_ref_product,
ensure_markets_only_have_one_reference_product,
ensure_mandatory_properties,
validate_markets,
)
29 changes: 29 additions & 0 deletions ocelot/transformations/validation.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
# -*- coding: utf-8 -*-
from .utils import allocatable_production
from ..collection import Collection
from ..errors import (
InvalidMarket,
InvalidMarketExchange,
InvalidMultioutputDataset,
MissingMandatoryProperty,
)


Expand Down Expand Up @@ -49,3 +51,30 @@ def ensure_markets_dont_consume_their_ref_product(data):
message = "Market dataset has exchanges which consume the ref. product:\n{}"
raise InvalidMarketExchange(message.format(ds['filepath']))
return data


validate_markets = Collection(
ensure_markets_only_have_one_reference_product,
ensure_markets_dont_consume_their_ref_product,
)


def ensure_mandatory_properties(data):
"""If an exchange has properties, it must include the mandatory properties.
dry mass, water in wet mass, water content, wet mass, carbon content fossil, and carbon content non fossil."""
MANDATORY = {"dry mass", "water in wet mass", "water content", "wet mass", "carbon content fossil", "carbon content non-fossil"}

for ds in data:
for exc in ds['exchanges']:
if exc.get("properties"):
for prop in exc['properties']:
if prop['name'] in MANDATORY:
if 'amount' not in prop:
raise ValueError
missing = MANDATORY.difference({p['name'] for p in exc['properties']})
if missing:
message = "Exchange is missing mandatory properties: {}\n{}"
raise MissingMandatoryProperty(message.format(missing, ds['filepath']))
return data

47 changes: 46 additions & 1 deletion tests/transformations/validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
InvalidMarket,
InvalidMarketExchange,
InvalidMultioutputDataset,
MissingMandatoryProperty,
)
from ocelot.transformations.validation import (
check_single_output_activity,
ensure_markets_only_have_one_reference_product,
ensure_mandatory_properties,
ensure_markets_dont_consume_their_ref_product,
ensure_markets_only_have_one_reference_product,
)
import pytest

Expand Down Expand Up @@ -121,3 +123,46 @@ def test_ensure_markets_dont_consume_their_ref_product():
}]
with pytest.raises(InvalidMarketExchange):
ensure_markets_dont_consume_their_ref_product(bad)

def test_ensure_mandatory_properties():
given = [{'exchanges': [{
'name': 'something',
'type': 'from technosphere'
}]
}]
assert ensure_mandatory_properties(given)

missing = [{
'filepath': '',
'exchanges': [{
'name': 'something',
'type': 'from technosphere',
'properties': [
{'amount': 1, 'name': "dry mass"},
{'amount': 1, 'name': "water in wet mass"},
{'amount': 1, 'name': "wet mass"},
{'amount': 1, 'name': "water content"},
{'amount': 1, 'name': "carbon content fossil"},
]
}]
}]
with pytest.raises(MissingMandatoryProperty):
ensure_mandatory_properties(missing)

missing = [{
'filepath': '',
'exchanges': [{
'name': 'something',
'type': 'from technosphere',
'properties': [
{'amount': 1, 'name': "dry mass"},
{'amount': 1, 'name': "water in wet mass"},
{'amount': 1, 'name': "wet mass"},
{'name': "water content"},
{'amount': 1, 'name': "carbon content fossil"},
{'amount': 1, 'name': "carbon content non-fossil"},
]
}]
}]
with pytest.raises(ValueError):
ensure_mandatory_properties(missing)

0 comments on commit daf43e4

Please sign in to comment.