Example #1
0
def test_true_value_allocation_factors(no_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': 2
        }]
    }, {
        'name': 'third',
        'type': 'biosphere',
        'amount': 30
    }]}
    obj, lst = economic_allocation(dataset)
    # Allocation by revenue; revenue is (4 * 2.5 = 10, 2 * 10 = 20) = (1/3, 2/3)
    assert [x[0] for x in lst] == [1/3, 2/3]
Example #2
0
def test_true_value_allocation_factors(no_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': 2
            }]
        }, {
            'name': 'third',
            'type': 'biosphere',
            'amount': 30
        }]
    }
    obj, lst = economic_allocation(dataset)
    # Allocation by revenue; revenue is (4 * 2.5 = 10, 2 * 10 = 20) = (1/3, 2/3)
    assert [x[0] for x in lst] == [1 / 3, 2 / 3]
Example #3
0
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
Example #4
0
def test_economic_allocation_outputs(no_allocation):
    dataset = {
        'exchanges': [{
            'type': 'reference product',
            'name': 'foo',
            'amount': 2,
            'properties': [{
                'name': 'price',
                'amount': 14
            }]
        }]
    }
    expected = {
        'type': 'reference product',
        'name': 'foo',
        'amount': 2,
        'properties': [{
            'name': 'price',
            'amount': 14
        }]
    }
    obj, lst = economic_allocation(dataset)
    assert obj is dataset
    assert list(lst) == [(1, expected)]
Example #5
0
def test_economic_allocation_outputs(no_allocation):
    dataset = {
        'exchanges': [{
            'type': 'reference product',
            'name': 'foo',
            'amount': 2,
            'properties': [{
                'name': 'price',
                'amount': 14
            }]
        }]
    }
    expected = {
        'type': 'reference product',
        'name': 'foo',
        'amount': 2,
        'properties': [{
            'name': 'price',
            'amount': 14
        }]
    }
    obj, lst = economic_allocation(dataset)
    assert obj is dataset
    assert list(lst) == [(1, expected)]
Example #6
0
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
Example #7
0
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
Example #8
0
def test_allocation_function_output_valid(cogen):
    for new_ds in economic_allocation(cogen):
        assert dataset_schema(new_ds)
Example #9
0
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
Example #10
0
def test_allocation_function_output_valid(cogen):
    for new_ds in economic_allocation(cogen):
        assert dataset_schema(new_ds)
Example #11
0
def test_normal_economic_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': 2
            }]
        }, {
            'name': 'third',
            'type': 'biosphere',
            'amount': 60
        }]
    }
    # Allocation by revenue; revenue is (4 * 2.5 = 10, 2 * 10 = 20) = (1/3, 2/3)
    # So biosphere amount is (20, 40)
    # Normalize to production of 1: 20 / 4, 40 / 10 = (5, 4)
    expected = [{
        'exchanges': [{
            'amount': 1.0,
            'name': 'first',
            'type': 'reference product',
            'properties': [{
                'name': 'price',
                'amount': 2.5
            }]
        }, {
            'amount': 0.0,
            'name': 'second',
            'type': 'dropped product',
            'properties': [{
                'name': 'price',
                'amount': 2
            }]
        }, {
            'amount': 5,
            'name': 'third',
            'type': 'biosphere',
        }]
    }, {
        'exchanges': [{
            'amount': 1.0,
            'name': 'second',
            'type': 'reference product',
            'properties': [{
                'name': 'price',
                'amount': 2
            }]
        }, {
            'type': 'dropped product',
            'amount': 0.0,
            'name': 'first',
            'properties': [{
                'name': 'price',
                'amount': 2.5
            }]
        }, {
            'type': 'biosphere',
            'name': 'third',
            'amount': 4
        }]
    }]
    assert economic_allocation(dataset) == expected
Example #12
0
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
Example #13
0
def test_normal_economic_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': 2
        }]
    }, {
        'name': 'third',
        'type': 'biosphere',
        'amount': 60
    }]}
    # Allocation by revenue; revenue is (4 * 2.5 = 10, 2 * 10 = 20) = (1/3, 2/3)
    # So biosphere amount is (20, 40)
    # Normalize to production of 1: 20 / 4, 40 / 10 = (5, 4)
    expected = [{
        'exchanges': [{
            'amount': 1.0,
            'name': 'first',
            'type': 'reference product',
            'properties': [{
                'name': 'price',
                'amount': 2.5}]
        }, {
            'amount': 0.0,
            'name': 'second',
            'type': 'dropped product',
            'properties': [{
                'name': 'price',
                'amount': 2
            }]
        }, {
            'amount': 5,
            'name': 'third',
            'type': 'biosphere',
        }]
    }, {
        'exchanges': [{
            'amount': 1.0,
            'name': 'second',
            'type': 'reference product',
            'properties': [{
                'name': 'price',
                'amount': 2
            }]
        }, {
            'type': 'dropped product',
            'amount': 0.0,
            'name': 'first',
            'properties': [{
                'name': 'price',
                'amount': 2.5
            }]
        }, {
            'type': 'biosphere',
            'name': 'third',
            'amount': 4
        }]
    }]
    assert economic_allocation(dataset) == expected
Example #14
0
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