AxisArray module#
- class ezmsg.util.messages.axisarray.ArrayWithNamedDims(data, dims)[source]#
Bases:
objectBase class for arrays with named dimensions.
This class provides a foundation for arrays where each dimension has a name, enabling more intuitive array manipulation and access patterns.
- Parameters:
- Raises:
ValueError – If dims length doesn’t match data.ndim or contains duplicate names
- class ezmsg.util.messages.axisarray.AxisArray(data, dims, axes=<factory>, attrs=<factory>, key='')[source]#
Bases:
ArrayWithNamedDimsA lightweight message class comprising a numpy ndarray and its metadata.
AxisArray extends ArrayWithNamedDims to provide a complete data structure for scientific computing with named dimensions, axis coordinate systems, and metadata. It’s designed to be similar to xarray.DataArray but optimized for message passing in streaming applications.
- Parameters:
data (npt.NDArray) – The underlying numpy array data (inherited from ArrayWithNamedDims)
dims (list[str]) – List of dimension names (inherited from ArrayWithNamedDims)
axes (dict[str, AxisBase]) – Dictionary mapping dimension names to their axis coordinate systems
attrs (dict[str, Any]) – Dictionary of arbitrary metadata attributes
key (str) – Optional key identifier for this array, typically used to specify source device (default is empty string)
- class Axis(unit='', gain=1.0, offset=0.0)[source]#
Bases:
LinearAxisDeprecated alias for LinearAxis.
Deprecated since version 3.6.0: Use
LinearAxisinstead.- index(val)[source]#
Convert axis value(s) to index(es) using the inverse linear transformation.
- Parameters:
v (float | npt.NDArray[
np.float64]) – Axis value or array of values to convert to indicesfn (Callable) – Function to apply for rounding (default: np.rint)
- Returns:
Corresponding index or array of indices
- Return type:
int | npt.NDArray[
numpy.int_]
- class AxisInfo(axis, idx, size=None)[source]#
Bases:
objectContainer for axis information including the axis object, index, and size.
This class provides a convenient way to access both the axis coordinate system and metadata about where that axis appears in the array structure.
- Parameters:
- Raises:
ValueError – If size rules are violated for the axis type
- class CoordinateAxis(data, dims, unit='')[source]#
Bases:
AxisBase,ArrayWithNamedDimsAn axis implementation that uses explicit coordinate values stored in an array.
This class allows for non-linear or irregularly spaced coordinate systems by storing the actual coordinate values in a data array.
Inherits from both AxisBase and ArrayWithNamedDims, combining axis functionality with named dimension support.
- Parameters:
- value(x)[source]#
Get coordinate value(s) at the given index(es).
- Parameters:
x (int | npt.NDArray[
numpy.int_]) – Index or array of indices to lookup- Returns:
Coordinate value(s) at the specified index(es)
- Return type:
Any | npt.NDArray
- class LinearAxis(unit='', gain=1.0, offset=0.0)[source]#
Bases:
AxisBaseAn axis implementation for sparse axes with regular intervals between elements.
It is called “linear” because it provides a simple linear mapping between indices and element values: value = (index * gain) + offset.
A typical example is a time axis (TimeAxis), with regular sampling rate.
- Parameters:
- classmethod create_time_axis(fs, offset=0.0)[source]#
Convenience method to construct a LinearAxis for time.
- Parameters:
- Returns:
A LinearAxis configured for time with gain=1/fs and unit=’s’
- Return type:
- index(v, fn=<ufunc 'rint'>)[source]#
Convert axis value(s) to index(es) using the inverse linear transformation.
- Parameters:
v (float | npt.NDArray[
np.float64]) – Axis value or array of values to convert to indicesfn (Callable) – Function to apply for rounding (default: np.rint)
- Returns:
Corresponding index or array of indices
- Return type:
int | npt.NDArray[
numpy.int_]
- value(x)[source]#
Convert index(es) to axis value(s) using the linear transformation.
- Parameters:
x (int | npt.NDArray[
numpy.int_]) – Index or array of indices to convert- Returns:
Corresponding axis value(s)
- Return type:
float | npt.NDArray[
np.float64]
- classmethod TimeAxis(fs, offset=0.0)[source]#
Convenience method to construct a LinearAxis for time.
- Parameters:
- Returns:
A LinearAxis configured for time with gain=1/fs and unit=’s’
- Return type:
- axis_idx(dim)[source]#
Get the axis index for a given dimension name or pass through if already an int.
- static concatenate(*aas, dim, axis=None, filter_key=None)[source]#
Concatenate multiple AxisArray objects along a specified dimension.
- Parameters:
- Returns:
New AxisArray with concatenated data
- Return type:
T
- Raises:
ValueError – If objects have incompatible dimensions
- get_axis(dim)[source]#
Get the axis coordinate system for a specified dimension.
- Parameters:
- Returns:
The axis coordinate system (defaults to LinearAxis if not specified)
- Return type:
- Raises:
ValueError – If dimension is not present in the object
- get_axis_idx(dim)[source]#
Get the axis index for a given dimension name.
- Parameters:
dim (str) – The dimension name
- Returns:
The axis index
- Return type:
- Raises:
ValueError – If dimension is not present in the object
- isel(indexers=None, **indexers_kwargs)[source]#
Select data using integer-based indexing along specified dimensions.
This method allows for flexible indexing using integers, slices, or arrays of integers to select subsets of the data along named dimensions.
- Parameters:
- Returns:
New AxisArray with selected data
- Return type:
T
- Raises:
ValueError – If indexer types are not supported (arrays, ints, or slices only)
- iter_over_axis(axis)[source]#
Iterate over slices along the specified axis.
Yields AxisArray objects for each slice along the given axis, with that dimension removed from the resulting arrays.
- sel(indexers=None, **indexers_kwargs)[source]#
Select data using label-based indexing along specified dimensions.
This method allows selection using real-world coordinate values rather than integer indices. Currently supports only slice objects and LinearAxis.
- Parameters:
- Returns:
New AxisArray with selected data
- Return type:
T
- Raises:
ValueError – If indexer is not a slice or axis is not a LinearAxis
- to_xr_dataarray()[source]#
Convert this AxisArray to an xarray DataArray.
This method creates an xarray DataArray with equivalent data, coordinates, dimensions, and attributes. Useful for interoperability with the xarray ecosystem.
- Returns:
xarray DataArray representation of this AxisArray
- Return type:
xarray.DataArray
- class ezmsg.util.messages.axisarray.AxisBase(unit='')[source]#
Bases:
ABCAbstract base class for axes types used by AxisArray.
- class ezmsg.util.messages.axisarray.CoordinateAxis(data, dims, unit='')[source]#
Bases:
AxisBase,ArrayWithNamedDimsAn axis implementation that uses explicit coordinate values stored in an array.
This class allows for non-linear or irregularly spaced coordinate systems by storing the actual coordinate values in a data array.
Inherits from both AxisBase and ArrayWithNamedDims, combining axis functionality with named dimension support.
- Parameters:
- value(x)[source]#
Get coordinate value(s) at the given index(es).
- Parameters:
x (int | npt.NDArray[
numpy.int_]) – Index or array of indices to lookup- Returns:
Coordinate value(s) at the specified index(es)
- Return type:
Any | npt.NDArray
- class ezmsg.util.messages.axisarray.LinearAxis(unit='', gain=1.0, offset=0.0)[source]#
Bases:
AxisBaseAn axis implementation for sparse axes with regular intervals between elements.
It is called “linear” because it provides a simple linear mapping between indices and element values: value = (index * gain) + offset.
A typical example is a time axis (TimeAxis), with regular sampling rate.
- Parameters:
- classmethod create_time_axis(fs, offset=0.0)[source]#
Convenience method to construct a LinearAxis for time.
- Parameters:
- Returns:
A LinearAxis configured for time with gain=1/fs and unit=’s’
- Return type:
- index(v, fn=<ufunc 'rint'>)[source]#
Convert axis value(s) to index(es) using the inverse linear transformation.
- Parameters:
v (float | npt.NDArray[
np.float64]) – Axis value or array of values to convert to indicesfn (Callable) – Function to apply for rounding (default: np.rint)
- Returns:
Corresponding index or array of indices
- Return type:
int | npt.NDArray[
numpy.int_]
- value(x)[source]#
Convert index(es) to axis value(s) using the linear transformation.
- Parameters:
x (int | npt.NDArray[
numpy.int_]) – Index or array of indices to convert- Returns:
Corresponding axis value(s)
- Return type:
float | npt.NDArray[
np.float64]
- ezmsg.util.messages.axisarray.as2d(in_arr, axis=0, *, xp)[source]#
Reshape array to 2D with specified axis first.
- Parameters:
in_arr (npt.NDArray) – Input array
axis (int) – Axis to move to first position (default: 0)
xp – Array namespace (numpy, cupy, etc.)
- Returns:
2D reshaped array
- Return type:
npt.NDArray
- ezmsg.util.messages.axisarray.shape2d(arr, axis=0)[source]#
Calculate the 2D shape when viewing array with specified axis first.
- ezmsg.util.messages.axisarray.slice_along_axis(in_arr, sl, axis)[source]#
- Slice the input array along a specified axis using the given slice object or integer index.
Integer arguments to sl will cause the sliced dimension to be dropped. Use slice(my_int, my_int+1, None) to keep the sliced dimension.
- Parameters:
- Returns:
The sliced array (view)
- Return type:
npt.NDArray
- Raises:
ValueError – If the axis value is invalid for the input array
- ezmsg.util.messages.axisarray.sliding_win_oneaxis(in_arr, nwin, axis, step=1)[source]#
Generates a view of an array using a sliding window of specified length along a specified axis of the input array. This is a slightly optimized version of nps.sliding_window_view with a few important differences:
This only accepts a single nwin and a single axis, thus we can skip some checks.
- The new win axis precedes immediately the original target axis, unlike sliding_window_view where the
target axis is moved to the end of the output.
- Combine this with slice_along_axis(…, sl=slice(None, None, step), axis=axis) to step the window
by more than 1 sample at a time.
- Parameters:
- Returns:
A view to the input array with the sliding window applied
- Return type:
npt.NDArray
- Note: There is a known edge case when nwin == shape[axis] + 1. While this should raise
an error because the window is larger than the input, the implementation ends up returning a 0-length window. We could check for this but this function is intended to have minimal latency so we have decided to skip the checks and deal with the support issues as they arise.
- ezmsg.util.messages.axisarray.view2d(in_arr, axis=0)[source]#
Context manager providing 2D view of the array, no matter what input dimensionality is. Yields a view of underlying data when possible, changes to data in yielded array will always be reflected in self.data, either via the view or copying.
NOTE: In practice, I’m not sure this is very useful because it requires modifying the numpy array data in-place, which limits its application to zero-copy messages
NOTE: The context manager allows the use of with when calling view2d.