brightway

How to solve AttributeError in LCIA bw25?


Recently, I started to learn brightway25 and I am stuck in an error (AttributeError: 'MethodConfig' object has no attribute 'get) when trying to an LCA with Multiple Functional Units and LCIA Sets (as explained in tutorial).

I would like to know if I am doing something wrong, already managed to do an LCA with Single Functional Unit and LCIA Set without any issue.

The code I did is the following:

import bw2data as bd
import bw2calc as bc
import bw2io as bi
import numpy as np

bd.projects.set_current(name='carlos-bw25')
ei = bd.Database('ecoinvent-3.10-cutoff')
m = bd.methods

# activities
pp_search = [a for a in ei if 'polypropylene' in a['name'] and 'granulate' in a['name'] and a['location'] == 'GLO' ]
hdpe_search = [a for a in ei if 'polyethylene' in a['name'] and 'high density' in a['name'] and a['location'] == 'GLO' ]
ldpe_search = [a for a in ei if 'polyethylene' in a['name'] and 'low density' in a['name'] and not 'linear' in a['name'] and not 'packaging' in a['name'] and a['location'] == 'GLO' ]
activities = [pp_search[0], hdpe_search[0], ldpe_search[0]]

# LCIA method
ef_list = [m for m in bd.methods if 'EF' in str(m) and 'v3.1' in str(m) and 'LT' not in str(m) and 'EN15804' not in str(m)]
lcia_method = [ef_list[0], ef_list[1], ef_list[8], ef_list[-5]]

# LCIA
functional_units = {
    "pp": {pp.id: 1},
    "hdpe": {hdpe.id: 1},
    "ldpe": {ldpe.id: 1},
}

methodconfig = bc.MethodConfig(impact_categories=lcia_method)
data_objs = bd.get_multilca_data_objs(functional_units, methodconfig)

The following error shows after running the last line of code to get the data_objs:

AttributeError                            Traceback (most recent call last)
Cell In[11], line 1
----> 1 data_objs = bd.get_multilca_data_objs(functional_units, methodconfig)

File ~\anaconda3\envs\ab\Lib\site-packages\bw2data\compat.py:174, in get_multilca_data_objs(functional_units, method_config)
    169 complete_database_names = set.union(
    170     *[Database(db_label).find_graph_dependents() for db_label in input_database_names]
    171 )
    172 data_objs = [Database(obj).datapackage() for obj in complete_database_names]
--> 174 for ic in set(method_config.get("impact_categories", [])):
    175     if ic not in methods:
    176         raise ValueError(f"Impact category (`Method`) {ic} not in this project")

File ~\anaconda3\envs\ab\Lib\site-packages\pydantic\main.py:856, in BaseModel.__getattr__(self, item)
    853     return super().__getattribute__(item)  # Raises AttributeError if appropriate
    854 else:
    855     # this is the current error
--> 856     raise AttributeError(f'{type(self).__name__!r} object has no attribute {item!r}')

AttributeError: 'MethodConfig' object has no attribute 'get'

I don’t really know what I can do, I just verified the data types and everything seems to be fine in the code.

My bw25 is:

Thank you in advance for any help,


Solution

  • You can define the config as a dictionary that contains the key impact_categories, then pass the list of categories to that key instead of using bc.MethodConfig directly.

    ef_list = [m for m in bd.methods if 'EF' in str(m) and 'v3.1' in str(m) and 'LT' not in str(m) and 'EN15804' not in str(m)]
    lcia_method = [ef_list[0], ef_list[1], ef_list[8], ef_list[-5]]
    
    config = {"impact_categories": lcia_method} # here create the dict.
    
    data_objs = bd.get_multilca_data_objs(functional_units=functional_units, method_config=config) # pass it here
    
    mlca = bc.MultiLCA(demands=functional_units, method_config=config, data_objs=data_objs) # and pass it here
    

    or if you want to use MethodConfig directly

    # MethodConfig approach - provides validation but requires conversion to dict
    method_config = bc.MethodConfig(
        impact_categories=lcia_method,
        normalizations={},  # Required to avoid None iteration errors
        weightings={}       # Required to avoid None iteration errors
    )
    config = method_config.model_dump()  # Convert to dictionary for use
    
    data_objs = bd.get_multilca_data_objs(functional_units=functional_units, method_config=config)
    mlca = bc.MultiLCA(demands=functional_units, method_config=config, data_objs=data_objs)