def test_update_project_parameters(lci): lci.write_project_parameters() assert ProjectParameter.get(name="PCB_area").amount == 0.25 obj = LCIImporter("PCB") obj.project_parameters = [{'amount': 5, 'name': 'PCB_area'}] obj.write_project_parameters() assert ProjectParameter.get(name="PCB_area").amount == 5
def test_activity_parameters_with_group_name(): DATA = [{ 'code': 'A', 'database': 'db', 'exchanges': [{ 'amount': 0.0, 'input': ('db', 'A'), 'location': 'GLO', 'name': 'mounted printed circuit board', 'type': 'production', 'unit': 'kilogram' }], 'location': 'GLO', 'name': 'mounted printed circuit board', 'parameters': [{ 'name': 'something_something', 'amount': 0.8, 'group': 'g' }], 'type': 'process', 'unit': 'kilogram', }] obj = LCIImporter("db") obj.data = deepcopy(DATA) obj.write_database(activate_parameters=True) assert Group.select().where(Group.name == 'g').count() == 1 assert Group.select().count() == 1 assert ActivityParameter.get(name='something_something').amount == 0.8
def test_delete_pe_delete_existing(lci): lci.write_project_parameters() lci.write_database(activate_parameters=True) assert ParameterizedExchange.select().count() new = [{ 'code': '32aa5ab78beda5b8c8efbc89587de7a5', 'database': 'PCB', 'exchanges': [], 'location': 'GLO', 'name': 'mounted printed circuit board', 'parameters': {}, 'type': 'process', 'unit': 'kilogram' }, { 'code': '45cb34db4147e510a2561cceec541f6b', 'database': 'PCB', 'exchanges': [], 'location': 'GLO', 'parameters': {}, 'name': 'unmounted printed circuit board', 'type': 'process', 'unit': 'square meter' }] obj = LCIImporter("PCB") obj.data = new obj.write_database(activate_parameters=True, delete_existing=True) assert not ParameterizedExchange.select().count()
def test_delete_project_parameters(lci): lci.write_project_parameters() assert ProjectParameter.select().count() d = LCIImporter("PCB") d.project_parameters = [] d.write_project_parameters() assert not ProjectParameter.select().count()
def test_update_activity_parameters(lci): lci.write_project_parameters() lci.write_database(activate_parameters=True) assert ActivityParameter.get(name='PCB_mass_total').formula == \ 'PCB_cap_mass_film + PCB_cap_mass_SMD + PCB_cap_mass_Tantalum' new = [ { 'code': '32aa5ab78beda5b8c8efbc89587de7a5', 'database': 'PCB', 'exchanges': [], 'location': 'GLO', 'name': 'mounted printed circuit board', 'parameters': [{ 'name': 'PCB_mass_total', 'amount': 0.6, 'formula': 'PCB_cap_mass_film + 2' }], 'type': 'process', 'unit': 'kilogram' }, ] obj = LCIImporter("PCB") obj.data = deepcopy(new) obj.write_database(activate_parameters=True) assert ActivityParameter.get(name='PCB_mass_total').formula == \ 'PCB_cap_mass_film + 2'
def test_nonuniquecode_error_code(): lci = LCIImporter("woo") data = [ { "code": "32aa5ab78beda5b8c8efbc89587de7a5", "database": "woo", "exchanges": [], "location": "GLO", "name": "mounted printed circuit board", "parameters": [], "type": "process", "unit": "kilogram", }, { "code": "32aa5ab78beda5b8c8efbc89587de7a5", "database": "woo", "exchanges": [], "location": "GLO", "parameters": [], "name": "unmounted printed circuit board", "type": "process", "unit": "square meter", }, ] with pytest.raises(NonuniqueCode): lci.write_database(data)
def test_delete_pe_delete_existing(lci): lci.write_project_parameters() lci.write_database(activate_parameters=True) assert ParameterizedExchange.select().count() new = [ { "code": "32aa5ab78beda5b8c8efbc89587de7a5", "database": "PCB", "exchanges": [], "location": "GLO", "name": "mounted printed circuit board", "parameters": {}, "type": "process", "unit": "kilogram", }, { "code": "45cb34db4147e510a2561cceec541f6b", "database": "PCB", "exchanges": [], "location": "GLO", "parameters": {}, "name": "unmounted printed circuit board", "type": "process", "unit": "square meter", }, ] obj = LCIImporter("PCB") obj.data = new obj.write_database(activate_parameters=True, delete_existing=True) assert not ParameterizedExchange.select().count()
def test_update_activity_parameters(lci): lci.write_project_parameters() lci.write_database(activate_parameters=True) assert ( ActivityParameter.get(name="PCB_mass_total").formula == "PCB_cap_mass_film + PCB_cap_mass_SMD + PCB_cap_mass_Tantalum" ) new = [ { "code": "32aa5ab78beda5b8c8efbc89587de7a5", "database": "PCB", "exchanges": [], "location": "GLO", "name": "mounted printed circuit board", "parameters": [ { "name": "PCB_mass_total", "amount": 0.6, "formula": "PCB_cap_mass_film + 2", } ], "type": "process", "unit": "kilogram", }, ] obj = LCIImporter("PCB") obj.data = deepcopy(new) obj.write_database(activate_parameters=True) assert ( ActivityParameter.get(name="PCB_mass_total").formula == "PCB_cap_mass_film + 2" )
def test_wrongdatabase_error_code(): lci = LCIImporter("woo") data = [ { "code": "32aa5ab78beda5b8c8efbc89587de7a5", "database": "woo", "exchanges": [], "location": "GLO", "name": "mounted printed circuit board", "parameters": [], "type": "process", "unit": "kilogram", }, { "code": "45cb34db4147e510a2561cceec541f6b", "database": "PCB", "exchanges": [], "location": "GLO", "parameters": [], "name": "unmounted printed circuit board", "type": "process", "unit": "square meter", }, ] with pytest.raises(WrongDatabase): lci.write_database(data)
def test_activity_parameters_with_group_name(): DATA = [ { "code": "A", "database": "db", "exchanges": [ { "amount": 0.0, "input": ("db", "A"), "location": "GLO", "name": "mounted printed circuit board", "type": "production", "unit": "kilogram", } ], "location": "GLO", "name": "mounted printed circuit board", "parameters": [ {"name": "something_something", "amount": 0.8, "group": "g"} ], "type": "process", "unit": "kilogram", } ] obj = LCIImporter("db") obj.data = deepcopy(DATA) obj.write_database(activate_parameters=True) assert Group.select().where(Group.name == "g").count() == 1 assert Group.select().count() == 1 assert ActivityParameter.get(name="something_something").amount == 0.8
def test_update_project_parameters(lci): lci.write_project_parameters() assert ProjectParameter.get(name="PCB_area").amount == 0.25 obj = LCIImporter("PCB") obj.project_parameters = [{"amount": 5, "name": "PCB_area"}] obj.write_project_parameters() assert ProjectParameter.get(name="PCB_area").amount == 5
def test_activity_parameters_delete_old_groupname(lci): lci.write_project_parameters() lci.write_database(activate_parameters=True) assert ( ActivityParameter.select() .where(ActivityParameter.group == "PCB:32aa5ab78beda5b8c8efbc89587de7a5") .count() ) assert ( not ActivityParameter.select() .where(ActivityParameter.group == "some other group") .count() ) new = [ { "code": "32aa5ab78beda5b8c8efbc89587de7a5", "database": "PCB", "exchanges": [], "location": "GLO", "name": "mounted printed circuit board", "parameters": [ { "name": "PCB_mass_total", "amount": 0.6, "group": "some other group", "formula": "PCB_cap_mass_film + 2", } ], "type": "process", "unit": "kilogram", }, ] obj = LCIImporter("PCB") obj.data = deepcopy(new) obj.write_database(activate_parameters=True) assert ( not ActivityParameter.select() .where(ActivityParameter.group == "PCB:32aa5ab78beda5b8c8efbc89587de7a5") .count() )
def test_no_delete_project_parameters(lci): lci.write_project_parameters() assert ProjectParameter.select().count() d = LCIImporter("PCB") assert d.project_parameters is None d.write_project_parameters() assert ProjectParameter.select().count() d.project_parameters = [] d.write_project_parameters(delete_existing=False) assert ProjectParameter.select().count()
def test_nonuniquecode_error_code(): lci = LCIImporter("woo") data = [{ 'code': '32aa5ab78beda5b8c8efbc89587de7a5', 'database': 'woo', 'exchanges': [], 'location': 'GLO', 'name': 'mounted printed circuit board', 'parameters': [], 'type': 'process', 'unit': 'kilogram' }, { 'code': '32aa5ab78beda5b8c8efbc89587de7a5', 'database': 'woo', 'exchanges': [], 'location': 'GLO', 'parameters': [], 'name': 'unmounted printed circuit board', 'type': 'process', 'unit': 'square meter' }] with pytest.raises(NonuniqueCode): lci.write_database(data)
def test_wrongdatabase_error_code(): lci = LCIImporter("woo") data = [{ 'code': '32aa5ab78beda5b8c8efbc89587de7a5', 'database': 'woo', 'exchanges': [], 'location': 'GLO', 'name': 'mounted printed circuit board', 'parameters': [], 'type': 'process', 'unit': 'kilogram' }, { 'code': '45cb34db4147e510a2561cceec541f6b', 'database': 'PCB', 'exchanges': [], 'location': 'GLO', 'parameters': [], 'name': 'unmounted printed circuit board', 'type': 'process', 'unit': 'square meter' }] with pytest.raises(WrongDatabase): lci.write_database(data)
def import_into_brightway2(data, database_name): """Import an Ocelot linked database into a Brightway2 project. Make sure you in the **correct project** before running this function! Arguments: * ``data`` (list): Database output from Ocelot system model * ``database_name`` (str): Name of new database. Should not already exist in the current project. """ if not bw2: raise ImportError("Brightway2 not found") assert isinstance(database_name, str), "Database name must be a string" print("Creating database `{}` in project `{}`".format( database_name, bw2.projects.current) ) assert database_name not in bw2.databases # Don't store two copies in memory data[:] = list(Brightway2Converter.convert_to_brightway2(data, database_name)) importer = LCIImporter(database_name) # Already have products del importer.strategies[2] importer.data = data importer.apply_strategies() importer.match_database("biosphere3") stats = importer.statistics() if not stats[2]: print("Writing database") importer.write_database() return bw2.Database(database_name) else: print("Unlinked exchanges; not writing database") print(importer.statistics()) return importer
def test_activity_parameters_delete_old_groupname(lci): lci.write_project_parameters() lci.write_database(activate_parameters=True) assert ActivityParameter.select().where( ActivityParameter.group == 'PCB:32aa5ab78beda5b8c8efbc89587de7a5').count() assert not ActivityParameter.select().where( ActivityParameter.group == 'some other group').count() new = [ { 'code': '32aa5ab78beda5b8c8efbc89587de7a5', 'database': 'PCB', 'exchanges': [], 'location': 'GLO', 'name': 'mounted printed circuit board', 'parameters': [{ 'name': 'PCB_mass_total', 'amount': 0.6, 'group': 'some other group', 'formula': 'PCB_cap_mass_film + 2' }], 'type': 'process', 'unit': 'kilogram' }, ] obj = LCIImporter("PCB") obj.data = deepcopy(new) obj.write_database(activate_parameters=True) assert not ActivityParameter.select().where( ActivityParameter.group == 'PCB:32aa5ab78beda5b8c8efbc89587de7a5').count()
def import_into_brightway2(data, database_name): """Import an Ocelot linked database into a Brightway2 project. Make sure you in the **correct project** before running this function! Arguments: * ``data`` (list): Database output from Ocelot system model * ``database_name`` (str): Name of new database. Should not already exist in the current project. """ if not bw2: raise ImportError("Brightway2 not found") assert isinstance(database_name, str), "Database name must be a string" print(("Creating database `{}` in project `{}`".format( database_name, bw2.projects.current))) assert database_name not in bw2.databases # Don't store two copies in memory data[:] = list( Brightway2Converter.convert_to_brightway2(data, database_name)) importer = LCIImporter(database_name) # Already have products del importer.strategies[2] importer.data = data importer.apply_strategies() importer.match_database("biosphere3") stats = importer.statistics() if not stats[2]: print("Writing database") importer.write_database() return bw2.Database(database_name) else: print("Unlinked exchanges; not writing database") print((importer.statistics())) return importer
def test_delete_pe_update_still_deletes(): DATA = [{ 'code': 'A', 'database': 'db', 'exchanges': [{ 'amount': 0.0, 'input': ('db', 'A'), 'location': 'GLO', 'type': 'production', 'formula': "3 + 4", }], 'location': 'GLO', 'name': 'mounted printed circuit board', 'parameters': [{ 'name': 'something_something', 'amount': 0.8, 'group': 'g' }], 'type': 'process', 'unit': 'kilogram', }, { 'code': 'B', 'database': 'db', 'exchanges': [{ 'amount': 0.0, 'input': ('db', 'B'), 'location': 'GLO', 'type': 'production', 'formula': '1 + 2', }], 'location': 'GLO', 'name': 'bla bla', 'parameters': [{ 'name': 'something_else', 'amount': 0.2, 'group': 'h' }], 'type': 'process', 'unit': 'kilogram', }] obj = LCIImporter("db") obj.data = DATA obj.write_database(activate_parameters=True) assert Group.select().where(Group.name == 'g').count() == 1 assert Group.select().where(Group.name == 'h').count() == 1 assert Group.select().count() == 2 assert ParameterizedExchange.get(group='g').formula == "3 + 4" assert ParameterizedExchange.get(group='h').formula == "1 + 2" new = [{ 'code': 'B', 'database': 'db', 'exchanges': [{ 'amount': 0.0, 'input': ('db', 'B'), 'location': 'GLO', 'type': 'production', 'formula': '6 + 7', }], 'location': 'GLO', 'name': 'bla bla', 'parameters': [{ 'name': 'something_else', 'amount': 0.2, 'group': 'h' }], 'type': 'process', 'unit': 'kilogram', }] obj = LCIImporter("db") obj.data = new obj.write_database(delete_existing=False, activate_parameters=True) assert not ParameterizedExchange.select().where( ParameterizedExchange.group == 'g').count() assert ParameterizedExchange.select().where( ParameterizedExchange.group == 'h').count() == 1 assert ParameterizedExchange.get(group='h').formula == '6 + 7'
def test_delete_pe_update_still_deletes(): DATA = [ { "code": "A", "database": "db", "exchanges": [ { "amount": 0.0, "input": ("db", "A"), "location": "GLO", "type": "production", "formula": "3 + 4", } ], "location": "GLO", "name": "mounted printed circuit board", "parameters": [ {"name": "something_something", "amount": 0.8, "group": "g"} ], "type": "process", "unit": "kilogram", }, { "code": "B", "database": "db", "exchanges": [ { "amount": 0.0, "input": ("db", "B"), "location": "GLO", "type": "production", "formula": "1 + 2", } ], "location": "GLO", "name": "bla bla", "parameters": [{"name": "something_else", "amount": 0.2, "group": "h"}], "type": "process", "unit": "kilogram", }, ] obj = LCIImporter("db") obj.data = DATA obj.write_database(activate_parameters=True) assert Group.select().where(Group.name == "g").count() == 1 assert Group.select().where(Group.name == "h").count() == 1 assert Group.select().count() == 2 assert ParameterizedExchange.get(group="g").formula == "3 + 4" assert ParameterizedExchange.get(group="h").formula == "1 + 2" new = [ { "code": "B", "database": "db", "exchanges": [ { "amount": 0.0, "input": ("db", "B"), "location": "GLO", "type": "production", "formula": "6 + 7", } ], "location": "GLO", "name": "bla bla", "parameters": [{"name": "something_else", "amount": 0.2, "group": "h"}], "type": "process", "unit": "kilogram", } ] obj = LCIImporter("db") obj.data = new obj.write_database(delete_existing=False, activate_parameters=True) assert ( not ParameterizedExchange.select() .where(ParameterizedExchange.group == "g") .count() ) assert ( ParameterizedExchange.select().where(ParameterizedExchange.group == "h").count() == 1 ) assert ParameterizedExchange.get(group="h").formula == "6 + 7"
def lci(): obj = LCIImporter("PCB") obj.project_parameters = [{'amount': 0.25, 'name': 'PCB_area'}] obj.data = deepcopy(DATA) obj.database_parameters = deepcopy(DB) return obj
def lci(): obj = LCIImporter("PCB") obj.project_parameters = [{"amount": 0.25, "name": "PCB_area"}] obj.data = deepcopy(DATA) obj.database_parameters = deepcopy(DB) return obj