bw2io
#
Subpackages#
bw2io.export
bw2io.extractors
bw2io.importers
bw2io.importers.base
bw2io.importers.base_lci
bw2io.importers.base_lcia
bw2io.importers.ecoinvent_lcia
bw2io.importers.ecospold1
bw2io.importers.ecospold1_lcia
bw2io.importers.ecospold2
bw2io.importers.ecospold2_biosphere
bw2io.importers.excel
bw2io.importers.excel_lcia
bw2io.importers.exiobase2
bw2io.importers.exiobase3_hiot
bw2io.importers.simapro_csv
bw2io.importers.simapro_lcia_csv
bw2io.strategies
Submodules#
Package Contents#
Classes#
This is a format for saving objects which implement the datastore API. Data is stored as a BZip2-compressed file of JSON data. This archive format is compatible across Python versions, and is, at least in theory, programming-language agnostic. |
|
Generic CSV importer |
|
Generic CSV LCIA importer |
|
Export a Gephi graph for a selection of activities from a database. |
|
Export a Gephi graph for a database. |
|
Generic Excel importer. |
|
Generic Excel LCIA importer. |
|
Import and process mutli-output datasets in the ecospold 1 format. |
|
Import and process single-output datasets in the ecospold 1 format. |
|
Base class for format-specific importers. |
|
Functions#
Backup data directory to a |
|
|
Backup project data directory to a |
|
|
Add pre-defined core migrations data files |
|
|
|
|
|
|
Generate unique ID for ecoinvent3 dataset. |
|
Fake docstring |
|
|
|
|
Restore backup created using |
Attributes#
- class bw2io.BW2Package[source]#
Bases:
object
This is a format for saving objects which implement the datastore API. Data is stored as a BZip2-compressed file of JSON data. This archive format is compatible across Python versions, and is, at least in theory, programming-language agnostic.
Validation is done with
bw2data.validate.bw2package_validator
.The data format is:
{ 'metadata': {}, # Dictionary of metadata to be written to metadata-store. 'name': basestring, # Name of object 'class': { # Data on the underlying class. A new class is instantiated # based on these strings. See _create_class. 'module': basestring, # e.g. "bw2data.database" 'name': basestring # e.g. "Database" }, 'unrolled_dict': bool, # Flag indicating if dictionary keys needed to # be modified for JSON (as JSON keys can't be tuples) 'data': object # Object data, e.g. LCIA method or LCI database }
- Perfect roundtrips between machines are not guaranteed:
All lists are converted to tuples (because JSON does not distinguish between lists and tuples).
Absolute filepaths in metadata would be specific to a certain computer and user.
Note
This class does not need to be instantiated, as all its methods are
classmethods
, i.e. doBW2Package.import_obj("foo")
instead ofBW2Package().import_obj("foo")
- APPROVED#
- classmethod _create_class(metadata, apply_whitelist=True)#
- classmethod _create_obj(data)#
- classmethod _get_class_metadata(obj)#
- classmethod _is_valid_package(data)#
- classmethod _is_whitelisted(metadata)#
- classmethod _load_obj(data, whitelist=True)#
- classmethod _prepare_obj(obj, backwards_compatible=False)#
- classmethod _write_file(filepath, data)#
- classmethod export_obj(obj, filename=None, folder='export', backwards_compatible=False)#
Export an object.
- Parameters
obj (*) – Object to export.
filename (*) – Name of file to create. Default is
obj.name
.folder (*) – Folder to create file in. Default is
export
.backwards_compatible (*) – Create package compatible with bw2data version 1.
- Returns
Filepath of created file.
- classmethod export_objs(objs, filename, folder='export', backwards_compatible=False)#
Export a list of objects. Can have heterogeneous types.
- Parameters
objs (*) – List of objects to export.
filename (*) – Name of file to create.
folder (*) – Folder to create file in. Default is
export
.backwards_compatible (*) – Create package compatible with bw2data version 1.
- Returns
Filepath of created file.
- classmethod import_file(filepath, whitelist=True)#
Import bw2package file, and create the loaded objects, including registering, writing, and processing the created objects.
- Parameters
filepath (*) – Path of file to import
whitelist (*) – Apply whitelist to allowed types. Default is
True
.
- Returns
Created object or list of created objects.
- classmethod load_file(filepath, whitelist=True)#
Load a bw2package file with one or more objects. Does not create new objects.
- Parameters
filepath (*) – Path of file to import
whitelist (*) – Apply whitelist of approved classes to allowed types. Default is
True
.
- Returns the loaded data in the bw2package dict data format, with the following changes:
"class"
is an actual Python class object (but not instantiated).
- class bw2io.CSVImporter(filepath)#
Bases:
ExcelImporter
Generic CSV importer
- extractor#
- format = 'CSV'#
- class bw2io.CSVLCIAImporter(filepath, name, description, unit, **metadata)#
Bases:
ExcelLCIAImporter
Generic CSV LCIA importer
- extractor#
- format = 'CSV'#
- class bw2io.DatabaseSelectionToGEXF(database, keys)#
Bases:
DatabaseToGEXF
Export a Gephi graph for a selection of activities from a database.
Also includes all inputs for the filtered activities.
- Parameters
database (*) – Database name.
keys (*) – The activity keys to export.
- class bw2io.DatabaseToGEXF(database, include_descendants=False)#
Bases:
object
Export a Gephi graph for a database.
Call
.export()
to export the file after class instantiation.- Parameters
database (*) – Database name.
include_descendants (*) – Include databases which are linked from
database
.
Warning
include_descendants
is not yet implemented.- export()#
Export the Gephi XML file. Returns the filepath of the created file.
- get_data(E)#
Get Gephi nodes and edges.
- class bw2io.Ecospold1LCIAImporter(filepath, biosphere=None)#
Bases:
bw2io.importers.base_lcia.LCIAImporter
- format = 'Ecospold1 LCIA'#
- class bw2io.ExcelImporter(filepath)#
Bases:
bw2io.importers.base_lci.LCIImporter
Generic Excel importer.
See the generic Excel example spreadsheet.
Excel spreadsheet should follow the following format:
- ::
Project parameters <variable>, <formula>, <amount>, metadata
Database, <name of database> <database field name>, <database field value>
Parameters <variable>, <formula>, <amount>, metadata
Activity, <name of activity> <database field name>, <database field value> Exchanges <field name>, <field name>, <field name> <value>, <value>, <value> <value>, <value>, <value>
Neither project parameters, parameters, nor exchanges for each activity are required.
An activity is marked as finished with a blank line.
In general, data is imported without modification. However, the following transformations are applied:
Numbers are translated from text into actual numbers.
Tuples, separated in the cell by the
::
string, are reconstructed.True
andFalse
are transformed to boolean values.Fields with the value
(Unknown)
are dropped.
- extractor#
- format = 'Excel'#
- get_activity(sn, ws)#
- get_database(data)#
- get_database_parameters(data)#
- get_labelled_section(sn, ws, index=0, transform=True)#
Turn a list of rows into a list of dictionaries.
The first line of
ws
is the column labels. All subsequent rows are the data values. Missing columns are dropped.transform
is a boolean: perform CSV transformation functions likecsv_restore_tuples
.
- get_metadata_section(sn, ws, index=0, transform=True)#
- get_project_parameters(data)#
Extract project parameters (variables and formulas).
Project parameters are a section that starts with a line with the string “project parameters” (case-insensitive) in the first cell, and ends with a blank line. There can be multiple project parameter sections.
- process_activities(data)#
Take list of (sheet names, raw data) and process it.
- write_activity_parameters(data=None, delete_existing=True)#
- write_database(**kwargs)#
Same as base
write_database
method, butactivate_parameters
is True by default.
- write_database_parameters(activate_parameters=True, delete_existing=True)#
Same as base
write_database_parameters
method, butactivate_parameters
is True by default.
- class bw2io.ExcelLCIAImporter(filepath, name, description, unit, **metadata)#
Bases:
bw2io.importers.base_lcia.LCIAImporter
Generic Excel LCIA importer.
See the documentation.
- extractor#
- format = 'Excel'#
- class bw2io.Migration(*args, **kwargs)[source]#
Bases:
bw2data.data_store.DataStore
- property description#
- _metadata#
- load()#
- validate(*args, **kwargs)#
- write(data, description)#
Write migration data. Requires a description.
- class bw2io.MultiOutputEcospold1Importer(*args, **kwargs)#
Bases:
SingleOutputEcospold1Importer
Import and process mutli-output datasets in the ecospold 1 format.
Works the same as the single-output importer, but first allocates multioutput datasets.
- class bw2io.SimaProCSVImporter(filepath, name=None, delimiter=';', encoding='latin-1', normalize_biosphere=True, biosphere_db=None)#
Bases:
bw2io.importers.base_lci.LCIImporter
- format = 'SimaPro CSV'#
- get_db_name()#
- match_ecoinvent2(db_name)#
- write_database(data=None, name=None, *args, **kwargs)#
- class bw2io.SimaProLCIACSVImporter(filepath, biosphere=None, delimiter=';', encoding='latin-1', normalize_biosphere=True)#
Bases:
bw2io.importers.base_lcia.LCIAImporter
- format = 'SimaPro CSV LCIA'#
- class bw2io.SingleOutputEcospold1Importer(filepath, db_name, use_mp=True, extractor=Ecospold1DataExtractor)#
Bases:
bw2io.importers.base_lci.LCIImporter
Import and process single-output datasets in the ecospold 1 format.
Applies the following strategies: #. If only one exchange is a production exchange, that is the reference product #. Delete (unreliable) integer codes from extracted data #. Drop
unspecified
subcategories from biosphere flows #. Normalize biosphere flow categories to ecoinvent 3.1 standard #. Normalize biosphere flow names to ecoinvent 3.1 standard #. Remove locations from biosphere exchanges #. Create acode
from the activity hash of the dataset #. Link biosphere exchanges to the default biosphere database #. Link internal technosphere exchanges- Parameters
filepath (*) – Either a file or directory.
db_name (*) – Name of database to create.
- format = 'Ecospold1'#
- class bw2io.SingleOutputEcospold2Importer(dirpath, db_name, extractor=Ecospold2DataExtractor, use_mp=True, signal=None)#
Bases:
bw2io.importers.base_lci.LCIImporter
Base class for format-specific importers.
Defines workflow for applying strategies.
Takes a database name (string) as initialization parameter.
- format = 'Ecospold2'#
- class bw2io.UnlinkedData[source]#
Bases:
bw2data.data_store.DataStore
- _intermediate_dir = 'unlinked'#
- _metadata#
- validate(*args, **kwargs)#
- bw2io.backup_data_directory()[source]#
Backup data directory to a
.tar.gz
(compressed tar archive).Backup archive is saved to the user’s home directory.
Restoration is done manually. Returns the filepath of the backup archive.
- bw2io.backup_project_directory(project)[source]#
Backup project data directory to a
.tar.gz
(compressed tar archive).project
is the name of a project.Backup archive is saved to the user’s home directory.
Restoration is done using
restore_project_directory
.Returns the filepath of the backup archive.
- bw2io.create_default_lcia_methods(overwrite=False, rationalize_method_names=False, shortcut=True)[source]#
- bw2io.es2_activity_hash(activity, flow)[source]#
Generate unique ID for ecoinvent3 dataset.
Despite using a million UUIDs, there is actually no unique ID in an ecospold2 dataset. Datasets are uniquely identified by the combination of activity and flow UUIDs.
- bw2io.lci_matrices_to_excel(database_name, include_descendants=True)#
Fake docstring
- bw2io.lci_matrices_to_matlab(database_name)#
- bw2io.restore_project_directory(fp)[source]#
Restore backup created using
backup_project_directory
.Raises an error is the project already exists.
fp
is the filepath of the backup archive.Returns the name of the newly created project.
- bw2io.migrations#
- bw2io.unlinked_data#