Parameters - importing the Excel example

This notebooks illustrates how to create parameterized datasets from an Excel import in Brightway2. You should be familiar with the input data types listed in the technical documentation.

In [1]:
from brightway2 import *
In [2]:
projects.set_current("parameters - Excel import")

The Excel importer assumes that certain base data is present

In [3]:
bw2setup()
Creating default biosphere

Writing activities to SQLite3 database:
0%                          100%
[#######                       ] | ETA: 00:00:00
Applying strategy: normalize_units
Applying strategy: drop_unspecified_subcategories
Applied 2 strategies in 0.01 seconds
[##############################] | ETA: 00:00:00
Total time elapsed: 00:00:00
Title: Writing activities to SQLite3 database:
  Started: 10/23/2017 22:00:46
  Finished: 10/23/2017 22:00:47
  Total time elapsed: 00:00:00
  CPU %: 101.30
  Memory %: 0.81
Created database: biosphere3
Creating default LCIA methods

Applying strategy: normalize_units
Applying strategy: set_biosphere_type
Applying strategy: drop_unspecified_subcategories
Applying strategy: link_iterable_by_fields
Applied 4 strategies in 2.02 seconds
Wrote 718 LCIA methods with 178008 characterization factors
Creating core data migrations

Next, download the parameterized template and adjust the path below:

In [4]:
fp = "/Users/cmutel/Downloads/sample_activities_with_variables.xlsx"
In [6]:
ei = ExcelImporter(fp)
ei.apply_strategies()
ei.match_database(fields=['name'])
ei.statistics()
Extracted 1 worksheets in 0.01 seconds
Applying strategy: csv_restore_tuples
Applying strategy: csv_restore_booleans
Applying strategy: csv_numerize
Applying strategy: csv_drop_unknown
Applying strategy: csv_add_missing_exchanges_section
Applying strategy: normalize_units
Applying strategy: normalize_biosphere_categories
Applying strategy: normalize_biosphere_names
Applying strategy: strip_biosphere_exc_locations
Applying strategy: set_code_by_activity_hash
Applying strategy: link_iterable_by_fields
Applying strategy: assign_only_product_as_production
Applying strategy: link_technosphere_by_activity_hash
Applying strategy: drop_falsey_uncertainty_fields_but_keep_zeros
Applying strategy: convert_uncertainty_types_to_integers
Applied 15 strategies in 0.30 seconds
Applying strategy: link_iterable_by_fields
2 datasets
2 exchanges
0 unlinked exchanges
  
Out[6]:
(2, 2, 0)

We now have a slightly tricky bit to remember - this import is for a single database, but can also have project-level variables. This means that we first have to add the project parameters explicitly.

In [8]:
ei.write_project_parameters()

We then write the database, and choose to activate the parameters we have imported. If we didn't activate them, they would be imported as Database(name)['parameters'] and `Activity()['parameters'].

In [9]:
ei.write_database(activate_parameters=True)
Writing activities to SQLite3 database:
0%  100%
[##] | ETA: 00:00:00
Total time elapsed: 00:00:00
Title: Writing activities to SQLite3 database:
  Started: 10/23/2017 22:13:20
  Finished: 10/23/2017 22:13:20
  Total time elapsed: 00:00:00
  CPU %: 113.80
  Memory %: 1.15
Created database: PCB
Out[9]:
Brightway2 SQLiteBackend: PCB

We check to make sure that we have imported the parameters:

In [10]:
len(parameters)
Out[10]:
5

We can also examine the parameter objects directly:

In [15]:
from bw2data.parameters import *

for p in ProjectParameter.select():
    print(p.amount, p)
    
for p in DatabaseParameter.select():
    print(p.amount, p)
    
for p in ActivityParameter.select():
    print(p.amount, p)
0.25 Project parameter: PCB_area
0.2 Database parameter: PCB:PCB_cap_mass_film
0.2 Database parameter: PCB:PCB_cap_mass_SMD
0.2 Database parameter: PCB:PCB_cap_mass_Tantalum
0.6000000000000001 Activity parameter: PCB (activities):PCB_mass_total