Exemplo n.º 1
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,
    ]
    ei.apply_strategies()
    ei.match_database()
    ei.write_database()
    lca = LCA({('Product example', 'B'): 1})
    lca.lci()
    keys = {('Product example', 'B'), ('Product example', 'C'),
            ('Product example', 'E')}
    for key in lca.product_dict:
        assert key in keys
    keys = {('Product example', 'A'), ('Product example', 'C'),
            ('Product example', 'D')}
    for key in lca.activity_dict:
        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)
Exemplo n.º 2
0
def test_process_activities(no_init, monkeypatch):
    monkeypatch.setattr("bw2io.importers.excel.ExcelImporter.get_activity",
                        lambda a, b, c: c)
    ei = ExcelImporter()
    with pytest.raises(ValueError):
        assert ei.process_activities([("name", [["cutoff", "foo"]])])

    given = [("n", [["cutoff", "2"], ["", ""], ["activity", "foo", "bar"],
                    ["1", "2", "3"]])]
    expected = [[["activity", "foo"], ["1", "2"]]]
    assert ei.process_activities(given) == expected
Exemplo n.º 3
0
def test_process_activities(no_init, monkeypatch):
    monkeypatch.setattr('bw2io.importers.excel.ExcelImporter.get_activity',
                        lambda a, b, c: c)
    ei = ExcelImporter()
    with pytest.raises(ValueError):
        assert ei.process_activities([('name', [['cutoff', 'foo']])])

    given = [('n', [['cutoff', '2'], ['', ''], ['activity', 'foo', 'bar'],
                    ['1', '2', '3']])]
    expected = [[['activity', 'foo'], ['1', '2']]]
    assert ei.process_activities(given) == expected
Exemplo n.º 4
0
class BiofuelInventory(BaseInventoryImport):
    """
    Biofuel datasets from the master thesis of Francesco Cozzolino (2018).
    """
    def load_inventory(self, path):
        self.import_db = ExcelImporter(path)

    def prepare_inventory(self):

        # Migrations for 3.6
        if self.version == 3.6:
            migrations = {
                'fields': ['name', 'reference product', 'location'],
                'data':
                [(('market for transport, freight, sea, transoceanic tanker', (
                    'transport, freight, sea, transoceanic tanker',
                ), 'GLO'), {
                    'name':
                    ('market for transport, freight, sea, tanker for liquid goods other than petroleum and liquefied natural gas'
                     ),
                    'reference product':
                    ('transport, freight, sea, tanker for liquid goods other than petroleum and liquefied natural gas'
                     ),
                }),
                 (('market for water, decarbonised, at user',
                   ('water, decarbonised, at user', ), 'GLO'), {
                       'name': ('market for water, decarbonised'),
                       'reference product': ('water, decarbonised'),
                       'location': ('DE'),
                   }),
                 (('market for water, completely softened, from decarbonised water, at user',
                   ('water, completely softened, from decarbonised water, at user',
                    ), 'GLO'), {
                        'name': ('market for water, completely softened'),
                        'reference product': ('water, completely softened'),
                        'location': ('RER'),
                    }),
                 (('market for concrete block', ('concrete block', ), 'GLO'), {
                     'location': ('DE'),
                 })]
            }

            Migration("biofuels_ecoinvent_36").write(
                migrations,
                description=
                "Change technosphere names due to change from 3.5 to 3.6")
            self.import_db.migrate("biofuels_ecoinvent_36")

        self.add_biosphere_links()
        self.add_product_field_to_exchanges()
Exemplo n.º 5
0
def test_write_only_activity_parameters(no_init):
    assert not ActivityParameter.select().count()
    Database("PCB").register()
    obj = ExcelImporter()
    obj.db_name = "PCB"
    obj.data = deepcopy(DATA)
    obj.write_database()
    assert ActivityParameter.select().count() == 1
    assert ActivityParameter.get().amount != 7

    NEW = [{
        'code':
        '32aa5ab78beda5b8c8efbc89587de7a5',
        'database':
        'PCB',
        'parameters': [{
            'name': 'PCB_mass_total',
            'amount': 11,
            'formula': '7'
        }],
    }]
    obj.write_activity_parameters(NEW)
    assert ActivityParameter.select().count() == 1
    a = ActivityParameter.get()
    assert a.formula == '7'
    assert a.amount == 7
