2

I'm getting into Brightway2 for some energy system modeling and I'm still getting used to the all of the concepts.

I've created a small custom demo database, and run lca.lci() and lca.lcia(). lca.inventory and lca.characterized_inventory both return sparse matrices of the results. My question, which may be very simple, is how can you connect the values in the matrix to the exchange names and keys. I.e., if I wanted to print the results to a file, how would I match the exchanges to the inventory values?

Thanks.

Jim
  • 105
  • 1
  • 7

1 Answers1

3

To really understand what is going on, it is useful to understand the difference between "intermediate" data (stored as structured text files) and "processed" data (stored as numpy structured arrays). These concepts are described both here and here.

However, to answer your question directly: what each row and column stand for in the different matrices and arrays (e.g. lca.inventory matrix, lca.supply_array, lca.characterized_inventory) are contained in a set of dictionaries that are associated with your LCA object. These are:

  • activity_dict: Columns in the technosphere matrix
  • product_dict : Rows in the technosphere matrix
  • biosphere_dict: Rows in the biosphere matrix

For example, lca.product_dict yields, in the case of an LCA I just did:

{('ei32_CU_U', '671c1ae85db847083176b9492f000a9d'): 8397,
 ('ei32_CU_U', '53398faeaf96420408204e309262b8c5'): 536,
 ('ei32_CU_U', 'fb8599da19dabad6929af8c3a3c3bad6'): 7774,
 ('ei32_CU_U', '28b3475e12e4ed0ec511cbef4dc97412'): 3051, ...}  

with the key in the dictionary being the actual product in my inventory database and the value is the row in the demand_array or the supply_array.

More useful may be the reverse of these dictionaries. Let's say you want to know what a value in e.g. your supply_array refers to, you can create a reverse dictionary using a dict comprehension :

inv_product_dict = {v: k for k, v in lca.product_dict.items()}  

and then simply use it directly to obtain the information you are after. Say you want to know what is in the 10th row of the supply_array, you can simply do inv_product_dict[10], which in my case yields ('ei32_CU_U', '4110733917e1fcdc7c55af3b3f068c72')

The same types of logic applies with biosphere (or elementary) flows, found in the lca.biosphere_dict (in LCA parlance, rows in the B matrix), and activities, found in the lca.activity_dict (columns of the A or B matrices).

Note that you can generate the reverse of the activity_dict/product_dict/biosphere_dict simultaneously using lca.reverse_dict(). The syntax then is:
rev_act_dict, rev_product_dict, rev_bio_dict = lca.reverse_dict()

MPa
  • 1,086
  • 1
  • 10
  • 25
  • Thanks. That helped a lot. I'm getting more in-depth into Python as I'm starting with Brightway2, and lot of the conventions and concepts aren't intuitive yet. – Jim Sep 16 '16 at 19:51
  • 1
    Just in case things like `('ei32_CU_U', '28b3475e12e4ed0ec511cbef4dc97412')` are mysterious, you can use the function `get_activity`, e.g.: `get_activity(('ecoinvent 3.2 cutoff', '3c76fc6ab34c7842c6be5fd74cfd1c18'))` >>> `'market for methyl-3-methoxypropionate' (kilogram, GLO, None)` – Chris Mutel Sep 19 '16 at 08:01