bw2data.utils#

Module Contents#

Functions#

as_uncertainty_dict(value)

Given either a number or a stats_arrays uncertainty dict, return an uncertainty dict

clean_exchanges(data)

Make sure all exchange inputs are tuples, not lists.

combine_databases(name, *dbs)

Combine databases into new database called name.

combine_methods(name, *ms)

Combine LCIA methods by adding duplicate characterization factors.

create_in_memory_zipfile_from_directory(path)

download_file(filename[, directory, url])

Download a file and write it to disk in downloads directory.

get_activity([key])

Support multiple ways to get exactly one activity node.

get_geocollection(location[, default_global_location])

conservative approach to finding geocollections. Won't guess about ecoinvent or other databases.

get_node(**kwargs)

maybe_path(x)

merge_databases(parent_db, other)

Merge other into parent_db, including updating exchanges.

natural_sort(l)

Sort the given list in the way that humans expect, e.g. 9 before 10.

random_string([length])

Generate a random string of letters and numbers.

recursive_str_to_unicode(data[, encoding])

Convert the strings inside a (possibly nested) python data structure to unicode strings using encoding.

safe_filename(*args, **kwargs)

set_data_dir(dirpath[, permanent])

Set the Brightway2 data directory to dirpath.

uncertainify(data[, distribution, bounds_factor, ...])

Add some rough uncertainty to exchanges.

Attributes#

DOWNLOAD_URL

POSITIVE_DISTRIBUTIONS

TYPE_DICTIONARY

bw2data.utils.as_uncertainty_dict(value)[source]#

Given either a number or a stats_arrays uncertainty dict, return an uncertainty dict

bw2data.utils.clean_exchanges(data)[source]#

Make sure all exchange inputs are tuples, not lists.

bw2data.utils.combine_databases(name, *dbs)[source]#

Combine databases into new database called name.

bw2data.utils.combine_methods(name, *ms)[source]#

Combine LCIA methods by adding duplicate characterization factors.

Args:
  • ms (one or more method id tuples): Any number of method ids, e.g. ("my method", "wow"), ("another method", "wheee").

Returns:

The new Method instance.

bw2data.utils.create_in_memory_zipfile_from_directory(path)[source]#
bw2data.utils.download_file(filename, directory='downloads', url=None)[source]#

Download a file and write it to disk in downloads directory.

If url is None, uses the Brightway2 data base URL. url should everything up to the filename, such that url + filename is the valid complete URL to download from.

Streams download to reduce memory usage.

Args:
  • filename (str): The filename to download.

  • directory (str, optional): Directory to save the file. Created if it doesn‚Äôt already exist.

  • url (str, optional): URL where the file is located, if not the default Brightway data URL.

Returns:

The path of the created file.

bw2data.utils.get_activity(key=None, **kwargs)[source]#

Support multiple ways to get exactly one activity node.

key can be an integer or a key tuple.

bw2data.utils.get_geocollection(location, default_global_location=False)[source]#

conservative approach to finding geocollections. Won’t guess about ecoinvent or other databases.

bw2data.utils.get_node(**kwargs)[source]#
bw2data.utils.maybe_path(x)[source]#
bw2data.utils.merge_databases(parent_db, other)[source]#

Merge other into parent_db, including updating exchanges.

All databases must be SQLite databases.

parent_db and other should be the names of databases.

Doesn’t return anything.

bw2data.utils.natural_sort(l)[source]#

Sort the given list in the way that humans expect, e.g. 9 before 10.

bw2data.utils.random_string(length=8)[source]#

Generate a random string of letters and numbers.

Args:
  • length (int): Length of string, default is 8

Returns:

A string (not unicode)

bw2data.utils.recursive_str_to_unicode(data, encoding='utf8')[source]#

Convert the strings inside a (possibly nested) python data structure to unicode strings using encoding.

bw2data.utils.safe_filename(*args, **kwargs)[source]#
bw2data.utils.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.utils.uncertainify(data, distribution=None, bounds_factor=0.1, sd_factor=0.1)[source]#

Add some rough uncertainty to exchanges.

Warning

This function only changes exchanges with no uncertainty type or uncertainty type UndefinedUncertainty, and does not change production exchanges!

Can only apply normal or uniform uncertainty distributions; default is uniform. Distribution, if specified, must be a stats_array uncertainty object.

data is a LCI data dictionary.

If using the normal distribution:

  • sd_factor will be multiplied by the mean to calculate the standard deviation.

  • If no bounds are desired, set bounds_factor to None.

  • Otherwise, the bounds will be [(1 - bounds_factor) * mean, (1 + bounds_factor) * mean].

If using the uniform distribution, then the bounds are [(1 - bounds_factor) * mean, (1 + bounds_factor) * mean].

Returns the modified data.

bw2data.utils.DOWNLOAD_URL = 'https://brightway.dev/data/'[source]#
bw2data.utils.POSITIVE_DISTRIBUTIONS[source]#
bw2data.utils.TYPE_DICTIONARY[source]#