{ } Raw JSON

bundles / scipy latest / scipy / ndimage / _measurements / value_indices

function

scipy.ndimage._measurements:value_indices

source: /scipy/ndimage/_measurements.py :311

Signature

def   value_indices ( arr * ignore_value = None )

Summary

Find indices of each distinct value in given array.

Parameters

arr : ndarray of ints

Array containing integer values.

ignore_value : int, optional

This value will be ignored in searching the arr array. If not given, all values found will be included in output. Default is None.

Returns

indices : dictionary

A Python dictionary of array indices for each distinct value. The dictionary is keyed by the distinct values, the entries are array index tuples covering all occurrences of the value within the array.

This dictionary can occupy significant memory, usually several times the size of the input array.

Notes

For a small array with few distinct values, one might use numpy.unique() to find all possible values, and (arr == val) to locate each value within that array. However, for large arrays, with many distinct values, this can become extremely inefficient, as locating each value would require a new search through the entire array. Using this function, there is essentially one search, with the indices saved for all distinct values.

This is useful when matching a categorical image (e.g. a segmentation or classification) to an associated image of other data, allowing any per-class statistic(s) to then be calculated. Provides a more flexible alternative to functions like scipy.ndimage.mean() and scipy.ndimage.variance().

Some other closely related functionality, with different strengths and weaknesses, can also be found in scipy.stats.binned_statistic() and the scikit-image function skimage.measure.regionprops().

Note for IDL users: this provides functionality equivalent to IDL's REVERSE_INDICES option (as per the IDL documentation for the HISTOGRAM function).

Array API Standard Support

value_indices 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-arrayapi for more information.

Examples

import numpy as np
from scipy import ndimage
a = np.zeros((6, 6), dtype=int)
a[2:4, 2:4] = 1
a[4, 4] = 1
a[:2, :3] = 2
a[0, 5] = 3
a
val_indices = ndimage.value_indices(a)
The dictionary `val_indices` will have an entry for each distinct value in the input array.
val_indices.keys()
The entry for each value is an index tuple, locating the elements with that value.
ndx1 = val_indices[1]
ndx1
This can be used to index into the original array, or any other array with the same shape.
a[ndx1]
If the zeros were to be ignored, then the resulting dictionary would no longer have an entry for zero.
val_indices = ndimage.value_indices(a, ignore_value=0)
val_indices.keys()

See also

extrema
label
maximum
mean
median
minimum_position
numpy.unique
numpy.where
standard_deviation
sum
variance

Aliases

  • scipy.ndimage.value_indices

Referenced by

This package