bw2calc.lca#

Module Contents#

Classes#

LCA

An LCI or LCIA calculation.

Attributes#

get_node

logger

class bw2calc.lca.LCA(demand: dict, method: Optional[tuple] = None, weighting: Optional[str] = None, normalization: Optional[str] = None, data_objs: Optional[Iterable[Union[pathlib.Path, fs.base.FS, bw_processing.DatapackageBase]]] = None, remapping_dicts: Optional[Iterable[dict]] = None, log_config: Optional[dict] = None, seed_override: Optional[int] = None, use_arrays: bool = False, use_distributions: bool = False)[source]#

Bases: collections.abc.Iterator

Inheritance diagram of bw2calc.lca.LCA

An LCI or LCIA calculation.

Compatible with Brightway2 and 2.5 semantics. Can be static, stochastic, or iterative (scenario-based), depending on the data_objs input data..

Create a new LCA calculation.

Args:
  • demand (dict): The demand or functional unit. Needs to be a dictionary to indicate amounts, e.g. {7: 2.5}.

  • method (tuple, optional): LCIA Method tuple, e.g. ("My", "great", "LCIA", "method"). Can be omitted if only interested in calculating the life cycle inventory.

Returns:

A new LCA object

property activity_dict[source]#
property biosphere_dict[source]#
property product_dict[source]#
property score: float[source]#

The LCIA score as a float.

Note that this is a property, so it is foo.lca, not foo.score()

matrix_labels = ['technosphere_mm', 'biosphere_mm', 'characterization_mm', 'normalization_mm', 'weighting_mm'][source]#
__check_demand(demand: Optional[dict] = None)[source]#
_switch(obj: Union[tuple, Iterable[Union[fs.base.FS, bw_processing.DatapackageBase]]], label: str, matrix: str, func: Callable) None[source]#

Switch a method, weighting, or normalization

build_demand_array(demand: Optional[dict] = None) None[source]#

Turn the demand dictionary into a NumPy array of correct size.

Args:
  • demand (dict, optional): Demand dictionary. Optional, defaults to self.demand.

Returns:

A 1-dimensional NumPy array

decompose_technosphere() None[source]#

Factorize the technosphere matrix into lower and upper triangular matrices, \(A=LU\). Does not solve the linear system \(Ax=B\).

Doesn’t return anything, but creates self.solver.

Warning

Incorrect results could occur if a technosphere matrix was factorized, and then a new technosphere matrix was constructed, as self.solver would still be the factorized older technosphere matrix. You are responsible for deleting self.solver when doing these types of advanced calculations.

ensure_bw2data_available()[source]#

Raises ImportError is bw2data not available or version < 4.

has(label: str) bool[source]#

Shortcut to find out if matrix data for type {label}_matrix is present in the given data objects.

Returns a boolean. Will return True even if data for a zero-dimensional matrix is given.

invert_technosphere_matrix()[source]#

Use pardiso to efficiently calculate the inverse of the technosphere matrix.

keep_first_iteration()[source]#

Set a flag to use the current values as first element when iterating.

When creating the class instance, we already use the first index. This method allows us to use the values for the first index.

Note that the methods .lci_calculation() and .lcia_calculation() will be called on the current values, even if these calculations have already been done.

lci(demand: Optional[dict] = None, factorize: bool = False) None[source]#

Calculate a life cycle inventory.

  1. Load LCI data, and construct the technosphere and biosphere matrices.

  2. Build the demand array

  3. Solve the linear system to get the supply array and life cycle inventory.

Args:
  • factorize (bool, optional): Factorize the technosphere matrix. Makes additional calculations with the same technosphere matrix much faster. Default is False; not useful is only doing one LCI calculation.

  • builder (MatrixBuilder object, optional): Default is bw2calc.matrices.MatrixBuilder, which is fine for most cases. Custom matrix builders can be used to manipulate data in creative ways before building the matrices.

Doesn’t return anything, but creates self.supply_array and self.inventory.

lci_calculation() None[source]#

The actual LCI calculation.

Separated from lci to be reusable in cases where the matrices are already built, e.g. redo_lci and Monte Carlo classes.

lcia(demand: Optional[dict] = None) None[source]#

Calculate the life cycle impact assessment.

  1. Load and construct the characterization matrix

  2. Multiply the characterization matrix by the life cycle inventory

Doesn’t return anything, but creates self.characterized_inventory.

lcia_calculation() None[source]#

The actual LCIA calculation.

Separated from lcia to be reusable in cases where the matrices are already built, e.g. redo_lcia and Monte Carlo classes.

load_lci_data(nonsquare_ok=False) None[source]#

Load inventory data and create technosphere and biosphere matrices.

load_lcia_data(data_objs: Optional[Iterable[Union[fs.base.FS, bw_processing.DatapackageBase]]] = None) None[source]#

Load data and create characterization matrix.

This method will filter out regionalized characterization factors.