Exemplo n.º 6
0
def test_example_notebook():
    ei = ExcelImporter(os.path.join(EXCEL_FIXTURES_DIR, "sample_activities_with_variables.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,
        convert_activity_parameters_to_list,
    ]
    ei.apply_strategies()
    ei.match_database(fields=['name'])
    ei.write_database()
Exemplo n.º 7
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))
Exemplo n.º 8
0
def test_parameterized_import():
    ei = ExcelImporter(os.path.join(EXCEL_FIXTURES_DIR, "basic_example.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,
    ]
    ei.apply_strategies()
    ei.match_database()
    assert ei.data == expected
Exemplo n.º 9
0
def test_write_only_activity_parameters(no_init):
    assert not ActivityParameter.select().count()
    Database("PCB").register()
    obj = ExcelImporter()
    obj.db_name = "PCB"
    obj.data = deepcopy(DATA)
    obj.write_database()
    assert ActivityParameter.select().count() == 1
    assert ActivityParameter.get().amount != 7

    NEW = [
        {
            "code": "32aa5ab78beda5b8c8efbc89587de7a5",
            "database": "PCB",
            "parameters": [{"name": "PCB_mass_total", "amount": 11, "formula": "7"}],
        }
    ]
    obj.write_activity_parameters(NEW)
    assert ActivityParameter.select().count() == 1
    a = ActivityParameter.get()
    assert a.formula == "7"
    assert a.amount == 7
Exemplo n.º 10
0
def test_no_valid_worksheets_all_columns_cutoff():
    ei = ExcelImporter(os.path.join(EXCEL_FIXTURES_DIR, "basic_all_cutoff.xlsx"))
    assert not ei.data
Exemplo n.º 11
0
def test_no_valid_worksheets():
    ei = ExcelImporter(os.path.join(EXCEL_FIXTURES_DIR, "empty.xlsx"))
    for attr in ("db_name", "data"):
        assert not hasattr(ei, attr)
Exemplo n.º 12
0
def test_empty_activity_parameters_dont_delete(no_init):
    # Empty activity parameters aren't considered at all
    # so they shouldn't be deleted if empty section provided
    Database("PCB").register()
    obj = ExcelImporter()
    obj.db_name = "PCB"
    obj.data = deepcopy(DATA)
    obj.write_database()
    assert len(parameters)

    NEW = deepcopy(DATA)
    for ds in NEW:
        if "parameters" in ds:
            del ds["parameters"]

    Database("PCB").register()
    obj = ExcelImporter()
    obj.db_name = "PCB"
    obj.data = NEW
    obj.write_database(delete_existing=False)
    assert len(parameters)
Exemplo n.º 13
0
def test_write_only_activity_parameters_no_activate_others(no_init):
    Database("PCB").register()
    obj = ExcelImporter()
    obj.db_name = "PCB"
    obj.data = deepcopy(DATA)
    obj.write_database(activate_parameters=False)

    NEW = [
        {
            "code": "45cb34db4147e510a2561cceec541f6b",
            "database": "PCB",
            "exchanges": [],
            "name": "unmounted printed circuit board",
            "type": "process",
            "unit": "square meter",
            "parameters": [{"name": "something_test", "amount": 2, "formula": "3 + 2"}],
        }
    ]
    obj = ExcelImporter()
    obj.db_name = "PCB"
    obj.write_activity_parameters(NEW)

    assert ActivityParameter.select().count() == 1
    assert ActivityParameter.get().formula == "3 + 2"
    assert "parameters" not in get_activity(("PCB", "45cb34db4147e510a2561cceec541f6b"))
    assert "parameters" in get_activity(("PCB", "32aa5ab78beda5b8c8efbc89587de7a5"))
Exemplo n.º 14
0
def test_write_only_database_parameters(no_init):
    Database("foo").register()
    obj = ExcelImporter()
    obj.db_name = "foo"
    obj.data = None
    obj.database_parameters = deepcopy(DB)
    obj.write_database_parameters(activate_parameters=False)
    assert not DatabaseParameter.select().count()
    assert "parameters" in obj.metadata

    obj.write_database_parameters()
    assert DatabaseParameter.select().count() == 3

    obj.database_parameters = deepcopy(DB[:1])
    obj.write_database_parameters()
    assert DatabaseParameter.select().count() == 1
Exemplo n.º 15
0
def test_get_activity(no_init):
    given = [
        [
            'Activity', 'mounted printed circuit board', '', '', '', '', '',
            '', '', '', '', '', '', ''
        ],
        [
            'comment', 'something important here maybe?', '', '', '', '', '',
            '', '', '', '', '', '', ''
        ],
        [
            'arbitrary', 'metadata', '', '', '', '', '', '', '', '', '', '',
            '', ''
        ],
        ['location', 'GLO', '', '', '', '', '', '', '', '', '', '', '', ''],
        ['type', 'process', '', '', '', '', '', '', '', '', '', '', '', ''],
        ['unit', 'kilogram', '', '', '', '', '', '', '', '', '', '', '', ''],
        ['', '', '', '', '', '', '', '', '', '', '', '', '', ''],
        ['Parameters', '', '', '', '', '', '', '', '', '', '', '', '', ''],
        [
            'name', 'amount', 'formula', '', '', '', '', '', '', '', '', '',
            '', ''
        ],
        [
            'PCB_mass_total', 0.6,
            'PCB_cap_mass_film + PCB_cap_mass_SMD + PCB_cap_mass_Tantalum', '',
            '', '', '', '', '', '', '', '', '', ''
        ],
        ['', '', '', '', '', '', '', '', '', '', '', '', '', ''],
        ['Exchanges', '', '', '', '', '', '', '', '', '', '', '', '', ''],
        [
            'name', 'amount', 'unit', 'database', 'location', 'type',
            'formula', '', '', '', '', '', '', ''
        ],
        [
            'unmounted printed circuit board', 0.0, 'square meter', 'PCB',
            'GLO', 'technosphere', 'PCB_area * 2', 'PCB_area * 2', '', '', '',
            '', '', ''
        ],
        [
            'mounted printed circuit board', 0.0, 'kilogram', 'PCB', 'GLO',
            'production', 'PCB_mass_total', 'PCB_mass_total', '', '', '', '',
            '', ''
        ],
        ['', '', '', '', '', '', '', '', '', '', '', '', '', ''],
        [
            'Activity', 'unmounted printed circuit board', '', '', '', '', '',
            '', '', '', '', '', '', ''
        ],
    ]
    ei = ExcelImporter()
    ei.db_name = 'db'
    expected = {
        'arbitrary':
        'metadata',
        'comment':
        'something important here maybe?',
        'database':
        'db',
        'exchanges': [{
            'database': 'PCB',
            'amount': 0.0,
            'formula': 'PCB_area * 2',
            'location': 'GLO',
            'name': 'unmounted printed circuit board',
            'type': 'technosphere',
            'unit': 'square meter'
        }, {
            'database': 'PCB',
            'amount': 0.0,
            'formula': 'PCB_mass_total',
            'location': 'GLO',
            'name': 'mounted printed circuit board',
            'type': 'production',
            'unit': 'kilogram'
        }],
        'location':
        'GLO',
        'name':
        'mounted printed circuit board',
        'parameters': {
            'PCB_mass_total': {
                'amount':
                0.6,
                'formula':
                'PCB_cap_mass_film + PCB_cap_mass_SMD + PCB_cap_mass_Tantalum'
            }
        },
        'type':
        'process',
        'unit':
        'kilogram',
        'worksheet name':
        'a'
    }
    assert ei.get_activity('a', given) == expected
Exemplo n.º 16
0
def test_excel_products_import():
    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,
    ]
    ei.apply_strategies()
    ei.match_database()
    expected = [
        {
            "code":
            "A",
            "database":
            "Product example",
            "exchanges": [
                {
                    "amount": 1.0,
                    "database": "Product example",
                    "input": ("Product example", "B"),
                    "loc": 1.0,
                    "location": "RNA",
                    "name": "Acetic acid",
                    "type": "production",
                    "uncertainty type": 0,
                    "unit": "kilogram",
                },
                {
                    "amount": 0.539,
                    "database": "Product example",
                    "input": ("Product example", "E"),
                    "loc": 0.539,
                    "location": "RNA",
                    "name": "Methanol",
                    "type": "technosphere",
                    "uncertainty type": 0,
                    "unit": "kilogram",
                },
            ],
            "location":
            "RNA",
            "name":
            "Acetic acid, at plant",
            "production amount":
            1.0,
            "reference product":
            "Acetic acid",
            "type":
            "process",
            "unit":
            "kilogram",
            "worksheet name":
            "first process",
        },
        {
            "code": "B",
            "database": "Product example",
            "exchanges": [],
            "location": "RNA",
            "name": "Acetic acid",
            "type": "product",
            "unit": "kilogram",
            "worksheet name": "first process",
        },
        {
            "categories": ("Utilities", "Utilities"),
            "code": "C",
            "database": "Product example",
            "exchanges": [],
            "location": "RNA",
            "name": "Electricity, at Grid, US, 2008",
            "unit": "kilowatt hour",
            "worksheet name": "other processes",
        },
        {
            "code":
            "D",
            "database":
            "Product example",
            "exchanges": [
                {
                    "amount": 1.0,
                    "database": "Product example",
                    "input": ("Product example", "E"),
                    "location": "RNA",
                    "name": "Methanol",
                    "type": "production",
                    "uncertainty type": 0,
                    "unit": "kilogram",
                },
                {
                    "amount": 0.00805,
                    "categories": ("Utilities", "Utilities"),
                    "database": "Product example",
                    "input": ("Product example", "C"),
                    "location": "RNA",
                    "name": "Electricity, at Grid, US, 2008",
                    "type": "technosphere",
                    "uncertainty type": 0,
                    "unit": "kilowatt hour",
                },
            ],
            "location":
            "RNA",
            "name":
            "Methanol, at plant",
            "production amount":
            1.0,
            "reference product":
            "Methanol",
            "unit":
            "kilogram",
            "worksheet name":
            "other processes",
        },
        {
            "code": "E",
            "database": "Product example",
            "exchanges": [],
            "location": "RNA",
            "name": "Methanol",
            "type": "product",
            "unit": "kilogram",
            "worksheet name": "other processes",
        },
    ]
    assert ei.data == expected
