bw2io#
Subpackages#
Submodules#
Attributes#
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. |
|
Install an existing Brightway project archive. |
|
Fake docstring |
|
|
|
|
|
Restore a backed up project data directory from a |
Package Contents#
- class bw2io.BW2Package[source]#
Bases:
objectThis 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")- classmethod export_obj(obj, filename=None, folder='export', backwards_compatible=False)[source]#
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)[source]#
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)[source]#
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)[source]#
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).
- APPROVED#
- class bw2io.CSVImporter(filepath)[source]#
Bases:
ExcelImporterGeneric CSV importer
- extractor#
- format = 'CSV'#
- class bw2io.CSVLCIAImporter(filepath, name, description, unit, **metadata)[source]#
Bases:
ExcelLCIAImporterGeneric CSV LCIA importer
- extractor#
- format = 'CSV'#
- class bw2io.DatabaseSelectionToGEXF(database, keys)[source]#
Bases:
DatabaseToGEXFExport 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.
- data#
- database#
- filepath#
- id_mapping#
- unfiltered_data#
- class bw2io.DatabaseToGEXF(database, include_descendants=False)[source]#
Bases:
objectExport 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_descendantsis not yet implemented.- data#
- database#
- descendants#
- filename#
- filepath#
- id_mapping#
- class bw2io.Ecospold1LCIAImporter(filepath, biosphere=None)[source]#
Bases:
bw2io.importers.base_lcia.LCIAImporter- data#
- format = 'Ecospold1 LCIA'#
- start#
- class bw2io.ExcelImporter(filepath)[source]#
Bases:
bw2io.importers.base_lci.LCIImporterGeneric 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.TrueandFalseare transformed to boolean values.Fields with the value
(Unknown)are dropped.
- get_labelled_section(sn, ws, index=0, transform=True)[source]#
Turn a list of rows into a list of dictionaries.
The first line of
wsis the column labels. All subsequent rows are the data values. Missing columns are dropped.transformis a boolean: perform CSV transformation functions likecsv_restore_tuples.
- get_project_parameters(data)[source]#
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.
- write_database(**kwargs)[source]#
Same as base
write_databasemethod, butactivate_parametersis True by default.
- write_database_parameters(activate_parameters=True, delete_existing=True)[source]#
Same as base
write_database_parametersmethod, butactivate_parametersis True by default.
- data#
- extractor#
- format = 'Excel'#
- start#
- strategies#
- class bw2io.ExcelLCIAImporter(filepath, name, description, unit, **metadata)[source]#
Bases:
bw2io.importers.base_lcia.LCIAImporterGeneric Excel LCIA importer.
See the documentation.
- data#
- extractor#
- format = 'Excel'#
- strategies#
- class bw2io.Migration(*args, **kwargs)[source]#
Bases:
bw2data.data_store.DataStore
- _intermediate_dir#
- _metadata#
- property description#
- class bw2io.MultiOutputEcospold1Importer(*args, **kwargs)[source]#
Bases:
SingleOutputEcospold1ImporterImport 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)[source]#
Bases:
bw2io.importers.base_lci.LCIImporter- format = 'SimaPro CSV'#
- start#
- strategies#
- class bw2io.SimaProLCIACSVImporter(filepath, biosphere=None, delimiter=';', encoding='latin-1', normalize_biosphere=True)[source]#
Bases:
bw2io.importers.base_lcia.LCIAImporter- data#
- format = 'SimaPro CSV LCIA'#
- start#
- class bw2io.SingleOutputEcospold1Importer(filepath, db_name, use_mp=True, extractor=Ecospold1DataExtractor)[source]#
Bases:
bw2io.importers.base_lci.LCIImporterImport 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
unspecifiedsubcategories 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 acodefrom 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.
- db_name#
- format = 'Ecospold1'#
- start#
- strategies#
- class bw2io.SingleOutputEcospold2Importer(dirpath, db_name, extractor=Ecospold2DataExtractor, use_mp=True, signal=None, reparametrize_lognormals=False)[source]#
Bases:
bw2io.importers.base_lci.LCIImporterBase class for format-specific importers.
Defines workflow for applying strategies.
Takes a database name (string) as initialization parameter.
Initializes the SingleOutputEcospold2Importer class instance.
- Parameters:
dirpath (str) – Path to the directory containing the ecospold2 file.
db_name (str) – Name of the LCI database.
extractor (class) – Class for extracting data from the ecospold2 file, by default Ecospold2DataExtractor.
use_mp (bool) – Flag to indicate whether to use multiprocessing, by default True.
signal (object) – Object to indicate the status of the import process, by default None.
reparametrize_lognormals (bool) – Flag to indicate if lognormal distributions for exchanges should be reparametrized such that the mean value of the resulting distribution meets the amount defined for the exchange.
- db_name#
- dirpath#
- format = 'Ecospold2'#
- signal#
- start#
- strategies#
- class bw2io.UnlinkedData[source]#
Bases:
bw2data.data_store.DataStore
- _intermediate_dir = 'unlinked'#
- _metadata#
- bw2io.backup_data_directory()[source]#
Backup data directory to a
.tar.gz(compressed tar archive) in the user’s home directory. Restoration is done manually.Examples
>>> bw2io.bw2setup() >>> bw2io.backup.backup_data_directory() Creating backup archive - this could take a few minutes...
- bw2io.backup_project_directory(project: str)[source]#
Backup project data directory to a
.tar.gz(compressed tar archive) in the user’s home directory.- Parameters:
project (str) – Name of the project to backup.
- Returns:
project_name – Name of the project that was backed up.
- Return type:
str
- Raises:
ValueError – If the project does not exist.
See also
bw2io.backup.restore_project_directoryTo restore a project directory from a backup.
- 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.install_project(project_key: str, project_name: str | None = None, projects_config: dict | None = get_projects(), url: str | None = 'https://files.brightway.dev/', overwrite_existing: bool | None = False, __recursive: bool | None = False)[source]#
Install an existing Brightway project archive.
By default uses
https://files.brightway.dev/as the file repository, but you can run your own.- Parameters:
project_key (str) – A string uniquely identifying a project, e.g.
ecoinvent-3.8-biosphere.project_name (str, optional) – The name of the new project to create. If not provided will be taken from the archive file.
projects_config (dict, optional) – A dictionary that maps
project_keyvalues to filenames at the repositoryurl (str, optional) – The URL, with trailing slash
/, where the file can be found.overwrite_existing (bool, optional) – Allow overwriting an existing project
__recursive (bool) – Internal flag used to determine if this function has errored out already
- Returns:
The name of the created project.
- Return type:
str
- bw2io.restore_project_directory(fp: str, project_name: str | None = None, overwrite_existing: bool | None = False)[source]#
Restore a backed up project data directory from a
.tar.gz(compressed tar archive) in the user’s home directory.- Parameters:
fp (str) – File path of the project to restore.
project_name (str, optional) – Name of new project to create
overwrite_existing (bool, optional)
- Returns:
project_name – Name of the project that was restored.
- Return type:
str
- Raises:
ValueError – If the project does not exist.
See also
bw2io.backup.backup_project_directoryTo restore a project directory from a backup.