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 matchExamples
>>> 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
- arr (ndarray) – The
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.
-
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,)