Working with matrices#
AequilibraE provides three objects for working with named matrices:
AequilibraeMatrixholds matrices in memory and is backed by a dictionary. Prefer this object for dense calculations and other in-memory work.AequilibraeSparseMatrixholds SciPy sparse arrays in memory and keeps their sparse formats and dtypes.MatrixStoreis backed by an OMX file. Use it to read and update dense and sparse matrices on disk through a similar interface to the in-memory objects. Sparse matrices use a CSR extension to OMX.
All three objects associate names with square matrices. All matrices in an object
share the same zone index, which gives the zone IDs in row and column order.
Use the from_file(), from_dict() and from_df()
functions to create the appropriate object from your source data, or the
AequilibraeMatrix constructor directly.
These objects replace the previous AequilibraeMatrix which mixed both
roles. For a migration guide, see Migrating to the matrix API.
AequilibraeMatrix, AequilibraeSparseMatrix and MatrixStore
implement the MutableMapping protocol and behave
very similarly to a dictionary. You can use mapping operations including
.keys(), .values(), .items() and .update().
Create an in-memory working set#
The recommended workflow is to open an OMX file with from_file(...,
subset=[...]), which returns an AequilibraeMatrix or an
AequilibraeSparseMatrix. If you don’t know
which matrices you need ahead of time, provide store=True to from_file
instead of subset to obtain a MatrixStore. Inspect the file, then use
its subset() method to select the matrices needed for a calculation. It
returns a new, independent in-memory collection of one storage type. Work on
that object in memory, then save the result when needed.
Both from_file() and MatrixStore.subset() infer the storage type from
the first selected matrix when sparse=None (the default). Pass
sparse=True or False to require sparse or dense matrices. This option
does not filter or convert matrices.
The following example assumes that demand.omx contains matrices named
car and bus and a zone mapping named zones. index_name is
optional when the file has exactly one zone mapping. If it has multiple
mappings, from_file() selects main_index when present, otherwise, pass
index_name explicitly.
from aequilibrae.matrix import from_file
working = from_file("demand.omx", index_name="zones", subset=["car", "bus"])
working["car"] *= 1.1
working["total"] = working["car"] + working["bus"]
working.save_as_omx("working_demand.omx")
from aequilibrae.matrix import from_file
with from_file("demand.omx", index_name="zones", store=True) as store:
print(list(store)) # Prints all matrices, include "car" and "bus"
working = store.subset(["car", "bus"])
working["car"] *= 1.1
working["total"] = working["car"] + working["bus"]
working.save_as_omx("working_demand.omx")
The working set remains available after the store closes. Changes to it do not
change the source file. subset() copies the zone index, file-level metadata
and per-matrix attributes as well as the specified matrices. Metadata values
are copied as well. Missing names raise KeyError. When selecting from a
file, the error includes a hint if the name exists in the other storage group.
This applies to store subsets, loading subsets and saving subsets from a store.
Use MatrixStore for manipulating files rather than working with it as if it
was in-memory. It reads the matrix from disk each time it is accessed. For this
reason, arrays obtained from it are copies. Mutating them changes the copy
rather than the file itself. Assign through .mat or .imat to change
the file (see Read and change values below).
Create matrices in memory#
Use from_dict() when arrays are already available:
import numpy as np
from aequilibrae.matrix import AequilibraeMatrix, from_dict
index = [101, 205, 309]
car = np.array([[0, 12, 8], [10, 0, 6], [7, 9, 0]], dtype=float)
bus = np.array([[0, 2, 1], [2, 0, 3], [4, 5, 0]], dtype=float)
demand = from_dict(
{"car": car, "bus": bus},
index=index,
index_name="zones",
)
demand.metadata.update({"description": "Morning demand", "year": 2030})
Dense array-likes, including nested lists, are accepted. Sparse inputs return
an AequilibraeSparseMatrix and keep each array’s format and dtype. Use an
AequilibraeMatrix or AequilibraeSparseMatrix constructor to get an
empty version.
Alternatively, start with no named matrices and add them using AequilibraeMatrix directly:
empty = AequilibraeMatrix(index=index, index_name="zones")
empty["car"] = np.zeros(empty.shape)
empty.update({"bus": bus})
Matrix assignment accepts array-like values with the object’s square shape.
Matrix values must use integer, floating-point, boolean or complex dtypes.
This limit does not apply to metadata. Whole-matrix assignment uses the
supplied dtype, even when replacing an existing matrix. AequilibraeMatrix
copies supplied arrays so changing an array supplied earlier does not change
the matrix.
Inspect and manage named matrices#
Matrix names must be non-empty strings, not paths. Names cannot contain a slash,
backslash or null character, and cannot be . or ...
Use normal dictionary operations to inspect names, add matrices or remove them:
print(list(demand))
print(len(demand))
print(demand.shape)
print(demand.index)
if "car" in demand:
print(demand["car"].sum())
demand["total"] = demand["car"] + demand["bus"]
for name, values in demand.items():
print(name, values.sum())
del demand["total"]
len(demand) returns the number of matrices. demand.shape is the shape
of each matrix. On a store, reading values through items() or values()
loads each matrix as it is read. Iterating over names does not load their
values.
clear() removes all named matrices and their attributes without reading
matrix values. It keeps the zone index and file-level metadata.
setdefault(name, values) adds the supplied matrix only if the name is
missing, then returns the matrix that would be read for that key. Unlike
dict.setdefault, the default of values=None is not a valid value.
Equality (==) compares matrix names and values, zone IDs, the index name,
file-level metadata and per-matrix attributes. NaNs are considered
equal. Matrix insertion order and numeric dtypes do not affect equality. The
OMX-managed file attributes listed below are ignored. For a MatrixStore,
the values must be read from disk.
Read and change values#
Use a single string name to read or replace a whole matrix. For selections, use
.imat with zero-based positions or .mat with zone IDs. Both indexers
accept a name, a name and row, or a name, row and column:
# all the same
values = demand["car"]
values = demand.mat["car"]
values = demand.imat["car"]
cell = demand.imat["car", 0, 1]
column = demand.imat["car", :, 1]
selected_rows = demand.imat["car", [0, 2]]
demand.imat["car", 0, 1] = 42
demand.imat["car", :, 2] = [1, 2, 3]
demand.imat["car", :2, :2] = [[0, 5], [6, 0]]
demand.imat["bus", :, :] *= 1.05
In this example, position 0 represents zone 101. Zone selections use
these IDs instead of positions:
cell = demand.mat["car", 101, 205]
selected_rows = demand.mat["car", 101:205]
pairs = demand.mat["car", [101, 205], [205, 309]]
demand.mat["car", [101, 205], [205, 309]] = [10, 20]
Label slices follow the stored zone order and include the stop label to allow
simple assess to a range of zones. Steps apply to positions after translating
the endpoints, not the zone IDs. Unknown zone IDs, including slice endpoints,
raise a KeyError.
Key-only assignments such as demand.mat["car"] = values replace or create a
whole matrix and require its full shape. Assignments to selections such as
demand.mat["car", :, :] = 0 use the backend’s broadcasting rules.
When working with an AequilibraeMatrix, selections follow NumPy
indexing. For dense matrices in a MatrixStore, selections follow the indexing
supported by the OMX (and h5py) library, which may have different restrictions
on fancy selections. Selections are passed to the underlying object after
translating zone IDs, so support for a selection depends on that object. This
allows for partial reads and writes to and from matrices both in memory and on
disk. Assignments to a selection keep the matrix’s dtype and use NumPy or h5py
conversion rules. Sparse matrices in a MatrixStore only support whole-matrix
reads and writes. Selections through mat or imat raise NotImplementedError.
Whole-matrix reads and basic slices of an AequilibraeMatrix may share its
memory, fancy NumPy selections may return copies. If an independent array is
needed, use demand["car"].copy().
Reads from a MatrixStore always returns a copy. To write changes back,
assign them through store.mat or store.imat rather than modifying an
array returned by a read.
Work with the zone index#
The index must be a one-dimensional sequence of unique, non-negative integer
zone IDs from 0 to 2**64 - 1. IDs are stored in memory as uint64.
Booleans and floating-point values are not accepted, even floats such as
1.0. The same index applies to origins and destinations. The default index
name is main_index. Pass index_name when a file uses another mapping
name to read it instead. Index names follow the same rules as matrix names:
they must be non-empty strings, not paths.
index is read-only when retrieved. Replace it as a whole to relabel zones:
demand.index = [1001, 1002, 1003]
The replacement must have the same length. Relabelling does not reorder matrix values. To change the number of zones, create a new object with the required index and matrices.
Opening a store with an existing mapping does not rewrite it unless an index is supplied. Supplying an index requires a writable store. Replacing its index keeps the mapping’s dtype and attributes.
Read and update files#
from_file(..., store=True) opens an existing file as read-only by default.
Only mode="r" and mode="r+" are allowed as the file must already
exist. Use mode="r+" to update an existing file, and a with block to
close it when finished:
with from_file(
"demand.omx", index_name="zones", mode="r+", store=True
) as store:
first_row = store.imat["car", 0]
store.imat["car", 0, 1] = 42
store.imat["bus", :, 2] = [1, 2, 3]
store.metadata["description"] = "Updated morning demand"
These assignments update the file directly because they use .imat. No
separate save_as_omx() call is needed. store["car"][:, 2] = ... would
only change the returned array, not the file. Use store.imat["car", :, 2] =
... instead.
Open a MatrixStore with a path, index and writable mode, then use
update() to populate it from arrays. mode="x" prevents replacement
of an existing file:
from aequilibrae.matrix import MatrixStore
with MatrixStore("new_demand.omx", index=index, index_name="zones", mode="x") as store:
store.update({"car": car, "bus": bus})
print(list(store))
update() accepts both dense and sparse arrays.
A store must stay open while it is being used. If a with block is not
suitable, close it explicitly with store.close(). An AequilibraeMatrix
does not need to be closed.
To load a whole OMX file into memory, omit store=True and
subset=[...]. from_file() returns an AequilibraeMatrix by default:
from aequilibrae.matrix import from_file
loaded = from_file("demand.omx", index_name="zones")
with from_file("demand.omx", index_name="zones", store=True) as store:
working = store.subset(["car", "bus"])
Providing subset and store=True at the same time is not supported. If
using store=True, use .subset() to load select matrices into a new
AequilibraeMatrix object.
Save matrices and metadata#
Use save_as_omx() to save a matrix. The default mode="x" creates a new
file and raises FileExistsError if the file already exists. Save all named
matrices, or use subset to select them by name or by their attributes:
demand.metadata["description"] = "Adjusted morning demand"
demand.save_as_omx("updated_demand.omx") # All matrices
demand.save_as_omx("car_demand.omx", subset=["car"])
Attribute queries use the source object’s matrix attributes. You can mix names and queries, and each selected matrix is saved once:
demand.matrix_metadata("car")["mode"] = "car"
demand.save_as_omx("car_by_mode.omx", subset=[{"mode": "car"}])
demand.save_as_omx("selected_demand.omx", subset=["bus", {"mode": "car"}])
All attributes in a query must match. If no matrices match, the zone mapping and file metadata are still written.
OMX manages the file attributes OMX_VERSION, OMX_CREATED_WITH,
SHAPE and TITLE. They are ignored when loading, copying, saving or
comparing file metadata. A store still exposes them through metadata, which
is backed by h5py. Do not change them directly.
The file includes the zone mapping, file metadata and matrix attributes. Use
mode to choose how to write the file:
"x"or"w-": create a new file, failing if it already exists."r+": update an existing file, failing if it does not exist."a": update an existing file, or create it if it does not exist."w": replace the whole file, or create it if it does not exist.
demand.save_as_omx("updated_demand.omx", mode="r+")
The zone mapping and supplied metadata are always written, be careful when
updating existing files. Saving selected matrices with mode="r+" or
mode="a" does not remove other matrices from the file. In contrast,
mode="w" removes all previous contents, even when saving a subset. When
overwriting a matrix, new attributes will replace matching attributes in the
file. Other attributes on that matrix are not modified.
Use matrix_metadata(name) to read or change attributes for an existing matrix:
demand.matrix_metadata("car")["mode"] = "car"
demand.matrix_metadata("bus")["mode"] = "bus"
This returns a dictionary in memory and the h5py attributes for a store. Read-only stores cannot change attributes. For a store, call the method again after changing a matrix’s dtype to get the new dataset’s attributes.
For both metadata and matrix_metadata(name), replace each attribute
value as a whole. Nested edits, such as demand.metadata["periods"][0] = 9,
are not supported.
demand.metadata["periods"] = [1, 2, 3]
demand.matrix_metadata("car")["periods"] = [1, 2]
Attributes are saved with their matrices:
demand.save_as_omx("tagged_demand.omx")
Matrix attributes are translated directly to OMX (and thus HDF5) attributes for the matrix datasets. See the h5py attributes documentation <https://docs.h5py.org/en/stable/high/attr.html> for more information.
Both objects can use these attributes to select an in-memory working set using attribute queries:
with from_file("tagged_demand.omx", index_name="zones", store=True) as store:
car_demand = store.subset([{"mode": "car"}])
column = car_demand.imat["car", :, 1]
print(store.matrix_metadata("car")["mode"])
car_demand = demand.subset([{"mode": "car"}])
All attributes within a query must match, and multiple queries select the combined matches, with each matrix included once.
Attribute queries are supported by subset() and the subset arguments
when loading or saving matrices. They are not supported by store[...],
.mat or .imat. subset() creates a new in-memory
AequilibraeMatrix with all matrices that matched the query. Use
store.omx for advanced OMX-specific operations, or store.omx["/"] to
access the underlying h5py root dataset.
Create matrices from tabular or sparse data#
Use from_df() for an origin-destination table:
import pandas as pd
from aequilibrae.matrix import from_df
trips = pd.DataFrame(
{"origin": [101, 101, 309], "destination": [205, 205, 101], "car": [2, 3, 7]}
)
demand_from_trips = from_df(
trips, row="origin", col="destination", subset=["car"],
index=[101, 205, 309], index_name="zones",
)
Repeated origin-destination pairs are summed. Missing pairs default to zero.
Use fill_value to specify another value. If no index is supplied, it is
inferred from the sorted unique origin and destination IDs. It’s highly
recommend to provide an explicit index in-order to include zones with no
trips. All origin and destination IDs must be present in that index. These IDs
must follow the zone-ID rules above, whether the index is supplied or inferred.
Use from_dict() when the input is a collection of SciPy sparse arrays:
from scipy.sparse import csr_array
from aequilibrae.matrix import from_dict
demand_from_sparse = from_dict(
{"car": csr_array(car)}, index=index, index_name="zones"
)
from_df() returns an AequilibraeMatrix by default. For sparse inputs,
from_dict() returns an AequilibraeSparseMatrix and copies the arrays
without changing their formats or dtypes. You can also create an empty sparse
collection with AequilibraeSparseMatrix(index=index) and assign named
sparse arrays. Whole-matrix reads return the stored sparse array. Selections
through mat and imat follow that sparse format’s SciPy indexing rules.
save_as_omx() converts sparse arrays to CSR for storage without changing
them in memory. Each matrix uses a group at /sparse/<name> with datasets
row_index (CSR row pointers), col_index (column positions) and
data. Other group attributes hold matrix metadata. Zone IDs remain under
/lookup. This is an extension to OMX, so standard OMX readers will not see
these sparse matrices.
Load these files with AequilibraeSparseMatrix.from_omx() or
from_file(..., sparse=True). The class method only reads /sparse and
ignores dense matrices in /data.
Work with sparse matrices on disk#
Use MatrixStore or from_file(..., store=True) to work with dense and
sparse matrices without loading the whole collection. Each sparse matrix read
returns an independent CSR array. Assign the whole array back to save
changes. Partial reads and writes are not supported for stored sparse matrices.
An existing matrix cannot change storage type so you should delete it before
assigning values of the other type. save_as_omx() can save mixed
selections, preserving each matrix’s storage type.
from aequilibrae.matrix import MatrixStore, from_file
with MatrixStore("sparse_demand.omx", index=index, index_name="zones", mode="x") as store:
store.update({"car": csr_array(car)})
store.matrix_metadata("car")["mode"] = "car"
with from_file("sparse_demand.omx", store=True, mode="r+") as store:
values = store["car"]
values *= 2
store["car"] = values
working = store.subset([{"mode": "car"}])
MatrixStore.update() writes sparse arrays as CSR and keeps their dtypes.
It accepts mixed dense and sparse values. Open a store with mode="x" to
create a new file and fail if the file already exists.
from_file(..., store=True) opens read-only unless mode="r+" is supplied.
Dense and sparse matrices share the store’s file metadata and zone mapping. A
store’s subset() returns an independent AequilibraeSparseMatrix when
selecting one or more sparse matrices, or an AequilibraeMatrix for dense or
empty selections. The result can be used after the store closes.