This notebook shows how to use the Activity
and Exchange
classes.
from brightway2 import *
Let's create a new project just for this notebook
projects.set_current("cats and dogs")
Let's start with a new database:
db = Database("a&e")
and insert some basic data:
db.write({
("a&e", "cat"): {
'name': 'cat',
'unit': 'kilogram',
'color': 'black', # Custom field - you can add whatever fields you need
'exchanges': [{
'input': ('a&e', 'cat food'),
'amount': 10,
'type': 'technosphere'
}, {
'input': ('a&e', 'kitty litter'),
'amount': 10,
'type': 'technosphere'
}, {
'input': ('a&e', 'smell'),
'amount': 1,
'type': 'biosphere'
}]
},
("a&e", "kitty litter"): {'name': 'yuck'},
("a&e", "cat food"): {'name': 'yum'},
("a&e", "smell"): {'name': 'stinky', 'type': 'biosphere'},
})
We can get an activity with .get()
:
act = db.get("cat")
act
The cat
process has no categories or location so far. Let's change that:
act['location'] = 'inside'
act['categories'] = ['felis', 'catus']
act
When we are ready, we can save our changes:
act.save()
We can iterate over the available data fields:
for key in act:
print(key, ':', act[key])
Note that the fields database
and code
are added automatically for us.
There are only a few methods for an activity. We can see the activity key (combination of database and code):
act.key
We can iterate over all exchanges:
for exc in act.exchanges():
print(exc)
Or just the technosphere/biosphere/production exchanges:
print("technosphere:")
for exc in act.technosphere():
print(exc)
print("biosphere:")
for exc in act.biosphere():
print(exc)
print("production:")
for exc in act.production():
print(exc)
You can also count exchanges (or any of the specific types of exchanges):
len(act.exchanges())
There weren't any production exchanges. Brightway2 will add a default production exchanges with amount of 1 when building the technosphere matrix if no production exchange is given.
We can also look at upstream exchanges - those that consume this activity's reference product. We don't have any yet, so let's create a new activity:
na = db.new_activity("dog")
na.save()
Oops, activities must have names. Let's fix that:
na['name'] = 'fido'
na.save()
We will also get a nice error message for invalid new exchanges:
na.new_exchange().save()
Let's add a link to our cat process. Don't worry, the dog won't really eat the cat!
new_exc = na.new_exchange(input=act, amount=1, type='technosphere')
new_exc.save()
We can now see that 'fido' links to 'cat':
for exc in na.technosphere():
print(exc)
for exc in act.upstream():
print(exc)
exc.input
and exc.output
will return activities:
new_exc.input, new_exc.output
Exchanges have a few more methods:
new_exc.unit, new_exc.amount, new_exc.uncertainty_type
Let's add some uncertainty to this exchange (see http://stats-arrays.readthedocs.org/en/latest/):
from stats_arrays import NormalUncertainty
new_exc['uncertainty type'] = NormalUncertainty.id
new_exc['loc'], new_exc['scale'] = 1, 0.25
new_exc.save()
We can now get an uncertainty dictionary for use in stats_arrays
functions:
new_exc.uncertainty
And can get a random sample:
new_exc.random_sample(n=10)
We can copy activities. This will copy the activities exchanges:
kudu = act.copy(name="kudu", code='antelope')
kudu
for exc in kudu.exchanges():
print(exc)
Upstream exchanges are (of course) not copied:
for exc in kudu.upstream():
print(exc)
You can delete the some or all of an activities exchanges:
print("Before:",len(kudu.exchanges()))
kudu.biosphere().delete()
print("After:", len(kudu.exchanges()))
You can also delete activities:
kudu.delete()
Our new activity is no longer in the database:
kudu in db