spateo.segmentation.label ========================= .. py:module:: spateo.segmentation.label .. autoapi-nested-parse:: Functions for use when labeling individual nuclei/cells, after obtaining a mask. Functions --------- .. autoapisummary:: spateo.segmentation.label._replace_labels spateo.segmentation.label.replace_labels spateo.segmentation.label._watershed spateo.segmentation.label.find_peaks_with_erosion spateo.segmentation.label.watershed spateo.segmentation.label._expand_labels spateo.segmentation.label.expand_labels spateo.segmentation.label._label_connected_components spateo.segmentation.label.label_connected_components spateo.segmentation.label._find_peaks spateo.segmentation.label.find_peaks spateo.segmentation.label.find_peaks_from_mask spateo.segmentation.label._augment_labels spateo.segmentation.label.augment_labels Module Contents --------------- .. py:function:: _replace_labels(labels: numpy.ndarray, mapping: Dict[int, int]) -> numpy.ndarray Replace labels according to mapping. :param labels: Numpy array containing integer labels. :param mapping: Dictionary mapping from labels to labels. :returns: Replaced labels .. py:function:: replace_labels(adata: anndata.AnnData, layer: str, mapping: Dict[int, int], out_layer: Optional[str] = None) Replace labels according to mapping. :param adata: Input Anndata :param layer: Layer containing labels to replace :param mapping: Dictionary mapping that defines label replacement. :param out_layer: Layer to save results. By default, the input layer is overridden. .. py:function:: _watershed(X: numpy.ndarray, mask: numpy.ndarray, markers: numpy.ndarray, k: int) -> numpy.ndarray Assign individual nuclei/cells using the Watershed algorithm. :param X: Data array. This array will be Gaussian blurred and used as the input values to Watershed. :param mask: Nucleus/cell mask. :param 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`. :param k: Size of the kernel to use for Gaussian blur. :returns: Watershed labels. .. py:function:: 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: Optional[float] = None, out_layer: Optional[str] = None) Find peaks for use in Watershed via iterative erosion. :param adata: Input Anndata :param 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. :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. :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. By default, a threshold is automatically determined by using Otsu method. :param out_layer: Layer to save results. By default, this will be `{layer}_markers`. .. py:function:: watershed(adata: anndata.AnnData, layer: str = SKM.STAIN_LAYER_KEY, k: int = 3, mask_layer: Optional[str] = None, markers_layer: Optional[str] = None, out_layer: Optional[str] = None) Assign individual nuclei/cells using the Watershed algorithm. :param adata: Input AnnData :param layer: Original data layer from which segmentation will derive from. :param k: Size of the kernel to use for Gaussian blur. :param mask_layer: Layer containing mask. This will default to `{layer}_mask`. :param 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`. :param out_layer: Layer to save results. Defaults to `{layer}_labels`. .. py:function:: _expand_labels(labels: numpy.ndarray, distance: int, max_area: int, mask: Optional[numpy.ndarray] = None) -> numpy.ndarray Expand labels up to a certain distance, while ignoring labels that are above a certain size. :param labels: Numpy array containing integer labels. :param distance: Distance to expand. Internally, this is used as the number of iterations of distance 1 dilations. :param max_area: Maximum area of each label. :param mask: Only expand within the provided mask. :returns: New label array with expanded labels. .. py:function:: expand_labels(adata: anndata.AnnData, layer: str, distance: int = 5, max_area: int = 400, mask_layer: Optional[str] = None, out_layer: Optional[str] = None) Expand labels up to a certain distance. :param adata: Input Anndata :param 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. :param distance: Distance to expand. Internally, this is used as the number of iterations of distance 1 dilations. :param max_area: Maximum area of each label. :param mask_layer: Layer containing mask to restrict expansion to within. :param out_layer: Layer to save results. By default, uses `{layer}_labels_expanded`. .. py:function:: _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: Optional[numpy.ndarray] = None) -> numpy.ndarray Label connected components while splitting components that are too large. :param X: Boolean mask to compute connected components from. :param area_threshold: Connected components with area greater than this value will be split into smaller portions by first eroding and then expanding. :param k: Kernel size for erosion. :param min_area: Don't erode labels smaller than this area. :param n_iter: Number of erosion operations. -1 means continue eroding until every label is less than `min_area`. :param distance: Distance to expand eroded labels. :param max_area: Maximum area when expanding labels. :param seed_labels: Seed labels. :returns: New label array .. py:function:: label_connected_components(adata: anndata.AnnData, layer: str, seed_layer: Optional[str] = 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: Optional[str] = None) Label connected components while splitting components that are too large. :param adata: Input Anndata :param layer: Data layer that was used to generate the mask. First, will look for `{layer}_mask`. Otherwise, this will be use as a literal. :param seed_layer: Layer containing seed labels. These are labels that should be used whenever possible in labeling connected components. :param area_threshold: Connected components with area greater than this value will be split into smaller portions by first eroding and then expanding. :param k: Kernel size for erosion. :param min_area: Don't erode labels smaller than this area. :param n_iter: Number of erosion operations. -1 means continue eroding until every label is less than `min_area`. :param distance: Distance to expand eroded labels. :param max_area: Maximum area when expanding labels. :param out_layer: Layer to save results. Defaults to `{layer}_labels`. :returns: New label array .. py:function:: _find_peaks(X: numpy.ndarray, **kwargs) -> Tuple[numpy.ndarray, numpy.ndarray] Find peaks from an arbitrary image. This function is a wrapper around :func:`feature.peak_local_max`. :param X: Array to find peaks from :param \*\*kwargs: Keyword arguments to pass to :func:`feature.peak_local_max`. :returns: Numpy array of the same size as `X` where each peak is labeled with a unique positive integer. .. py:function:: find_peaks(adata: anndata.AnnData, layer: str, k: int, min_distance: int, mask_layer: Optional[str] = None, out_layer: Optional[str] = None) Find peaks from an array. :param adata: Input AnnData :param layer: Layer to use as values to find peaks from. :param k: Apply a Gaussian blur with this kernel size prior to peak detection. :param min_distance: Minimum distance, in pixels, between peaks. :param mask_layer: Find peaks only in regions specified by the mask. :param out_layer: Layer to save identified peaks as markers. By default, uses `{layer}_markers`. .. py:function:: find_peaks_from_mask(adata: anndata.AnnData, layer: str, min_distance: int, distances_layer: Optional[str] = None, markers_layer: Optional[str] = None) Find peaks from a boolean mask. Used to obatin Watershed markers. :param adata: Input AnnData :param layer: Layer containing boolean mask. This will default to `{layer}_mask`. If not present in the provided AnnData, this argument used as a literal. :param min_distance: Minimum distance, in pixels, between peaks. :param 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`. :param markers_layer: Layer to save identified peaks as markers. By default, uses `{layer}_markers`. .. py:function:: _augment_labels(source_labels: numpy.ndarray, target_labels: numpy.ndarray) -> numpy.ndarray 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. :param source_labels: Numpy array containing labels to (possibly) transfer. :param target_labels: Numpy array containing labels to augment. :returns: New Numpy array containing augmented labels. .. py:function:: augment_labels(adata: anndata.AnnData, source_layer: str, target_layer: str, out_layer: Optional[str] = None) Augment the labels in one label array using the labels in another. :param adata: Input Anndata :param source_layer: Layer containing source labels to (possibly) take labels from. :param target_layer: Layer containing target labels to augment. :param out_layer: Layer to save results. Defaults to `{target_layer}_augmented`.