Loading [MathJax]/extensions/tex2jax.js
scipy 1.10.1 Pypi GitHub Homepage
Other Docs

ParametersReturns
labeled_comprehension(input, labels, index, func, out_dtype, default, pass_positions=False)

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_like

Data from which to select labels to process.

labels : array_like or None

Labels to objects in input. If not None, array must be same shape as input. If None, func is applied to raveled input.

index : int, sequence of ints or None

Subset of labels to which to apply func. If a scalar, a single value is returned. If None, func is applied to all non-zero values of labels.

func : callable

Python function to apply to labels from input.

out_dtype : dtype

Dtype to use for result.

default : int, float or None

Default return value when a element of index does not exist in labels.

pass_positions : bool, optional

If True, pass linear indices to func as a second argument. Default is False.

Returns

result : ndarray

Result of applying func to each of labels to input in index.

Roughly equivalent to [func(input[labels == i]) for i in index].

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)
Falling back to `default`:
lbls = np.arange(1, nlbl+2)
ndimage.labeled_comprehension(a, lbl, lbls, np.mean, float, -1)
Passing positions:
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)
See :

Local connectivity graph

Hover to see nodes names; edges to Self not shown, Caped at 50 nodes.

Using a canvas is more power efficient and can get hundred of nodes ; but does not allow hyperlinks; , arrows or text (beyond on hover)

SVG is more flexible but power hungry; and does not scale well to 50 + nodes.

All aboves nodes referred to, (or are referred from) current nodes; Edges from Self to other have been omitted (or all nodes would be connected to the central node "self" which is not useful). Nodes are colored by the library they belong to, and scaled with the number of references pointing them


GitHub : /scipy/ndimage/_measurements.py#424
type: <class 'function'>
Commit: