brightway-lca / matrix_utils

Utilities to build and iterate on matrices using datapackages
BSD 3-Clause "New" or "Revised" License
4 stars 2 forks source link
matrix numpy

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:

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

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, in another file, used to flip the signs of data elements before matrix insertion.

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:

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.