brightway-lca / bw_hybrid

Hybrid (Input-Output/Process-based) Life-Cycle Assessment
https://docs.brightway.dev/projects/hybrid/
BSD 3-Clause "New" or "Revised" License
2 stars 1 forks source link

`KeyError: 'method'` when reading pickled hybridized databases using `pylcaio.Analysis` #25

Closed michaelweinold closed 2 years ago

michaelweinold commented 2 years ago

When attempting to read a pickled pylcaio.LCAIO class instance with the pylcaio.Analysis method, the following error is thrown:

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
File ~/pylcaio/src/pylcaio.py:1915, in Analysis.__init__(self, path_to_hybrid_system)
   1914 try :
-> 1915     self.C = pd.concat([pd.DataFrame(self.C_f.todense(), index=self.IMP['method'], columns=self.STR['MATRIXID']),
   1916                pd.DataFrame(self.C_io.todense(), index=self.impact_categories_IO,
   1917                             columns=self.flows_of_IO)]).fillna(0)
   1918 except KeyError:

KeyError: 'method'

During handling of the above exception, another exception occurred:

KeyError                                  Traceback (most recent call last)
Cell In [10], line 1
----> 1 Analysis = pylcaio.Analysis(path_file_hybrid)

File ~/pylcaio/src/pylcaio.py:1919, in Analysis.__init__(self, path_to_hybrid_system)
   1915     self.C = pd.concat([pd.DataFrame(self.C_f.todense(), index=self.IMP['method'], columns=self.STR['MATRIXID']),
   1916                pd.DataFrame(self.C_io.todense(), index=self.impact_categories_IO,
   1917                             columns=self.flows_of_IO)]).fillna(0)
   1918 except KeyError:
-> 1919     self.C = pd.concat([pd.DataFrame(self.C_f.todense(), index=self.IMP['method'], columns=self.STR['MATRIXID']),
   1920                pd.DataFrame(self.C_io.todense(), index=self.impact_categories_IO,
   1921                             columns=self.flows_of_IO)]).fillna(0)
   1922 self.C_index = self.C.index.tolist()
   1923 self.C = back_to_sparse(self.C)

KeyError: 'method'
michaelweinold commented 2 years ago

The error is resolved by using a characterization file (such as LCIA_implementation_3.5.xlsx) when reading the Ecoinvent database with the ecospold2matrix package:

parser = e2m.Ecospold2Matrix(
    sys_dir = path_dir_ecoinvent_input,
    project_name = e2m_project_name,
    characterisation_file = path_file_ecoinvent_characterisation,
    out_dir = path_dir_databases_pickle,
    positive_waste = False,
    nan2null = True
)
michaelweinold commented 2 years ago

Resolved with https://github.com/michaelweinold/Autumn-School-2022/commit/4ac0713999ae821572cf7ce65b69da65fdacce55