oie-mines-paristech / lca_algebraic

Layer over brightway2 for algebraic definition of parametric models and super fast computation of LCA
BSD 2-Clause "Simplified" License
34 stars 18 forks source link

Missmatch between chemical formula and parameter formula #44

Closed inrDL closed 2 months ago

inrDL commented 4 months ago

Hello,

In ecoinvent, some emissions have an attribute 'formula', which gives the chemical formula of the molecule emitted, such as 'CO2' for carbon dioxide. When freezing a foreground database, that has some ecoinvent activities in it with such emissions, I get the error : 'TypeError: Cannot convert expression to float' followed by 'Exception: Context : CO2'

From what I understand, freezing convert all the parameters formulas into float. But when it come accros a chemical formula, it raises an error because the parameter CO2 does not exists. So it seems that the same attribute name 'formula' is given to two distinct fields. It is correct ? How could this be corrected ? (except renaming in ecoinvent all 'formula' field into 'chemical formula', which takes ~2 hour)

Thank you !

raphaeljolivet commented 4 months ago

Hi,

That's a known bug of Brightway that they have fixed in DEV/beta release but not on older versions yet. Until it is fixed, you can simply cleanup all "formula" attribute of your ecoinvent database with a sample code like :

db = bw.Database("name_of_eco_db")
for act in db:
    for ex in act.exchanges():
        if "formula" in ex: 
            print(ex, ex["formula"])
            del ex["formula"]
            ex.save()

It takes a couple of minutes, not two hours.

raphaeljolivet commented 4 months ago

BTW : the problem only happens if you have copied a background ecoinvent activity, containg chemical formulas, in your foreground and then try to freeze it : You are not supposed to "freeze" your background database, which is static / not parametrized.

inrDL commented 4 months ago

Hello,

Thank you very much. Okay for the cleaning, I had done something similar that took too long, I will use your code !

I understand we are not supposed to freeze background database, but indeed it happen if you copy an ecoinvent activity to your foreground database and modify it but keeping old exchanges, which was my case.