spateo.segmentation.label

Functions for use when labeling individual nuclei/cells, after obtaining a mask.

Functions

_replace_labels(→ numpy.ndarray)

Replace labels according to mapping.

replace_labels(adata, layer, mapping[, out_layer])

Replace labels according to mapping.

_watershed(→ numpy.ndarray)

Assign individual nuclei/cells using the Watershed algorithm.

find_peaks_with_erosion(adata[, layer, k, square, ...])

Find peaks for use in Watershed via iterative erosion.

watershed(adata[, layer, k, mask_layer, ...])

Assign individual nuclei/cells using the Watershed algorithm.

_expand_labels(→ numpy.ndarray)

Expand labels up to a certain distance, while ignoring labels that are

expand_labels(adata, layer[, distance, max_area, ...])

Expand labels up to a certain distance.

_label_connected_components(→ numpy.ndarray)

Label connected components while splitting components that are too large.

label_connected_components(adata, layer[, seed_layer, ...])

Label connected components while splitting components that are too large.

_find_peaks(→ Tuple[numpy.ndarray, numpy.ndarray])

Find peaks from an arbitrary image.

find_peaks(adata, layer, k, min_distance[, ...])

Find peaks from an array.

find_peaks_from_mask(adata, layer, min_distance[, ...])

Find peaks from a boolean mask. Used to obatin Watershed markers.

_augment_labels(→ numpy.ndarray)

Augment the labels in one label array using the labels in another.

augment_labels(adata, source_layer, target_layer[, ...])

Augment the labels in one label array using the labels in another.

Module Contents

spateo.segmentation.label._replace_labels(labels: numpy.ndarray, mapping: Dict[int, int]) numpy.ndarray[source]

Replace labels according to mapping.

Parameters:
labels

Numpy array containing integer labels.

mapping

Dictionary mapping from labels to labels.

Returns:

Replaced labels

spateo.segmentation.label.replace_labels(adata: anndata.AnnData, layer: str, mapping: Dict[int, int], out_layer: str | None = None)[source]

Replace labels according to mapping.

Parameters:
adata

Input Anndata

layer

Layer containing labels to replace

mapping

Dictionary mapping that defines label replacement.

out_layer

Layer to save results. By default, the input layer is overridden.

spateo.segmentation.label._watershed(X: numpy.ndarray, mask: numpy.ndarray, markers: numpy.ndarray, k: int) numpy.ndarray[source]

Assign individual nuclei/cells using the Watershed algorithm.

Parameters:
X

Data array. This array will be Gaussian blurred and used as the input values to Watershed.

mask

Nucleus/cell mask.

markers

Numpy array indicating where the Watershed markers are. May either be a boolean or integer array. If this is a boolean array, the markers are identified by calling cv2.connectedComponents.

k

Size of the kernel to use for Gaussian blur.

Returns:

Watershed labels.

spateo.segmentation.label.find_peaks_with_erosion(adata: anndata.AnnData, layer: str = SKM.STAIN_LAYER_KEY, k: int = 3, square: bool = False, min_area: int = 80, n_iter: int = -1, float_k: int = 5, float_threshold: float | None = None, out_layer: str | None = None)[source]

Find peaks for use in Watershed via iterative erosion.

Parameters:
adata

Input Anndata

layer

Layer that was used to create scores or masks. If {layer}_scores is present, that is used. Otherwise if {layer}_mask is present, that is used. Otherwise, the layer is taken as a literal.

k

Erosion kernel size

square

Whether to use a square kernel

min_area

Minimum area

n_iter

Number of erosions to perform.

float_k

Morphological close and open kernel size when X is a float array.

float_threshold

Threshold to use to determine connected components when X is a float array. By default, a threshold is automatically determined by using Otsu method.

out_layer

Layer to save results. By default, this will be {layer}_markers.

spateo.segmentation.label.watershed(adata: anndata.AnnData, layer: str = SKM.STAIN_LAYER_KEY, k: int = 3, mask_layer: str | None = None, markers_layer: str | None = None, out_layer: str | None = None)[source]

Assign individual nuclei/cells using the Watershed algorithm.

Parameters:
adata

Input AnnData

layer

Original data layer from which segmentation will derive from.

k

Size of the kernel to use for Gaussian blur.

mask_layer

Layer containing mask. This will default to {layer}_mask.

markers_layer

Layer containing Watershed markers. This will default to {layer}_markers. May either be a boolean or integer array. If this is a boolean array, the markers are identified by calling cv2.connectedComponents.

out_layer

Layer to save results. Defaults to {layer}_labels.

spateo.segmentation.label._expand_labels(labels: numpy.ndarray, distance: int, max_area: int, mask: numpy.ndarray | None = None) numpy.ndarray[source]

Expand labels up to a certain distance, while ignoring labels that are above a certain size.

Parameters:
labels

Numpy array containing integer labels.

distance

Distance to expand. Internally, this is used as the number of iterations of distance 1 dilations.

max_area

Maximum area of each label.

mask

Only expand within the provided mask.

Returns:

New label array with expanded labels.

