def add_metadata(nodes, lca): """Add metadata to nodes, like name, unit, and category.""" ra, rp, rb = lca.reverse_dict() new_nodes = {} for key, value in nodes.items(): new_value = copy.deepcopy(value) if key == -1: new_value.update({ "name": "Functional unit", "unit": "unit", "categories": ["Functional unit"], }) else: if "row" in value: index = value["row"] else: index = key code = ra[index] ds = Database(code[0]).get([code[1]]).as_dict() new_value.update({ "name": ds.get("name", "Unknown"), "categories": ds.get("categories", []), "unit": ds.get("unit", "Unknown"), "key": code, }) new_nodes[key] = new_value return new_nodes
def test_existing_db_locations_update(): db = Database("foo") db.write({ ("foo", "1"): {'location': 'nowhere', 'name': 'b'}, ("foo", "2"): {'location': 'SGCC', 'name': 'a'}, }) assert db.get("2")['location'] == 'SGCC' assert update_db_ecoinvent_locations('foo') == 1 assert db.get("2")['location'] == 'CN-SGCC' assert update_db_ecoinvent_locations('bar') == 0
def test_existing_db_locations_update(): db = Database("foo") db.write({ ("foo", "1"): { "location": "nowhere", "name": "b" }, ("foo", "2"): { "location": "SGCC", "name": "a" }, }) assert db.get("2")["location"] == "SGCC" assert update_db_ecoinvent_locations("foo") == 1 assert db.get("2")["location"] == "CN-SGCC" assert update_db_ecoinvent_locations("bar") == 0
def format_node(node_key, node_data, ra): if node_key == -1: return { "name": "Functional unit", "unit": "unit", "location": config.global_location, "categories": "", "id": next(counter), "amount": 1.0, } if "row" in node_data: node_key = node_data["row"] key = ra[node_key] ds = Database(key[0]).get([key[1]]).as_dict() return { "name": ds.get("name", "Unknown"), "unit": ds.get("unit", "Unknown"), "location": ds.get("location", "Unknown"), "categories": ", ".join(ds.get("categories", [])), "id": next(counter), "amount": node_data["amount"], }
def test_get_activity_singlefile(self): database = Database("a database", "singlefile") database.write({ ("a database", "foo"): { 'exchanges': [{ 'input': ("a database", "foo"), 'amount': 1, 'type': 'production', }], 'location': 'bar', 'name': 'baz' }, }) self.assertTrue(isinstance( get_activity(("a database", "foo")), SFActivity )) self.assertTrue(isinstance( Database.get(("a database", "foo")), SFActivity ))
def find_biosphere(self, row, col): inp = self.rb[row] inp_data = Database(inp[0]).load()[inp] outp = self.ra[col] outp_data = Database(outp[0]).load()[outp] try: exc = [ x for x in outp_data.get("exchanges", []) if x["input"] == inp ][0] except IndexError: raise ValueError("Can't find this exchange") return { "input": { "key": inp, "data": inp_data }, "output": { "key": outp, "data": outp_data }, "exchange": exc, }
def lci_matrices_to_excel(database_name, include_descendants=True): """Fake docstring""" from bw2calc import LCA print("Starting Excel export. This can be slow for large matrices!") safe_name = safe_filename(database_name, False) filepath = os.path.join(projects.output_dir, safe_name + ".xlsx") lca = LCA({Database(database_name).random(): 1}) lca.load_lci_data() lca.fix_dictionaries() if not include_descendants: lca.activity_dict = { key: value for key, value in lca.activity_dict.items() if key[0] == database_name } # Drop biosphere flows with zero references # TODO: This will ignore (-1 + 1 = 0) references lca.biosphere_dict = { key: value for key, value in lca.biosphere_dict.items() if lca.biosphere_matrix[lca.biosphere_dict[key], :].sum() != 0 } workbook = xlsxwriter.Workbook(filepath) bold = workbook.add_format({'bold': True}) print("Sorting objects") sorted_activity_keys = sorted([(Database.get(key).get("name") or u"Unknown", key) for key in lca.activity_dict]) sorted_product_keys = sorted([(Database.get(key).get("name") or u"Unknown", key) for key in lca.product_dict]) sorted_bio_keys = sorted([(Database.get(key).get("name") or u"Unknown", key) for key in lca.biosphere_dict]) tm_sheet = workbook.add_worksheet('technosphere') tm_sheet.set_column('A:A', 50) data = Database(database_name).load() # Labels for index, data in enumerate(sorted_activity_keys): tm_sheet.write_string(0, index + 1, data[0]) for index, data in enumerate(sorted_product_keys): tm_sheet.write_string(index + 1, 0, data[0]) print("Entering technosphere matrix data") coo = lca.technosphere_matrix.tocoo() # Translate row index to sorted product index act_dict = {obj[1]: idx for idx, obj in enumerate(sorted_activity_keys)} pro_dict = {obj[1]: idx for idx, obj in enumerate(sorted_product_keys)} bio_dict = {obj[1]: idx for idx, obj in enumerate(sorted_bio_keys)} pro_lookup = {v: pro_dict[k] for k, v in lca.product_dict.items()} bio_lookup = {v: bio_dict[k] for k, v in lca.biosphere_dict.items()} act_lookup = {v: act_dict[k] for k, v in lca.activity_dict.items()} # Matrix values for row, col, value in zip(coo.row, coo.col, coo.data): tm_sheet.write_number(pro_lookup[row] + 1, act_lookup[col] + 1, value) bm_sheet = workbook.add_worksheet('biosphere') bm_sheet.set_column('A:A', 50) data = Database(database_name).load() # Labels for index, data in enumerate(sorted_activity_keys): bm_sheet.write_string(0, index + 1, data[0]) for index, data in enumerate(sorted_bio_keys): bm_sheet.write_string(index + 1, 0, data[0]) print("Entering biosphere matrix data") coo = lca.biosphere_matrix.tocoo() # Matrix values for row, col, value in zip(coo.row, coo.col, coo.data): bm_sheet.write_number(bio_lookup[row] + 1, act_lookup[col] + 1, value) COLUMNS = (u"Index", u"Name", u"Reference product", u"Unit", u"Categories", u"Location") tech_sheet = workbook.add_worksheet('technosphere-labels') tech_sheet.set_column('B:B', 60) tech_sheet.set_column('C:C', 30) tech_sheet.set_column('D:D', 15) tech_sheet.set_column('E:E', 30) print("Writing metadata") # Header for index, col in enumerate(COLUMNS): tech_sheet.write_string(0, index, col, bold) tech_sheet.write_comment( 'C1', "Only for ecoinvent 3, where names =/= products.", ) for index, data in enumerate(sorted_activity_keys): obj = Database.get(data[1]) tech_sheet.write_number(index + 1, 0, index + 1) tech_sheet.write_string(index + 1, 1, obj.get(u'name') or u'Unknown') tech_sheet.write_string(index + 1, 2, obj.get(u'reference product') or u'') tech_sheet.write_string(index + 1, 3, obj.get(u'unit') or u'Unknown') tech_sheet.write_string(index + 1, 4, u" - ".join(obj.get(u'categories') or [])) tech_sheet.write_string(index + 1, 5, obj.get(u'location') or u'Unknown') COLUMNS = ( u"Index", u"Name", u"Unit", u"Categories", ) bio_sheet = workbook.add_worksheet('biosphere-labels') bio_sheet.set_column('B:B', 60) bio_sheet.set_column('C:C', 15) bio_sheet.set_column('D:D', 30) # Header for index, col in enumerate(COLUMNS): bio_sheet.write_string(0, index, col, bold) for index, data in enumerate(sorted_bio_keys): obj = Database.get(data[1]) bio_sheet.write_number(index + 1, 0, index + 1) bio_sheet.write_string(index + 1, 1, obj.get(u'name') or u'Unknown') bio_sheet.write_string(index + 1, 2, obj.get(u'unit') or u'Unknown') bio_sheet.write_string(index + 1, 3, u" - ".join(obj.get(u'categories') or [])) workbook.close() return filepath
def get_static_forest_keys(): """get the key of the forest sequestation processes in the set `forest`. Needed to consider forest sequestation dynamic in already installed dbs that are static. When unit processes from other databases need to be evaluated dynamically is enough to add their name to the set set `forest` inside the function. #Wrapped into a function otherwise it points to the default project if temporalis is imported before the current project is set """ #they are all underbark, check if the bark CO2 is considered or not...if needed ei_22 = ["hardwood, Scandinavian, standing, under bark, in forest", "hardwood, standing, under bark, in forest", "eucalyptus ssp., standing, under bark, u=50%, in plantation", "meranti, standing, under bark, u=70%, in rainforest", "softwood, standing, under bark, in forest", "azobe (SFM), standing, under bark, in rain forest", "paraná pine, standing, under bark, in rain forest", "softwood, Scandinavian, standing, under bark, in forest"] ei_32_33 = [ "softwood forestry, paraná pine, sustainable forest management", "hardwood forestry, mixed species, sustainable forest management", "hardwood forestry, beech, sustainable forest management", "softwood forestry, spruce, sustainable forest management", "hardwood forestry, meranti, sustainable forest management", "hardwood forestry, azobe, sustainable forest management", "softwood forestry, pine, sustainable forest management", #"import of roundwood, azobe from sustainable forest management, CM, debarked", # no #"import of roundwood, meranti from sustainable forest management, MY, debarked", # no "softwood forestry, mixed species, sustainable forest management", "hardwood forestry, oak, sustainable forest management", "hardwood forestry, birch, sustainable forest management", "hardwood forestry, eucalyptus ssp., sustainable forest management" ] #it should be the first one, but as it is not temporalis must be the second #~estelle=[ #~'_Wood waste, sorted at panel plant, 20% water on dry mass basis/ EU U', #~'_Wood construction waste, 20% water on dry mass basis', #~] estelle=[ #'_Allocation correction, spruce, 20% water on dry basis', #'_Aqueous microporous paint for wood, SIPEV/FR S_26082014', #'_Aqueous phase coating, SIPEV/FR S_20140527', #'_Polyurethane glue, Bostik, at plant/FR S_20140627', #'_Transport, lorry >28t, fleet average/CH S_litre', #'Polyethylene, HDPE, granulate, at plant/RER', '_Beech industrial roundwood, 80% water on dry mass basis, at forest road/FR S_20120830', '_Douglas-fir industrial roundwood, 65% water on dry mass basis, at forest road/FR S_20120830', '_Maritime pine industrial roundwood, 100% water on dry mass basis, at forest road/FR S_20120830', '_Oak industrial roundwood, 80% water on dry mass basis, at forest road/FR S_20120830', '_Scotch pine industrial roundwood, 104% water on dry mass basis, at forest road/FR S_20120830', '_Spruce industrial roundwood, 111% water on dry mass basis, at forest road/FR S_20120830', '_Wood waste, sorted at panel plant, 20% water on dry mass basis/ EU U', '_Wood construction waste, 20% water on dry mass basis', ] test=['co2bio_test'] #dunno why this below returns skips some datasets #FORMIT_forest=[x['name'] for x in Database(db) if x['name'].split(',')[-1]==' NPP' for db in ['BAU0', 'BAU26', 'BAU45', 'BAU85', 'SCEN2_45', 'SCEN3_45', 'SCEN4_45', 'SCEN5_45', 'SCEN6_45']] #~FORMIT_forest=[] #~for db in ['BAU0', 'BAU26', 'BAU45', 'BAU85', 'SCEN2_45', 'SCEN3_45', 'SCEN4_45', 'SCEN5_45', 'SCEN6_45','trial']: #~FORMIT_forest.extend([x['name'] for x in Database(db) if x['name'].split(',')[-1]==' biogenic']) forest = set( ei_22 + ei_32_33 + estelle + test )# + FORMIT_forest) projects.set_current("{}".format(projects.current)) #need to do this otherwise uses default project if imported before setting the proj db = Database(config.biosphere) #search 'Carbon dioxide, in air' sequestered from processes in `forest` (empty set when biopshere not existing like in tests) return set() if not db else \ set([x.output.key for x in db.get('cc6a1abb-b123-4ca6-8f16-38209df609be').upstream(kinds=None) if x.output.get('name') in forest])