Search code examples
brightway

index error on presamples of characterisation factors


I'm trying to create some presamples of CF to use in Brightway, but I am getting an index error, and I am not sure if it is because I am doing something wrong or I have to report it as a bug. This is the easiest example I could came up with.

import brightway2 as bw
import presamples as ps
import numpy as np
import random

cf_possible_values_invented={
 ('biosphere3', '9d9246d4-0ce3-4b47-8491-cf62e562ad38'): [6.79,0.00371],
 ('biosphere3', '8bbe0b4c-5c9c-4959-a878-572f680e428d'): [7.86,0.00371],
 ('biosphere3', '8d8e393e-39c4-44e1-b424-8eeefcd33560'): [2.07,0.0186],
 ('biosphere3', '9be02ae0-3170-4544-8665-f03a1b43f2bd'): [2.14e-05,3.29e-06],
 ('biosphere3', '777160e1-99a9-4a03-b189-8c1af57ee560'): [4.71,4.8],
 ('biosphere3', '1869fd74-ed68-4814-9596-12315b1e810d'): [0.114,0.00107],
 ('biosphere3', '4d466648-95dd-4866-97ad-6008746d32ab'): [0.107,0.00114],
 ('biosphere3', '71d27d29-04f2-421a-981f-5c5c2b00486f'): [0.107,0.00107]}

nsamples = 5000

samples = np.array([random.choices(values,k=nsamples) for key,values in cf_possible_values.items()])
indices = [k for k in cf_possible_values.keys()]

for i in indices:
    assert i in bw.Database('biosphere3')

ionising_rad_cf = {key:CF for key,CF in bw.Method(('ILCD 2.0 2018 midpoint', 'human health', 'ionising radiation')).load()}

# verify that the method has values for those keys
for i in indices:
    assert i in ionising_rad_cf

scenario_matrix_data=[(samples,indices,'cf')]

cfsamples_id_discrete, cfsamples_fp_discrete = ps.create_presamples_package(matrix_data=scenario_matrix_data,
                                                                            name='faulty example')

ecoinvent_act = bw.Database('ei_36con').random()
ecoinvent_act

ionrad_lca = bw.LCA({ecoinvent_act:1},('ILCD 2.0 2018 midpoint', 'human health', 'ionising radiation'),presamples=[cfsamples_fp_discrete])

ionrad_lca.lci()

ionrad_lca.lcia()

If I try to execute this I get an IndexError: index (4294967295) out of range. I've noticed I get the same index error with other methods. If I read the values stored in the package such high index value appears not to be there,

enter image description here

I don't really understand what is going on. I am using bw2calc version 1.8 and presamples version 0.2.6


Solution

  • The problem is that the biosphere exchange that is part of your presamples package is not in your biosphere matrix (and so, of your CF matrix). You can see this by doing something like:

    ionrad_lca = bw.LCA(
        {ecoinvent_act:1},
        ('ILCD 2.0 2018 midpoint', 'human health', 'ionising radiation'),
    )
    ionrad_lca.lci() 
    ionrad_lca.lcia() 
    for ef in indices:
       if ef not in ionrad_lca.biosphere_dict:
           print("Not in biosphere matrix, nor in cf matrices: ", ef)
    

    Since they are not in your cf matrix, presamples doesn't know how to find the appropriate row index (and so leaves it at 4294967295, a very telling number used in brightway2 as a placeholder before indices can be found during matrix building).

    Presamples would ideally give a better error message.

    There should also be a method for filtering out indices that don't exist in the matrices.

    I'm sure a PR solving either of these would be appreciated.

    For now, I would suggest changing your code to only include cfs for elementary flows that are used in the LCI database that you are interested in (in your case, ecoinvent 3.6).