spateo.segmentation.label.expand_labels(adata: anndata.AnnData, layer: str, distance: int = 5, max_area: int = 400, mask_layer: str | None = None, out_layer: str | None = None)[source]

Expand labels up to a certain distance.

Parameters:
adata

Input Anndata

layer

Layer from which the labels were derived. Then, {layer}_labels is used as the labels. If not present, it is taken as a literal.

distance

Distance to expand. Internally, this is used as the number of iterations of distance 1 dilations.

max_area

Maximum area of each label.

mask_layer

Layer containing mask to restrict expansion to within.

out_layer

Layer to save results. By default, uses {layer}_labels_expanded.

spateo.segmentation.label._label_connected_components(X: numpy.ndarray, area_threshold: int = 500, k: int = 3, min_area: int = 100, n_iter: int = -1, distance: int = 8, max_area: int = 400, seed_labels: numpy.ndarray | None = None) numpy.ndarray[source]

Label connected components while splitting components that are too large.

Parameters:
X

Boolean mask to compute connected components from.

area_threshold

Connected components with area greater than this value will be split into smaller portions by first eroding and then expanding.

k

Kernel size for erosion.

min_area

Don’t erode labels smaller than this area.

n_iter

Number of erosion operations. -1 means continue eroding until every label is less than min_area.

distance

Distance to expand eroded labels.

max_area

Maximum area when expanding labels.

seed_labels

Seed labels.

Returns:

New label array

spateo.segmentation.label.label_connected_components(adata: anndata.AnnData, layer: str, seed_layer: str | None = None, area_threshold: int = 500, k: int = 3, min_area: int = 100, n_iter: int = -1, distance: int = 8, max_area: int = 400, out_layer: str | None = None)[source]

Label connected components while splitting components that are too large.

Parameters:
adata

Input Anndata

layer

Data layer that was used to generate the mask. First, will look for {layer}_mask. Otherwise, this will be use as a literal.

seed_layer

Layer containing seed labels. These are labels that should be used whenever possible in labeling connected components.

area_threshold

Connected components with area greater than this value will be split into smaller portions by first eroding and then expanding.

k

Kernel size for erosion.

min_area

Don’t erode labels smaller than this area.

n_iter

Number of erosion operations. -1 means continue eroding until every label is less than min_area.

distance

Distance to expand eroded labels.

max_area

Maximum area when expanding labels.

out_layer

Layer to save results. Defaults to {layer}_labels.

Returns:

New label array

spateo.segmentation.label._find_peaks(X: numpy.ndarray, **kwargs) Tuple[numpy.ndarray, numpy.ndarray][source]

Find peaks from an arbitrary image.

This function is a wrapper around feature.peak_local_max().

Parameters:
X

Array to find peaks from

**kwargs

Keyword arguments to pass to feature.peak_local_max().

Returns:

Numpy array of the same size as X where each peak is labeled with a unique positive integer.

spateo.segmentation.label.find_peaks(adata: anndata.AnnData, layer: str, k: int, min_distance: int, mask_layer: str | None = None, out_layer: str | None = None)[source]

Find peaks from an array.

Parameters:
adata

Input AnnData

layer

Layer to use as values to find peaks from.

k

Apply a Gaussian blur with this kernel size prior to peak detection.

min_distance

Minimum distance, in pixels, between peaks.

mask_layer

Find peaks only in regions specified by the mask.

out_layer

Layer to save identified peaks as markers. By default, uses {layer}_markers.

spateo.segmentation.label.find_peaks_from_mask(adata: anndata.AnnData, layer: str, min_distance: int, distances_layer: str | None = None, markers_layer: str | None = None)[source]

Find peaks from a boolean mask. Used to obatin Watershed markers.

Parameters:
adata

Input AnnData

layer

Layer containing boolean mask. This will default to {layer}_mask. If not present in the provided AnnData, this argument used as a literal.

min_distance

Minimum distance, in pixels, between peaks.

distances_layer

Layer to save distance from each pixel to the nearest zero (False) pixel (a.k.a. distance transform). By default, uses {layer}_distances.

markers_layer

Layer to save identified peaks as markers. By default, uses {layer}_markers.

spateo.segmentation.label._augment_labels(source_labels: numpy.ndarray, target_labels: numpy.ndarray) numpy.ndarray[source]

Augment the labels in one label array using the labels in another.

This function modifies the labels in target_labels in the following way. Note that this function operates on a copy of target_labels. It does NOT modify in-place. * Any labels that are in source_labels that have no overlap with

any labels in target_labels is copied over to target_labels.

  • Any labels that are in target_labels that have no overlap with any labels

    in source_labels is removed.

Parameters:
source_labels

Numpy array containing labels to (possibly) transfer.

target_labels

Numpy array containing labels to augment.

Returns:

New Numpy array containing augmented labels.

spateo.segmentation.label.augment_labels(adata: anndata.AnnData, source_layer: str, target_layer: str, out_layer: str | None = None)[source]

Augment the labels in one label array using the labels in another.

Parameters:
adata

Input Anndata

source_layer

Layer containing source labels to (possibly) take labels from.

target_layer

Layer containing target labels to augment.

out_layer

Layer to save results. Defaults to {target_layer}_augmented.