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_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.º 3
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.º 4
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.º 5
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.º 6
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.º 7
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.º 8
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.º 9
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