Skip to content

Commit

Permalink
Add single_output decorator
Browse files Browse the repository at this point in the history
  • Loading branch information
cmutel committed Sep 19, 2016
1 parent fc71914 commit ca8bf23
Show file tree
Hide file tree
Showing 12 changed files with 54 additions and 308 deletions.
4 changes: 3 additions & 1 deletion ocelot/model.py
Expand Up @@ -17,11 +17,13 @@
import os
import shutil
import sys
import wrapt


def apply_transformation(function, counter, data, output_dir, save_strategy):
# A `function` can be a list of functions
if isinstance(function, Iterable):
if (isinstance(function, Iterable)
and not isinstance(function, wrapt.FunctionWrapper)):
for obj in function:
data = apply_transformation(obj, counter, data,
output_dir, save_strategy)
Expand Down
3 changes: 1 addition & 2 deletions ocelot/transformations/__init__.py
@@ -1,6 +1,5 @@
# -*- coding: utf-8 -*-
from ..collection import Collection
from ..wrapper import TransformationWrapper
from .activity_links import (
check_activity_link_validity,
add_hard_linked_production_volumes,
Expand All @@ -23,5 +22,5 @@
pv_cleanup = Collection(
ensure_production_exchanges_have_production_volume,
add_pv_to_allocatable_byproducts,
TransformationWrapper(create_pv_parameters),
create_pv_parameters,
)
2 changes: 1 addition & 1 deletion ocelot/transformations/cutoff/allocation.py
Expand Up @@ -162,5 +162,5 @@ def allocation_method_filter(ds):
*[TransformationWrapper(func,
create_allocation_filter(label))
for label, func in ALLOCATION_METHODS[1:]],
TransformationWrapper(delete_allocation_method),
delete_allocation_method,
)
2 changes: 2 additions & 0 deletions ocelot/transformations/cutoff/utils.py
Expand Up @@ -2,6 +2,7 @@
from ..utils import (
choose_reference_product_exchange,
get_single_reference_product,
single_input,
)


Expand Down Expand Up @@ -36,6 +37,7 @@ def flip_non_allocatable_byproducts(dataset):
return dataset


@single_input
def delete_allocation_method(dataset):
"""Delete key ``allocation method`` if present"""
if "allocation method" in dataset:
Expand Down
5 changes: 3 additions & 2 deletions ocelot/transformations/cutoff/wastes.py
@@ -1,10 +1,10 @@
# -*- coding: utf-8 -*-
from ...collection import Collection
from ...wrapper import TransformationWrapper
from ..utils import (
allocatable_production,
choose_reference_product_exchange,
get_single_reference_product,
single_input,
)
from ..uncertainty import scale_exchange
from .economic import economic_allocation
Expand Down Expand Up @@ -142,6 +142,7 @@ def create_recycled_content_datasets(data):
}


@single_input
def flip_non_allocatable_byproducts(dataset):
"""Change non-allocatable byproducts (i.e. classification ``recyclable`` or ``waste``) from outputs to technosphere to inputs from technosphere.
Expand Down Expand Up @@ -176,5 +177,5 @@ def flip_non_allocatable_byproducts(dataset):
handle_waste_outputs = Collection(
rename_recyclable_content_exchanges,
create_recycled_content_datasets,
TransformationWrapper(flip_non_allocatable_byproducts),
flip_non_allocatable_byproducts,
)
2 changes: 2 additions & 0 deletions ocelot/transformations/parameterization/production_volumes.py
@@ -1,8 +1,10 @@
# -*- coding: utf-8 -*-
from ..utils import single_input
import logging
import uuid


@single_input
def create_pv_parameters(dataset):
"""Remove all production volume parameterization.
Expand Down
11 changes: 10 additions & 1 deletion ocelot/transformations/utils.py
Expand Up @@ -5,6 +5,7 @@
from pprint import pformat
import hashlib
import pandas as pd
import wrapt


### Activity identifiers
Expand Down Expand Up @@ -259,7 +260,6 @@ def choose_reference_product_exchange(dataset, exchange, allocation_factor=1):
* ``production volume`` is deleted if present.
"""
# TODO: Make sure exchange in allocatable_production(dataset)?
obj = deepcopy(dataset)
if not exchange['amount']:
message = "Zero production amount for new reference product exchange:\n{}\nIn dataset:\n{}"
Expand All @@ -277,3 +277,12 @@ def choose_reference_product_exchange(dataset, exchange, allocation_factor=1):
for exc in nonproduction_exchanges(dataset)
]
return normalize_reference_production_amount(obj)


### Function helpers

@wrapt.decorator
def single_input(wrapped, instance, args, kwargs):
"""Decorator to allow a transformation to transformation function to take a single dataset input."""
data = kwargs.get('data') or args[0]
return [ds for elem in data for ds in wrapped(elem)]

0 comments on commit ca8bf23

Please sign in to comment.