brightway-lca / hackathons

Organization of Brightway hackathons
5 stars 1 forks source link

OpenLCA' JSONs import cleanup #9

Closed shirubana closed 1 year ago

shirubana commented 1 year ago

Wanted to suggest maybe a Hackaton topic in case it gets traction/interest, but will be happy to work on anything there and learn some mroe.

Describe the Major Goal OpenLCA allows multiple exchange outputs (BW lingo: multiple productions) in a process beyond their main technosphere flow one (openLCA lingo: quantitative reference, LCA lingo: reference flow. BW lingo: production)
If these outputs are of the category 'technosphere', when they get imported into BW, they are imported into multiple processes with single productions each.
If these outputs are of the category 'waste', they get imported into BW as multiple productions. This causes issues when attempting to run bc.lci() because the matrix is not square

Expected behavior When importing, identify 'waste' exchange outputs, and save as multiple processes with single product.

To Reproduce

import bw2data as bd
import bw2calc as bc
import bw2io as bi
import bw_processing # Not sure yet if I need this
import bw_migrations # Not sure yet if I need this
from bw2io.importers.json_ld_lcia import JSONLDLCIAImporter

import os
import numpy as np
import pandas as pd

bd.projects.set_current('USLCI_FY22_Q2_3')

path = '../7_BW2_Importing_USLCI/databases/FY22_Q2_01_Zolca_LCIA_methods_mapping_FEDEFL_3'
uslci = bi.importers.JSONLDImporter(
    path, 
    "USLCI_FY22_Q2_1", 
    preferred_allocation="PHYSICAL_ALLOCATION" # Most USLCI allocations are based on physical allocations (source: Rebe Feraldi)
)

# Fix openLCA bug: on OpenLCA, on some processes (i.e. Natural Gas), the output exchanges there is a column named 
# 'Avoided product', and when it saves into JSON it sets the exchange['input'] to True instead of to False
for process_key, process_values in uslci.data['processes'].items():
    for i, exchange in enumerate(process_values['exchanges']):
        if (exchange['avoidedProduct'] == True) & (exchange['input'] == True):
            print(exchange['input'], exchange['flow']['name'])
            exchange['input'] = False
            uslci.data['processes'][process_key]['exchanges'][i] = exchange

uslci.apply_strategies()

uslci.apply_strategy(bi.strategies.special.add_dummy_processes_and_rename_exchanges)

uslci.write_separate_biosphere_database()
uslci.write_database()
uslci_methods = bi.importers.JSONLDLCIAImporter(path)
uslci_methods.apply_strategies()
uslci_methods.match_biosphere_by_id('USLCI_FY22_Q2_1 biosphere')
uslci_methods.statistics()
method_key = [act for act in bd.methods if 'Global warming' in str(act) and 'TRACI' in str(act)][0]
my_method = bd.Method(method_key)
alu = [act for act in db if act['name'].lower().startswith('aluminium, extrusion')][0]
functional_unit = {alu : 5}
lca = bc.LCA(functional_unit, method_key) 
lca.lci()  # Fails here   < -- FAIL 
lca.lcia()
lca.score

Screenshots image

Versions: Some updates for getting to this point on importing the JSON in bw2data and bw2io bw2data: image -BW requires to have a location and if the JSON has a "None" it fails. When running the calculations, it would throw an error otherwise. bw2io: -Failed allocation when trying to do one of the strategies. -Bug that only allowed Waste flows to be outputs; when Waste flows are inputs it allows them to be a technosphere input. image -allow bw2io>strategies>special.py to identify dummy processes by exchanges that don't have inputs or amounts which is the new way that USLCI is handling it. Old way (with "dummy" in the name is commented out. image

Other "minor" things to address [ ] units for flows do not get imported. They are present in exchanges though. [ ] if bw2data.projects.set_current runs twice, score is 0 as a result. [ ] regionalization is not working because codes don't match the mapping for brightways.

Goals for contribution

tngTUDOR commented 1 year ago

@shirubana,

shirubana commented 1 year ago
  1. USLCI database, Zolca file. Donwload latest version and open in OpenLCA. (Version 1.1.10) https://github.com/uslci-admin/uslci-content/blob/dev/downloads/uslci_fy22_q2_01_olca_10_3_.zolca
  2. Export as JSON, Unzim and point example code above to unziped contained folder.

OpenLCA -- v1.1.10

Sorry not sure what do you mean by fixure

tngTUDOR commented 1 year ago

A fixture can be a the set of data or variable values that help you test something 👉https://en.wikipedia.org/wiki/Test_fixture#Software 🤓 Your previous comment does add what I suppose is necessary to be able to reproduce the bug and be able to find solutions.

cmutel commented 1 year ago

Issue closed during cleanup for Brightcon 2023