brightway-lca / brightway2-io

Importing and exporting for the Brightway LCA framework
BSD 3-Clause "New" or "Revised" License
26 stars 40 forks source link

AttributeError in add_missing_cfs(): 'ExcelLCIAImporter' object has no attribute 'biosphere_name' #249

Open Paul-Robineau opened 4 months ago

Paul-Robineau commented 4 months ago

Hello there,

I am trying to import Ucrad as an LCIA method in brightway2, through its Excel file containing the characterization factors for various radionuclides. I've followed the documentation and use the code below, which works fine in theory (tested on the already present ionising radiations: human health method in EFv3.1, by extracting the data from the .json, building an Excel in the correct form, and do the import).

# Initialize and set project
import brightway2 as bw
import bw2io
bw.projects.set_current('myproject')
# bw.bw2setup() have already been done previously  

# Necessary variables
NewLCIAm_path = r"mypath\Ucrad-BqU235AirEquivBq-CF.xlsx"
NewLCIAm_name = ("Ucrad", "Ucrad midpoint Bq U235 Air-Eq")
NewLCIAm_description = ""
NewLCIAm_unit = "Bq U235 Air-Eq"

# Import from Excel
NewLCIAm = bw2io.ExcelLCIAImporter(
    NewLCIAm_path, 
    NewLCIAm_name, 
    NewLCIAm_description,
    NewLCIAm_unit,
    )
NewLCIAm.apply_strategies()

Output:

Applying strategy: csv_restore_tuples Applying strategy: csv_numerize Applying strategy: csv_drop_unknown Applying strategy: set_biosphere_type Applying strategy: drop_unspecified_subcategories Applying strategy: link_iterable_by_fields Applying strategy: drop_falsey_uncertainty_fields_but_keep_zeros Applying strategy: convert_uncertainty_types_to_integers Applied 8 strategies in 0.15 seconds

Then:

NewLCIAm.statistics()

Returns:

1 methods 345 cfs 214 unlinked cfs

As the unlinked cfs come from the ones addressing unpresent biosphere flows, I thus do:

NewLCIAm.add_missing_cfs()

Which returns:

AttributeError Traceback (most recent call last) Cell In[11], line 1 ----> 1 NewLCIAm.add_missing_cfs()

File ~\AppData\Local\miniconda3\envs\bw2\Lib\site-packages\bw2io\importers\base_lcia.py:118, in LCIAImporter.add_missing_cfs(self) 116 if "input" not in cf: 117 cf["code"] = str(uuid.uuid4()) --> 118 while (self.biosphere_name, cf["code"]) in mapping: 119 cf["code"] = str(uuid.uuid4()) 120 new_flows.append(cf)

AttributeError: 'ExcelLCIAImporter' object has no attribute 'biosphere_name'

I don't understand the problem, did I miss something obvious?

Paul

Paul-Robineau commented 4 months ago

Some update:

By looking at the code, it seems like the init of the ExcelLCIAImporter class in the excel_lcia.py prevents or cancels the init of the LCIAImporter class in the base_lcia.py.

The LCIAImporter class init in base_lcia.py:

class LCIAImporter(ImportBase):
    def __init__(self, filepath, biosphere=None):
        print("init yup")
        self.applied_strategies = []
        self.filepath = filepath
        self.biosphere_name = biosphere or config.biosphere
        if self.biosphere_name not in databases:
            raise ValueError(
                "Can't find biosphere database {}".format(self.biosphere_name)
            )
        self.strategies = [

# [...] the code continues

The excel_lcia.py import the LCIAImporter class, so biosphere_name should be ok, but then we have the ExcelLCIAImporter class init :

class ExcelLCIAImporter(LCIAImporter):

# [...] Some code commmentaries

    format = "Excel"
    extractor = ExcelExtractor

    def __init__(self, filepath, name, description, unit, **metadata):
        assert isinstance(name, tuple)
        super(ExcelLCIAImporter, self).__init__(filepath, biosphere=None)
        self.strategies = [

# [...] the code continues

The line super(ExcelLCIAImporter, self).__init__(filepath, biosphere=None) was added on our side after looking at the structure of the init of the SimaProLCIACSVImporter class, which contains an analog line, which gave the idea. When it's added, the AttributeError on biosphere_name of the ExcelLCIAImporter disappears.

Not sure if it's the proper way to fix the issue, but hope it helps!