示例#1
0
def test_basic_setup(bw25_setup):
    fu, data_objs, _ = prepare_lca_inputs({("c", "2"): 1}, method=("d",))
    lca = LCA(fu, data_objs=data_objs)
    lca.lci()
    lca.lcia()

    print(lca.technosphere_matrix.toarray())
    print(lca.biosphere_matrix.toarray())
    print(lca.supply_array)
    print(lca.score)

    assert np.allclose(lca.score, 44)
示例#2
0
def test_excel_products_lca():
    ei = ExcelImporter(os.path.join(EXCEL_FIXTURES_DIR, "with_products.xlsx"))
    ei.strategies = [
        csv_restore_tuples,
        csv_restore_booleans,
        csv_numerize,
        csv_drop_unknown,
        csv_add_missing_exchanges_section,
        normalize_units,
        set_code_by_activity_hash,
        assign_only_product_as_production,
        link_technosphere_by_activity_hash,
        drop_falsey_uncertainty_fields_but_keep_zeros,
        convert_uncertainty_types_to_integers,
    ]
    print(ei.data)
    ei.apply_strategies()
    ei.match_database()
    ei.write_database()
    fu, data_objs, _ = prepare_lca_inputs({("Product example", "B"): 1})
    lca = LCA(fu, data_objs=data_objs)
    lca.lci()
    keys = {
        get_id(("Product example", "B")),
        get_id(("Product example", "C")),
        get_id(("Product example", "E")),
    }
    for key in lca.dicts.product:
        assert key in keys
    keys = {
        get_id(("Product example", "A")),
        get_id(("Product example", "C")),
        get_id(("Product example", "D")),
    }
    for key in lca.dicts.activity:
        assert key in keys
    for value in lca.supply_array:
        assert (np.allclose(value, 1) or np.allclose(value, 0.539)
                or np.allclose(value, 0.539 * 0.00805))


# Code for bw2

import bw2data as bd, bw2calc as bc
bd.projects.set_current("ecoinvent 3.7.1 bw2")
bd.databases
a = bd.get_activity(('ecoinvent 3.7.1', 'f57568b2e553864152a6ac920595216f'))
a
ipcc = ('IPCC 2013', 'climate change', 'GWP 100a')

curry = partial(bc.LCA, demand={a: 1}, method=ipcc)
profile_func(partial(run_curried_lca, func=curry))


# Code for bw2.5

import bw2data as bd, bw2calc as bc
bd.projects.set_current("ecoinvent 3.7.1")
bd.databases
a = bd.get_activity(('ecoinvent 3.7.1', 'f57568b2e553864152a6ac920595216f'))
a
ipcc = ('IPCC 2013', 'climate change', 'GWP 100a')

fu, data_objs, _ = bd.prepare_lca_inputs({a: 1}, method=ipcc)
curry = partial(bc.LCA, demand=fu, data_objs=data_objs)
profile_func(partial(run_curried_lca, func=curry))


示例#4
0
def test_bw25_integration_simple(bw25_setup):
    # data = extract_brightway2_databases(["c"])
    modified = [
        {
            "location": "c1-2",
            "database": "c",
            "code": "1",
            "name": "c1-1",
            "reference product": "",
            "unit": "c1-3",
            "exchanges": [
                {
                    "amount": 1,
                    "type": "production",
                    "database": "c",
                    "name": "c1-1",
                    "location": "c1-2",
                    "unit": "c1-3",
                    "product": "",
                },
                {
                    "amount": 2,
                    "modified": True,
                    "type": "technosphere",
                    "input": ("a", "1"),
                    "database": "a",
                },
                {
                    "amount": 2000000,  # Should be ignored, not marked as modified
                    "uncertainty type": 0,
                    "loc": 2,
                    "type": "technosphere",
                    "production volume": None,
                    "input": ("a", "2"),
                    "name": None,
                    "product": "",
                    "unit": None,
                    "location": None,
                    "database": "a",
                },
            ],
        },
        {
            "location": "c2-2",
            "database": "c",
            "code": "2",
            "name": "c2-1",
            "reference product": "",
            "unit": "c2-3",
            "exchanges": [
                {
                    "amount": 1,
                    "type": "production",
                    "database": "c",
                    "name": "c2-1",
                    "location": "c2-2",
                    "unit": "c2-3",
                    "product": "",
                },
                {
                    "amount": 5,
                    "type": "technosphere",
                    "product": "",
                    "name": "c1-1",
                    "unit": "c1-3",
                    "location": "c1-2",
                    "modified": True,
                    "database": "c",
                },
                {
                    "amount": 14,
                    "type": "biosphere",
                    "input": ("b", "1"),
                    "database": "b",
                    "modified": True,
                },
                {
                    "amount": 4000000,  # Should be ignored, not marked as modified
                    "type": "biosphere",
                    "input": ("b", "2"),
                    "database": "b",
                },
            ],
        },
        {
            "location": "c3-2",
            "database": "c",
            "modified": True,
            "code": "1",
            "name": "c3-1",
            "reference product": "",
            "unit": "c3-3",
            "exchanges": [
                {
                    "amount": 1,
                    "type": "technosphere",
                    "product": "",
                    "database": "c",
                    "name": "c2-1",
                    "unit": "c2-3",
                    "location": "c2-2",
                },
                {
                    "amount": 1,
                    "type": "production",
                    "name": "c3-1",
                    "database": None,
                    "product": "",
                    "unit": "c3-3",
                    "location": "c3-2",
                },
            ],
        },
    ]
    write_brightway25_database(modified, "new")  # , add_implicit_production=False)

    fu, data_objs, _ = prepare_lca_inputs({("new", "1"): 1}, method=("d",))
    lca = LCA(fu, data_objs=data_objs)
    lca.lci()
    lca.lcia()

    print(lca.technosphere_matrix.toarray())
    print(lca.biosphere_matrix.toarray())
    print(lca.supply_array)
    print(lca.score)

    assert not np.allclose(lca.score, 44)

    assert lca.technosphere_matrix[lca.dicts.product[get_id(("a", "2"))], lca.dicts.activity[get_id(("c", "1"))]] == -2
    assert lca.technosphere_matrix[lca.dicts.product[get_id(("a", "1"))], lca.dicts.activity[get_id(("c", "1"))]] == -2
    assert lca.technosphere_matrix[lca.dicts.product[get_id(("c", "1"))], lca.dicts.activity[get_id(("c", "2"))]] == -5
    assert lca.biosphere_matrix[lca.dicts.biosphere[get_id(("b", "1"))], lca.dicts.activity[get_id(("c", "2"))]] == 14
    assert lca.biosphere_matrix[lca.dicts.biosphere[get_id(("b", "2"))], lca.dicts.activity[get_id(("c", "2"))]] == 0
    assert lca.technosphere_matrix[lca.dicts.product[get_id(("c", "2"))], lca.dicts.activity[get_id(("new", "1"))]] == -1
    assert lca.technosphere_matrix[lca.dicts.product[get_id(("new", "1"))], lca.dicts.activity[get_id(("new", "1"))]] == 1