spateo.segmentation.utils ========================= .. py:module:: spateo.segmentation.utils .. autoapi-nested-parse:: Utility functions for cell segmentation. Functions --------- .. autoapisummary:: spateo.segmentation.utils.circle spateo.segmentation.utils.knee_threshold spateo.segmentation.utils.gaussian_blur spateo.segmentation.utils.median_blur spateo.segmentation.utils.conv2d spateo.segmentation.utils.scale_to_01 spateo.segmentation.utils.scale_to_255 spateo.segmentation.utils.mclose_mopen spateo.segmentation.utils.apply_threshold spateo.segmentation.utils.safe_erode spateo.segmentation.utils.label_overlap spateo.segmentation.utils.clahe spateo.segmentation.utils.cal_cell_area spateo.segmentation.utils.filter_cell_labels_by_area spateo.segmentation.utils.get_cell_shape Module Contents --------------- .. py:function:: circle(k: int) -> numpy.ndarray Draw a circle of diameter k. :param k: Diameter :returns: 8-bit unsigned integer Numpy array with 1s and 0s :raises ValueError: if `k` is even or less than 1 .. py:function:: knee_threshold(X: numpy.ndarray, n_bins: int = 256, clip: int = 5) -> float 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. :param X: Numpy array of values :param n_bins: Number of bins to use if `X` is a float array. :returns: Knee .. py:function:: gaussian_blur(X: numpy.ndarray, k: int) -> numpy.ndarray Gaussian blur This function is not designed to be called directly. Use :func:`conv2d` with `mode="gauss"` instead. :param X: UMI counts per pixel. :param k: Radius of gaussian blur. :returns: Blurred array .. py:function:: median_blur(X: numpy.ndarray, k: int) -> numpy.ndarray Median blur This function is not designed to be called directly. Use :func:`conv2d` with `mode="median"` instead. :param X: UMI counts per pixel. :param k: Radius of median blur. :returns: Blurred array .. py:function:: conv2d(X: numpy.ndarray, k: int, mode: typing_extensions.Literal[gauss, median, circle, square], bins: Optional[numpy.ndarray] = None) -> numpy.ndarray Convolve an array with the specified kernel size and mode. :param X: The array to convolve. :param k: Kernel size. Must be odd. :param mode: Convolution mode. Supported modes are: gauss: circle: square: :param 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` .. py:function:: scale_to_01(X: numpy.ndarray) -> numpy.ndarray Scale an array to [0, 1]. :param X: Array to scale :returns: Scaled array .. py:function:: scale_to_255(X: numpy.ndarray) -> numpy.ndarray Scale an array to [0, 255]. :param X: Array to scale :returns: Scaled array .. py:function:: mclose_mopen(mask: numpy.ndarray, k: int, square: bool = False) -> numpy.ndarray Perform morphological close and open operations on a boolean mask. :param X: Boolean mask :param k: Kernel size :param 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 .. py:function:: apply_threshold(X: numpy.ndarray, k: int, threshold: Optional[Union[float, numpy.ndarray]] = None) -> numpy.ndarray Apply a threshold value to the given array and perform morphological close and open operations. :param X: The array to threshold :param k: Kernel size of the morphological close and open operations. :param threshold: Threshold to apply. By default, the knee is used. :returns: A boolean mask. .. py:function:: safe_erode(X: numpy.ndarray, k: int, square: bool = False, min_area: int = 1, n_iter: int = -1, float_k: Optional[int] = None, float_threshold: Optional[float] = None) -> numpy.ndarray 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. :param X: Array to erode. :param k: Erosion kernel size :param square: Whether to use a square kernel :param min_area: Minimum area :param n_iter: Number of erosions to perform. If -1, then erosion is continued until every connected component is <= `min_area`. :param float_k: Morphological close and open kernel size when `X` is a float array. :param 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 .. py:function:: label_overlap(X: numpy.ndarray, Y: numpy.ndarray) -> scipy.sparse.csr_matrix 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. :param X: First label array. Labels in this array are the rows of the resulting array. :param 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. .. py:function:: clahe(X: numpy.ndarray, clip_limit: float = 1.0, tile_grid: Tuple[int, int] = (100, 100)) -> numpy.ndarray Contrast-limited adaptive histogram equalization (CLAHE). :param X: Image to equalize :param clip_limit: Contrast clipping. Lower values retain more of homogeneous regions. :param tile_grid: Apply histogram equalization to tiles of this size. :returns: Equalized image .. py:function:: cal_cell_area(cell_labels: numpy.ndarray) Calculate spot numbers for each cell. :param cell_labels: cell labels. :returns: dict .. py:function:: filter_cell_labels_by_area(adata: anndata.AnnData, layer: str, area_cutoff: int = 7) Filter out cells with area less than `area_cutoff` :param adata: Input Anndata :param layer: Layer that contains UMI counts to use :param area_cutoff: cells with area less than this cutoff would be dicarded. .. py:function:: get_cell_shape(adata: anndata.AnnData, layer: str, thickness: int = 1, out_layer: Optional[str] = None) Set cell boundaries as 255 with thickness as `thickness`. :param adata: Input Anndata :param layer: Layer that contains cell labels to use :param thickness: The thickness of cell boundaries :param out_layer: Layer to save results. By default, this will be `{layer}_boundary`.