# bw2calc.single_matrix#

## Module Contents#

### Classes#

 SingleMatrixLCA An LCA which puts everything into one matrix.

### Attributes#

class bw2calc.single_matrix.SingleMatrixLCA(demand, data_filepath, log_config=None, presamples=None, seed=None, override_presamples_seed=False)[source]#

Bases: object

An LCA which puts everything into one matrix.

Comes with advantages and disadvantages, and designed exclusively for BONSAI via beebee.

Create a new single-matrix 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}.

Returns

A new SingleMatrixLCA object

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

calculate(factorize=False, builder=SingleMatrixBuilder)[source]#

Calculate an LCA score.

Creates self.supply_array, a vector of activities, flows, and characterization pathways which satisfy the demand.

Creates self.scores, a dictionary of {'LCIA identifier': LCA score}.

Create self.contributions, a dictionary of {'LCIA identifier': []}.

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 (*) – Custom matrix builders can be used to manipulate data in creative ways before building the matrices.

Doesn’t return anything.

calculate_scores()[source]#
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 deleting self.solver when doing these types of advanced calculations.

fix_dictionaries(row_mapping, col_mapping)[source]#

Fix the row and column dictionaries from {integer: row/col index} to {label: row/col index}.

lcia(*args, **kwargs)[source]#

Load beebee export data package.

This is a compressed file which contains:

• A stats_arrays file used to create the single matrix.

• A mapping dictionary from meaningful labels to the integer row ids

• A mapping dictionary from meaningful labels to the integer column ids

• A mapping dictionary from {"method URI": {labels}} which allows for LCIA sums

rebuild_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_calculate(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, and self.inventory.

Warning

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

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 and self.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.