convster.helper#

Array utility functions for the convster package.

This module provides low-level helper functions for inspecting and manipulating NumPy arrays. Currently, it exposes utilities for locating the first and last non-zero element along a given axis, which are used internally during filter application to determine valid data ranges within raster bands.

Functions#

first_nonzero(data[, axis, no_value])

Return the index of the first non-zero value along the given axis.

last_nonzero(data[, axis, no_value])

Return the index of the last non-zero value along the given axis.

Module Contents#

convster.helper.first_nonzero(data, axis=0, no_value=-1)[source]#

Return the index of the first non-zero value along the given axis.

Parameters:
  • data (NDArray) – Input array to examine.

  • axis (int) – Array axis along which to search for the first non-zero. Default is 0.

  • no_value (int) – Value to return when no non-zero entries are found along an axis. Default is -1.

Returns:

Array indices of the first non-zero values along the specified axis. If no non-zero is found, returns no_value for that slice.

Return type:

indices

See also

last_nonzero()

Return the index of the last non-zero value.

Examples

>>> a = np.array([
...     [0, 0, 3, 0],
...     [1, 0, 0, 0],
...     [0, 2, 0, 0],
...     [0, 0, 0, 4]
... ])
>>> first_nonzero(a, axis=1)
array([2, 0, 1, 3])
>>> first_nonzero(a, axis=0)
array([1, 2, 0, 3])
convster.helper.last_nonzero(data, axis=0, no_value=-1)[source]#

Return the index of the last non-zero value along the given axis.

Parameters:
  • data (NDArray) – Input array to examine.

  • axis (int) – Array axis along which to search for the last non-zero. Default is 0.

  • no_value (int) – Value to return when no non-zero entries are found along an axis. Default is -1.

Returns:

Array indices of the last non-zero values along the specified axis. If no non-zero is found, returns no_value for that slice.

Return type:

indices

See also

first_nonzero()

Return the index of the first non-zero value.

Examples

>>> a = np.array([
...     [0, 0, 3, 0],
...     [1, 0, 0, 0],
...     [0, 2, 0, 0],
...     [0, 0, 0, 4]
... ])
>>> last_nonzero(a, axis=1)
array([2, 0, 1, 3])
>>> last_nonzero(a, axis=0)
array([1, 2, 2, 3])