Exemplo n.º 17
0
    def extract_excel_data(self, excelfilepath):

        print(excelfilepath)

        sp = ExcelImporter(excelfilepath)

        if not migrations:
            create_core_migrations()

        if 'biosphere3' not in databases:
            create_default_biosphere3()

        # link the biosphere exchanges
        sp.apply_strategies(verbose=False)

        # make the internal links
        sp.match_database(fields=["name", "unit", "location"])

        # make the links to existing data
        still_unlinked = link_iterable_by_fields(
            sp.data,
            self.db,
            fields=["reference product", "name", "unit", "location"])
        still_unlinked = link_iterable_by_fields(
            still_unlinked, self.db, fields=["name", "unit", "location"])

        # Change GLO to RoW
        if still_unlinked:
            for x in still_unlinked:
                for exc in x.get('exchanges', []):
                    if not exc.get("input"):
                        if exc.get('location') == 'GLO':
                            exc['location'] = 'RoW'

            still_unlinked = link_iterable_by_fields(
                still_unlinked, self.db, fields=["name", "unit", "location"])

        # Make processes less specific, by snipping off trailing descriptions (e.g. ', at user', ', metallurgical' etc.)
        if still_unlinked:
            attempts = 2
            for i in range(attempts):
                for x in still_unlinked:
                    for exc in x.get('exchanges', []):
                        if not exc.get("input"):
                            if exc['name'].rfind(',') != -1:
                                exc['name'] = exc['name'][:exc['name'].
                                                          rfind(',')]

                still_unlinked = link_iterable_by_fields(
                    still_unlinked,
                    self.db,
                    fields=["name", "unit", "location"])

        # Try switching despecified items back to GLO
        if still_unlinked:
            for x in still_unlinked:
                for exc in x.get('exchanges', []):
                    if not exc.get("input"):
                        if exc.get('location') == 'RoW':
                            exc['location'] = 'GLO'

            still_unlinked = link_iterable_by_fields(
                still_unlinked, self.db, fields=["name", "unit", "location"])

        if sp.statistics()[2] != 0:
            fp = sp.write_excel()
            print(fp)
            assert sp.statistics()[2] == 0, "Unlinked exchanges"

        fix_products_and_locations_external(sp.data, self.db)

        self.db.extend(sp.data)
        self.database_names.append(sp.db_name)
