This notebook shows you have to work with Database classes.
from brightway2 import *
Create a project for this notebook:
projects.current = "databases demo"
Before going any further, please read about what a database is in Brightway2.
One of the nicest ideas in Brightway2 is that data is just data, and not hidden behind a database and object-relational mapping (ORM). This makes it very easy to do data manipulation, and to add new fields to datasets.
Let's define an example database:
db = Database("example")
example_data = {
("example", "A"): {
"name": "A",
"exchanges": [{
"amount": 1.0,
"input": ("example", "B"),
"type": "technosphere"
}],
'unit': 'kilogram',
'location': 'here',
'categories': ("very", "interesting")
},
("example", "B"): {
"name": "B",
"exchanges": [],
'unit': 'microgram',
'location': 'there',
'categories': ('quite', 'boring')
}
}
This is quite a simple example - two activities, one of whom has no inputs. In fact, this example dataset has only a few fields - things like unit and categories could be also included, but are not required. In fact, there are no required fields in datasets, only some suggested ones, and general guidelines on how to use those suggested fields. It's like not wearing underwear - Brightway2 gives you the freedom to do it, but most people you are interacting with would prefer that you didn't.
Let's talk a bit about the fields in example_data:
name: This one is pretty easy :)exchanges: This is a list of inputs and outputs, like how much energy or material is needed for this dataset's production. Every exchange is an intput, unless it has the value type = "production". A production exchange defines how much of the output is produced. Most of the time, you can ignore this, as the default value is one - this is what we do in the example data. However, sometimes it is useful to define a dataset that produces more.See also: What happens with a non-unitary production amount in LCA?.
We need to write our example data to the database.
db.write(example_data)
If you just need an example dataset activity, use the .random() method:
db.random()
We can also loop over our database, and try to get some useful information. For example, say we were interested in the number of exchanges in each activity:
num_exchanges = [(activity, len(activity.exchanges())) for activity in db]
num_exchanges
db.search("*")
You delete databases by deleting them from the databases object:
del databases[db.name]