bw2data#

Subpackages#

Submodules#

Package Contents#

Classes#

DataStore

Base class for all Brightway2 data stores. Subclasses should define:

IndexManager

JsonWrapper

Normalization

LCIA normalization data - used to transform meaningful units, like mass or damage, into "person-equivalents" or some such thing.

ProcessedDataStore

Brightway2 data stores that can be processed to NumPy arrays. In addition to metadata and (optionally) validator, subclasses should define:

Searcher

Weighting

LCIA weighting data - used to combine or compare different impact categories.

Functions#

Database(name[, backend])

A method that returns a database class instance. The default database type is SingleFileDatabase. JSONDatabase stores each process dataset in indented JSON in a separate file. Database types are specified in databases[database_name]['backend'].

convert_backend(database_name, backend)

Convert a Database to another backend.

get_activity(key)

set_data_dir(dirpath[, permanent])

Set the Brightway2 data directory to dirpath.

Attributes#

calculation_setups

config

databases

dynamic_calculation_setups

geomapping

mapping

methods

normalizations

parameters

preferences

projects

weightings

class bw2data.DataStore(name)[source]#

Bases: object

Base class for all Brightway2 data stores. Subclasses should define:

  • metadata: A serialized-dict instance, e.g. databases or methods. The custom is that each type of data store has a new metadata store, so the data store Foo would have a metadata store foos.

  • validator: A data validator. Optional. See bw2data.validate.

property filename#

Remove filesystem-unsafe characters and perform unicode normalization on self.name using utils.safe_filename().

property registered#
_intermediate_dir = 'intermediate'#
_metadata#
metadata#
validator#
_get_metadata()#
_set_metadata(value)#
backup()#

Save a backup to backups folder.

Returns

File path of backup.

copy(name)#

Make a copy of this object with a new name.

This method only changes the name, but not any of the data or metadata.

Parameters

name (*) – Name of the new object.

Returns

The new object.

deregister()#

Remove an object from the metadata store. Does not delete any files.

load()#

Load the intermediate data for this object.

Returns

The intermediate data.

register(**kwargs)#

Register an object with the metadata store. Takes any number of keyword arguments.

validate(data)#

Validate data. Must be called manually.

write(data)#

Serialize intermediate data to disk.

Parameters

data (*) – The data

class bw2data.IndexManager(database_path, dir_name='whoosh')#

Bases: object

_format_dataset(ds)#
add_dataset(ds)#
add_datasets(datasets)#
create()#
delete_database()#
delete_dataset(ds)#
get()#
update_dataset(ds)#
class bw2data.JsonWrapper[source]#

Bases: object

classmethod dump(data, filepath)#
classmethod dump_bz2(data, filepath)#
classmethod dumps(data)#
classmethod load(file)#
classmethod load_bz2(filepath)#
classmethod loads(data)#
class bw2data.Normalization[source]#

Bases: bw2data.ia_data_store.ImpactAssessmentDataStore

Inheritance diagram of bw2data.Normalization

LCIA normalization data - used to transform meaningful units, like mass or damage, into “person-equivalents” or some such thing.

The data schema for IA normalization is:

Schema([
    [valid_tuple, maybe_uncertainty]
])
where:
  • valid_tuple is a dataset identifier, like ("biosphere", "CO2")

  • maybe_uncertainty is either a number or an uncertainty dictionary

_metadata#
dtype_fields = [(), ()]#
validator#
add_mappings(data)#

Add each normalization flow (should be biosphere flows) to global mapping

process_data(row)#

Return values that match dtype_fields, as well as number or uncertainty dictionary

class bw2data.ProcessedDataStore(name)[source]#

Bases: DataStore

Inheritance diagram of bw2data.ProcessedDataStore

Brightway2 data stores that can be processed to NumPy arrays. In addition to metadata and (optionally) validator, subclasses should define:

  • dtype_fields: A list of fields to construct a NumPy structured array, e.g. [('foo', np.int), ('bar', np.float)]. Fields names must be bytestrings, not unicode (i.e. b"foo" instead of "foo"). Uncertainty fields (base_uncertainty_fields) are added automatically.

