Search code examples
pythonbrightway

use an iterface to replace characterization factors of an lca object with Brightway


I am trying to change the characterization factors specifying new values using a datapackage, but I am facing some unexpected problems. For example if I do

import bw2data as bd
import bw_processing as bwp
import numpy as np
import bw2calc as bc

mobility_db = bd.Database('Mobility example')
combustion_car_driving = mobility_db.get(name='Driving an combustion car')
co2 = mobility_db.get(name='CO2')

# create datapackage with alternative values for the GWP of CO2
indices_array = np.array([(co2.id,co2.id)],dtype=bwp.INDICES_DTYPE)
values_array = np.array([[10,12]])
dp_c = bwp.create_datapackage()
dp_c.add_dynamic_array(
    matrix='characterization_matrix',
    indices_array=indices_array,
    interface=values_array,
)

fu, data_objs , _ = bd.prepare_lca_inputs({combustion_car_driving:1},
                                          method=('IPCC','simple'))
lca = bc.LCA(demand=fu,
       method=('IPCC','simple'),
       data_objs=data_objs+[dp_c],
       use_arrays=True)
lca.lci()
lca.lcia()

I get an InconsistentGlobalIndex error: Multiple global index values found: [1, None]. If multiple LCIA datapackages are present, they must use the same value for GLO, the global location, in order for filtering for site-generic LCIA to work correctly.

If I filter the resources in the dp_c

dp_c.filter_by_attribute('matrix','characterization_matrix').filter_by_attribute("kind", "indices").resources

it does not have a field of global_index, but the data_objs do.

I've tried adding manually to the resource a global_index of:

bd.method.geomapping[bd.method.config.global_location]

and the calculation can continue (although I get always the same results after next(lca), which is probably a different problem.

Am I doing something wrong ? I am used to change values in A or B matrices, but not C


Solution

  • ok, following @cmutel I tried with this and it works, although I need to add the global_index manually to avoid the InconsistentGlobalIndex error

    
    import bw2data as bd
    import bw_processing as bwp
    import numpy as np
    import bw2calc as bc
    
    mobility_db = bd.Database('Mobility example')
    combustion_car_driving = mobility_db.get(name='Driving an combustion car')
    co2 = mobility_db.get(name='CO2')
    
    # create datapackage with alternative values for the GWP of CO2
    indices_array = np.array([(co2.id,
                               bd.geomapping[bd.config.global_location])],
                               dtype=bwp.INDICES_DTYPE)
    
    values_array = np.array([list(range(1,10))])
    dp_c = bwp.create_datapackage(sequential=True)
    dp_c.add_dynamic_array(
        matrix='characterization_matrix',
        indices_array=indices_array,
        interface=values_array,
    )
    
    ## hack
    r = dp_c.filter_by_attribute('matrix',
        'characterization_matrix').filter_by_attribute("kind","indices").resources[0]
    r["global_index"] = bd.geomapping['GLO']
    
    ##
    
    fu, data_objs , _ = bd.prepare_lca_inputs({combustion_car_driving:1},
                                              method=('IPCC','simple'))
    lca = bc.LCA(demand=fu,
           method=('IPCC','simple'),
           data_objs=data_objs+[dp_c],
           use_arrays=True)
    lca.lci()
    lca.lcia()
    
    for i in range(10):
        next(lca)
        print(lca.score)