load_normalization_data(data_objs: Optional[Iterable[Union[fs.base.FS, bw_processing.DatapackageBase]]] = None) None[source]#

Load normalization data.

load_weighting_data(data_objs: Optional[Iterable[Union[fs.base.FS, bw_processing.DatapackageBase]]] = None) None[source]#

Load normalization data.

normalization_calculation() None[source]#

The actual normalization calculation.

Creates self.normalized_inventory.

normalize() None[source]#

Multiply characterized inventory by flow-specific normalization factors.

redo_lci(demand: Optional[dict] = None) None[source]#

Redo LCI with same databases but different demand.

Args:
  • demand (dict): A demand dictionary.

Doesn’t return anything, but overwrites self.demand_array, self.supply_array, and self.inventory.

Warning

If you want to redo the LCIA as well, use redo_lcia(demand) directly.

redo_lcia(demand: Optional[dict] = None) None[source]#

Redo LCIA, optionally with new demand.

Args:
  • demand (dict, optional): New demand dictionary. Optional, defaults to self.demand.

Doesn’t return anything, but overwrites self.characterized_inventory. If demand is given, also overwrites self.demand_array, self.supply_array, and self.inventory.

remap_inventory_dicts() None[source]#

Remap self.dicts.activity|product|biosphere and self.demand from database integer IDs to keys ((database name, code)).

Uses remapping dictionaries in self.remapping_dicts.

reverse_dict()[source]#
solve_linear_system() None[source]#

Master solution function for linear system \(Ax=B\).

To most numerical analysts, matrix inversion is a sin.

—Nicolas Higham, Accuracy and Stability of Numerical Algorithms, Society for Industrial and Applied Mathematics, Philadelphia, PA, USA, 2002, p. 260.

We use UMFpack, which is a very fast solver for sparse matrices.

If the technosphere matrix has already been factorized, then the decomposed technosphere (self.solver) is reused. Otherwise the calculation is redone completely.

switch_method(method=Union[tuple, Iterable[Union[FS, bwp.DatapackageBase]]]) None[source]#

Load a new method and replace .characterization_mm and .characterization_matrix.

Does not do any new calculations or change .characterized_inventory.

switch_normalization(normalization=Union[tuple, Iterable[Union[FS, bwp.DatapackageBase]]]) None[source]#

Load a new normalization and replace .normalization_mm and .normalization_matrix.

Does not do any new calculations or change .normalized_inventory.

switch_weighting(weighting=Union[tuple, Iterable[Union[FS, bwp.DatapackageBase]]]) None[source]#

Load a new weighting and replace .weighting_mm and .weighting_matrix.

Does not do any new calculations or change .weighted_inventory.

to_dataframe(matrix_label: str = 'characterized_inventory', row_dict: Optional[dict] = None, col_dict: Optional[dict] = None, annotate: bool = True, cutoff: numbers.Number = 200, cutoff_mode: str = 'number') pandas.DataFrame[source]#

Return all nonzero elements of the given matrix as a Pandas dataframe.

The LCA class instance must have the matrix matrix_label already; common labels are:

  • characterized_inventory

  • inventory

  • technosphere_matrix

  • biosphere_matrix

  • characterization_matrix

For these common matrices, we already have row_dict and col_dict which link row and column indices to database ids. For other matrices, or if you have a custom mapping dictionary, override row_dict and/or col_dict. They have the form {matrix index: identifier}.

If bw2data is installed, this function will try to look up metadata on the row and column objects. To turn this off, set annotate to False.

Instead of returning all possible values, you can apply a cutoff. This cutoff can be specified in two ways, controlled by cutoff_mode, which should be either fraction or number.

If cutoff_mode is number (the default), then cutoff is the number of rows in the DataFrame. Data values are first sorted by their absolute value, and then the largest cutoff are taken.

If cutoff_mode is fraction, then only values whose absolute value is greater than cutoff * total_score are taken. cutoff must be between 0 and 1.

The returned DataFrame will have the following columns:

  • amount

  • col_index

  • row_index

If row or columns dictionaries are available, the following columns are added:

  • col_id

  • row_id

If bw2data is available, then the following columns are added:

  • col_code

  • col_database

  • col_location

  • col_name

  • col_reference_product

  • col_type

  • col_unit

  • row_categories

  • row_code

  • row_database

  • row_location

  • row_name

  • row_type

  • row_unit

  • source_product

Returns a pandas DataFrame.

weight() None[source]#

Multiply characterized inventory by weighting value.

Can be done with or without normalization.

weighting() None[source]#

Backwards compatibility. Switching to verb form consistent with .normalize.

weighting_calculation() None[source]#

The actual weighting calculation.

Multiples weighting value by normalized inventory, if available, otherwise by characterized inventory.

Creates self.weighted_inventory.

bw2calc.lca.get_node[source]#
bw2calc.lca.logger[source]#