Indexing Functions and Utilities

The following functions are useful for performing indexing and oter associated operations.

broadcast_to(arr, shape)

Broadcast an array to a new shape.

count_nonzero(ndarr[, axis])

Return number of nonzero values along axes.

expand_dims(array[, axis])

Expand the shape of an array by adding new axes at the specified positions.

indices(array[, order])

Return the indices of a sorted array following the specified order.

meshgrid(*arrays[, indexing])

Returns coordinate matrices from coordinate vectors.

sort(array[, order])

Return a sorted array following the specified order.

squeeze(x[, axis])

Remove single-dimensional entries from the shape of the array.

take(x, indices[, axis])

Returns elements of an array along an axis.

take_along_axis(x, indices[, axis])

Returns elements of an array along an axis.

blosc2.broadcast_to(arr, shape)[source]

Broadcast an array to a new shape. Warning: Computes a lazyexpr, so probably a bit suboptimal

Parameters:
  • array (NDArray) – The array to broadcast.

  • shape (tuple) – The shape of the desired array.

Returns:

  • broadcast (NDArray)

  • A new array with the given shape.

blosc2.count_nonzero(ndarr: NDArray | NDField | blosc2.C2Array | blosc2.LazyExpr, axis: int | Sequence[int] | None = None) int[source]

Return number of nonzero values along axes.

Parameters:
  • ndarr (NDArray or NDField or C2Array or LazyExpr) – The input array.

  • axis (int | Sequence[int] | None) – Axes along which to count nonzero entries. If None, sum over whole array. Default: None.

Returns:

out – Number of nonzero elements.

Return type:

int

References

np.count_nonzero

blosc2.expand_dims(array: NDArray, axis=0) NDArray[source]

Expand the shape of an array by adding new axes at the specified positions.

Parameters:
  • array (NDArray) – The array to be expanded.

  • axis (int or list of int, optional) – Position in the expanded axes where the new axis (or axes) is placed. Default is 0.

Returns:

out – A new NDArray with the expanded shape.

Return type:

NDArray

blosc2.indices(array: NDArray, order: str | list[str] | None = None, **kwargs: Any) NDArray[source]

Return the indices of a sorted array following the specified order.

This is only valid for 1-dim structured arrays.

Parameters:
  • array (NDArray) – The (structured) array to be sorted.

  • order (str, list of str, optional) – Specifies which fields to compare first, second, etc. A single field can be specified as a string. Not all fields need to be specified, only the ones by which the array is to be sorted. If None, the array is not sorted.

  • kwargs (Any, optional) – Keyword arguments that are supported by the empty() constructor.

Returns:

out – The sorted array.

Return type:

NDArray

blosc2.meshgrid(*arrays: NDArray, indexing: str = 'xy') Sequence[NDArray][source]

Returns coordinate matrices from coordinate vectors.

Parameters:
  • arrays (NDArray) – An arbitrary number of one-dimensional arrays representing grid coordinates. Each array should have the same numeric data type.

  • indexing (str) – Cartesian ‘xy’ or matrix ‘ij’ indexing of output. If provided zero or one one-dimensional vector(s) the indexing keyword is ignored. Default: ‘xy’.

Returns:

out – List of N arrays, where N is the number of provided one-dimensional input arrays, with same dtype. For N one-dimensional arrays having lengths Ni = len(xi),

  • if matrix indexing ij, then each returned array has shape (N1, N2, N3, …, Nn).

  • if Cartesian indexing xy, then each returned array has shape (N2, N1, N3, …, Nn).

Return type:

(List[NDArray])

blosc2.sort(array: NDArray, order: str | list[str] | None = None, **kwargs: Any) NDArray[source]

Return a sorted array following the specified order.

This is only valid for 1-dim structured arrays.

Parameters:
  • array (NDArray) – The (structured) array to be sorted.

  • order (str, list of str, optional) – Specifies which fields to compare first, second, etc. A single field can be specified as a string. Not all fields need to be specified, only the ones by which the array is to be sorted.

  • kwargs (Any, optional) – Keyword arguments that are supported by the empty() constructor.

Returns:

out – The sorted array.

Return type:

NDArray

blosc2.squeeze(x: NDArray, axis: int | None = None) NDArray[source]

Remove single-dimensional entries from the shape of the array.

This method modifies the array in-place. If mask is None removes any dimensions with size 1. If axis is provided, it should be an int or tuple of ints and the corresponding dimensions (of size 1) will be removed.

Returns:

out

Return type:

NDArray

Examples

>>> import blosc2
>>> shape = [1, 23, 1, 11, 1]
>>> # Create an array
>>> b = blosc2.full(shape, 2**30)
>>> b.shape
(1, 23, 1, 11, 1)
>>> # Squeeze the array
>>> blosc2.squeeze(b)
>>> b.shape
(23, 11)
blosc2.take(x: NDArray, indices: NDArray[int] | np.ndarray[int], axis: int | None = None) NDArray[source]

Returns elements of an array along an axis.

Parameters:
  • x (NDArray) – Input array. Should have one or more dimensions (axes).

  • indices (array-like) – Array indices. The array must be one-dimensional and have an integer data type.

  • axis (int | None) – Axis over which to select values. If x is a one-dimensional array, providing an axis is optional; however, if x has more than one dimension, providing an axis is required. Default: None.

Returns:

out – Selected indices of x.

Return type:

NDArray

blosc2.take_along_axis(x: NDArray, indices: NDArray[int] | np.ndarray[int], axis: int = -1) NDArray[source]

Returns elements of an array along an axis.

Parameters:
  • x (NDArray) – Input array. Should have one or more dimensions (axes).

  • indices (array-like) – Array indices. The array must have same number of dimensions as x and have an integer data type.

  • axis (int) – Axis over which to select values. Default: -1.

Returns:

out – Selected indices of x.

Return type:

NDArray