Пример #1
0
def test_rebalance_skip_no_uncertainty(data_for_testing):
    wb = DatabaseWaterBalancer(ecoinvent_version='test_db',
                               database_name="test_db",
                               biosphere="biosphere")
    ab = ActivityWaterBalancer(('test_db', 'K'), wb)
    ab._identify_strategy()
    assert ab.strategy == 'skip'
    assert getattr(ab, "static_ratio", "Nope") is "Nope"
    assert getattr(ab, "static_balance", "Nope") is "Nope"
Пример #2
0
def test_reset(data_for_testing):
    """Check _reset function"""
    wb = DatabaseWaterBalancer(ecoinvent_version='test_db',
                               database_name="test_db",
                               biosphere="biosphere")
    ab = ActivityWaterBalancer(('test_db', 'A'), wb)
    ab._identify_strategy()
    ab._define_balancing_parameters()
    assert ab._processed()
    ab._reset()
    assert not ab._processed()
    assert getattr(ab, "static_ratio") is None
    assert getattr(ab, "static_balance") is None
    assert getattr(ab, "activity_params") == []
Пример #3
0
def test_initially_unprocessed(data_for_testing):
    """Ensure processind happens at right moment """
    wb = DatabaseWaterBalancer(ecoinvent_version='test_db',
                               database_name="test_db",
                               biosphere="biosphere")
    ab = ActivityWaterBalancer(('test_db', 'A'), wb)
    assert not ab._processed()
    ab._identify_strategy()
    assert not ab._processed()
    ab._define_balancing_parameters()
    assert ab._processed()
Пример #4
0
def test_rebalance_tap_water_market(data_for_testing):
    wb = DatabaseWaterBalancer(ecoinvent_version='test_db',
                               database_name="test_db",
                               biosphere="biosphere")
    ab = ActivityWaterBalancer(('test_db', 'U'), wb)
    ab._identify_strategy()
    assert ab.strategy == 'set_static'
    matrix_data = ab.generate_samples(5)
    assert len(matrix_data) == 1
    assert matrix_data[0][0].shape[1] == 5
    assert matrix_data[0][0].shape[0] == 1
    assert np.allclose(0.1 * np.ones(shape=(1, 5)), matrix_data[0][0])
    assert len(matrix_data[0][1]) == 1
    assert matrix_data[0][1][0] == (('biosphere', 'Water 1, to water, in kg'),
                                    ('test_db', 'U'))
Пример #5
0
def test_rebalance_tap_water_market_losses(data_for_testing):
    wb = DatabaseWaterBalancer(ecoinvent_version='test_db',
                               database_name="test_db",
                               biosphere="biosphere")
    ab = ActivityWaterBalancer(('test_db', 'V'), wb)
    ab._identify_strategy()
    assert ab.strategy == 'default'
    ab._define_balancing_parameters()
    assert ab.static_ratio == 1
    assert ab.static_balance == 0
    matrix_data = ab.generate_samples(5)
    assert matrix_data[0][0].shape[1] == 5
    assert matrix_data[0][0].shape[0] == 1
    assert matrix_data[1][0].shape[1] == 5
    assert matrix_data[1][0].shape[0] == 3
    production_indices = [
        1 if t[2] == 'production' else 0 for t in matrix_data[1][1]
    ]
    techno_indices = [
        0 if t[2] == 'production' else 1 for t in matrix_data[1][1]
    ]
    in_total = (matrix_data[1][0] *
                np.array(techno_indices).reshape(-1, 1)).sum(axis=0)
    out_total = (matrix_data[1][0] * np.array(production_indices).reshape(
        -1, 1)).sum(axis=0) + matrix_data[0][0]
    assert np.allclose(in_total, out_total)
Пример #6
0
def test_rebalance_set_static_one_output(data_for_testing):
    wb = DatabaseWaterBalancer(ecoinvent_version='test_db',
                               database_name="test_db",
                               biosphere="biosphere")
    ab = ActivityWaterBalancer(('test_db', 'H'), wb)
    ab._identify_strategy()
    assert ab.strategy == 'set_static'
    exc_initial = [
        exc for exc in ab.act.exchanges()
        if exc.input.key == ('biosphere', 'Water 1, to water, in kg')
    ][0]
    assert exc_initial.get('uncertainty type', 0) == 2
    assert exc_initial.get('loc', 0) == np.log(exc_initial['amount'])

    ab._define_balancing_parameters()
    assert getattr(ab, "static_ratio", "Nope") == "Not calculated"
    assert getattr(ab, "static_balance", "Nope") == "Not calculated"
    exc_after = [
        exc for exc in ab.act.exchanges()
        if exc.input.key == ('biosphere', 'Water 1, to water, in kg')
    ][0]
    assert exc_after.get('water_formula', 0) == 'cst'
    assert ab.activity_params[0]['name'] == 'cst'
    assert ab.activity_params[0]['amount'] == exc_initial['amount']
    assert ab.activity_params[0]['loc'] == exc_initial['amount']
    assert ab.activity_params[0]['uncertainty type'] == 0
    matrix_data = ab.generate_samples(5)
    assert len(matrix_data) == 1
    assert matrix_data[0][0].shape[1] == 5
    assert matrix_data[0][0].shape[0] == 1
    assert np.allclose(np.ones(shape=(1, 5)), matrix_data[0][0])
    assert len(matrix_data[0][1]) == 1
    assert matrix_data[0][1][0] == (('biosphere', 'Water 1, to water, in kg'),
                                    ('test_db', 'H'))
