Core

The core module introduces the key building blocks of the representations zounds deals in: ArrayWithUnits, a numpy.ndarray-derived class that supports semantically meaningful indexing, and Dimension, a common base class for custom, user-defined dimensions.

Numpy Arrays with Semantically Meaningful Indexing

class zounds.core.ArrayWithUnits[source]

ArrayWithUnits is an numpy.ndarray subclass that allows for indexing by more semantically meaningful slices.

It supports most methods on numpy.ndarray, and makes a best-effort to maintain meaningful dimensions throughout those operations.

Parameters:
  • arr (ndarray) – The numpy.ndarray instance containing the raw data for this instance
  • dimensions (list or tuple) – list or tuple of Dimension-derived classes
Raises:

ValueError – when arr.ndim and len(dimensions) do not match

Examples

>>> from zounds import ArrayWithUnits, TimeDimension, Seconds, TimeSlice
>>> import numpy as np
>>> data = np.zeros(100)
>>> awu = ArrayWithUnits(data, [TimeDimension(Seconds(1))])
>>> sliced = awu[TimeSlice(Seconds(10))]
>>> sliced.shape
(10,)
classmethod from_example(data, example)[source]

Produce a new ArrayWithUnits instance given some raw data and an example instance that has the desired dimensions

Custom Dimensions

class zounds.core.Dimension[source]

Common base class representing one dimension of a numpy array. Sub-classes can define behavior making custom slices (e.g., time spans or frequency bands) possible.

Implementors are primarily responsible for determining how custom slices are transformed into integer indexes and slices that numpy can use directly.

metaslice(index, size)[source]

Produce a new instance of this dimension, given a custom slice

integer_based_slice(index)[source]

Subclasses define behavior that transforms a custom, user-defined slice into integer indices that numpy can understand

Parameters:index (custom slice) – A user-defined slice instance
validate(size)[source]

Subclasses check to ensure that the dimensions size does not validate any assumptions made by this instance

class zounds.core.IdentityDimension[source]

A custom dimension that does not transform indices in any way, simply acting as a pass-through.

Examples

>>> from zounds import ArrayWithUnits, IdentityDimension
>>> import numpy as np
>>> data = np.zeros(100)
>>> arr = ArrayWithUnits(data, [IdentityDimension()])
>>> sliced = arr[4:6]
>>> sliced.shape
(2,)
integer_based_slice(index)[source]

Subclasses define behavior that transforms a custom, user-defined slice into integer indices that numpy can understand

Parameters:index (custom slice) – A user-defined slice instance