ImageContainer.features_custom(func, layer, channels=None, feature_name=None, **kwargs)

Calculate features using a custom function.

The feature extractor func can be any callable(), as long as it has the following signature: numpy.ndarray (height, width, z, channels) -> float/Sequence.

  • func (Callable[[ndarray[Any, dtype[Any]]], Any]) – Feature extraction function.

  • layer (Optional[str]) – Image layer in img that should be processed. If None and only 1 layer is present, it will be selected.

  • channels (Union[int, Sequence[int], None]) – Channels for this feature is computed. If None, use all channels.

  • feature_name (Optional[str]) – Base name of feature in resulting feature values dict.

  • kwargs (Any) – Keyword arguments for func. If additional_layers is present, all layers with the specified names will be passed to func. using func(..., <layer-name>=<layer-values>) for each layer name in additional_layers.

Return type

Dict[str, Any]


Returns features with the following keys:

  • '{feature_name}_{i}' - i-th feature value.


Simple example would be to calculate the mean of a specified channel, as already done in squidpy.im.ImageContainer.features_summary():

img = squidpy.im.ImageContainer(...)
img.features_custom(imd_id=..., func=numpy.mean, channels=0)