bw2calc.lca
#
Module Contents#
Classes#
A static LCI or LCIA calculation. |
Attributes#
- class bw2calc.lca.LCA(demand, method=None, weighting=None, normalization=None, database_filepath=None, log_config=None, presamples=None, seed=None, override_presamples_seed=False)[source]#
Bases:
object
A static LCI or LCIA calculation.
Following the general philosophy of Brightway2, and good software practices, there is a clear separation of concerns between retrieving and formatting data and doing an LCA. Building the necessary matrices is done with MatrixBuilder objects (matrixbuilders). The LCA class only does the LCA calculations themselves.
Create a new LCA calculation.
- Parameters
demand (*) – The demand or functional unit. Needs to be a dictionary to indicate amounts, e.g.
{("my database", "my process"): 2.5}
.method (*) – 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[source]#
The LCIA score as a
float
.Note that this is a property, so it is
foo.lca
, notfoo.score()
- build_demand_array(demand=None)[source]#
Turn the demand dictionary into a NumPy array of correct size.
- Parameters
demand (*) – Demand dictionary. Optional, defaults to
self.demand
.- Returns
A 1-dimensional NumPy array
- decompose_technosphere()[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.
- fix_dictionaries()[source]#
Fix technosphere and biosphere dictionaries from this:
{mapping integer id: matrix row/column index}
To this:
{(database, key): matrix row/column index}
This isn’t needed for the LCA calculation itself, but is helpful when interpreting results.
Doesn’t require any arguments or return anything, but changes
self.activity_dict
,self.product_dict
andself.biosphere_dict
.
- lci(factorize=False, builder=TBMBuilder)[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.
- Parameters
factorize (*) – 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 (*) – Default is
bw2calc.matrices.TechnosphereBiosphereMatrixBuilder
, which is fine for most cases. Custom matrix builders can be used to manipulate data in creative ways before building the matrices.
Warning
Custom matrix builders should inherit from
TechnosphereBiosphereMatrixBuilder
, because technosphere inputs need to have their signs flipped to be negative, as we do \(A^{-1}f\) directly instead of \((I - A^{-1})f\).Doesn’t return anything, but creates
self.supply_array
andself.inventory
.
- lci_calculation()[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(builder=MatrixBuilder)[source]#
Calculate the life cycle impact assessment.
Load and construct the characterization matrix
Multiply the characterization matrix by the life cycle inventory
- Parameters
builder (*) – 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 characterization matrix.
Doesn’t return anything, but creates
self.characterized_inventory
.
- lcia_calculation()[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(fix_dictionaries=True, builder=TBMBuilder)[source]#
Load data and create technosphere and biosphere matrices.
- load_lcia_data(builder=MatrixBuilder)[source]#
Load data and create characterization matrix.
This method will filter out regionalized characterization factors. This filtering needs access to
bw2data
- therefore, regionalized methods will cause incorrect results ifbw2data
is not importable.
- normalization_calculation()[source]#
The actual normalization calculation.
Creates
self.normalized_inventory
.
- rebuild_biosphere_matrix(vector)[source]#
Build a new biosphere matrix using the same row and column indices, but different values. Useful for Monte Carlo iteration or sensitivity analysis.
- Parameters
vector (*) – 1-dimensional NumPy array with length (# of biosphere parameters), in same order as
self.bio_params
.
Doesn’t return anything, but overwrites
self.biosphere_matrix
.
- rebuild_characterization_matrix(vector)[source]#
Build a new characterization matrix using the same row and column indices, but different values. Useful for Monte Carlo iteration or sensitivity analysis.
- Parameters
vector (*) – 1-dimensional NumPy array with length (# of characterization parameters), in same order as
self.cf_params
.
Doesn’t return anything, but overwrites
self.characterization_matrix
.
- rebuild_technosphere_matrix(vector)[source]#
Build a new technosphere matrix using the same row and column indices, but different values. Useful for Monte Carlo iteration or sensitivity analysis.
- Parameters
vector (*) – 1-dimensional NumPy array with length (# of technosphere parameters), in same order as
self.tech_params
.
Doesn’t return anything, but overwrites
self.technosphere_matrix
.
- redo_lci(demand=None)[source]#
Redo LCI with same databases but different demand.
- Parameters
demand (*) – 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=None)[source]#
Redo LCIA, optionally with new demand.
- Parameters
demand (*) – 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
.
- reverse_dict()[source]#
Construct reverse dicts from technosphere and biosphere row and col indices to activity_dict/product_dict/biosphere_dict keys.
- Returns
(reversed
self.activity_dict
,self.product_dict
andself.biosphere_dict
)
- solve_linear_system()[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.
- to_dataframe(cutoff=200)[source]#
Return all nonzero elements of characterized inventory as Pandas dataframe