Skip to content

Commit

Permalink
Repair all uncertainty distributions. Fixes #104.
Browse files Browse the repository at this point in the history
  • Loading branch information
cmutel committed Sep 19, 2016
1 parent 6e79fc9 commit 7b13874
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 0 deletions.
2 changes: 2 additions & 0 deletions ocelot/transformations/parameterization/__init__.py
Expand Up @@ -10,11 +10,13 @@
replace_reserved_words,
)
from .recalculation import recalculate
from .uncertainty import repair_all_uncertainty_distributions
from .validation import variable_names_are_unique

fix_ecoinvent_parameters = Collection(
fix_specific_ecoinvent_issues,
replace_implicit_references,
repair_all_uncertainty_distributions,
fix_known_bad_formula_strings,
lowercase_all_parameters,
fix_math_formulas,
Expand Down
17 changes: 17 additions & 0 deletions ocelot/transformations/parameterization/uncertainty.py
@@ -0,0 +1,17 @@
# -*- coding: utf-8 -*-
from ..uncertainty import get_uncertainty_class
from ..utils import iterate_all_uncertainties
import logging


def repair_all_uncertainty_distributions(data):
"""Repair all uncertainty distributions.
Uses distribution-specific ``repair`` methods."""
for ds in data:
for obj in iterate_all_uncertainties(ds):
if not obj.get('uncertainty'):
continue
# TODO: Add detailed log message
obj = get_uncertainty_class(obj).repair(obj)
return data
16 changes: 16 additions & 0 deletions ocelot/transformations/utils.py
Expand Up @@ -106,6 +106,22 @@ def iterate_all_parameters(dataset):
yield parameter


def iterate_all_uncertainties(dataset):
"""Generator that returns all objects with an uncertainty distribution."""
for exc in dataset['exchanges']:
if "uncertainty" in exc:
yield exc
pv = exc.get("production volume", {})
if "uncertainty" in pv:
yield pv
for prop in exc.get('properties', []):
if "uncertainty" in prop:
yield prop
for parameter in dataset.get('parameters', []):
if "uncertainty" in parameter:
yield parameter


def get_biggest_pv_to_exchange_ratio(dataset):
"""Return the largest ration of production volume to exchange amount.
Expand Down
36 changes: 36 additions & 0 deletions tests/parameterization/parameterizaton_uncertainty.py
@@ -0,0 +1,36 @@
# -*- coding: utf-8 -*-
from ocelot.transformations.parameterization.uncertainty import repair_all_uncertainty_distributions as repair
import numpy as np


def test_repair():
given = [{
'exchanges': [{
'amount': -4,
'uncertainty': {
'type': 'lognormal',
'variance with pedigree uncertainty': 0.7
},
'pedigree matrix': {}
}, {
'amount': 0,
'uncertainty': {'type': 'lognormal'}
}]
}]
expected = [{
'exchanges': [{
'amount': -4,
'pedigree matrix': {},
'uncertainty': {
'type': 'lognormal',
'mean': 4,
'mu': np.log(4),
'negative': True,
'variance': 0.7,
'variance with pedigree uncertainty': 0.7
},
}, {
'amount': 0
}]
}]
assert repair(given) == expected
38 changes: 38 additions & 0 deletions tests/transformations/transformations_utils.py
Expand Up @@ -328,6 +328,44 @@ def test_iterate_all_parameters(parameterized_ds):
assert next(generator) == parameterized_ds['parameters'][0]
assert next(generator) == parameterized_ds['parameters'][1]

@pytest.fixture(scope="function")
def uncertain_ds():
return {
'exchanges': [{
'amount': 3.1415926535,
'uncertainty': '',
'production volume': { # Nonsensical but should work
'uncertainty': '',
'amount': 42
},
'properties': [{
'uncertainty': '',
'amount': 17
}]
}, {
'uncertainty': '',
'properties': [{
'uncertainty': '',
}]
}],
'parameters': [{
'uncertainty': '',
}, {
'uncertainty': '',
'amount': 1
}]
}

def test_iterate_all_uncertainties(uncertain_ds):
generator = iterate_all_uncertainties(uncertain_ds)
assert next(generator) == uncertain_ds['exchanges'][0]
assert next(generator) == uncertain_ds['exchanges'][0]['production volume']
assert next(generator) == uncertain_ds['exchanges'][0]['properties'][0]
assert next(generator) == uncertain_ds['exchanges'][1]
assert next(generator) == uncertain_ds['exchanges'][1]['properties'][0]
assert next(generator) == uncertain_ds['parameters'][0]
assert next(generator) == uncertain_ds['parameters'][1]

def test_activity_hash():
given = {
'name': 'a',
Expand Down

0 comments on commit 7b13874

Please sign in to comment.