brightway-lca / brightway2

Metapackage for brightway2 imports and documentation
https://brightway.dev/
BSD 3-Clause "New" or "Revised" License
100 stars 37 forks source link

Ecoinvent local import: .WrongDatabase: Activity database must be ecoinvent39, but set() was also found #54

Closed Joshua-Schroijen closed 1 year ago

Joshua-Schroijen commented 1 year ago

Hello everyone,

I am trying to get started doing automated LCA using the ecoinvent 3.9 database, Brightway2 2.4.2 and Python (3.10). I'm not very knowledgeable yet so please bear with me. I am trying to import ecoinvent and write it to a regular Brightway2 database like so:

from pathlib import Path
from brightway2 import *

projects.set_current("TestEcoinventImport")
pte = Path("C:\\") / "Users" / "Joshua Schroijen" / "ecoinvent" / "ecoinvent 3.9_cutoff_ecoSpold02"
print(pte)
importer = SingleOutputEcospold2Importer(pte, "ecoinvent39")
importer.apply_strategies()
db = importer.write_database()

But this code snippet gives me the following exception on the last line:

Traceback (most recent call last):
  File "C:\Users\Joshua Schroijen\leaf-backend\business_logic\ecoinvent.py", line 9, in <module>
    db = importer.write_database()
  File "C:\Users\Joshua Schroijen\.conda\envs\bw2\lib\site-packages\bw2io\importers\base_lci.py", line 237, in write_database
    raise WrongDatabase(error)
bw2io.errors.WrongDatabase: Activity database must be ecoinvent39, but set() was also found

I will include all of the output of the snippet at the end of this post.

In other words, it seems like importing ecoinvent 3.9 locally doesn't work. From the documentation I can't infer that I am missing something. People using the Activity Browser are recommended to import ecoinvent by downloading it. But I don't want to use the Activity Browser (since I'm experimenting with LCA automation) and don't want to have to download this pretty big database every time (not to mention have the cybersecurity implications of doing so, since ecoinvent requires credentials). Is this an ecoinvent data format issue or a Brightway2 issue? I'm erring on the first, but in the snippet output I also see the message "Extracting XML data from 0 datasets", which I find odd since there very much is a lot of data in the directory I'm pointing the script at. I would appreciate any suggestions for a workaround. Am I missing something? Is this something that will be fixed or is easily fixable with the right trick?

Thanks a lot in advance for your help and input!

Joshua Schroijen

Complete snippet output:

Extracting XML data from 0 datasets
Extracted 0 datasets in 0.04 seconds
Applying strategy: normalize_units
Applying strategy: update_ecoinvent_locations
Applying strategy: remove_zero_amount_coproducts
Applying strategy: remove_zero_amount_inputs_with_no_activity
Applying strategy: remove_unnamed_parameters
Applying strategy: es2_assign_only_product_with_amount_as_reference_product
Applying strategy: assign_single_product_as_activity
Applying strategy: create_composite_code
Applying strategy: drop_unspecified_subcategories
Applying strategy: fix_ecoinvent_flows_pre35
C:\Users\Joshua Schroijen\.conda\envs\bw2\lib\site-packages\bw2io\strategies\ecospold2.py:246: UserWarning: Skipping migration 'fix-ecoinvent-flows-pre-35' because it isn't installed
  warnings.warn(
Applying strategy: drop_temporary_outdated_biosphere_flows
Applying strategy: link_biosphere_by_flow_uuid
Applying strategy: link_internal_technosphere_by_composite_code
Applying strategy: delete_exchanges_missing_activity
Applying strategy: delete_ghost_exchanges
Applying strategy: remove_uncertainty_from_negative_loss_exchanges
Applying strategy: fix_unreasonably_high_lognormal_uncertainties
Applying strategy: set_lognormal_loc_value
Applying strategy: convert_activity_parameters_to_list
Applying strategy: add_cpc_classification_from_single_reference_product
Applying strategy: delete_none_synonyms
Applied 21 strategies in 0.00 seconds
Traceback (most recent call last):
  File "C:\Users\Joshua Schroijen\leaf-backend\business_logic\ecoinvent.py", line 9, in <module>
    db = importer.write_database()
  File "C:\Users\Joshua Schroijen\.conda\envs\bw2\lib\site-packages\bw2io\importers\base_lci.py", line 237, in write_database
    raise WrongDatabase(error)
bw2io.errors.WrongDatabase: Activity database must be ecoinvent39, but set() was also found
BenPortner commented 1 year ago

Hi @Joshua-Schroijen,

The first line of the full output states Extracting XML data from 0 datasets. This indicates that the path to the ecospold files is incorrect. Can you check?

Related to https://github.com/brightway-lca/brightway2-io/issues/97

Joshua-Schroijen commented 1 year ago

Hi Ben Portner,

Thanks for the comment. I have since realized that I had to point the SingleOutputEcospold2Importer to the "datasets" subfolder, or that

pte = Path("C:\\") / "Users" / "Joshua Schroijen" / "ecoinvent" / "ecoinvent 3.9_cutoff_ecoSpold02" / "datasets"

instead of

pte = Path("C:\\") / "Users" / "Joshua Schroijen" / "ecoinvent" / "ecoinvent 3.9_cutoff_ecoSpold02"

fixes the problem.

BenPortner commented 1 year ago

Hi @Joshua-Schroijen

I'm glad that you could fix the problem. I will close this issue then. Sorry for the inconvenience, btw. I personally believe that brightway should give a clear error message in such cases. I will put this high on the agenda!