def list_techno_inputs(id, data): ds = get_dataset_by_id(id, data) inputs = [i for i in ds['exchanges'] if i['type'] == "from technosphere"] print('There are {} inputs to the technosphere in {} {}'.format( len(inputs), ds['name'], ds['location'])) to_df = [] if len(inputs) > 0: for e in inputs: a = e['amount'] u = e['unit'] item = get_dataset_by_code(e['code'], data) ref_product = get_single_reference_product(item) pv = ref_product['production volume']['amount'] to_df.append({ 'name': item['name'], 'location': item['location'], 'amount': "{0:.4f}".format(a), 'unit': u, 'percentage': "{0:.2f}%".format(a * 100) }) df = pd.DataFrame(to_df) df.sort_values(['name', 'percentage'], ascending=False, inplace=True) cols = ['name', 'location', 'amount', 'unit', 'percentage'] #print(df[cols]) return df[cols]
def test_normal_true_value_allocation(): dataset = {'exchanges': [{ 'name': 'first', 'type': 'reference product', 'amount': 4, 'properties': [{ 'name': 'price', 'amount': 2.5 }] }, { 'name': 'second', 'type': 'reference product', 'amount': 10, 'properties': [{ 'name': 'price', 'amount': 4 }, { 'name': 'true value relation', 'amount': 7.5 }] }, { 'name': 'third', 'type': 'reference product', 'amount': 10, 'properties': [{ 'name': 'price', 'amount': 5 }, { 'name': 'true value relation', 'amount': 2.5 }] }, { 'name': 'fourth', 'type': 'biosphere', 'amount': 100 }]} # Total revenue is 100, of which 90 has true value # Total true value is 100, of which 75 comes from ``second`` get_biosphere_exchange = lambda x: [exc for exc in x['exchanges'] if exc['name'] == 'fourth'][0] for ds in economic_allocation(dataset): assert len(ds['exchanges']) == 4 assert sum(1 for exc in ds['exchanges'] if exc['type'] == 'dropped product') == 2 assert sum(1 for exc in ds['exchanges'] if exc['amount'] == 0) == 2 rp = get_single_reference_product(ds) bio_exchange = get_biosphere_exchange(ds) assert rp['type'] == 'reference product' assert rp['amount'] == 1 if rp['name'] == 'first': assert bio_exchange['amount'] == 100 / 4 * (4 * 2.5 / 100) elif rp['name'] == 'second': assert bio_exchange['amount'] == (7.5 * 10) / (7.5 * 10 + 2.5 * 10) * 90 / 100 * 100 / 10 else: assert bio_exchange['amount'] == (2.5 * 10) / (7.5 * 10 + 2.5 * 10) * 90 / 100 * 100 / 10
def edit_pv_of_reference_products(data, edits_to_make): """edit the production volumes of a set of activities this is specified in a dictionary with the keys as the ids and the values as the new production volume Note - this function is 'curried' with its dictionary to become edit_specified_pvs(data)""" for edit in edits_to_make: dataset = get_dataset_by_id(edit, data) ref_product = get_single_reference_product(dataset) original_pv = ref_product['production volume']['amount'] new_pv = edits_to_make[edit] ref_product['production volume']['amount'] = float(new_pv) print ('Production volume for {} {} changed from {} to {}'.format(dataset['name'], dataset['location'],original_pv, new_pv)) logging.info({ 'type': 'table element', 'data': (dataset['name'], dataset['location'],original_pv, new_pv) }) return data
def test_normal_true_value_allocation(): dataset = { 'exchanges': [{ 'name': 'first', 'type': 'reference product', 'amount': 4, 'properties': [{ 'name': 'price', 'amount': 2.5 }] }, { 'name': 'second', 'type': 'reference product', 'amount': 10, 'properties': [{ 'name': 'price', 'amount': 4 }, { 'name': 'true value relation', 'amount': 7.5 }] }, { 'name': 'third', 'type': 'reference product', 'amount': 10, 'properties': [{ 'name': 'price', 'amount': 5 }, { 'name': 'true value relation', 'amount': 2.5 }] }, { 'name': 'fourth', 'type': 'biosphere', 'amount': 100 }] } # Total revenue is 100, of which 90 has true value # Total true value is 100, of which 75 comes from ``second`` get_biosphere_exchange = lambda x: [ exc for exc in x['exchanges'] if exc['name'] == 'fourth' ][0] for ds in economic_allocation(dataset): assert len(ds['exchanges']) == 4 assert sum(1 for exc in ds['exchanges'] if exc['type'] == 'dropped product') == 2 assert sum(1 for exc in ds['exchanges'] if exc['amount'] == 0) == 2 rp = get_single_reference_product(ds) bio_exchange = get_biosphere_exchange(ds) assert rp['type'] == 'reference product' assert rp['amount'] == 1 if rp['name'] == 'first': assert bio_exchange['amount'] == 100 / 4 * (4 * 2.5 / 100) elif rp['name'] == 'second': assert bio_exchange['amount'] == (7.5 * 10) / ( 7.5 * 10 + 2.5 * 10) * 90 / 100 * 100 / 10 else: assert bio_exchange['amount'] == (2.5 * 10) / ( 7.5 * 10 + 2.5 * 10) * 90 / 100 * 100 / 10
def test_allocate_cogeneration_dataset(cogen): """Cogeneration dataset produces: 1. heat, district or industrial, natural gas * Reference product * Price: 0.0106 €/MJ * True value: 0.184213553594 €/MJ * Amount: 4.1 MJ * Revenue: 0.04346 € * True value revenue: 0.7552755697353999 € 2. electricity, high voltage * Allocatable byproduct * Price: 0.0977 €/kWh * True value: 3.6 €/kWh * Amount: 1 kWh * Revenue: 0.0977 € * True value revenue: 3.6 € Allocation factors: 0.3078775857183338, 0.6921224142816661 TV allocation factors: 0.17341625291951063, 0.8265837470804893 Elementary exchange: 0.622135922330097 kg Carbon dioxide, fossil Input: 6.47766990291262E-10 unit gas power plant, 100MW electrical Input: 0.298730395817774 m3 natural gas, high pressure This real world dataset does not test for a mix of true-value and non-true-value exchanges, but this is tested in the artificial test cases above. """ datasets = economic_allocation(cogen) assert len(datasets) == 2 for ds in datasets: assert len(ds['exchanges']) == 5 for key, value in list(cogen.items()): if key == 'exchanges': continue else: assert ds[key] == value rp = get_single_reference_product(ds) if rp['name'] == 'heat, district or industrial, natural gas': expected = { 'heat, district or industrial, natural gas': { 'amount': 1, 'type': 'reference product', }, 'electricity, high voltage': { 'amount': 0, 'type': 'dropped product', }, 'Carbon dioxide, fossil': { 'amount': 0.17341625291951063 * 0.622135922330097 / 4.1, }, 'gas power plant, 100MW electrical': { 'amount': 0.17341625291951063 * 6.47766990291262E-10 / 4.1, }, 'natural gas, high pressure': { 'amount': 0.17341625291951063 * 0.298730395817774 / 4.1, }, } else: expected = { 'heat, district or industrial, natural gas': { 'amount': 0, 'type': 'dropped product', }, 'electricity, high voltage': { 'amount': 1, 'type': 'reference product', }, 'Carbon dioxide, fossil': { 'amount': 0.8265837470804893 * 0.622135922330097, }, 'gas power plant, 100MW electrical': { 'amount': 0.8265837470804893 * 6.47766990291262E-10, }, 'natural gas, high pressure': { 'amount': 0.8265837470804893 * 0.298730395817774, }, } for exc in ds['exchanges']: print((exc['name'])) for key, value in list(expected[exc['name']].items()): if isinstance(value, Number): assert allclose(exc[key], value) else: assert exc[key] == value
def test_allocate_cogeneration_dataset(cogen): """Cogeneration dataset produces: 1. heat, district or industrial, natural gas * Reference product * Price: 0.0106 €/MJ * True value: 0.184213553594 €/MJ * Amount: 4.1 MJ * Revenue: 0.04346 € * True value revenue: 0.7552755697353999 € 2. electricity, high voltage * Allocatable byproduct * Price: 0.0977 €/kWh * True value: 3.6 €/kWh * Amount: 1 kWh * Revenue: 0.0977 € * True value revenue: 3.6 € Allocation factors: 0.3078775857183338, 0.6921224142816661 TV allocation factors: 0.17341625291951063, 0.8265837470804893 Elementary exchange: 0.622135922330097 kg Carbon dioxide, fossil Input: 6.47766990291262E-10 unit gas power plant, 100MW electrical Input: 0.298730395817774 m3 natural gas, high pressure This real world dataset does not test for a mix of true-value and non-true-value exchanges, but this is tested in the artificial test cases above. """ datasets = economic_allocation(cogen) assert len(datasets) == 2 for ds in datasets: assert len(ds['exchanges']) == 5 for key, value in cogen.items(): if key == 'exchanges': continue else: assert ds[key] == value rp = get_single_reference_product(ds) if rp['name'] == 'heat, district or industrial, natural gas': expected = { 'heat, district or industrial, natural gas': { 'amount': 1, 'type': 'reference product', }, 'electricity, high voltage': { 'amount': 0, 'type': 'dropped product', }, 'Carbon dioxide, fossil': { 'amount': 0.17341625291951063 * 0.622135922330097 / 4.1, }, 'gas power plant, 100MW electrical': { 'amount': 0.17341625291951063 * 6.47766990291262E-10 / 4.1, }, 'natural gas, high pressure': { 'amount': 0.17341625291951063 * 0.298730395817774 / 4.1, }, } else: expected = { 'heat, district or industrial, natural gas': { 'amount': 0, 'type': 'dropped product', }, 'electricity, high voltage': { 'amount': 1, 'type': 'reference product', }, 'Carbon dioxide, fossil': { 'amount': 0.8265837470804893 * 0.622135922330097, }, 'gas power plant, 100MW electrical': { 'amount': 0.8265837470804893 * 6.47766990291262E-10, }, 'natural gas, high pressure': { 'amount': 0.8265837470804893 * 0.298730395817774, }, } for exc in ds['exchanges']: print(exc['name']) for key, value in expected[exc['name']].items(): if isinstance(value, Number): assert allclose(exc[key], value) else: assert exc[key] == value
def test_allocation_cardboard(cardboard): """Cardboard dataset produces: 1. corrugated board box * Reference product * Price: 0.843326883 €/kg * Amount: 1000 kg * Revenue: 843.326883 € 2. residual softwood, wet * Allocatable byproduct * Price: 19.4156914399732 €/m3 * Amount: 0.00209150326797386 m3 * Revenue: 0.04060798209667605 € Allocation factors: 0.9999518501927914, 4.814980720846648e-05 Elementary exchange: 0.000163 kg Propane Elementary exchange: 0.03 kg Nitrogen oxides Input: 5.95 kg potato starch Input: 19.9481865284974 m3 natural gas, low pressure Waste: 0.046 kg waste mineral oil """ datasets = economic_allocation(cardboard) assert len(datasets) == 2 for ds in datasets: assert len(ds['exchanges']) == 7 for key, value in list(cardboard.items()): if key == 'exchanges': continue else: assert ds[key] == value rp = get_single_reference_product(ds) if rp['name'] == 'corrugated board box': expected = { 'corrugated board box': { 'amount': 1, 'type': 'reference product', }, 'residual softwood, wet': { 'amount': 0, 'type': 'dropped product', }, 'waste mineral oil': { 'amount': 0.9999518501927914 * 0.046 / 1000, }, 'natural gas, low pressure': { 'amount': 0.9999518501927914 * 19.9481865284974 / 1000, }, 'potato starch': { 'amount': 0.9999518501927914 * 5.95 / 1000, }, 'Propane': { 'amount': 0.9999518501927914 * 0.000163 / 1000, }, 'Nitrogen oxides': { 'amount': 0.9999518501927914 * 0.03 / 1000 } } else: expected = { 'corrugated board box': { 'amount': 0, 'type': 'dropped product', }, 'residual softwood, wet': { 'amount': 1, 'type': 'reference product', }, 'waste mineral oil': { 'amount': 4.814980720846648e-05 * 0.046 / 0.00209150326797386, }, 'natural gas, low pressure': { 'amount': 4.814980720846648e-05 * 19.9481865284974 / 0.00209150326797386, }, 'potato starch': { 'amount': 4.814980720846648e-05 * 5.95 / 0.00209150326797386, }, 'Propane': { 'amount': 4.814980720846648e-05 * 0.000163 / 0.00209150326797386, }, 'Nitrogen oxides': { 'amount': 4.814980720846648e-05 * 0.03 / 0.00209150326797386 } } for exc in ds['exchanges']: print((exc['name'])) for key, value in list(expected[exc['name']].items()): if isinstance(value, Number): assert allclose(exc[key], value) else: assert exc[key] == value
def test_allocation_cardboard(cardboard): """Cardboard dataset produces: 1. corrugated board box * Reference product * Price: 0.843326883 €/kg * Amount: 1000 kg * Revenue: 843.326883 € 2. residual softwood, wet * Allocatable byproduct * Price: 19.4156914399732 €/m3 * Amount: 0.00209150326797386 m3 * Revenue: 0.04060798209667605 € Allocation factors: 0.9999518501927914, 4.814980720846648e-05 Elementary exchange: 0.000163 kg Propane Elementary exchange: 0.03 kg Nitrogen oxides Input: 5.95 kg potato starch Input: 19.9481865284974 m3 natural gas, low pressure Waste: 0.046 kg waste mineral oil """ datasets = economic_allocation(cardboard) assert len(datasets) == 2 for ds in datasets: assert len(ds['exchanges']) == 7 for key, value in cardboard.items(): if key == 'exchanges': continue else: assert ds[key] == value rp = get_single_reference_product(ds) if rp['name'] == 'corrugated board box': expected = { 'corrugated board box': { 'amount': 1, 'type': 'reference product', }, 'residual softwood, wet': { 'amount': 0, 'type': 'dropped product', }, 'waste mineral oil': { 'amount': 0.9999518501927914 * 0.046 / 1000, }, 'natural gas, low pressure': { 'amount': 0.9999518501927914 * 19.9481865284974 / 1000, }, 'potato starch': { 'amount': 0.9999518501927914 * 5.95 / 1000, }, 'Propane': { 'amount': 0.9999518501927914 * 0.000163 / 1000, }, 'Nitrogen oxides': { 'amount': 0.9999518501927914 * 0.03 / 1000 } } else: expected = { 'corrugated board box': { 'amount': 0, 'type': 'dropped product', }, 'residual softwood, wet': { 'amount': 1, 'type': 'reference product', }, 'waste mineral oil': { 'amount': 4.814980720846648e-05 * 0.046 / 0.00209150326797386, }, 'natural gas, low pressure': { 'amount': 4.814980720846648e-05 * 19.9481865284974 / 0.00209150326797386, }, 'potato starch': { 'amount': 4.814980720846648e-05 * 5.95 / 0.00209150326797386, }, 'Propane': { 'amount': 4.814980720846648e-05 * 0.000163 / 0.00209150326797386, }, 'Nitrogen oxides': { 'amount': 4.814980720846648e-05 * 0.03 / 0.00209150326797386 } } for exc in ds['exchanges']: print(exc['name']) for key, value in expected[exc['name']].items(): if isinstance(value, Number): assert allclose(exc[key], value) else: assert exc[key] == value