Exemplo n.º 18
0
def test_parameterized_import():
    ei = ExcelImporter(
        os.path.join(EXCEL_FIXTURES_DIR,
                     "sample_activities_with_variables.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,
    ]
    ei.apply_strategies()
    ei.match_database()
    assert ei.project_parameters == [{'amount': 0.25, 'name': 'PCB_area'}]
    expected = [{
        'amount': 0.2,
        'maximum': 1.0,
        'minimum': 0.0,
        'name': 'PCB_cap_mass_film',
        'uncertainty type': 4.0,
        'unit': 'kilogram'
    }, {
        'amount': 0.2,
        'maximum': 1.0,
        'minimum': 0.0,
        'name': 'PCB_cap_mass_SMD',
        'uncertainty type': 4.0,
        'unit': 'kilogram'
    }, {
        'amount': 0.2,
        'maximum': 1.0,
        'minimum': 0.0,
        'name': 'PCB_cap_mass_Tantalum',
        'uncertainty type': 4.0,
        'unit': 'kilogram'
    }]
    assert ei.database_parameters == expected
    expected = \
        [{'arbitrary': 'metadata',
          'code': 'mpcb',
          'comment': 'something important here maybe?',
          'database': 'PCB',
          'exchanges': [{'amount': 0.0,
                         'database': 'PCB',
                         'formula': 'PCB_area * 2',
                         'location': 'GLO',
                         'name': 'unmounted printed circuit board',
                         'type': 'technosphere',
                         'unit': 'square meter'},
                        {'amount': 0.0,
                         'database': 'PCB',
                         'formula': 'PCB_mass_total',
                         'location': 'GLO',
                         'name': 'mounted printed circuit board',
                         'type': 'production',
                         'unit': 'kilogram'}],
          'location': 'GLO',
          'name': 'mounted printed circuit board',
          'parameters': {'PCB_mass_total': {'amount': 0.6,
                                            'group': 'alpha group!',
                                            'formula': 'PCB_cap_mass_film + '
                                                       'PCB_cap_mass_SMD + '
                                                       'PCB_cap_mass_Tantalum'}},
          'production amount': 0.0,
          'reference product': 'mounted printed circuit board',
          'type': 'process',
          'unit': 'kilogram',
          'worksheet name': 'PCB inventory'},
         {'categories': ('electronics', 'board'),
          'code': '45cb34db4147e510a2561cceec541f6b',
          'comment': 'one input',
          'database': 'PCB',
          'exchanges': [{'amount': 1.0,
                         'database': 'PCB',
                         'location': 'GLO',
                         'name': 'unmounted printed circuit board',
                         'type': 'production',
                         'uncertainty type': 0,
                         'unit': 'square meter'}],
          'location': 'GLO',
          'name': 'unmounted printed circuit board',
          'production amount': 1.0,
          'reference product': 'unmounted printed circuit board',
          'type': 'process',
          'unit': 'square meter',
          'worksheet name': 'PCB inventory'}]
    assert ei.data == expected
Exemplo n.º 19
0
def test_get_labelled_section(no_init):
    ei = ExcelImporter()
    with pytest.raises(AssertionError):
        assert ei.get_labelled_section(None, [["", ""]])
    with pytest.raises(AssertionError):
        assert ei.get_labelled_section(None, [["foo", "", "bar"]])

    data = [
        ["Parameters", "", "", "", "", "", "", "", "", "", "", "", "", ""],
        [
            "name", "amount", "formula", "", "", "", "", "", "", "", "", "",
            "", ""
        ],
        [
            "something::something",
            0.6,
            "A + B",
            "",
            "",
            "",
            "",
            "",
            "",
            "",
            "",
            "",
            "",
            "",
        ],
        [
            "Nope", 1.3, "", "Will be skipped", "", "", "", "", "", "", "", "",
            "", ""
        ],
        ["", "", "", "", "", "", "", "", "", "", "", "", "", ""],
    ]
    expected = [
        {
            "name": "something::something",
            "amount": 0.6,
            "formula": "A + B"
        },
        {
            "name": "Nope",
            "amount": 1.3
        },
    ]
    assert ei.get_labelled_section(None, data, 1, transform=False) == expected
    expected = [
        {
            "name": ("something", "something"),
            "amount": 0.6,
            "formula": "A + B"
        },
        {
            "name": "Nope",
            "amount": 1.3
        },
    ]
    assert ei.get_labelled_section(None, data, 1) == expected
    given = [
        [
            "name", "amount", "formula", "", "", "", "", "", "", "", "", "",
            "", ""
        ],
        [
            "PCB_mass_total",
            0.6,
            "PCB_cap_mass_film + PCB_cap_mass_SMD + PCB_cap_mass_Tantalum",
            "",
            "",
            "",
            "",
            "",
            "",
            "",
            "",
            "",
            "",
            "",
        ],
    ]
    expected = [{
        "name":
        "PCB_mass_total",
        "amount":
        0.6,
        "formula":
        "PCB_cap_mass_film + PCB_cap_mass_SMD + PCB_cap_mass_Tantalum",
    }]
    assert ei.get_labelled_section(None, given) == expected
Exemplo n.º 20
0
 def load_inventory(self, path):
     self.import_db = ExcelImporter(path)
Exemplo n.º 21
0
def test_get_labelled_section(no_init):
    ei = ExcelImporter()
    with pytest.raises(AssertionError):
        assert ei.get_labelled_section(None, [['', '']])
    with pytest.raises(AssertionError):
        assert ei.get_labelled_section(None, [['foo', '', 'bar']])

    data = [
        ['Parameters', '', '', '', '', '', '', '', '', '', '', '', '', ''],
        [
            'name', 'amount', 'formula', '', '', '', '', '', '', '', '', '',
            '', ''
        ],
        [
            'something::something', 0.6, 'A + B', '', '', '', '', '', '', '',
            '', '', '', ''
        ],
        [
            'Nope', 1.3, '', 'Will be skipped', '', '', '', '', '', '', '', '',
            '', ''
        ],
        ['', '', '', '', '', '', '', '', '', '', '', '', '', ''],
    ]
    expected = [{
        'name': 'something::something',
        'amount': 0.6,
        'formula': 'A + B'
    }, {
        'name': 'Nope',
        'amount': 1.3
    }]
    assert ei.get_labelled_section(None, data, 1, transform=False) == expected
    expected = [{
        'name': ("something", "something"),
        'amount': 0.6,
        'formula': 'A + B'
    }, {
        'name': 'Nope',
        'amount': 1.3
    }]
    assert ei.get_labelled_section(None, data, 1) == expected
    given = [
        [
            'name', 'amount', 'formula', '', '', '', '', '', '', '', '', '',
            '', ''
        ],
        [
            'PCB_mass_total', 0.6,
            'PCB_cap_mass_film + PCB_cap_mass_SMD + PCB_cap_mass_Tantalum', '',
            '', '', '', '', '', '', '', '', '', ''
        ]
    ]
    expected = [{
        'name':
        'PCB_mass_total',
        'amount':
        0.6,
        'formula':
        'PCB_cap_mass_film + PCB_cap_mass_SMD + PCB_cap_mass_Tantalum'
    }]
    assert ei.get_labelled_section(None, given) == expected
Exemplo n.º 22
0
class CarmaCCSInventory(BaseInventoryImport):
    def load_inventory(self, path):
        self.import_db = ExcelImporter(path)

    def prepare_inventory(self):
        if (self.version == 3.6):
            # apply some updates to comply with ei 3.6
            new_technosphere_data = {
                'fields': ['name', 'reference product', 'location'],
                'data':
                [(('market for water, decarbonised, at user', (), 'GLO'), {
                    'name': ('market for water, decarbonised'),
                    'reference product': ('water, decarbonised'),
                    'location': ('DE'),
                }),
                 (('market for water, completely softened, from decarbonised water, at user',
                   (), 'GLO'), {
                       'name': ('market for water, completely softened'),
                       'reference product': ('water, completely softened'),
                       'location': ('RER'),
                   }),
                 (('market for steam, in chemical industry', (), 'GLO'), {
                     'location': ('RER'),
                     'reference product': ('steam, in chemical industry'),
                 }),
                 (('market for steam, in chemical industry', (), 'RER'), {
                     'reference product': ('steam, in chemical industry'),
                 }),
                 (('zinc-lead mine operation', ('zinc concentrate', ), 'GLO'),
                  {
                      'name': ('zinc mine operation'),
                      'reference product': ('bulk lead-zinc concentrate'),
                  }),
                 (('market for aluminium oxide', ('aluminium oxide', ), 'GLO'),
                  {
                      'name':
                      ('market for aluminium oxide, non-metallurgical'),
                      'reference product':
                      ('aluminium oxide, non-metallurgical'),
                      'location': ('IAI Area, EU27 & EFTA'),
                  }),
                 (('platinum group metal mine operation, ore with high rhodium content',
                   ('nickel, 99.5%', ), 'ZA'),
                  {
                      'name':
                      ('platinum group metal, extraction and refinery operations'
                       ),
                  })]
            }

            Migration("migration_36").write(
                new_technosphere_data,
                description=
                "Change technosphere names due to change from 3.5 to 3.6")
            self.import_db.migrate("migration_36")

        self.add_biosphere_links()
        self.add_product_field_to_exchanges()

        # Add carbon storage for CCS technologies
        print("Add fossil carbon dioxide storage for CCS technologies.")
        self.add_negative_CO2_flows_for_biomass_CCS()

    def add_negative_CO2_flows_for_biomass_CCS(self):
        """
        Rescale the amount of all exchanges of carbon dioxide, non-fossil by a factor -9 (.9/-.1),
        to account for sequestered CO2.

        All CO2 capture and storage in the Carma datasets is assumed to be 90% efficient.
        Thus, we can simply find out what the new CO2 emission is and then we know how much gets stored in the ground.
        It's very important that we ONLY do this for biomass CCS plants, as only they will have negative emissions!

        Modifies in place (does not return anything).

        """
        for ds in ws.get_many(self.db, ws.contains('name', 'storage'),
                              ws.equals('database', 'Carma CCS')):
            for exc in ws.biosphere(
                    ds, ws.equals('name', 'Carbon dioxide, non-fossil')):
                wurst.rescale_exchange(exc, (0.9 / -0.1),
                                       remove_uncertainty=True)
Exemplo n.º 23
0
def test_excel_products_import():
    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,
    ]
    ei.apply_strategies()
    ei.match_database()
    expected = [{
        'code':
        'A',
        'database':
        'Product example',
        'exchanges': [{
            'amount': 1.0,
            'database': 'Product example',
            'input': ('Product example', 'B'),
            'loc': 1.0,
            'location': 'RNA',
            'name': 'Acetic acid',
            'type': 'production',
            'uncertainty type': 0,
            'unit': 'kilogram'
        }, {
            'amount': 0.539,
            'database': 'Product example',
            'input': ('Product example', 'E'),
            'loc': 0.539,
            'location': 'RNA',
            'name': 'Methanol',
            'type': 'technosphere',
            'uncertainty type': 0,
            'unit': 'kilogram'
        }],
        'location':
        'RNA',
        'name':
        'Acetic acid, at plant',
        'production amount':
        1.0,
        'reference product':
        'Acetic acid',
        'type':
        'process',
        'unit':
        'kilogram',
        'worksheet name':
        'first process'
    }, {
        'code': 'B',
        'database': 'Product example',
        'exchanges': [],
        'location': 'RNA',
        'name': 'Acetic acid',
        'type': 'product',
        'unit': 'kilogram',
        'worksheet name': 'first process'
    }, {
        'categories': ('Utilities', 'Utilities'),
        'code': 'C',
        'database': 'Product example',
        'exchanges': [],
        'location': 'RNA',
        'name': 'Electricity, at Grid, US, 2008',
        'unit': 'kilowatt hour',
        'worksheet name': 'other processes'
    }, {
        'code':
        'D',
        'database':
        'Product example',
        'exchanges': [{
            'amount': 1.0,
            'database': 'Product example',
            'input': ('Product example', 'E'),
            'location': 'RNA',
            'name': 'Methanol',
            'type': 'production',
            'uncertainty type': 0,
            'unit': 'kilogram'
        }, {
            'amount': 0.00805,
            'categories': ('Utilities', 'Utilities'),
            'database': 'Product example',
            'input': ('Product example', 'C'),
            'location': 'RNA',
            'name': 'Electricity, at Grid, US, 2008',
            'type': 'technosphere',
            'uncertainty type': 0,
            'unit': 'kilowatt hour'
        }],
        'location':
        'RNA',
        'name':
        'Methanol, at plant',
        'production amount':
        1.0,
        'reference product':
        'Methanol',
        'unit':
        'kilogram',
        'worksheet name':
        'other processes'
    }, {
        'code': 'E',
        'database': 'Product example',
        'exchanges': [],
        'location': 'RNA',
        'name': 'Methanol',
        'type': 'product',
        'unit': 'kilogram',
        'worksheet name': 'other processes'
    }]
    assert ei.data == expected
