Skip to content

Commit

Permalink
Remove arrow. Fixes #120.
Browse files Browse the repository at this point in the history
  • Loading branch information
cmutel committed Nov 15, 2016
1 parent 7d4f8a7 commit 62d9e34
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 4 deletions.
26 changes: 23 additions & 3 deletions ocelot/transformations/uncertainty/pedigree.py
@@ -1,5 +1,5 @@
# -*- coding: utf-8 -*-
import arrow
from datetime import datetime


PEDIGREE_MATRIX_VALUES = {
Expand Down Expand Up @@ -32,12 +32,32 @@ def get_pedigree_variance(pm, version="original"):
return sum(PEDIGREE_MATRIX_VALUES[version][k][v - 1] for k, v in pm.items())


def get_difference_in_years(first, second):
"""Get absolute value of difference in years between ``first`` and ``second``.
Input values can be integers, or datetime strings like "2002-12-31". Only uses the year of a datetime string, and no rounding, so the difference between "2002-01-01" and "2002-12-31" is zero.
Returns an integer.
"""
year = lambda x: x if isinstance(x, int) else datetime.strptime(x, "%Y-%m-%d").year
return abs(year(first) - year(second))


def adjust_pedigree_matrix_time(ds, exc, year):
"""Adjust values of ``temporal correlation`` in the pedigree matrix of exchange ``exc`` to a new year ``year``.
Does nothing if pedigree matrix not present in exchange.
Datasets are defined for a certain temporal period. If the baseline year is outside that period, the values of ``temporal correlation`` need to be modified to adjust the increased uncertainty that comes from applying the dataset to a different year. Modified numbers from a table provided by IFU Hamburg.
Modifies the exchange in place, and returns the modified exchange.
"""
if 'pedigree matrix' not in exc:
return exc

new_year = year if isinstance(year, int) else arrow.get(year).year
difference = new_year - arrow.get(ds['end date']).year
difference = get_difference_in_years(ds['end date'], year)
current_value = exc['pedigree matrix']['temporal correlation']

if difference > 0:
Expand Down
1 change: 0 additions & 1 deletion setup.py
Expand Up @@ -38,7 +38,6 @@ def package_files(directory):
},
install_requires=[
'appdirs',
'arrow',
'bw2parameters',
'docopt',
'docutils',
Expand Down
16 changes: 16 additions & 0 deletions tests/uncertainty/pedigree.py
Expand Up @@ -6,6 +6,8 @@
apmt = adjust_pedigree_matrix_time


### Test changing pedigree matrix variance

def test_get_pedigree_variance_error():
matrix = {
"reliability": 1,
Expand Down Expand Up @@ -36,6 +38,20 @@ def test_get_pedigree_variance_no_keys():
def test_adjust_pedigree_matrix_time_no_pm():
assert apmt(None, {}, None) == {}


### Test changing pedigree matrix temporal correlation

def test_get_difference_in_years():
a = "2002-12-31"
b = "2002-01-01"
c = "2012-01-31"
assert get_difference_in_years(a, b) == 0
assert get_difference_in_years(b, a) == 0
assert get_difference_in_years(b, c) == 10
assert get_difference_in_years(c, a) == 10
assert get_difference_in_years(2006, a) == 4


def test_adjust_pedigree_matrix_time():
def ds_and_exc(current=0):
return {'end date': '2000-01-01'}, {'pedigree matrix': {'temporal correlation': current}}
Expand Down

0 comments on commit 62d9e34

Please sign in to comment.