Skip to main content

matrix_utils

Library for building matrices from data packages from bw_processing. Designed for use with the Brightway life cycle assessment framework.

Table of Contents

Background

The calculation library of the Brightway LCA framework has traditionally include matrix-building functionality. As the new capabilities in bw_processing have increased matrix-building complexity, this library is a refactoring to split matrix utilities from the LCA classes, which will remain in the calculation library.

matrix_utils supports all the features made available in bw_processing: static and dynamic resources, data package policies, vector and array resources. It also improves on the previous matrix building code by speeding up the mapping from data source ids to row and column ids.

Backwards compatibility

This library presents a completely different API than the functions previously present in bw2calc. Most ideas become easier, or even possible; however, some things are more complicated. In particular, the notion that we have a single array that defines a matrix is no longer true - a matrix can be defined by many input arrays, and they can interact with each other (either adding to existing matrix values or replacing them altogether).

Install

Install using pip or conda (channel cmutel).

Depends on numpy, scipy, pandas, bw_processing, stats_arrays.

Usage

MappedMatrix class

The primary use case for matrix_utils is the MappedMatrix class:

In [1]: from matrix_utils import MappedMatrix

In [2]: mm = MappedMatrix(packages=[some_datapackage], matrix="foo")

In [3]: mm.matrix
Out[3]:
<8x8 sparse matrix of type '<class 'numpy.float32'>'
    with 11 stored elements in Compressed Sparse Row format>

