bw2calc.matrices#

Module Contents#

Classes#

MatrixBuilder

The class, and its subclasses, load structured arrays, manipulate them, and generate SciPy sparse matrices.

SingleMatrixBuilder

Subclass of MatrixBuilder that supports consumption (i.e. multiply by -1).

TechnosphereBiosphereMatrixBuilder

Subclass of MatrixBuilder that separates technosphere and biosphere parameters

class bw2calc.matrices.MatrixBuilder[source]#

Bases: object

The class, and its subclasses, load structured arrays, manipulate them, and generate SciPy sparse matrices.

Matrix builders use an array of row indices, an array of column indices, and an array of values to create a coordinate (coo) matrix, which is then converted to a compressed sparse row (csr) matrix.

See the following for more information on structured arrays:

These classes are not instantiated, and have only classmethods. They are not really true classes, but more organizational. In other words, you should use:

MatrixBuilder.build(args)

and not:

mb = MatrixBuilder()
mb.build(args)
classmethod build(paths, data_label, row_id_label, row_index_label, col_id_label=None, col_index_label=None, row_dict=None, col_dict=None, one_d=False, drop_missing=True)[source]#

Build a sparse matrix from NumPy structured array(s).

See more detailed documentation at building-matrices.

This method does the following:

TODO: Update

  1. Load and concatenate the structured arrays files in filepaths paths using the function utils.load_arrays() into a parameter array.

  2. If not row_dict, use build_dictionary() to build row_dict from the parameter array column row_id_label.

  3. Using the row_id_label and the row_dict, use the method add_matrix_indices() to add matrix indices to the row_index_label column.

  4. If not one_d, do the same to col_dict and col_index_label, using col_id_label.

  5. If not one_d, use build_matrix() to build a sparse matrix using data_label for the matrix data values, and row_index_label and col_index_label for row and column indices.

  6. Else if one_d, use build_diagonal_matrix() to build a diagonal matrix using data_label for diagonal matrix data values and row_index_label as row/column indices.

  7. Return the loaded parameter arrays from step 1, row and column dicts from steps 2 & 4, and matrix from step 5 or 6.

Parameters
  • paths (*) – List of array filepaths to load.

  • data_label (*) – Label of column in parameter arrays with matrix data values.

  • row_id_label (*) – Label of column in parameter arrays with row ID values, i.e. the integer values returned from mapping.

  • row_index_label (*) – Label of column in parameter arrays where matrix row indices will be stored.

  • col_id_label (*) – Label of column in parameter arrays with column ID values, i.e. the integer values returned from mapping. Not needed for diagonal matrices.

  • col_index_label (*) – Label of column in parameter arrays where matrix column indices will be stored. Not needed for diagonal matrices.

  • row_dict (*) – Mapping dictionary linking row_id_label values to row_index_label values. Will be built if not given.

  • col_dict (*) – Mapping dictionary linking col_id_label values to col_index_label values. Will be built if not given.

  • one_d (*) – Build diagonal matrix.

  • drop_missing (*) – Remove rows from the parameter array which aren’t mapped by row_dict or col_dict. Default is True. Advanced use only.

Returns

A numpy parameter array, the row mapping dictionary, the column mapping dictionary, and a COO sparse matrix.

classmethod build_diagonal_matrix(array, row_dict, index_label, data_label=None, new_data=None)[source]#

Build diagonal sparse matrix.

classmethod build_matrix(array, row_dict, col_dict, row_index_label, col_index_label, data_label=None, new_data=None)[source]#

Build sparse matrix.

class bw2calc.matrices.SingleMatrixBuilder[source]#

Bases: MatrixBuilder

Inheritance diagram of bw2calc.matrices.SingleMatrixBuilder

Subclass of MatrixBuilder that supports consumption (i.e. multiply by -1).

classmethod build(path)[source]#

Build the technosphere and biosphere sparse matrices.

classmethod build_single_matrix(array, row_dict, col_dict, new_data=None)[source]#
classmethod fix_supply_use(array, vector)[source]#

Make technosphere inputs negative.

class bw2calc.matrices.TechnosphereBiosphereMatrixBuilder[source]#

Bases: MatrixBuilder

Inheritance diagram of bw2calc.matrices.TechnosphereBiosphereMatrixBuilder

Subclass of MatrixBuilder that separates technosphere and biosphere parameters

classmethod build(paths)[source]#

Build the technosphere and biosphere sparse matrices.

classmethod build_technosphere_matrix(array, activity_dict, product_dict, new_data=None)[source]#
classmethod fix_supply_use(array, vector)[source]#

Make technosphere inputs negative.

classmethod get_biosphere_inputs_mask(array)[source]#

Get boolean mask of biosphere flows from array (i.e. the ones to include when building the biosphere matrix).

classmethod get_technosphere_inputs_mask(array)[source]#

Get boolean mask of technosphere inputs from array (i.e. the ones to include when building the technosphere matrix).

classmethod select_biosphere_array(array)[source]#

Create a new array with biosphere matrix exchanges

classmethod select_technosphere_array(array)[source]#

Create a new array with technosphere matrix exchanges