This notebook shows how to create parameterized inventories with constants, variables, and formulas.
Note that Brightway2 allows you to write real models in Python, and this is preferable to writing a model in formulas inside an inventory. However, for simple cases parameterized inventories can add real power without too much work.
We start by creating a new project and adding the basic data.
from brightway2 import *
projects.current = "parameterized"
bw2setup()
This will hold our parameterized inventories.
db = Database("Woweee!")
db.write({})
Each activity has its own namespace for variable and formula names.
Lets also define some simple parameters.
act = db.new_activity("first", name="first parameterized inventory", unit="kilogram")
act['parameters'] = {
'speed': {'amount': 40, 'unit': 'km/h'},
'time': {'amount': 2.5, 'unit': 'hours', 'comment': 'A long time, but no so long'},
'distance': {'formula': 'speed * time', 'arbitrary fields': 'can be added as needed'}
}
act.save()
Evaluate our parameter set using the bw2parameters library.
from bw2parameters import ParameterSet
ps = ParameterSet(act['parameters'])
ps.evaluate()
We don't have any other technosphere processes, but we can paramerize links to biosphere flows.
exc = act.new_exchange(input=Database("biosphere3").random(), type='biosphere')
exc.input
We have a bunch of built in functions from the Python math library and numpy. Note that we don't define the amount
field - this will be added later when the formula is evaluated.
exc['formula'] = 'sqrt(distance) * pi'
Let's do another one:
exc2 = act.new_exchange(input=Database("biosphere3").random(), type='biosphere')
exc2['formula'] = 'log(speed) ** 2'
The syntax to evaluate the parameterized inventories is maybe not the most intuitive, and could change in the future. We use our ParameterSet
as a function, and call it with a list of our exchanges:
ps([exc, exc2])
Our exchanges have been evaluated, and the amount
field has been added. This is the basic form of a parameterized inventory.
We can also have database-level parameters which can be reference by any activity, even one without it's own local parameters.
Currently we have no global parameters:
db.parameters
Let's add a constant, a variable, and a formula:
from stats_arrays import *
db.parameters['constant'] = {'amount': 42}
db.parameters['variable'] = {
'amount': 1,
'loc': 1, # Uncertainty parameters
'minimum': 0,
'maximum': 3,
'uncertainty type': TriangularUncertainty.id
}
db.parameters['formula'] = {'formula': 'constant + variable'}
act2 = db.new_activity("second", name="second parameterized inventory", unit="kilogram")
exc3 = act2.new_exchange(input=Database("biosphere3").random(), type='biosphere')
exc3['formula'] = 'formula + 7'
When evaluating with global parameters, we still have to pass our local parameter set. Because we don't have any, we can pass an empty dictionary.
Note that we have to evaluate the global parameters.
ps = ParameterSet({}, global_params=db.parameters.evaluate())
ps([exc3])
This is the current state of parameterized inventories in Brightway2. One important caveat is that parameterized inventories dont work yet with Monte Carlo sampling - it is not simple to do this in an efficient way, and the methodology is still being thought about.
For anything complicated, it is (again) recommended to write a separate model in Python and use that model to dynamically generate life cycle inventories.