Update a parameter value in Brightway

281 Views Asked by At

It seems to be a simple question but I have a hard time to find an answer to it. I already have a project with several parameters (project and database parameters). I would like to obtain the LCA results for several scenarios with my parameters having different values each time. I was thinking of the following simple procedure:

  1. change the parameters' value,
  2. update the exchanges in my project,
  3. calculate the LCA results.

I know that the answer should be in the documentation somewhere, but I have a hard time to understand how I should apply it to my ProjectParameters, DatabaseParameters and ActivityParameters.

Thanks in advance!

EDIT: Thanks to @Nabla, I was able to come up with this:

For ProjectParameter

for pjparam in ProjectParameter.select():
    if pjparam.name=='my_param_name':
        break
pjparam.amount = 3
pjparam.save()
bw.parameters.recalculate()

For DatabaseParameter

for dbparam in DatabaseParameter.select():
    if dbparam.name=='my_param_name':
        break
dbparam.amount = 3
dbparam.save()
bw.parameters.recalculate()

For ActivityParameter

for param in ActivityParameter.select():
    if param.name=='my_param_name':
        break    
param.amount = 3
param.save()
param.recalculate_exchanges(param.group)
1

There are 1 best solutions below

3
On BEST ANSWER

You could import DatabaseParameter and ActivityParameter iterate until you find the parameter you want to change, update the value, save it and recalculate the exchanges. I think you need to do it in tiers. First you update the project parameters (if any) then the database parameters that may depend on project parameters and then the activity parameters that depend on them.

A simplified case without project parameters:

from bw2data.parameters import ActivityParameter,DatabaseParameter

# find the database parameter to be updated
for dbparam in DatabaseParameter.select():
    if (dbparam.database == uncertain_db.name) and (dbparam.name=='foo'):
        break

dbparam.amount = 3
dbparam.save()

#there is also this method if foruma depend on something else 
#dbparam.recalculate(uncertain_db.name)

# here updating the exchanges of a particular activity (act)

for param in ActivityParameter.select():
    if param.group == ":".join(act.key):
        param.recalculate_exchanges(param.group)

you may want to update all the activities in the project instead of a single one like in the example. you just need to change the condition when looping through the activity parameters.