bw2calc.lca
#
Module Contents#
Classes#
An LCI or LCIA calculation. |
Attributes#
- 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
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 score: float[source]#
The LCIA score as a
float
.Note that this is a property, so it is
foo.lca
, notfoo.score()
- matrix_labels = ['technosphere_mm', 'biosphere_mm', 'characterization_mm', 'normalization_mm', 'weighting_mm'][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 deletingself.solver
when doing these types of advanced calculations.
- 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.
Load LCI data, and construct the technosphere and biosphere matrices.
Build the demand array
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 isbw2calc.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
andself.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.
Load and construct the characterization matrix
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
.
- 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
, andself.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
. Ifdemand
is given, also overwritesself.demand_array
,self.supply_array
, andself.inventory
.
- remap_inventory_dicts() None [source]#
Remap
self.dicts.activity|product|biosphere
andself.demand
from database integer IDs to keys ((database name, code)
).Uses remapping dictionaries in
self.remapping_dicts
.
- 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
andcol_dict
which link row and column indices to database ids. For other matrices, or if you have a custom mapping dictionary, overriderow_dict
and/orcol_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, setannotate
toFalse
.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 eitherfraction
ornumber
.If
cutoff_mode
isnumber
(the default), thencutoff
is the number of rows in the DataFrame. Data values are first sorted by their absolute value, and then the largestcutoff
are taken.If
cutoff_mode
isfraction
, then only values whose absolute value is greater thancutoff * 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.