Exemplo n.º 24
0
def test_get_activity(no_init):
    given = [
        [
            "Activity",
            "mounted printed circuit board",
            "",
            "",
            "",
            "",
            "",
            "",
            "",
            "",
            "",
            "",
            "",
            "",
        ],
        [
            "comment",
            "something important here maybe?",
            "",
            "",
            "",
            "",
            "",
            "",
            "",
            "",
            "",
            "",
            "",
            "",
        ],
        [
            "arbitrary", "metadata", "", "", "", "", "", "", "", "", "", "",
            "", ""
        ],
        ["location", "GLO", "", "", "", "", "", "", "", "", "", "", "", ""],
        ["type", "process", "", "", "", "", "", "", "", "", "", "", "", ""],
        ["unit", "kilogram", "", "", "", "", "", "", "", "", "", "", "", ""],
        ["", "", "", "", "", "", "", "", "", "", "", "", "", ""],
        ["Parameters", "", "", "", "", "", "", "", "", "", "", "", "", ""],
        [
            "name", "amount", "formula", "", "", "", "", "", "", "", "", "",
            "", ""
        ],
        [
            "PCB_mass_total",
            0.6,
            "PCB_cap_mass_film + PCB_cap_mass_SMD + PCB_cap_mass_Tantalum",
            "",
            "",
            "",
            "",
            "",
            "",
            "",
            "",
            "",
            "",
            "",
        ],
        ["", "", "", "", "", "", "", "", "", "", "", "", "", ""],
        ["Exchanges", "", "", "", "", "", "", "", "", "", "", "", "", ""],
        [
            "name",
            "amount",
            "unit",
            "database",
            "location",
            "type",
            "formula",
            "",
            "",
            "",
            "",
            "",
            "",
            "",
        ],
        [
            "unmounted printed circuit board",
            0.0,
            "square meter",
            "PCB",
            "GLO",
            "technosphere",
            "PCB_area * 2",
            "PCB_area * 2",
            "",
            "",
            "",
            "",
            "",
            "",
        ],
        [
            "mounted printed circuit board",
            0.0,
            "kilogram",
            "PCB",
            "GLO",
            "production",
            "PCB_mass_total",
            "PCB_mass_total",
            "",
            "",
            "",
            "",
            "",
            "",
        ],
        ["", "", "", "", "", "", "", "", "", "", "", "", "", ""],
        [
            "Activity",
            "unmounted printed circuit board",
            "",
            "",
            "",
            "",
            "",
            "",
            "",
            "",
            "",
            "",
            "",
            "",
        ],
    ]
    ei = ExcelImporter()
    ei.db_name = "db"
    expected = {
        "arbitrary":
        "metadata",
        "comment":
        "something important here maybe?",
        "database":
        "db",
        "exchanges": [
            {
                "database": "PCB",
                "amount": 0.0,
                "formula": "PCB_area * 2",
                "location": "GLO",
                "name": "unmounted printed circuit board",
                "type": "technosphere",
                "unit": "square meter",
            },
            {
                "database": "PCB",
                "amount": 0.0,
                "formula": "PCB_mass_total",
                "location": "GLO",
                "name": "mounted printed circuit board",
                "type": "production",
                "unit": "kilogram",
            },
        ],
        "location":
        "GLO",
        "name":
        "mounted printed circuit board",
        "parameters": {
            "PCB_mass_total": {
                "amount":
                0.6,
                "formula":
                "PCB_cap_mass_film + PCB_cap_mass_SMD + PCB_cap_mass_Tantalum",
            }
        },
        "type":
        "process",
        "unit":
        "kilogram",
        "worksheet name":
        "a",
    }
    assert ei.get_activity("a", given) == expected
