bw2data#

Subpackages#

Submodules#

Attributes#

Classes#

DataStore

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

IndexManager

Method

A manager for an impact assessment method. This class can register or deregister methods, write intermediate data, process data to parameter arrays, validate, and copy methods.

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.

Package Contents#

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.

_get_metadata()[source]#
_set_metadata(value)[source]#
backup()[source]#

Save a backup to backups folder.

Returns:

File path of backup.

copy(name)[source]#

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()[source]#

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

load()[source]#

Load the intermediate data for this object.

Returns:

The intermediate data.

register(**kwargs)[source]#

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

validate(data)[source]#

Validate data. Must be called manually.

write(data)[source]#

Serialize intermediate data to disk.

Parameters:

data (*) – The data

_intermediate_dir = 'intermediate'#
_metadata = None#
property filename#
Remove filesystem-unsafe characters and perform unicode normalization on ``self.name`` using :func:`.utils.safe_filename`.
metadata#
name#
property registered#
validator = None#
class bw2data.IndexManager(database_path, dir_name='whoosh')[source]#

Bases: object

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

Bases: bw2data.ia_data_store.ImpactAssessmentDataStore

Inheritance diagram of bw2data.Method

A manager for an impact assessment method. This class can register or deregister methods, write intermediate data, process data to parameter arrays, validate, and copy methods.

The Method class never holds intermediate data, but it can load or write intermediate data. The only attribute is name, which is the name of the method being managed.

Instantiation does not load any data. If this method is not yet registered in the metadata store, a warning is written to stdout.

Methods are hierarchally structured, and this structure is preserved in the method name. It is a tuple of strings, like ('ecological scarcity 2006', 'total', 'natural resources').

The data schema for IA methods is:

Schema([Any(
    [valid_tuple, maybe_uncertainty],         # site-generic
    [valid_tuple, maybe_uncertainty, object]  # regionalized
)])
where:
  • valid_tuple (tuple): A dataset identifier, like ("biosphere", "CO2").

  • maybe_uncertainty (uncertainty dict or number): Either a number or an uncertainty dictionary.

  • object (object, optional) is a location identifier, used only for regionalized LCIA.

Parameters:

name (*) – Name of impact assessment method to manage.

add_mappings(data)[source]#
process_data(row)[source]#
write(data, process=True)[source]#

Serialize intermediate data to disk.

Sets the metadata key num_cfs automatically.

_metadata#
dtype_fields#
validator#
class bw2data.Normalization(name)[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

add_mappings(data)[source]#

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

process_data(row)[source]#

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

_metadata#
dtype_fields#
validator#
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.

add_mappings(data)[source]#

Add objects to mapping or geomapping, if necessary.

Parameters:

data (*) – The data

as_uncertainty_dict(value)[source]#

Convert floats to stats_arrays uncertainty dict, if necessary

dtype_field_order(dtype=None)[source]#
filepath_processed()[source]#
process()[source]#

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)[source]#

Translate data into correct order

validate(data)[source]#

Validate data. Must be called manually.

write(data, process=True)[source]#

Serialize intermediate data to disk.

Parameters:

data (*) – The data

base_uncertainty_fields#
property dtype#
Returns both the generic ``base_uncertainty_fields`` plus class-specific ``dtype_fields``. ``dtype`` determines the columns of the :ref:`processed array <processing-data>`.
dtype_fields = None#
class bw2data.Searcher(database)[source]#

Bases: object

search(string, limit=25, facet=None, proxy=True, boosts=None, filter=None, mask=None)[source]#
_database#
class bw2data.Weighting(name)[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)
))
process_data(row)[source]#

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

write(data)[source]#

Because of DataStore assumptions, need a one-element list

_metadata#
dtype_fields = []#
validator#
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)[source]#

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)[source]#
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[source]#
bw2data.preferences[source]#
bw2data.projects[source]#
bw2data.weightings[source]#