Skip to main content

Numpy arrays with labeled axes, similar to xarray but with support for uncertainties

Project description

named-arrays

tests codecov Ruff Documentation Status PyPI version

named-arrays is an implementation of a named tensor, which assigns names to each axis of an n-dimensional array such as a numpy array.

When using a Numpy n-dimensional array, the programmer must manually keep track of the physical meaning of each axis in the array. Furthermore, it is often necessary to insert singleton dimensions at the end of the array to allow it to broadcastable against other arrays. Named tensors solve this problem by giving each axis a name, which allows for automatic axis alignment without the need for inserting extra dimensions. named-arrays provides a very unapologetic implementation of a named tensor, since axes can only be accessed using their names, unlike xarray which allows for both name and index.

Features

Installation

named-arrays is available on PyPi and can be installed using pip

pip install named-arrays

Examples

ScalarArray

The fundamental type of named-arrays is the ScalarArray, which is a composition of a numpy ndarray-like object and a tuple of axis names which must have the same length as the number of dimensions in the array.

import numpy as np
import named_arrays as na

a = na.ScalarArray(np.array([1, 2, 3]), axes=('x',))

If we create another array with a different axis name, it will be broadcasted automatically against the first array if we add them together

b = na.ScalarArray(np.array([4, 5]), axes=('y',))
c = a + b
c
ScalarArray(
    ndarray=[[5, 6],
             [6, 7],
             [7, 8]],
    axes=('x', 'y'),
)

All the usual numpy reduction operations use the axis name instead of the axis index

c.mean('x')
ScalarArray(
    ndarray=[6., 7.],
    axes=('y',),
)

To index the array we can use a dictionary with the axis names as the keys

c[dict(x=0)]
ScalarArray(
    ndarray=[5, 6],
    axes=('y',),
)

ScalarLinearSpace

We recommend that you rarely directly create instances of ScalarArray directly. Instead, you can use the implicit array classes: ScalarLinearSpace, ScalarLogarithmicSpace, and ScalarGeometricSpace to create arrays in a similar fashion to numpy.linspace(), numpy.logspace(), and numpy.geomspace() with the advantage of being able to access the inputs to these functions at a later point.

d = na.ScalarLinearSpace(0, 1, axis='z', num=4)
d
ScalarLinearSpace(start=0, stop=1, axis='z', num=4, endpoint=True)

Thses implicit array classes work just like ScalarArray and can be used with any of the usual array operations.

a + d
ScalarArray(
    ndarray=[[1.        , 1.33333333, 1.66666667, 2.        ],
             [2.        , 2.33333333, 2.66666667, 3.        ],
             [3.        , 3.33333333, 3.66666667, 4.        ]],
    axes=('x', 'z'),
)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

named_arrays-1.3.0.tar.gz (236.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

named_arrays-1.3.0-py3-none-any.whl (242.8 kB view details)

Uploaded Python 3

File details

Details for the file named_arrays-1.3.0.tar.gz.

File metadata

  • Download URL: named_arrays-1.3.0.tar.gz
  • Upload date:
  • Size: 236.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for named_arrays-1.3.0.tar.gz
Algorithm Hash digest
SHA256 bcf9180f2005b9e3deaba1904055b4efaae7a626c9a5d72f869e262e2d6758e9
MD5 036541faad3d7b5b3d0c4d4434be4cb9
BLAKE2b-256 eabeff953360ed457309f8e72e844ae93046bb2cb3aa1d5acddcf81c73a95d80

See more details on using hashes here.

File details

Details for the file named_arrays-1.3.0-py3-none-any.whl.

File metadata

  • Download URL: named_arrays-1.3.0-py3-none-any.whl
  • Upload date:
  • Size: 242.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for named_arrays-1.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 0c2c034cbf39f04a8515accd116b5546f1aba5b7dfe499d3bec2f13931b75c75
MD5 9a788ef1c5cb638578bfe229462b7c82
BLAKE2b-256 1f3975b38225563969758845b4c3d481a6323019d1828b4cc4358378a5f81c89

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page