Calculation setups are a way to efficiently do LCA calculations for multiple functional units and methods at the same time.
This notebook builds on the Getting started with Brightway2
notebook, and won't work if you haven't done that notebook yet.
from brightway2 import *
if "Calculation setups" not in projects:
projects.set_current("BW2 introduction")
projects.copy_project("Calculation setups")
else:
projects.set_current("Calculation setups")
A calculation setups is defined by three things:
In this example, we will choose both activities and methods at random.
functional_units = [{Database('forwast').random(): 1} for _ in range(20)]
We can't choose methods completely at random, though, as the forwast
database doesn't have as many biosphere flows as ecoinvent, so many methods will only characterize flows that aren't provided anywhere in forwast
. So lets only choose from the methods which will have a non-zero LCA score:
import random
all_forwast_flows = {exc.input for ds in Database("forwast") for exc in ds.biosphere()}
suitable_methods = [method
for method in methods
if {cf[0] for cf in Method(method).load()}.intersection(all_forwast_flows)]
print("Can use {} of {} LCIA methods".format(len(suitable_methods), len(methods)))
chosen_methods = random.sample(suitable_methods, 8)
A calculation setup is a normal Python dictionary, with keys inv
and ia
, for the functional units and LCIA methods, respectively.
my_calculation_setup = {'inv': functional_units, 'ia': chosen_methods}
You define a calculation setup by name in the metadata store calculation_setups
, similar to the way that LCIA methods are defined.
calculation_setups['some random stuff'] = my_calculation_setup
We can examine what we have just randomly created:
calculation_setups['some random stuff']
The normal create, update, and delete machanisms apply:
calculation_setups['some random stuff'] = some_new_stuff
.del
, i.e. del calculation_setups['some random stuff']
.Use the MultiLCA
class to get LCA results for a calculation setup. Note that this class does all the calculations as soon as you create it.
mlca = MultiLCA('some random stuff')
mlca.results
%matplotlib inline
You can ignore any errors that show up here.
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
# From https://stanford.edu/~mwaskom/software/seaborn/tutorial/color_palettes.html
cmap = sns.cubehelix_palette(8, start=.5, rot=-.75, as_cmap=True)
sns.heatmap(
mlca.results / np.average(mlca.results, axis=0), # Normalize to get relative results
annot=True,
linewidths=.05,
cmap=cmap,
xticklabels=["\n".join(x) for x in mlca.methods]
)
plt.xticks(rotation=70)
Could also adapt https://stanford.edu/~mwaskom/software/seaborn/examples/structured_heatmap.html, but this is more work.