spateo.segmentation.utils#

Utility functions for cell segmentation.

Module Contents#

Functions#

circle(→ numpy.ndarray)

Draw a circle of diameter k.

knee_threshold(→ float)

Find the knee thresholding point of an arbitrary array.

gaussian_blur(→ numpy.ndarray)

Gaussian blur

median_blur(→ numpy.ndarray)

Median blur

conv2d(→ numpy.ndarray)

Convolve an array with the specified kernel size and mode.

scale_to_01(→ numpy.ndarray)

Scale an array to [0, 1].

scale_to_255(→ numpy.ndarray)

Scale an array to [0, 255].

mclose_mopen(→ numpy.ndarray)

Perform morphological close and open operations on a boolean mask.

apply_threshold(→ numpy.ndarray)

Apply a threshold value to the given array and perform morphological close

safe_erode(→ numpy.ndarray)

Perform morphological erosion, but don't erode connected regions that

label_overlap(→ scipy.sparse.csr_matrix)

Compuate the overlaps between two label arrays.

clahe() → numpy.ndarray)

Contrast-limited adaptive histogram equalization (CLAHE).

cal_cell_area(cell_labels)

Calculate spot numbers for each cell.

filter_cell_labels_by_area(adata, layer[, area_cutoff])

Filter out cells with area less than area_cutoff

get_cell_shape(adata, layer[, thickness, out_layer])

Set cell boundaries as 255 with thickness as thickness.

spateo.segmentation.utils.circle(k: int) numpy.ndarray[source]#

Draw a circle of diameter k.

Parameters:
k

Diameter

Returns:

8-bit unsigned integer Numpy array with 1s and 0s

Raises:

ValueError – if k is even or less than 1

spateo.segmentation.utils.knee_threshold(X: numpy.ndarray, n_bins: int = 256, clip: int = 5) float[source]#

Find the knee thresholding point of an arbitrary array.

Note

This function does not find the actual knee of X. It computes a value to be used to threshold the elements of X by finding the knee of the cumulative counts.

Parameters:
X

Numpy array of values

n_bins

Number of bins to use if X is a float array.

Returns:

Knee

spateo.segmentation.utils.gaussian_blur(X: numpy.ndarray, k: int) numpy.ndarray[source]#

Gaussian blur

This function is not designed to be called directly. Use conv2d() with mode=”gauss” instead.

Parameters:
X

UMI counts per pixel.

k

Radius of gaussian blur.

Returns:

Blurred array

spateo.segmentation.utils.median_blur(X: numpy.ndarray, k: int) numpy.ndarray[source]#

Median blur

This function is not designed to be called directly. Use conv2d() with mode=”median” instead.

Parameters:
X

UMI counts per pixel.

k

Radius of median blur.

Returns:

Blurred array

spateo.segmentation.utils.conv2d(X: numpy.ndarray, k: int, mode: typing_extensions.Literal[gauss, median, circle, square], bins: numpy.ndarray | None = None) numpy.ndarray[source]#

Convolve an array with the specified kernel size and mode.

Parameters:
X

The array to convolve.

k

Kernel size. Must be odd.

mode

Convolution mode. Supported modes are: gauss: circle: square:

bins

Convolve per bin. Zeros are ignored.

Returns:

The convolved array

Raises:

ValueError – if k is even or less than 1, or if mode is not a valid mode, or if bins does not have the same shape as X

spateo.segmentation.utils.scale_to_01(X: numpy.ndarray) numpy.ndarray[source]#

Scale an array to [0, 1].

Parameters:
X

Array to scale

Returns:

Scaled array

spateo.segmentation.utils.scale_to_255(X: numpy.ndarray) numpy.ndarray[source]#

Scale an array to [0, 255].

Parameters:
X

Array to scale

Returns:

Scaled array

spateo.segmentation.utils.mclose_mopen(mask: numpy.ndarray, k: int, square: bool = False) numpy.ndarray[source]#

Perform morphological close and open operations on a boolean mask.

Parameters:
X

Boolean mask

k

Kernel size

square

Whether or not the kernel should be square

Returns:

New boolean mask with morphological close and open operations performed.

Raises:

ValueError – if k is even or less than 1

spateo.segmentation.utils.apply_threshold(X: numpy.ndarray, k: int, threshold: float | numpy.ndarray | None = None) numpy.ndarray[source]#

Apply a threshold value to the given array and perform morphological close and open operations.

Parameters:
X

The array to threshold

k

Kernel size of the morphological close and open operations.

threshold

Threshold to apply. By default, the knee is used.

Returns:

A boolean mask.

spateo.segmentation.utils.safe_erode(X: numpy.ndarray, k: int, square: bool = False, min_area: int = 1, n_iter: int = -1, float_k: int | None = None, float_threshold: float | None = None) numpy.ndarray[source]#

Perform morphological erosion, but don’t erode connected regions that have less than the provided area.

Note

It is possible for this function to miss some small regions due to how erosion works. For instance, a region may have area > min_area which may be eroded in its entirety in one iteration. In this case, this region will not be saved.

Parameters:
X

Array to erode.

k

Erosion kernel size

square

Whether to use a square kernel

min_area

Minimum area

n_iter

Number of erosions to perform. If -1, then erosion is continued until every connected component is <= min_area.

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.

Returns:

Eroded array as a boolean mask

Raises:

ValueError – If X has floating point dtype but float_threshold is not provided

spateo.segmentation.utils.label_overlap(X: numpy.ndarray, Y: numpy.ndarray) scipy.sparse.csr_matrix[source]#

Compuate the overlaps between two label arrays.

The integer labels in X and Y are used as the row and column indices of the resulting array.

Note

The overlap array contains background overlap (index 0) as well.

Parameters:
X

First label array. Labels in this array are the rows of the resulting array.

Y

Second label array. Labels in this array are the columns of the resulting array.

Returns:

A (max(X)+1, max(Y)+1) shape sparse array containing how many pixels for

each label are overlapping.

spateo.segmentation.utils.clahe(X: numpy.ndarray, clip_limit: float = 1.0, tile_grid: Tuple[int, int] = (100, 100)) numpy.ndarray[source]#

Contrast-limited adaptive histogram equalization (CLAHE).

Parameters:
X

Image to equalize

clip_limit

Contrast clipping. Lower values retain more of homogeneous regions.

tile_grid

Apply histogram equalization to tiles of this size.

Returns:

Equalized image

spateo.segmentation.utils.cal_cell_area(cell_labels: numpy.ndarray)[source]#

Calculate spot numbers for each cell.

Parameters:
cell_labels

cell labels.

Returns:

dict

spateo.segmentation.utils.filter_cell_labels_by_area(adata: anndata.AnnData, layer: str, area_cutoff: int = 7)[source]#

Filter out cells with area less than area_cutoff

Parameters:
adata

Input Anndata

layer

Layer that contains UMI counts to use

area_cutoff

cells with area less than this cutoff would be dicarded.

spateo.segmentation.utils.get_cell_shape(adata: anndata.AnnData, layer: str, thickness: int = 1, out_layer: str | None = None)[source]#

Set cell boundaries as 255 with thickness as thickness.

Parameters:
adata

Input Anndata

layer

Layer that contains cell labels to use

thickness

The thickness of cell boundaries

out_layer

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