Exemplo n.º 25
0
def test_get_activity_metadata_indexing(no_init):
    given = [
        [
            'Activity', 'mounted printed circuit board', '', '', '', '', '',
            '', '', '', '', '', '', ''
        ],
        [
            'arbitrary', 'metadata', '', '', '', '', '', '', '', '', '', '',
            '', ''
        ],
        ['unit', 'kilogram', '', '', '', '', '', '', '', '', '', '', '', ''],
        ['', '', '', '', '', '', '', '', '', '', '', '', '', ''],
        ['Parameters', '', '', '', '', '', '', '', '', '', '', '', '', ''],
    ]
    ei = ExcelImporter()
    ei.db_name = 'db'
    expected = {
        'arbitrary': 'metadata',
        'database': 'db',
        'name': 'mounted printed circuit board',
        'exchanges': [],
        'unit': 'kilogram',
        'worksheet name': 'a'
    }
    assert ei.get_activity('a', given) == expected

    given = [
        [
            'Activity', 'mounted printed circuit board', '', '', '', '', '',
            '', '', '', '', '', '', ''
        ],
        [
            'arbitrary', 'metadata', '', '', '', '', '', '', '', '', '', '',
            '', ''
        ],
        ['unit', 'kilogram', '', '', '', '', '', '', '', '', '', '', '', ''],
        ['', '', '', '', '', '', '', '', '', '', '', '', '', ''],
    ]
    ei = ExcelImporter()
    ei.db_name = 'db'
    expected = {
        'arbitrary': 'metadata',
        'database': 'db',
        'name': 'mounted printed circuit board',
        'exchanges': [],
        'unit': 'kilogram',
        'worksheet name': 'a'
    }
    assert ei.get_activity('a', given) == expected

    given = [
        [
            'Activity', 'mounted printed circuit board', '', '', '', '', '',
            '', '', '', '', '', '', ''
        ],
        [
            'arbitrary', 'metadata', '', '', '', '', '', '', '', '', '', '',
            '', ''
        ],
        ['unit', 'kilogram', '', '', '', '', '', '', '', '', '', '', '', ''],
    ]
    ei = ExcelImporter()
    ei.db_name = 'db'
    expected = {
        'arbitrary': 'metadata',
        'database': 'db',
        'name': 'mounted printed circuit board',
        'exchanges': [],
        'unit': 'kilogram',
        'worksheet name': 'a'
    }
    assert ei.get_activity('a', given) == expected
