spateo.segmentation.icell#

Identify cells from RNA signal. Functions in this file are used to generate a cell mask, NOT to identify individual cells.

Original author @HailinPan, refactored by @Lioscro.

Module Contents#

Functions#

_mask_cells_from_stain(→ numpy.ndarray)

Create a boolean mask indicating cells from stained image.

_mask_nuclei_from_stain(→ numpy.ndarray)

Create a boolean mask indicating nuclei from stained nuclei image.

mask_cells_from_stain(adata[, otsu_classes, ...])

Create a boolean mask indicating cells from stained image.

mask_nuclei_from_stain(adata[, otsu_classes, ...])

Create a boolean mask indicating nuclei from stained nuclei image, and

_initial_nb_params(→ Union[Dict[str, Tuple[float, ...)

Calculate initial estimates for the negative binomial mixture.

_score_pixels(→ numpy.ndarray)

Score each pixel by how likely it is a cell. Values returned are in

score_and_mask_pixels(adata, layer, k, method[, ...])

Score and mask pixels by how likely it is occupied.

spateo.segmentation.icell._mask_cells_from_stain(X: numpy.ndarray, otsu_classes: int = 3, otsu_index: int = 0, mk: int = 7) numpy.ndarray[source]#

Create a boolean mask indicating cells from stained image.

spateo.segmentation.icell._mask_nuclei_from_stain(X: numpy.ndarray, otsu_classes: int = 3, otsu_index: int = 0, local_k: int = 55, offset: int = 5, mk: int = 5) numpy.ndarray[source]#

Create a boolean mask indicating nuclei from stained nuclei image. See mask_nuclei_from_stain() for arguments.

spateo.segmentation.icell.mask_cells_from_stain(adata: anndata.AnnData, otsu_classes: int = 3, otsu_index: int = 0, mk: int = 7, layer: str = SKM.STAIN_LAYER_KEY, out_layer: str | None = None)[source]#

Create a boolean mask indicating cells from stained image.

Parameters:
adata

Input Anndata

otsu_classes

Number of classes to assign pixels to for cell detection

otsu_index

Which threshold index should be used for classifying cells. All pixel intensities >= the value at this index will be classified as cell.

mk

Size of the kernel used for morphological close and open operations applied at the very end.

layer

Layer that contains staining image.

out_layer

Layer to put resulting nuclei mask. Defaults to {layer}_mask.

spateo.segmentation.icell.mask_nuclei_from_stain(adata: anndata.AnnData, otsu_classes: int = 3, otsu_index: int = 0, local_k: int = 55, offset: int = 5, mk: int = 5, layer: str = SKM.STAIN_LAYER_KEY, out_layer: str | None = None)[source]#

Create a boolean mask indicating nuclei from stained nuclei image, and save this mask in the AnnData as an additional layer.

Parameters:
adata

Input Anndata

otsu_classes

Number of classes to assign pixels to for background detection.

otsu_index

Which threshold index should be used for background. All pixel intensities less than the value at this index will be classified as background.

local_k

The size of the local neighborhood of each pixel to use for local (adaptive) thresholding to identify the foreground (i.e. nuclei).

offset

Offset to local thresholding values such that values > 0 lead to more “strict” thresholding, and therefore may be helpful in distinguishing nuclei in dense regions.

mk

Size of the kernel used for morphological close and open operations applied at the very end.

layer

Layer containing nuclei staining

out_layer

Layer to put resulting nuclei mask. Defaults to {layer}_mask.

spateo.segmentation.icell._initial_nb_params(X: numpy.ndarray, bins: numpy.ndarray | None = None) Dict[str, Tuple[float, float]] | Dict[int, Dict[str, Tuple[float, float]]][source]#

Calculate initial estimates for the negative binomial mixture.

Parameters:
X

UMI counts

bins

Density bins

Returns:

Dictionary containing initial w, mu, var parameters. If bins is also provided, the dictionary is nested with the outer dictionary containing each bin label as the keys. If zero_inflated=True, then the dictionary also contains a z key.

spateo.segmentation.icell._score_pixels(X: scipy.sparse.spmatrix | numpy.ndarray, k: int, method: typing_extensions.Literal[gauss, spateo.segmentation.moran, EM, EM + gauss, EM + BP, VI + gauss, VI + BP], moran_kwargs: dict | None = None, em_kwargs: dict | None = None, vi_kwargs: dict | None = None, bp_kwargs: dict | None = None, certain_mask: numpy.ndarray | None = None, bins: numpy.ndarray | None = None) numpy.ndarray[source]#

Score each pixel by how likely it is a cell. Values returned are in [0, 1].

Parameters:
X

UMI counts per pixel as either a sparse or dense array.

k

Kernel size for convolution.

method

Method to use. Valid methods are: gauss: Gaussian blur moran: Moran’s I based method EM: EM algorithm to estimate cell and background expression

parameters.

EM+gauss: Negative binomial EM algorithm followed by Gaussian blur. EM+BP: EM algorithm followed by belief propagation to estimate the

marginal probabilities of cell and background.

VI+gauss: Negative binomial VI algorithm followed by Gaussian blur.

Note that VI also supports the zero-inflated negative binomial (ZINB) by providing zero_inflated=True.

VI+BP: VI algorithm followed by belief propagation. Note that VI also

supports the zero-inflated negative binomial (ZINB) by providing zero_inflated=True.

moran_kwargs

Keyword arguments to the moran.run_moran() function.

em_kwargs

Keyword arguments to the em.run_em() function.

bp_kwargs

Keyword arguments to the bp.run_bp() function.

certain_mask

A boolean Numpy array indicating which pixels are certain to be occupied, a-priori. For example, if nuclei staining is available, this would be the nuclei segmentation mask.

bins

Pixel bins to segment separately. Only takes effect when the EM algorithm is run.

Returns:

[0, 1] score of each pixel being a cell.

Raises:

SegmentationError – If bins and/or certain_mask was provided but their sizes do not match X

spateo.segmentation.icell.score_and_mask_pixels(adata: anndata.AnnData, layer: str, k: int, method: typing_extensions.Literal[gauss, spateo.segmentation.moran, EM, EM + gauss, EM + BP, VI + gauss, VI + BP], moran_kwargs: dict | None = None, em_kwargs: dict | None = None, vi_kwargs: dict | None = None, bp_kwargs: dict | None = None, threshold: float | None = None, use_knee: bool | None = False, mk: int | None = None, bins_layer: typing_extensions.Literal[False] | str | None = None, certain_layer: str | None = None, scores_layer: str | None = None, mask_layer: str | None = None)[source]#

Score and mask pixels by how likely it is occupied.

Parameters:
adata

Input Anndata

layer

Layer that contains UMI counts to use

k

Kernel size for convolution

method

Method to use to obtain per-pixel scores. Valid methods are: gauss: Gaussian blur moran: Moran’s I based method EM: EM algorithm to estimate cell and background expression

parameters.

EM+gauss: Negative binomial EM algorithm followed by Gaussian blur. EM+BP: EM algorithm followed by belief propagation to estimate the

marginal probabilities of cell and background.

VI+gauss: Negative binomial VI algorithm followed by Gaussian blur.

Note that VI also supports the zero-inflated negative binomial (ZINB) by providing zero_inflated=True.

VI+BP: VI algorithm followed by belief propagation. Note that VI also

supports the zero-inflated negative binomial (ZINB) by providing zero_inflated=True.

moran_kwargs

Keyword arguments to the moran.run_moran() function.

em_kwargs

Keyword arguments to the em.run_em() function.

bp_kwargs

Keyword arguments to the bp.run_bp() function.

threshold

Score cutoff, above which pixels are considered occupied. By default, a threshold is automatically determined by using Otsu thresholding.

use_knee

Whether to use knee point as threshold. By default is False. If True, threshold would be ignored.

mk

Kernel size of morphological open and close operations to reduce noise in the mask. Defaults to k`+2 if EM or VI is run. Otherwise, defaults to `k-2.

bins_layer

Layer containing assignment of pixels into bins. Each bin is considered separately. Defaults to {layer}_bins. This can be set to False to disable binning, even if the layer exists.

certain_layer

Layer containing a boolean mask indicating which pixels are certain to be occupied. If the array is not a boolean array, it is casted to boolean.

scores_layer

Layer to save pixel scores before thresholding. Defaults to {layer}_scores.

mask_layer

Layer to save the final mask. Defaults to {layer}_mask.