brightway-lca / brightway2-data

Tools for the management of inventory databases and impact assessment methods. Part of the Brightway LCA framework.
https://docs.brightway.dev/
BSD 3-Clause "New" or "Revised" License
8 stars 21 forks source link

`Exchange.lca()` is broken #92

Closed BenPortner closed 2 years ago

BenPortner commented 2 years ago

Exchange.lca() fails with AttributeError: 'LCA' object has no attribute 'fix_dictionaries'. The error originates in this line. Apparently, the method does not exist anymore.

Minimum Error Example:

    database = DatabaseChooser("testy")
    data = {
        ("testy", "A"): {},
        ("testy", "C"): {"type": "biosphere"},
        ("testy", "B"): {
            "exchanges": [
                {"input": ("testy", "A"), "amount": 1, "type": "technosphere"},
                {"input": ("testy", "B"), "amount": 1, "type": "production"},
                {"input": ("testy", "C"), "amount": 1, "type": "biosphere"},
            ]
        },
    }
    database.write(data)

   act = get_activity(("testy","B"))
   exc = list(act.exchanges())[0]
   exc.lca()
tngTUDOR commented 2 years ago

I have been bitten by the same bug:

In [1]: import bw2data as bd
In [2]: bd.projects
Out[2]: 
Brightway2 projects manager with 2 objects:
default
im_project
Use `projects.report()` to get a report on all projects.

In [3]: bd.projects.set_current('im_project')

In [4]: bd.databases
Out[4]: 
Databases dictionary with 2 object(s):
biosphere3
imdb-cutoff

In [5]: db = bd.Database('imdb-cutoff')

In [6]: a = db.random()

In [7]: a_lca = a.lca()
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Input In [7], in <cell line: 1>()
----> 1 a_lca = a.lca()

File /opt/conda/lib/python3.10/site-packages/bw2data/proxies.py:114, in ActivityProxyBase.lca(self, method, amount)
112 if method is not None:
113     lca.lcia()
--> 114 lca.fix_dictionaries()
115 return lca

AttributeError: 'LCA' object has no attribute 'fix_dictionaries'

In [8]: bd.version.version
Out[8]: (4, 0, 'DEV16')

In [9]: import bw2calc as bc

In [10]: bc.version.version
Out[10]: (2, 0, 'DEV7')
tngTUDOR commented 2 years ago

the "fix_dictionaries" method from the LCA class was removed after commit cc92331

The removal took place exactly here: c3a0252e

LCA.reverse_dict is removed; all reversed dictionaries are available at LCA.dicts.{name}.reversed.

tngTUDOR commented 2 years ago

old fix_dictionaries docstrings return part:

Doesn't require any arguments or return anything, but changes self.activity_dict, self.product_dict and self.biosphere_dict.

but newer LCA objects give access to such dictionaries at: self.dicts.activity|product|biosphere.

tngTUDOR commented 2 years ago

Brightway2:

from brightway2 import *
projects.set_current('im')
db = Database('imdb')
a = db.random()
a_lca = LCA({a:1})
a_lca.lci() # lci will call load_lci_data 
# that will call fix_dictionaries [this is a bool argument to load_lci_data set to True as default
# a dictionary of the form :  
# { ...
# ('imdb', '3bec80a23977b9a87692eef35d2f4696'): 996,
# ('imdb', 'f89b229b8988e5567c5763ac96f4058b'): 997,
# ('imdb', '7f412e5c53d9f1407fa8c6d9f040b710'): 998,
#('imdb', '5698628595cc6322ce8449a548a58114'): 999,
# ...}
# will be already available at:
a_lca.activity_dict

Brightway25:

import bw2data as bd
import bw2calc as bc
bd.projects
bd.projects.set_current('im')
db = bd.Database('imdb')
a = db.random()
a_lca = LCA({a:1})
a_lca.lci()
a_lca.remap_inventory_dicts()
# a dictionary of the form:
# { ...
# ('imdb', 'f0253628810ae8e5e058b1b7a0228adb'): 996,
# ('imdb', '7713e1ce9fa532d09452a206e03f1fa0'): 997,
# ('imdb', '2596232e969a929c42ac37ffdb779837'): 998,
# ('imdb', '03e0389304cf39a6c80afa42f25e61a2'): 999,
# ...}
# will be available at:
lca.dicts.activity