Exemplo n.º 26
0
def test_get_activity_metadata_indexing(no_init):
    given = [
        [
            "Activity",
            "mounted printed circuit board",
            "",
            "",
            "",
            "",
            "",
            "",
            "",
            "",
            "",
            "",
            "",
            "",
        ],
        [
            "arbitrary", "metadata", "", "", "", "", "", "", "", "", "", "",
            "", ""
        ],
        ["unit", "kilogram", "", "", "", "", "", "", "", "", "", "", "", ""],
        ["", "", "", "", "", "", "", "", "", "", "", "", "", ""],
        ["Parameters", "", "", "", "", "", "", "", "", "", "", "", "", ""],
    ]
    ei = ExcelImporter()
    ei.db_name = "db"
    expected = {
        "arbitrary": "metadata",
        "database": "db",
        "name": "mounted printed circuit board",
        "exchanges": [],
        "unit": "kilogram",
        "worksheet name": "a",
    }
    assert ei.get_activity("a", given) == expected

    given = [
        [
            "Activity",
            "mounted printed circuit board",
            "",
            "",
            "",
            "",
            "",
            "",
            "",
            "",
            "",
            "",
            "",
            "",
        ],
        [
            "arbitrary", "metadata", "", "", "", "", "", "", "", "", "", "",
            "", ""
        ],
        ["unit", "kilogram", "", "", "", "", "", "", "", "", "", "", "", ""],
        ["", "", "", "", "", "", "", "", "", "", "", "", "", ""],
    ]
    ei = ExcelImporter()
    ei.db_name = "db"
    expected = {
        "arbitrary": "metadata",
        "database": "db",
        "name": "mounted printed circuit board",
        "exchanges": [],
        "unit": "kilogram",
        "worksheet name": "a",
    }
    assert ei.get_activity("a", given) == expected

    given = [
        [
            "Activity",
            "mounted printed circuit board",
            "",
            "",
            "",
            "",
            "",
            "",
            "",
            "",
            "",
            "",
            "",
            "",
        ],
        [
            "arbitrary", "metadata", "", "", "", "", "", "", "", "", "", "",
            "", ""
        ],
        ["unit", "kilogram", "", "", "", "", "", "", "", "", "", "", "", ""],
    ]
    ei = ExcelImporter()
    ei.db_name = "db"
    expected = {
        "arbitrary": "metadata",
        "database": "db",
        "name": "mounted printed circuit board",
        "exchanges": [],
        "unit": "kilogram",
        "worksheet name": "a",
    }
    assert ei.get_activity("a", given) == expected