Пример #7
0
def test_rebalance_inverse_ratio_2(data_for_testing):
    wb = DatabaseWaterBalancer(ecoinvent_version='test_db',
                               database_name="test_db",
                               biosphere="biosphere")
    ab = ActivityWaterBalancer(('test_db', 'D'), wb)
    ab._identify_strategy()
    assert ab.strategy == 'inverse'
    ab._define_balancing_parameters()
    assert ab.static_ratio == 0.5
    assert ab.static_balance == -20
    matrix_data = ab.generate_samples(5)
    assert matrix_data[0][0].shape[1] == 5
    assert matrix_data[1][0].shape[1] == 5
    in_sum, out_sum = helper_get_matrix_data_sums_for_test(ab, matrix_data)
    assert np.allclose(out_sum / in_sum, ab.static_ratio)
Пример #8
0
def test_rebalance_default_ratio_1(data_for_testing):
    wb = DatabaseWaterBalancer(ecoinvent_version='test_db',
                               database_name="test_db",
                               biosphere="biosphere")
    assert wb.matrix_indices == []
    assert wb.matrix_samples is None
    ab = ActivityWaterBalancer(('test_db', 'A'), wb)
    ab._identify_strategy()
    assert ab.strategy == 'default'
    ab._define_balancing_parameters()
    assert ab.static_ratio == 1
    assert ab.static_balance == 0
    matrix_data = ab.generate_samples(5)
    assert matrix_data[0][0].shape[1] == 5
    assert matrix_data[0][0].shape[0] == 5
    assert matrix_data[1][0].shape[1] == 5
    assert matrix_data[1][0].shape[0] == 4
    in_sum, out_sum = helper_get_matrix_data_sums_for_test(ab, matrix_data)
    assert np.allclose(in_sum / out_sum, ab.static_ratio)
    wb.add_samples_for_act(ab.act, 5)
    assert len(wb.matrix_indices) == len(ab.water_exchanges)
    assert wb.matrix_samples.shape[0] == len(ab.water_exchanges)
    assert wb.matrix_samples.shape[1] == 5
Пример #9
0
def test_water_exchange_formulas_removed(data_for_testing):
    """ Make sure formulas are properly removed from exchanges"""
    act = get_activity(("test_db", "A"))
    exc = [
        exc for exc in act.exchanges()
        if exc.input.key == ('biosphere', 'Water 1, from nature, in kg')
    ][0]
    assert exc['formula'] == 'some_good_formula'
    wb = DatabaseWaterBalancer(ecoinvent_version='test_db',
                               database_name="test_db",
                               biosphere="biosphere")
    ab = ActivityWaterBalancer(('test_db', 'A'), wb)
    exc_1 = [
        exc for exc in ab.act.exchanges()
        if exc.input.key == ('biosphere', 'Water 1, from nature, in kg')
    ][0]
    exc_in_list_1 = [
        exc for exc in ab.water_exchanges
        if exc.input.key == ('biosphere', 'Water 1, from nature, in kg')
    ][0]
    exc_2 = [
        exc for exc in ab.act.exchanges()
        if exc.input.key == ('biosphere', 'Water 2, from nature, in kg')
    ][0]
    exc_in_list_2 = [
        exc for exc in ab.water_exchanges
        if exc.input.key == ('biosphere', 'Water 2, from nature, in kg')
    ][0]

    assert exc_1.get('formula') is None
    assert exc_in_list_1.get('formula') is None
    assert exc_1.get('temp_formula') == 'some_good_formula'
    assert exc_in_list_1.get('temp_formula') == 'some_good_formula'
    assert exc_2.get('formula') is None
    assert exc_in_list_2.get('formula') is None
    assert exc_2.get('temp_formula') == 'some bad formula'
    assert exc_in_list_2.get('temp_formula') == 'some bad formula'

    ab._define_balancing_parameters()

    exc_1 = [
        exc for exc in ab.act.exchanges()
        if exc.input.key == ('biosphere', 'Water 1, from nature, in kg')
    ][0]
    exc_in_list_1 = [
        exc for exc in ab.water_exchanges
        if exc.input.key == ('biosphere', 'Water 1, from nature, in kg')
    ][0]
    exc_2 = [
        exc for exc in ab.act.exchanges()
        if exc.input.key == ('biosphere', 'Water 2, from nature, in kg')
    ][0]
    exc_in_list_2 = [
        exc for exc in ab.water_exchanges
        if exc.input.key == ('biosphere', 'Water 2, from nature, in kg')
    ][0]

    assert exc_1.get('formula') is None
    assert exc_1.get('temp_formula') == 'some_good_formula'
    assert exc_1.get('water_formula') is not None
    assert exc_in_list_1.get('formula') is None
    assert exc_in_list_1.get('temp_formula') == 'some_good_formula'
    assert exc_in_list_1.get('water_formula') is not None

    assert exc_2.get('formula') is None
    assert exc_2.get('temp_formula') == 'some bad formula'
    assert exc_2.get('water_formula') is not None
    assert exc_in_list_2.get('formula') is None
    assert exc_in_list_2.get('temp_formula') == 'some bad formula'
    assert exc_in_list_2.get('water_formula') is not None

    ab.generate_samples(2)
    exc_1 = [
        exc for exc in ab.act.exchanges()
        if exc.input.key == ('biosphere', 'Water 1, from nature, in kg')
    ][0]
    exc_2 = [
        exc for exc in ab.act.exchanges()
        if exc.input.key == ('biosphere', 'Water 2, from nature, in kg')
    ][0]
    assert exc_1.get('formula') == 'some_good_formula'
    assert exc_1.get('temp_formula') is None
    assert exc_2.get('formula') == 'some bad formula'
    assert exc_2.get('temp_formula') is None