bundles / scipy latest / scipy / ndimage / _measurements / labeled_comprehension
function
scipy.ndimage._measurements:labeled_comprehension
Signature
def labeled_comprehension ( input , labels , index , func , out_dtype , default , pass_positions = False ) Summary
Roughly equivalent to [func(input[labels == i]) for i in index].
Extended Summary
Sequentially applies an arbitrary function (that works on array_like input) to subsets of an N-D image array specified by labels and index. The option exists to provide the function with positional parameters as the second argument.
Parameters
input: array_likeData from which to select
labelsto process.labels: array_like or NoneLabels to objects in
input. If not None, array must be same shape asinput. If None,funcis applied to raveledinput.index: int, sequence of ints or NoneSubset of
labelsto which to applyfunc. If a scalar, a single value is returned. If None,funcis applied to all non-zero values oflabels.func: callablePython function to apply to
labelsfrominput.out_dtype: dtypeDtype to use for result.
default: int, float or NoneDefault return value when an element of
indexdoes not exist inlabels.pass_positions: bool, optionalIf True, pass linear indices to
funcas a second argument. Default is False.
Returns
result: ndarrayResult of applying
functo each oflabelstoinputinindex.
Notes
Array API Standard Support
labeled_comprehension has experimental support for Python Array API Standard compatible backends in addition to NumPy. Please consider testing these features by setting an environment variable SCIPY_ARRAY_API=1 and providing CuPy, PyTorch, JAX, or Dask arrays as array arguments. The following combinations of backend and device (or other capability) are supported.
==================== ==================== ==================== Library CPU GPU ==================== ==================== ==================== NumPy ✅ n/a CuPy n/a ✅ PyTorch ✅ ⛔ JAX ⚠️ no JIT ⛔ Dask ⚠️ computes graph n/a ==================== ==================== ====================
See
dev-arrayapifor more information.
Examples
import numpy as np a = np.array([[1, 2, 0, 0], [5, 3, 0, 4], [0, 0, 0, 7], [9, 3, 0, 0]]) from scipy import ndimage lbl, nlbl = ndimage.label(a) lbls = np.arange(1, nlbl+1)✓
ndimage.labeled_comprehension(a, lbl, lbls, np.mean, float, 0)
✗lbls = np.arange(1, nlbl+2) ndimage.labeled_comprehension(a, lbl, lbls, np.mean, float, -1)✓
def fn(val, pos): print("fn says: %s : %s" % (val, pos)) return (val.sum()) if (pos.sum() % 2 == 0) else (-val.sum()) ndimage.labeled_comprehension(a, lbl, lbls, fn, float, 0, True)✓
Aliases
-
scipy.ndimage.labeled_comprehension