Exemplo n.º 27
0
def test_write_only_activity_parameters_no_activate_others(no_init):
    Database("PCB").register()
    obj = ExcelImporter()
    obj.db_name = "PCB"
    obj.data = deepcopy(DATA)
    obj.write_database(activate_parameters=False)

    NEW = [{
        'code':
        '45cb34db4147e510a2561cceec541f6b',
        'database':
        'PCB',
        'exchanges': [],
        'name':
        'unmounted printed circuit board',
        'type':
        'process',
        'unit':
        'square meter',
        'parameters': [{
            'name': 'something_test',
            'amount': 2,
            'formula': '3 + 2'
        }],
    }]
    obj = ExcelImporter()
    obj.db_name = "PCB"
    obj.write_activity_parameters(NEW)

    assert ActivityParameter.select().count() == 1
    assert ActivityParameter.get().formula == '3 + 2'
    assert 'parameters' not in get_activity(
        ('PCB', '45cb34db4147e510a2561cceec541f6b'))
    assert 'parameters' in get_activity(
        ('PCB', '32aa5ab78beda5b8c8efbc89587de7a5'))
Exemplo n.º 28
0
def test_parameterized_import():
    ei = ExcelImporter(
        os.path.join(EXCEL_FIXTURES_DIR,
                     "sample_activities_with_variables.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,
    ]
    ei.apply_strategies()
    ei.match_database()
    assert ei.project_parameters == [{"amount": 0.25, "name": "PCB_area"}]
    expected = [
        {
            "amount": 0.2,
            "maximum": 1.0,
            "minimum": 0.0,
            "name": "PCB_cap_mass_film",
            "uncertainty type": 4.0,
            "unit": "kilogram",
        },
        {
            "amount": 0.2,
            "maximum": 1.0,
            "minimum": 0.0,
            "name": "PCB_cap_mass_SMD",
            "uncertainty type": 4.0,
            "unit": "kilogram",
        },
        {
            "amount": 0.2,
            "maximum": 1.0,
            "minimum": 0.0,
            "name": "PCB_cap_mass_Tantalum",
            "uncertainty type": 4.0,
            "unit": "kilogram",
        },
    ]
    assert ei.database_parameters == expected
    expected = [
        {
            "arbitrary":
            "metadata",
            "code":
            "mpcb",
            "comment":
            "something important here maybe?",
            "database":
            "PCB",
            "exchanges": [
                {
                    "amount": 0.0,
                    "database": "PCB",
                    "formula": "PCB_area * 2",
                    "location": "GLO",
                    "name": "unmounted printed circuit board",
                    "type": "technosphere",
                    "unit": "square meter",
                },
                {
                    "amount": 0.0,
                    "database": "PCB",
                    "formula": "PCB_mass_total",
                    "location": "GLO",
                    "name": "mounted printed circuit board",
                    "type": "production",
                    "unit": "kilogram",
                },
            ],
            "location":
            "GLO",
            "name":
            "mounted printed circuit board",
            "parameters": {
                "PCB_mass_total": {
                    "amount":
                    0.6,
                    "group":
                    "alpha group!",
                    "formula":
                    "PCB_cap_mass_film + "
                    "PCB_cap_mass_SMD + "
                    "PCB_cap_mass_Tantalum",
                }
            },
            "production amount":
            0.0,
            "reference product":
            "mounted printed circuit board",
            "type":
            "process",
            "unit":
            "kilogram",
            "worksheet name":
            "PCB inventory",
        },
        {
            "categories": ("electronics", "board"),
            "code":
            "45cb34db4147e510a2561cceec541f6b",
            "comment":
            "one input",
            "database":
            "PCB",
            "exchanges": [{
                "amount": 1.0,
                "database": "PCB",
                "location": "GLO",
                "name": "unmounted printed circuit board",
                "type": "production",
                "uncertainty type": 0,
                "unit": "square meter",
            }],
            "location":
            "GLO",
            "name":
            "unmounted printed circuit board",
            "production amount":
            1.0,
            "reference product":
            "unmounted printed circuit board",
            "type":
            "process",
            "unit":
            "square meter",
            "worksheet name":
            "PCB inventory",
        },
    ]
    assert ei.data == expected
Exemplo n.º 29
0
def test_correct_reference_product():
    ei = ExcelImporter(
        os.path.join(EXCEL_FIXTURES_DIR, "basic_example_with_reference_product.xlsx")
    )
    ei.apply_strategy(assign_only_product_as_production)
    assert ei.data[0]["reference product"] == "Acetic acid, at plant"