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

Biosphere flows: Can't properly relate to some of the flows #29

Closed froborg closed 2 years ago

froborg commented 2 years ago

Hi all

I try to include some biosphere flows into my LCA but have some issues with Activities whose name is only one word. Not sure if that is only a coincidence or correlation though... Here what I am doing (using Jupyter & Python 3.8):

db = bw2.Database('biosphere3')
[act for act in db if 'Ammonia' in act['name']  and 'non-urban air' in str(act['categories'])]
KeyError                                  Traceback (most recent call last)
/var/folders/tf/hzv31v4x42q4_mnw4n8ldhsm0000gn/T/ipykernel_13952/3710977775.py in <module>
----> 1 [act for act in db if 'Ammonia' in act['name'] 
      2                                             and 'non-urban air' in str(act['categories'])]

/var/folders/tf/hzv31v4x42q4_mnw4n8ldhsm0000gn/T/ipykernel_13952/3710977775.py in <listcomp>(.0)
      1 [act for act in db if 'Ammonia' in act['name'] 
----> 2                                             and 'non-urban air' in str(act['categories'])]

~/opt/miniconda3/lib/python3.8/site-packages/bw2data/proxies.py in __getitem__(self, key)
     89         elif key == 1:
     90             return self['code']
---> 91         return self._data[key]
     92 
     93     def __delitem__(self, key):

KeyError: 'categories'

I have established, that the flow exists: bw2.Database('biosphere3').search('Ammonia', filter={'categories':'air'})

Excluding 1 filtered results
['Ammonia' (kilogram, None, ('air', 'urban air close to ground')),
 'Ammonia' (kilogram, None, ('air', 'lower stratosphere + upper troposphere')),
 'Ammonia' (kilogram, None, ('air',)),
 'Ammonia' (kilogram, None, ('air', 'non-urban air or from high stacks')),
 'Ammonia' (kilogram, None, ('air', 'low population density, long-term'))]

And the above code works with e.g. Carbon dioxide: [act for act in db if 'Carbon dioxide' in act['name'] and 'non-urban air' in str(act['categories'])]

['Carbon dioxide, from soil or biomass stock' (kilogram, None, ('air', 'non-urban air or from high stacks')),
 'Carbon dioxide, non-fossil' (kilogram, None, ('air', 'non-urban air or from high stacks')),
 'Carbon dioxide, fossil' (kilogram, None, ('air', 'non-urban air or from high stacks'))]

The search worked nicely also for dinitrogen monoxide and nitrogen oxides but resulted in the same error for Phosphorus and Phosphate. Am I missing something?

froborg commented 2 years ago

The following answer received, thanks Chris!

The specific issue you are having is due to the presence of a flow in your 'biosphere3' database which doesn't have the 'categories' attribute. You can fix this by relaxing this assumption:

[act for act in db if 'Ammonia' in act['name'] and 'non-urban air' in str(act.get('categories'))]

You could also find the offending flow(s) with:

[flow for flow in db if not flow.get('categories')]