MappedMatrix takes the following arguments. Note that all arguments must be keyword arguments:

  • packages: list, required. List of bw_processing data packages. The packages must be instantiated, you can't give file locations of PyFilesystem2 file systems.
  • matrix: str, required. Label of matrix to build. Used to filter data in packages, so must be identical to the matrix value in the package(s).
  • use_vectors: bool, default True. Include vector data resources when building matrices.
  • use_arrays: bool, default True. Include array data resources when building matrices. Note that each data package resource group can only provide either vector or array data.
  • use_distributions: bool, default False. Include probability distribution data resources when building matrices.
  • row_mapper: matrix_utils.ArrayMapper, default None. Optional mapping class used to translate data source ids to matrix row indices. In LCA, one would reuse this mapping class to make sure the dimensions of multiple matrices align.
  • col_mapper: matrix_utils.ArrayMapper, default None. Optional mapping class used to translate data source ids to matrix column indices. In LCA, one would reuse this mapping class to make sure the dimensions of multiple matrices align.
  • seed_override: int, default None. Override the random seed given in the data package. Note that this is ignored if the data package is combinatorial.
  • derive_group_seeds: bool, default True. Give each resource group its own RNG seed for sampling from distributions, derived deterministically from the base seed (seed_override or the data package seed), the matrix label, the identifier (e.g. the MappedMatrixDict key), and the group label. Without this, all groups start from the same RNG state and can draw perfectly correlated samples (#50). Results stay reproducible, but a given seed gives different samples than in matrix_utils 0.9 and earlier; pass False to get the old behaviour. ResourceGroup.seed holds the derived seed. If several data packages have groups with the same label, their seeds also depend on the order of packages.

MappedMatrix is iterable; calling next() will draw new samples from all included stochastic resources, and rebuild the matrix.

Indexers

Each datapackage is assigned exactly one indexer, shared across all its resource groups. This indexer determines which column of an array resource is used on each iteration. The design assumption is one indexer per datapackage — MappedMatrix.indexers returns a {name: indexer} dict at that level. MappedMatrix.local_indexers returns a {group_label: indexer} dict for what each resource group is actually using, which will normally be the same object (or a Proxy wrapping it for combinatorial packages). You may replace group.indexer on any individual group after construction, but you are then responsible for the consequences — in particular, next() only advances the package-level indexers, so any custom group-level indexer must be advanced manually.

NaN as a sentinel value

A NaN value in a data vector or array is treated as "no data insertion" — that element is skipped when the matrix is built or rebuilt, leaving the matrix cell at its current value. This is useful for scenario or override packages: place NaN at positions you want to inherit from an earlier package, and a real number where you want to override. Non-NaN elements behave normally. ResourceGroup.data_current still carries the raw NaN so input_data_vector() reflects the actual datapackage contents.

You may also find it useful to iterate through MappedMatrix.groups, which are instances of ResourceGroup, documented below.

ResourceGroup class

A bw_processing data package is essentially a metadata file and a bag of data resources. These resources are grouped, for multiple resources are needed to build one matrix, or one component of one matrix. For example, one needs not only the data vector, but also the row and column indices to build a simple matrix. One could also have a flip vector (to negate signs before insertion) or a scale vector (to apply a per-exchange multiplicative factor before insertion, e.g. for allocation or unit conversion).

The ResourceGroup class provides a single interface to these data files and their metadata. ResourceGroup instances are created automatically by MappedMatrix, and available via MappedMatrix.groups. The source code is pretty readable, and in general you probably don't need to worry about this low-level class, but the following could be useful:

  • ResourceGroup.data_original: The data as it is present in the datapackage, before masking (i.e. the Numpy data vector or array, or the data interface). This is the raw input data, duplicate elements are not aggregated (if applicable).
  • ResourceGroup.data_current: The data sample used (before aggregation) to build the matrix. It is masked, flipped (if a flip array is present), and scaled (if a scale array is present).
  • ResourceGroup.scale: The scale array with all masks applied. Raises KeyError if no scale array is present.
  • ResourceGroup.row|col_mapped: Mapped row and column indices. Has the same length as the datapackage resource, but uses -1 for values which weren't mapped.
  • ResourceGroup.row|col_masked: The data after the custom filter and mapping mask have been applied.
  • ResourceGroup.row|col_matrix: Row and column indices (but not data) for insertion into the matrix. These indices are after aggregation within the resource group (if any).
  • ResourceGroup.calculate(vector=None): Function to recalculate matrix row, column, and data vectors. Uses the current state of the indexers, but re-draws values from data iterators. If vector is given, use this instead of the given data source.
  • ResourceGroup.indexer: The instance of the Indexer class applicable for this ResourceGroup. Only used for data arrays. By design each datapackage has one indexer shared across all its resource groups; this is set up automatically by MappedMatrix. You may assign a different indexer to individual groups after construction, but you are then responsible for keeping them consistent (e.g. ensuring they advance together when next() is called).
  • ResourceGroup.ncols: The integer number of columns in a data array. Returns None if a data vector is present.

Contributing

Your contribution is welcome! Please follow the pull request workflow, even for minor changes.

When contributing to this repository with a major change, please first discuss the change you wish to make via issue, email, or any other method with the owners of this repository.

Please note we have a code of conduct, please follow it in all your interactions with the project.

Documentation and coding standards

Maintainers

License

BSD-3-Clause. Copyright 2020 Chris Mutel.

Metadata

Release files for matrix-utils 1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for matrix-utils 1.0
File Size Uploaded Provenance Actions
matrix_utils-1.0.tar.gz 34.8 kB With provenance Details for matrix_utils-1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for matrix-utils 1.0
File Interpreter ABI Platform Actions
matrix_utils-1.0-py3-none-any.whl Python 3 none any Details for matrix_utils-1.0-py3-none-any.whl

Total release size: 63.2 kB

Release files / matrix_utils-1.0.tar.gz

Download URL matrix_utils-1.0.tar.gz
Size 34.8 kB
Tags Source
SHA-256 checksum
How to use checksums
46770b1233a750551386fa40c1681fcc982e521680624507db21b7017179dc4f
BLAKE2b-256 checksum
How to use checksums
e02e8e15b9b2d563417d10bcdc055a5988c56e7bd2df321a9d726f32af078fec
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 20, 2026.

Transparency log

Release files / matrix_utils-1.0-py3-none-any.whl

Download URL matrix_utils-1.0-py3-none-any.whl
Size 28.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
e0b7cfbf5ae51889476a8273eb8deafc7b7b6a652a94d0b3df16a4e1167fc484
BLAKE2b-256 checksum
How to use checksums
15d680fa871a0db088ed70a1babb443513b6c8c3ad47f57db6ee92553f5cc265
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 20, 2026.

Transparency log

Security for matrix-utils 1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for matrix_utils-1.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 20, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page