michaelweinold / pylcaio

A Python class that structures, manipulates and facilitates the hybridization of life cycle assessment and environmentally extended input output at the process or database level.
GNU General Public License v2.0
0 stars 0 forks source link

`export_to_brightway2` throws error `InvalidExchange` (when executed via SSH in VS Code) #10

Closed michaelweinold closed 2 years ago

michaelweinold commented 2 years ago

Attempting to export the hybridized database to a Brightway project using the a method from the pylcaio.Analysis class:

Analysis.export_to_brightway2(
    bw2_project_name = 'my_project',
    created_database_name = 'hybrid-ecoinvent',
    path_to_ecoinvent_ecospold_datasets = path_dir_ecoinvent_input,
    aggregated = False
)

throws the following error:

---------------------------------------------------------------------------
InvalidExchange                           Traceback (most recent call last)
Cell In [10], line 1
----> 1 Analysis.export_to_brightway2(
      2     bw2_project_name = 'my_project',
      3     created_database_name = 'hybrid-ecoinvent',
      4     path_to_ecoinvent_ecospold_datasets = path_dir_ecoinvent_input,
      5     aggregated = False
      6 )

File ~/pylcaio/src/pylcaio.py:2174, in Analysis.export_to_brightway2(self, bw2_project_name, created_database_name, path_to_ecoinvent_ecospold_datasets, aggregated)
   2172 eco_importer = SingleOutputEcospold2Importer(path_to_ecoinvent_ecospold_datasets, 'hybrid-ecoinvent')
   2173 eco_importer.apply_strategies()
-> 2174 eco_importer.write_database()
   2176 if not aggregated:
   2177     # importing exiobase into the project
   2178     self.import_exiobase_into_brightway2()

File ~/miniconda3/envs/hybridization/lib/python3.10/site-packages/bw2io/importers/base_lci.py:269, in LCIImporter.write_database(self, data, delete_existing, backend, activate_parameters, **kwargs)
    266 self.write_database_parameters(activate_parameters, delete_existing)
    268 existing.update(data)
--> 269 db.write(existing)
    271 if activate_parameters:
    272     self._write_activity_parameters(activity_parameters)

File ~/miniconda3/envs/hybridization/lib/python3.10/site-packages/bw2data/project.py:358, in writable_project(wrapped, instance, args, kwargs)
    356 if projects.read_only:
    357     raise ReadOnlyProject(READ_ONLY_PROJECT)
--> 358 return wrapped(*args, **kwargs)

File ~/miniconda3/envs/hybridization/lib/python3.10/site-packages/bw2data/backends/peewee/database.py:260, in SQLiteBackend.write(self, data, process)
    258 if data:
    259     try:
--> 260         self._efficient_write_many_data(data)
    261     except:
    262         # Purge all data from database, then reraise
    263         self.delete(warn=False)

File ~/miniconda3/envs/hybridization/lib/python3.10/site-packages/bw2data/backends/peewee/database.py:204, in SQLiteBackend._efficient_write_many_data(self, data, indices)
    197     self.pbar = pyprind.ProgBar(
    198         len(data),
    199         title="Writing activities to SQLite3 database:",
    200         monitor=True
    201     )
    203 for index, (key, ds) in enumerate(data.items()):
--> 204     exchanges, activities = self._efficient_write_dataset(
    205         index, key, ds, exchanges, activities
    206     )
    208 if not getattr(config, "is_test", None):
    209     print(self.pbar)

File ~/miniconda3/envs/hybridization/lib/python3.10/site-packages/bw2data/backends/peewee/database.py:156, in SQLiteBackend._efficient_write_dataset(self, index, key, ds, exchanges, activities)
    154 for exchange in ds.get('exchanges', []):
    155     if 'input' not in exchange or 'amount' not in exchange:
--> 156         raise InvalidExchange
    157     if 'type' not in exchange:
    158         raise UntypedExchange

InvalidExchange: 
michaelweinold commented 2 years ago

Attempting to debug the cell additionally lead to a loop of error messages in VS Code:

Screenshot 2022-10-22 at 12 08 37

Compare also https://github.com/microsoft/vscode/issues/164676

michaelweinold commented 2 years ago

Compare the notebook by Agez et al.: https://github.com/OASES-project/pylcaio/blob/master/doc/export%20to%20brightway2.ipynb

cmutel commented 2 years ago

Not sure how this interfaces with VSCode, but I would run this in ipython and then use %debug to figure out what is missing. But I guess that this is the input attribute, meaning that you tried to consume something, but couldn't find the exact match for the name or reference product or similar. Some names changed in ecoinvent 3.9, but maybe also against earlier versions as well.

You could also hotpatch the breaking code to log the attributes of the exchange causing the problem.

tngTUDOR commented 2 years ago

before "ecoi_importer.write_database" it would be safer to make sure there aren't any unlinked exchanges, and for "debug" purposes print the unlinked ones.

File ~/pylcaio/src/pylcaio.py:2174, in Analysis.export_to_brightway2(self, bw2_project_name, created_database_name, path_to_ecoinvent_ecospold_datasets, aggregated)
   2172 eco_importer = SingleOutputEcospold2Importer(path_to_ecoinvent_ecospold_datasets, 'hybrid-ecoinvent')
   2173 eco_importer.apply_strategies()
-> 2174 eco_importer.write_database()
   2176 if not aggregated:
   2177     # importing exiobase into the project
   2178     self.import_exiobase_into_brightway2()
michaelweinold commented 2 years ago

The error message is caused by unmatched exchanges. Compare also the brightway documentation.