bw2data.backends#
Subpackages#
Submodules#
Classes#
A data store for LCI databases. Stores each dataset in a separate file, serialized to JSON. |
|
A base class for LCI backends. |
|
A base class for LCI backends. |
|
A data store for LCI databases where each database is stored as a |
Functions#
|
Convert a Database to another backend. |
Package Contents#
- class bw2data.backends.JSONDatabase(name)[source]#
Bases:
bw2data.backends.base.LCIBackendA data store for LCI databases. Stores each dataset in a separate file, serialized to JSON.
Instead of loading all the data at once,
.load()creates aSynchronousJSONDict, which loads values on demand.Use this backend by setting
"backend":"json"in the database metadata. This is done automatically if you call.register()from this class.- load(as_dict=False, *args, **kwargs)[source]#
Instantiate
SynchronousJSONDictfor this database.
- register(**kwargs)[source]#
Register a database with the metadata store, using the correct value for
backend, and creates database directory.
- write(data, process=True)[source]#
Serialize data to disk. Most of the time, this data has already been saved to disk, so this is a no-op. The only exception is if
datais a new database dictionary.Normalizes units when found.
- Parameters:
data (*) – Inventory data
- backend = 'json'#
- class bw2data.backends.LCIBackend(name)[source]#
Bases:
bw2data.data_store.ProcessedDataStore
A base class for LCI backends.
Subclasses must support at least the following calls:
load()write(data)
In addition, they should specify their backend with the
backendattribute (a unicode string).LCIBackendprovides the following, which should not need to be modified:renamecopyfind_dependentsrandomprocess
For new classes to be recognized by the
DatabaseChooser, they need to be registered with theconfigobject, e.g.:config.backends['backend type string'] = BackendClass
Instantiation does not load any data. If this database is not yet registered in the metadata store, a warning is written to
stdout.The data schema for databases in voluptuous is:
exchange = { Required("input"): valid_tuple, Required("type"): basestring, } exchange.update(uncertainty_dict) lci_dataset = { Optional("categories"): Any(list, tuple), Optional("location"): object, Optional("unit"): basestring, Optional("name"): basestring, Optional("type"): basestring, Optional("exchanges"): [exchange] } db_validator = Schema({valid_tuple: lci_dataset}, extra=True)
- where:
valid_tupleis a dataset identifier, like("ecoinvent", "super strong steel")uncertainty_fieldsare fields from an uncertainty dictionary.
Processing a Database actually produces two parameter arrays: one for the exchanges, which make up the technosphere and biosphere matrices, and a geomapping array which links activities to locations.
- Parameters:
*name* (unicode string) – Name of the database to manage.
- copy(name)[source]#
Make a copy of the database.
Internal links within the database will be updated to match the new database name, i.e.
("old name", "some id")will be converted to("new name", "some id")for all exchanges.- Parameters:
name (*) – Name of the new database. Must not already exist.
- delete(**kwargs)[source]#
Delete data from this instance. For the base class, only clears cached data.
- find_dependents(data=None, ignore=None)[source]#
Get sorted list of direct dependent databases (databases linked from exchanges).
- Parameters:
data (*) – Inventory data
ignore (*) – List of database names to ignore
- Returns:
List of database names
- find_graph_dependents()[source]#
Recursively get list of all dependent databases.
- Returns:
A set of database names
- abstract load(*args, **kwargs)[source]#
Load the intermediate data for this database.
If
load()does not return a dictionary, then the returned object must have at least the following dictionary-like methods:__iter____contains____getitem____setitem____delitem____len__keys()values()items()items()
However, this method must support the keyword argument
as_dict, and.load(as_dict=True)must return a normal dictionary with all Database data. This is necessary for JSON serialization.It is recommended to subclass
collections.{abc.}MutableMapping(seeSynchronousJSONDictfor an example of data loaded on demand).
- process(*args, **kwargs)[source]#
Process inventory documents.
Creates both a parameter array for exchanges, and a geomapping parameter array linking inventory activities to locations.
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.
- Parameters:
version (*) – The version of the database to process
Doesn’t return anything, but writes two files to disk.
- random()[source]#
Return a random activity key.
Returns a random activity key, or
None(and issues a warning) if the current database is empty.
- register(**kwargs)[source]#
Register a database with the metadata store.
Databases must be registered before data can be written.
- Writing data automatically sets the following metadata:
depends: Names of the databases that this database references, e.g. “biosphere”
number: Number of processes in this database.
- Parameters:
format (*) – Format that the database was converted from, e.g. “Ecospold”
- relabel_data(data, new_name)[source]#
Relabel database keys and exchanges.
In a database which internally refer to the same database, update to new database name
new_name.Needed to copy a database completely or cut out a section of a database.
For example:
data = { ("old and boring", 1): {"exchanges": [ {"input": ("old and boring", 42), "amount": 1.0}, ] }, ("old and boring", 2): {"exchanges": [ {"input": ("old and boring", 1), "amount": 4.0} ] } } print(relabel_database(data, "shiny new")) >> { ("shiny new", 1): {"exchanges": [ {"input": ("old and boring", 42), "amount": 1.0}, ] }, ("shiny new", 2): {"exchanges": [ {"input": ("shiny new", 1), "amount": 4.0} ] } }
In the example, the exchange to
("old and boring", 42)does not change, as this is not part of the updated data.- Parameters:
data (*) – The data to modify
new_name (*) – The name of the modified database
- Returns:
The modified data
- rename(name)[source]#
Rename a database. Modifies exchanges to link to new name. Deregisters old database.
- Parameters:
name (*) – New name.
- Returns:
New
Databaseobject.
- abstract write(data)[source]#
Serialize data to disk.
datamust be a dictionary of the form:{ ('database name', 'dataset code'): {dataset} }
- _metadata#
- dtype_fields#
- dtype_fields_geomapping#
- property filename#
- Remove filesystem-unsafe characters and perform unicode normalization on ``self.name`` using :func:`.utils.safe_filename`.
- validator = None#
- class bw2data.backends.SQLiteBackend(*args, **kwargs)[source]#
Bases:
bw2data.backends.base.LCIBackendA base class for LCI backends.
Subclasses must support at least the following calls:
load()write(data)
In addition, they should specify their backend with the
backendattribute (a unicode string).LCIBackendprovides the following, which should not need to be modified:renamecopyfind_dependentsrandomprocess
For new classes to be recognized by the
DatabaseChooser, they need to be registered with theconfigobject, e.g.:config.backends['backend type string'] = BackendClass
Instantiation does not load any data. If this database is not yet registered in the metadata store, a warning is written to
stdout.The data schema for databases in voluptuous is:
exchange = { Required("input"): valid_tuple, Required("type"): basestring, } exchange.update(uncertainty_dict) lci_dataset = { Optional("categories"): Any(list, tuple), Optional("location"): object, Optional("unit"): basestring, Optional("name"): basestring, Optional("type"): basestring, Optional("exchanges"): [exchange] } db_validator = Schema({valid_tuple: lci_dataset}, extra=True)
- where:
valid_tupleis a dataset identifier, like("ecoinvent", "super strong steel")uncertainty_fieldsare fields from an uncertainty dictionary.
Processing a Database actually produces two parameter arrays: one for the exchanges, which make up the technosphere and biosphere matrices, and a geomapping array which links activities to locations.
- Parameters:
*name* (unicode string) – Name of the database to manage.
- load(*args, **kwargs)[source]#
Load the intermediate data for this database.
If
load()does not return a dictionary, then the returned object must have at least the following dictionary-like methods:__iter____contains____getitem____setitem____delitem____len__keys()values()items()items()
However, this method must support the keyword argument
as_dict, and.load(as_dict=True)must return a normal dictionary with all Database data. This is necessary for JSON serialization.It is recommended to subclass
collections.{abc.}MutableMapping(seeSynchronousJSONDictfor an example of data loaded on demand).
- process()[source]#
Process inventory documents to NumPy structured arrays.
Use a raw SQLite3 cursor instead of Peewee for a ~2 times speed advantage.
- random(filters=True, true_random=False)[source]#
True random requires loading and sorting data in SQLite, and can be resource-intensive.
- search(string, **kwargs)[source]#
Search this database for
string.The searcher include the following fields:
name
comment
categories
location
reference product
stringcan include wild cards, e.g."trans*".By default, the
namefield is given the most weight. The full weighting set is called theboostdictionary, and the default weights are:{ "name": 5, "comment": 1, "product": 3, "categories": 2, "location": 3 }
Optional keyword arguments:
limit: Number of results to return.boosts: Dictionary of field names and numeric boosts - see default boost values above. New values must be in the same format, but with different weights.filter: Dictionary of criteria that search results must meet, e.g.{'categories': 'air'}. Keys must be one of the above fields.mask: Dictionary of criteria that exclude search results. Same format asfilter.facet: Field to facet results. Must be one ofname,product,categories,location, ordatabase.proxy: ReturnActivityproxies instead of raw Whoosh documents. Default isTrue.
Returns a list of
Activitydatasets.
- write(data, process=True)[source]#
Write
datato database.datamust be a dictionary of the form:{ ('database name', 'dataset code'): {dataset} }
Writing a database will first deletes all existing data.
- _filters#
- _order_by = None#
- property _searchable#
- backend = 'sqlite'#
- filters#
- order_by#
- class bw2data.backends.SingleFileDatabase(name)[source]#
Bases:
bw2data.backends.base.LCIBackendA data store for LCI databases where each database is stored as a
picklefile.Databases are automatically versioned. See below for reversion, etc. methods
- Parameters:
*name* (str) – Name of the database to manage.
- filename_for_version(version=None)[source]#
Filename for given version; Default is current version.
- Returns:
Filename (not path)
- load(version=None, **kwargs)[source]#
Load the intermediate data for this database.
Can also load previous versions of this database’s intermediate data.
- Parameters:
version (*) – Version of the database to load. Default
versionis the latest version.- Returns:
The intermediate data, a dictionary.
- make_latest_version()[source]#
Make the current version the latest version.
Requires loading data because a new intermediate data file is created.
- register(**kwargs)[source]#
Register a database with the metadata store.
Databases must be registered before data can be written.
- revert(version)[source]#
Return data to a previous state.
Warning
Reverting can lead to data loss, e.g. if you revert from version 3 to version 1, and then save your database, you will overwrite version 2. Use
make_latest_version()before saving, which will set the current version to 4.- Parameters:
version (*) – Number of the version to revert to.
- versions()[source]#
Get a list of available versions of this database.
- Returns:
List of (version, datetime created) tuples.
- backend = 'singlefile'#
- property filename#
- Remove filesystem-unsafe characters and perform unicode normalization on ``self.name`` using :func:`.utils.safe_filename`.
- validator#
- property version#
- The current version number (integer) of this database.
- Returns:
Version number
- bw2data.backends.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.