Skip to main content

GA Matrix

CI PyPI Python

GA Matrix provides small NumPy-backed matrix containers with label-based access, long-form pandas import/export helpers, and optional timestamp support for origin-destination data.

The PyPI distribution is named ga-matrix; the import package is named matrix.

Installation

python -m pip install ga-matrix

Development and test tools are available as extras:

python -m pip install -e ".[test]"
python -m pip install -e ".[dev]"

Quick Start

from matrix import MatrixOD

rows = ["A", "B"]
cols = ["X", "Y"]

od = MatrixOD(rows, cols, init={"A": {"X": 10}, "B": {"Y": 5}})
od["A", "Y"] = 3

assert od["A", "X"] == 10
assert od.sum() == 18

MatrixOD

MatrixOD stores a 2D origin-destination matrix. Rows and columns can be passed as label sequences or existing label-to-position mappings.

import pandas as pd

from matrix import MatrixOD

df = pd.DataFrame(
    [
        {"origin": "A", "destination": "X", "trips": 10},
        {"origin": "B", "destination": "Y", "trips": 5},
    ]
)

od = MatrixOD.read_df(
    rows=["A", "B"],
    cols=["X", "Y"],
    df=df,
    o_field="origin",
    d_field="destination",
    value_field="trips",
)

roundtrip = od.write_df(o_field="origin", d_field="destination", value_field="trips")

Supported operations are element-wise addition, subtraction, multiplication, and division with either a scalar or another matrix with the same labels.

scaled = od * 1.2
delta = scaled - od
col_totals = od.sum(axis=0)
row_totals = od.sum(axis=1)

MatrixODT

MatrixODT stores one MatrixOD per timestamp.

from matrix import MatrixODT

odt = MatrixODT(
    rows=["A", "B"],
    cols=["X", "Y"],
    timestamps=[0, 1],
    init={
        0: {"A": {"X": 10}},
        1: {"B": {"Y": 5}},
    },
)

assert odt["A", "X", 0] == 10
assert odt["A", "X", 99] == 0
assert odt.sum(axis=2)["A", "X"] == 10

MatrixODT.read_df() accepts a long-form DataFrame with origin, destination, timestamp, and value columns. If timestamps is omitted, timestamp labels are inferred from the DataFrame in first-seen order.

LabeledMatrix

LabeledMatrix is a more generic 2D labeled array with .loc and .iloc indexers:

from matrix import LabeledMatrix

table = LabeledMatrix(
    [[1, 2], [3, 4]],
    row_index=["a", "b"],
    col_index=["x", "y"],
)

assert table.loc["b", "y"] == 4

API Summary

  • MatrixOD(rows, cols, init=None, copy=False, mode=None)
  • MatrixOD.read_df(rows, cols, df, o_field="o", d_field="d", value_field="value")
  • MatrixOD.read_csv(rows, cols, file, ...)
  • MatrixOD.write_df(...)
  • MatrixOD.write_csv(file, ...)
  • MatrixODT(rows, cols, timestamps, init=None, copy=False, mode=None)
  • MatrixODT.read_df(rows, cols, timestamps=None, df=None, ...)
  • MatrixODT.read_csv(rows, cols, file, timestamps=None, ...)
  • MatrixODT.write_df(...)
  • MatrixODT.write_csv(file, ...)
  • LabeledMatrix(data, row_index=..., col_index=..., dtype=None, copy=False)

Development

GA Matrix supports Python 3.10 and newer.

python -m pip install -e ".[dev]"
python -m compileall -q src
python -m pytest --cov=matrix --cov-report=term-missing
ruff check .
mypy
python -m pip check
python -m build
python -m twine check dist/*

Releases

src/matrix/_version.py is the only version source. To publish a release:

  1. Update __version__ in _version.py and commit the release changes.
  2. Push main and wait for CI to pass.
  3. Configure the PyPI Trusted Publisher with project ga-matrix, owner andreagemma, repository matrix, workflow release.yml, and environment pypi.
  4. Run the Create release GitHub Actions workflow. With no override it creates the v<version> tag, creates release notes, and dispatches the build and PyPI publication workflow.

PyPI versions are immutable. Increment _version.py before publishing different content.

License

GA Matrix is distributed under the MIT License. See LICENSE.

Release files for ga-matrix 0.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 ga-matrix 0.1.0
File Size Uploaded
ga_matrix-0.1.0.tar.gz 15.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for ga-matrix 0.1.0
File Interpreter ABI Platform
ga_matrix-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 29.7 kB

Release files / ga_matrix-0.1.0.tar.gz

Download URL ga_matrix-0.1.0.tar.gz
Size 15.8 kB
Tags Source
SHA-256 checksum
How to use checksums
520572a5494b245f9e3faac65906f4e52a22a044d98d9fd963a83348e86da589
BLAKE2b-256 checksum
How to use checksums
e677007eb4516599a245c49d4ad65717ab8819a682b31000a7cc3aaa00102430
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

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 Sep 1, 2026.

Transparency log

Release files / ga_matrix-0.1.0-py3-none-any.whl

Download URL ga_matrix-0.1.0-py3-none-any.whl
Size 14.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
5d745a43e62266eda3d1b89d9e72eff723c8b06d07d5d096ce81818c9aee7de2
BLAKE2b-256 checksum
How to use checksums
59b402a7e0b88dab459b7ab0d89331766aa2b625095ee71732159a9966e505be
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

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 Sep 1, 2026.

Transparency log

Release history Release notifications | RSS feed

0.1.2

2 release files

0.1.1

2 release files

This release

0.1.0 This release

2 release files

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