Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/sun-data/named-arrays
Python library that implements named tensors with Astropy Quantity support.
https://github.com/sun-data/named-arrays
astropy named-arrays named-tensor numpy python scientific-computing
Last synced: about 1 month ago
JSON representation
Python library that implements named tensors with Astropy Quantity support.
- Host: GitHub
- URL: https://github.com/sun-data/named-arrays
- Owner: sun-data
- Created: 2022-08-14T22:15:51.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2024-05-23T04:41:16.000Z (6 months ago)
- Last Synced: 2024-05-23T04:45:41.737Z (6 months ago)
- Topics: astropy, named-arrays, named-tensor, numpy, python, scientific-computing
- Language: Python
- Homepage:
- Size: 1.08 MB
- Stars: 1
- Watchers: 1
- Forks: 1
- Open Issues: 2
-
Metadata Files:
- Readme: README.ipynb
Awesome Lists containing this project
README
{
"cells": [
{
"cell_type": "markdown",
"source": [
"# named-arrays\n",
"\n",
"[![tests](https://github.com/sun-data/named-arrays/actions/workflows/tests.yml/badge.svg)](https://github.com/sun-data/named-arrays/actions/workflows/tests.yml)\n",
"[![codecov](https://codecov.io/gh/sun-data/named-arrays/graph/badge.svg?token=1GhdcsgwO0)](https://codecov.io/gh/sun-data/named-arrays)\n",
"[![Documentation Status](https://readthedocs.org/projects/named-arrays/badge/?version=latest)](https://named-arrays.readthedocs.io/en/latest/?badge=latest)\n",
"[![PyPI version](https://badge.fury.io/py/named-arrays.svg)](https://badge.fury.io/py/named-arrays)\n",
"\n",
"`named-arrays` is an implementation of a [named tensor](https://nlp.seas.harvard.edu/NamedTensor), which assigns names to each axis of an n-dimensional array such as a numpy array.\n",
"\n",
"When using a Numpy n-dimensional array, the programmer must manually keep track of the physical meaning of each axis in the array.\n",
"Furthermore, it is often necessary to insert singleton dimensions at the end of the array to allow it to broadcastable against other arrays.\n",
"Named tensors solve this problem by giving each axis a name, which allows for automatic axis alignment without the need for inserting extra dimensions.\n",
"`named-arrays` provides a very unapologetic implementation of a named tensor, since axes can _only_ be accessed using their names,\n",
"unlike [`xarray`](https://github.com/pydata/xarray) which allows for both name and index.\n",
"\n",
"## Features\n",
"\n",
" - [`ScalarArray`](https://named-arrays.readthedocs.io/en/latest/_autosummary/named_arrays.ScalarArray.html), a named tensor implementation with Astropy Quantity support. Analogue of [`xarray.Variable`](https://docs.xarray.dev/en/latest/generated/xarray.Variable.html)\n",
" - [`UncertainScalarArray`](https://named-arrays.readthedocs.io/en/latest/_autosummary/named_arrays.UncertainScalarArray.html), a named tensor implementation with automatic uncertainty propagation.\n",
" - [`Cartesian2dVectorArray`](https://named-arrays.readthedocs.io/en/latest/_autosummary/named_arrays.Cartesian2dVectorArray.html), representation of a 2D vector.\n",
" - [`Cartesian3dVectorArray`](https://named-arrays.readthedocs.io/en/latest/_autosummary/named_arrays.Cartesian3dVectorArray.html), representation of a 3D vector.\n",
" - [`FunctionArray`](https://named-arrays.readthedocs.io/en/latest/_autosummary/named_arrays.FunctionArray.html), representation of a discrete function. Analogue of an [`xarray.DataArray`](https://docs.xarray.dev/en/latest/generated/xarray.DataArray.html#xarray.DataArray)\n"
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "markdown",
"source": [
"## Installation\n",
"`named-arrays` is available on PyPi and can be installed using pip\n",
"```bash\n",
"pip install named-arrays\n",
"```"
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "markdown",
"source": [
"## Examples\n",
"\n",
"### ScalarArray\n",
"The fundamental type of `named-arrays` is the [`ScalarArray`](https://named-arrays.readthedocs.io/en/latest/_autosummary/named_arrays.ScalarArray.html), 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."
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"execution_count": null,
"outputs": [],
"source": [
"import numpy as np\n",
"import named_arrays as na\n",
"\n",
"a = na.ScalarArray(np.array([1, 2, 3]), axes=('x',))"
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "markdown",
"source": [
"If we create another array with a different axis name, it will be broadcasted automatically against the first array if we add them together"
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"execution_count": null,
"outputs": [],
"source": [
"b = na.ScalarArray(np.array([4, 5]), axes=('y',))\n",
"c = a + b\n",
"c"
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "markdown",
"source": [
"All the usual numpy reduction operations use the axis name instead of the axis index"
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"execution_count": null,
"outputs": [],
"source": [
"c.mean('x')"
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "markdown",
"source": [
"To index the array we can use a dictionary with the axis names as the keys"
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"execution_count": null,
"outputs": [],
"source": [
"c[dict(x=0)]"
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "markdown",
"source": [
"### ScalarLinearSpace\n",
"We recommend that you rarely directly create instances of [`ScalarArray`](https://named-arrays.readthedocs.io/en/latest/_autosummary/named_arrays.ScalarArray.html) directly. Instead, you can use the implicit array classes: [`ScalarLinearSpace`](https://named-arrays.readthedocs.io/en/latest/_autosummary/named_arrays.ScalarLinearSpace.html), [`ScalarLogarithmicSpace`](https://named-arrays.readthedocs.io/en/latest/_autosummary/named_arrays.ScalarLogarithmicSpace.html), and [`ScalarGeometricSpace`](https://named-arrays.readthedocs.io/en/latest/_autosummary/named_arrays.ScalarGeometricSpace.html) to create arrays in a similar fashion to [`numpy.linspace()`](https://numpy.org/doc/stable/reference/generated/numpy.linspace.html), [`numpy.logspace()`](https://numpy.org/doc/stable/reference/generated/numpy.logspace.html#numpy.logspace), and [`numpy.geomspace()`](https://numpy.org/doc/stable/reference/generated/numpy.geomspace.html#numpy.geomspace) with the advantage of being able to access the inputs to these functions at a later point."
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"execution_count": null,
"outputs": [],
"source": [
"d = na.ScalarLinearSpace(0, 1, axis='z', num=4)\n",
"d"
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "markdown",
"source": [
"Thses implicit array classes work just like `ScalarArray` and can be used with any of the usual array operations."
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"execution_count": null,
"outputs": [],
"source": [
"a + d"
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"execution_count": null,
"outputs": [],
"source": [],
"metadata": {
"collapsed": false
}
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.6"
}
},
"nbformat": 4,
"nbformat_minor": 0
}