nparray logo

High-Level Wrappers for Building and Manipulating Numpy Arrays

badge badge badge badge badge

Create numpy arrays (via arange, linspace, etc) and manipulate the creation arguments at any time. The created object acts as a numpy array but only stores the input parameters until its value is accessed.

The following snippet should give you a better idea of the purpose of nparray. Note that under-the-hood are actual numpy arrays, meaning passing directly to matplotlib works fine, but using isinstance or type will currently not recognize the numpy array (at least for now - see this issue).

import nparray as npa

a = npa.arange(0,1,0.1)
print(a)
# <arange start=0 stop=1 step=0.1>
print(a[-1])
# 0.9
a.step=0.5
print(a)
# <arange start=0 stop=1 step=0.5>
print(a[-1])
# 0.5
b = a.to_linspace()
print(b)
# <linspace start=0 stop=0.5 num=2, endpoint=True>
print(b*3)
# <linspace start=0, stop=1.5, num=2, endpoint=True>
b[1] = 99
print(b)
# <array value=[0, 99]>

Getting Started

Dependencies

nparray requires the following dependencies:

  • python 2.7+ or 3.6+
  • numpy 1.10+
  • collections (should be standard python module)

and the following optional dependencies:

  • astropy 1.0+ (required for units support)

You can see the Travis testing matrix for details on what exact versions have been tested and ensured to work. If you run into any issues with dependencies, please submit an issue.

Installation

nparray is available via pip:

pip install nparray

Alternatively, to install from source, use the standard python setup.py commands.

To install globally:

python setup.py build
sudo python setup.py install

Or to install locally:

python setup.py build
python setup.py install --user

Import

Now from within python we can import the nparray package:

import nparray

Supported Array Types

nparray currently supports the following with all arguments (except for dtype - see open issue):

Math on Array Objects

Whenever possible, math on nparray objects will attempt to return another nparray object of the same type. When not possible, an nparray.Array object will be return which only stores the underlying array value itself.

Comparing Array Objects

Comparison operators on nparray objects will act on the computed array object itself.

import nparray

a = nparray.linspace(0,1,10,False)
b = nparray.arange(0,1,0.1)
a==b

Support for Units

NOTE: astropy is required for units support.

import nparray
import astropy.units as u

a = nparray.linspace(0,1,11)

q = a * u.m

print(a)
print(q)
print(q.value)

MonkeyPatching Numpy

Optionally, you can monkeypatch numpy so that calling any of the numpy versions (np.array, np.linspace, etc), will actually call the nparray version instead.

import nparray
import numpy

nparray.monkeypatch()
print(np.linspace(0,1,11))

API Documentation

See the API documentation for full details on each type of supported array.

Contributors

Kyle Conroy

Contributions are welcome! Feel free to file an issue or fork and create a pull-request.