Numpy arrays with labeled axes, similar to xarray but with support for uncertainties
Project description
named-arrays
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
ScalarArray
, a named tensor implementation with Astropy Quantity support. Analogue ofxarray.Variable
UncertainScalarArray
, a named tensor implementation with automatic uncertainty propagation.Cartesian2dVectorArray
, representation of a 2D vector.Cartesian3dVectorArray
, representation of a 3D vector.FunctionArray
, representation of a discrete function. Analogue of anxarray.DataArray
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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file named_arrays-0.17.0.tar.gz
.
File metadata
- Download URL: named_arrays-0.17.0.tar.gz
- Upload date:
- Size: 195.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | e5e1f4c23db2eff0591eda62255f84498c2513e418a612e02b7709d2f5ac80c1 |
|
MD5 | 18d9bf2a281bf623949949b6b9892c81 |
|
BLAKE2b-256 | 838606b6f8ec2af30e9192d5fdf88faaef05555f8dd2d58f6fec6fccec388fe4 |
File details
Details for the file named_arrays-0.17.0-py3-none-any.whl
.
File metadata
- Download URL: named_arrays-0.17.0-py3-none-any.whl
- Upload date:
- Size: 193.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d11c12edc03cf0c180dc7b163d09e45950c16ab0caf8cd045263a0efc3c7ac15 |
|
MD5 | ebfad757e308fa7d63cb71b97b8806d4 |
|
BLAKE2b-256 | b0ba9ad5184ce93b8163e775935b59f5a0f3fac324359b3c4f35c3890502e712 |