spateo.segmentation.icell ========================= .. py:module:: spateo.segmentation.icell .. autoapi-nested-parse:: 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. Functions --------- .. autoapisummary:: spateo.segmentation.icell._mask_cells_from_stain spateo.segmentation.icell._mask_nuclei_from_stain spateo.segmentation.icell.mask_cells_from_stain spateo.segmentation.icell.mask_nuclei_from_stain spateo.segmentation.icell._initial_nb_params spateo.segmentation.icell._score_pixels spateo.segmentation.icell.score_and_mask_pixels Module Contents --------------- .. py:function:: _mask_cells_from_stain(X: numpy.ndarray, otsu_classes: int = 3, otsu_index: int = 0, mk: int = 7) -> numpy.ndarray Create a boolean mask indicating cells from stained image. .. py:function:: _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 Create a boolean mask indicating nuclei from stained nuclei image. See :func:`mask_nuclei_from_stain` for arguments. .. py:function:: 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: Optional[str] = None) Create a boolean mask indicating cells from stained image. :param adata: Input Anndata :param otsu_classes: Number of classes to assign pixels to for cell detection :param otsu_index: Which threshold index should be used for classifying cells. All pixel intensities >= the value at this index will be classified as cell. :param mk: Size of the kernel used for morphological close and open operations applied at the very end. :param layer: Layer that contains staining image. :param out_layer: Layer to put resulting nuclei mask. Defaults to `{layer}_mask`. .. py:function:: 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: Optional[str] = None) Create a boolean mask indicating nuclei from stained nuclei image, and save this mask in the AnnData as an additional layer. :param adata: Input Anndata :param otsu_classes: Number of classes to assign pixels to for background detection. :param 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. :param local_k: The size of the local neighborhood of each pixel to use for local (adaptive) thresholding to identify the foreground (i.e. nuclei). :param 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. :param mk: Size of the kernel used for morphological close and open operations applied at the very end. :param layer: Layer containing nuclei staining :param out_layer: Layer to put resulting nuclei mask. Defaults to `{layer}_mask`. .. py:function:: _initial_nb_params(X: numpy.ndarray, bins: Optional[numpy.ndarray] = None) -> Union[Dict[str, Tuple[float, float]], Dict[int, Dict[str, Tuple[float, float]]]] Calculate initial estimates for the negative binomial mixture. :param X: UMI counts :param 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. .. py:function:: _score_pixels(X: Union[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: Optional[dict] = None, em_kwargs: Optional[dict] = None, vi_kwargs: Optional[dict] = None, bp_kwargs: Optional[dict] = None, certain_mask: Optional[numpy.ndarray] = None, bins: Optional[numpy.ndarray] = None) -> numpy.ndarray Score each pixel by how likely it is a cell. Values returned are in [0, 1]. :param X: UMI counts per pixel as either a sparse or dense array. :param k: Kernel size for convolution. :param 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`. :param moran_kwargs: Keyword arguments to the :func:`moran.run_moran` function. :param em_kwargs: Keyword arguments to the :func:`em.run_em` function. :param bp_kwargs: Keyword arguments to the :func:`bp.run_bp` function. :param 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. :param 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` .. py:function:: 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: Optional[dict] = None, em_kwargs: Optional[dict] = None, vi_kwargs: Optional[dict] = None, bp_kwargs: Optional[dict] = None, threshold: Optional[float] = None, use_knee: Optional[bool] = False, mk: Optional[int] = None, bins_layer: Optional[Union[typing_extensions.Literal[False], str]] = None, certain_layer: Optional[str] = None, scores_layer: Optional[str] = None, mask_layer: Optional[str] = None) Score and mask pixels by how likely it is occupied. :param adata: Input Anndata :param layer: Layer that contains UMI counts to use :param k: Kernel size for convolution :param 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`. :param moran_kwargs: Keyword arguments to the :func:`moran.run_moran` function. :param em_kwargs: Keyword arguments to the :func:`em.run_em` function. :param bp_kwargs: Keyword arguments to the :func:`bp.run_bp` function. :param threshold: Score cutoff, above which pixels are considered occupied. By default, a threshold is automatically determined by using Otsu thresholding. :param use_knee: Whether to use knee point as threshold. By default is False. If True, threshold would be ignored. :param 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. :param 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. :param 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. :param scores_layer: Layer to save pixel scores before thresholding. Defaults to `{layer}_scores`. :param mask_layer: Layer to save the final mask. Defaults to `{layer}_mask`.