Skip to main content

Improved numpy typing anotations

Project description

This package adds cleaner typing to numpy arrays. It has been designed for deep learning and data processing tasks which generally need a lot of numpy arrays of large dimensions.

To use the library modify all your import with :

from numpy_typing import np, ax

The modified version of numpy imported there contain sumplementary annotations in order to have smart and automatic inferred annotation.

Remark : The numpy imported there it is not slower than the classical numpy (import numpy as np). When calling a numpy function, its code is directly called without any wrapper. Numpy-typing only overload numpy's function annotations.

You can then use new annotation like:

from numpy_typing import np, ax

float32Array3d:np.float32_3d[ax.batch, ax.sample, ax.feature] = np.zeros((3, 3, 3))
v = float32Array3d[0, 0, 0] # v automatically inferred as float32

The library add new array_types annotation:

  • np.float32_nd
  • np.float64_nd
  • np.int32_nd
  • np.int64_nd
  • np.int8_nd
  • np.bool_nd
  • np.str_nd

Where n is the number of dimension of the array. The value of n is for instance only supported between [1, 4].

Moreover, as the library dosen't support yet all the numpy types, you can also use the generic type np.array_(1-4)d[dtype, axis1, ...] without type.

Then you should specify for each dimension the role of the dimension:

  • ax.batch: The axis select the nth batch
  • ax.sample: The axis select the nth sample of the batch
  • ax.feature: The axis contain features
  • ax.time: The axis represent the time
  • ax.label: the axis contain labels
  • ax.x: The axis represent the x coordinate
  • ax.y: The axis represent the y coordinate
  • ax.z: The axis represent the z coordinate
  • ax.rgb: the axis contain a rgb value [0] for red, [1] for green and [2] for blue
  • ax.rgba: the axis contain a rgba value [0] for red, [1] for green, [2] for blue and [3] for alpha

Here is an example of useful smart annotation:

from numpy_typing import np, ax

a:np.float32_1d[ax.time] = np.zeros((64))
b:np.float32_1d[ax.time] = np.zeros((64))
c = np.concatenate([a, b])
# automatically infer the type of c as np.float32_1d[ax.time]

If you want the library to support more types, more numpy functions or if you have any suggestion, feel free to open an issue on our github.

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

numpy_typing-1.0.1.tar.gz (3.5 kB view hashes)

Uploaded Source

Supported by

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