In order to use dtype_fields, subclasses should override the method process_data. This function takes rows of data, and returns the correct values for the custom dtype fields (as a tuple), and the amount field with its associated uncertainty. This second part is a little flexible - if there is no uncertainty, a number can be returned; otherwise, an uncertainty dictionary should be returned.

Subclasses should also override add_mappings. This method takes the entire dataset, and loads objects to mapping or geomapping as needed.

property dtype#

Returns both the generic base_uncertainty_fields plus class-specific dtype_fields. dtype determines the columns of the processed array.

base_uncertainty_fields = [(), (), (), (), (), (), (), ()]#
dtype_fields#
add_mappings(data)#

Add objects to mapping or geomapping, if necessary.

Parameters

data (*) – The data

as_uncertainty_dict(value)#

Convert floats to stats_arrays uncertainty dict, if necessary

dtype_field_order(dtype=None)#
filepath_processed()#
process()#

Process intermediate data from a Python dictionary to a stats_arrays array, which is a NumPy Structured Array. A structured array (also called record array) is a heterogeneous array, where each column has a different label and data type.

Processed arrays are saved in the processed directory.

If the uncertainty type is no uncertainty, undefined, or not specified, then the ‘amount’ value is used for ‘loc’ as well. This is needed for the random number generator.

Doesn’t return anything, but writes a file to disk.

abstract process_data(row)#

Translate data into correct order

validate(data)#

Validate data. Must be called manually.

write(data, process=True)#

Serialize intermediate data to disk.

Parameters

data (*) – The data

class bw2data.Searcher(database)#

Bases: object

search(string, limit=25, facet=None, proxy=True, boosts=None, filter=None, mask=None)#
class bw2data.Weighting[source]#

Bases: bw2data.ia_data_store.ImpactAssessmentDataStore

Inheritance diagram of bw2data.Weighting

LCIA weighting data - used to combine or compare different impact categories.

The data schema for weighting is a one-element list:

Schema(All(
    [uncertainty_dict],
    Length(min=1, max=1)
))
_metadata#
dtype_fields = []#
validator#
process_data(row)#

Return an empty tuple (as dtype_fields is empty), and the weighting uncertainty dictionary.

write(data)#

Because of DataStore assumptions, need a one-element list

bw2data.Database(name, backend=None)#

A method that returns a database class instance. The default database type is SingleFileDatabase. JSONDatabase stores each process dataset in indented JSON in a separate file. Database types are specified in databases[database_name][‘backend’].

New database types can be registered with the config object:

config.backends['backend type string'] = MyNewBackendClass

Warning

Registering new backends must be done each time you start the Python interpreter.

To test whether an object is a database subclass, do:

from bw2data.backends import LCIBackend
isinstance(my_database, LCIBackend)
bw2data.convert_backend(database_name, backend)#

Convert a Database to another backend.

bw2data currently supports the default and json backends.

Parameters
  • database_name (*) – Name of database.

  • backend (*) – Type of database. backend should be recoginized by DatabaseChooser.

Returns False if the old and new backend are the same. Otherwise returns an instance of the new Database object.

bw2data.get_activity(key)#
bw2data.set_data_dir(dirpath, permanent=True)[source]#

Set the Brightway2 data directory to dirpath.

If permanent is True, then set dirpath as the default data directory.

Creates dirpath if needed. Also creates basic directories, and resets metadata.

bw2data.calculation_setups[source]#
bw2data.config[source]#
bw2data.databases[source]#
bw2data.dynamic_calculation_setups[source]#
bw2data.geomapping[source]#
bw2data.mapping[source]#
bw2data.methods[source]#
bw2data.normalizations[source]#
bw2data.parameters#
bw2data.preferences[source]#
bw2data.projects[source]#
bw2data.weightings[source]#