spateo.digitization.contour

Written by @Jinerhal, adapted by @Xiaojieqiu.

Functions

gen_cluster_image(→ numpy.ndarray)

Generate matrix/image of spatial clusters with distinct labels/colors.

extract_cluster_contours(→ Tuple[Tuple, numpy.ndarray, ...)

Extract contour(s) for area(s) formed by buckets of the same spatial cluster.

set_domains(→ None)

Set the domains for each bucket based on spatial clusters. Use adata object of low resolution for contour

Module Contents

spateo.digitization.contour.gen_cluster_image(adata: anndata.AnnData, bin_size: int | None = None, spatial_key: str = 'spatial', cluster_key: str = 'scc', label_mapping_key: str = 'cluster_img_label', cmap: str = 'tab20', show: bool = True) numpy.ndarray[source]

Generate matrix/image of spatial clusters with distinct labels/colors.

Parameters:
adata

The adata object used to create the matrix/image for clusters.

bin_size

The size of the binning.

spatial_key

The key name of the spatial coordinates in adata.obs

cluster_key

The key name of the spatial cluster in adata.obs

label_mapping_key

The key name to store the label index values, mapped from the cluster names in adata.obs. Note that background is 0 so label_mapping_key starts from 1.

cmap

The colormap that will be used to draw colors for the resultant cluster image.

show

Whether to visualize the cluster image.

Returns:

A numpy array that stores the image of clusters, each with a distinct color. When show

is True, plt.imshow(cluster_rgb_image) will be used to plot the clusters each with distinct labels prepared from the designated cmap.

Return type:

cluster_label_image

spateo.digitization.contour.extract_cluster_contours(cluster_label_image: numpy.ndarray, cluster_labels: int | List, bin_size: int, k_size: float = 2, min_area: float = 9, close_kernel: int = cv2.MORPH_ELLIPSE, show: bool = True) Tuple[Tuple, numpy.ndarray, numpy.ndarray][source]

Extract contour(s) for area(s) formed by buckets of the same spatial cluster.

Parameters:
cluster_label_image

the image that sets the pixels of the cluster of interests as the front color (background is 0).

cluster_labels

The label value(s) of clusters of interests.

bin_size

The size of the binning.

k_size

Kernel size of the elliptic structuring element.

min_area

Minimal area threshold corresponding to the resulting contour(s).

close_kernel

The value to indicate the structuring element. By default, we use a circular structuring element.

show

Visualize the result.

Returns:

The Tuple coordinates of contours identified. cluster_image_close: The resultant image of the area of interest with small area removed. cluster_image_contour: The resultant image of the contour, generated from cluster_image_close.

Return type:

contours

spateo.digitization.contour.set_domains(adata_high_res: anndata.AnnData, adata_low_res: anndata.AnnData | None = None, spatial_key: str = 'spatial', cluster_key: str = 'scc', domain_key_prefix: str = 'domain', bin_size_high: int | None = None, bin_size_low: int | None = None, k_size: float = 2, min_area: float = 9) None[source]
Set the domains for each bucket based on spatial clusters. Use adata object of low resolution for contour

identification but adata object of high resolution for domain assignment.

Parameters:
adata_high_res

The anndata object in high spatial resolution. The adata with smaller binning (or single cell segmetnation) is more suitable to define more fine grained spatial domains.

adata_low_res

The anndata object in low spatial resolution. When using data with big binning, it can often produce better spatial domain clustering results with the scc method and thus domain/domain contour identification.

spatial_key

The key in .obsm of the spatial coordinate for each bucket. Should be same key in both adata_high_res and adata_low_res.

cluster_key

The key in .obs (adata_low_res) to the spatial cluster.

domain_key_prefix

The key prefix in .obs (in adata_high_res) that will be used to store the spatial domain for each bucket. The full key name will be set as: domain_key_prefix + “_” + cluster_key.

bin_size_low

The binning size of the adata_high_res object.

bin_size_low

The binning size of the adata_low_res object (only works when adata_low_res is provided).

k_size

Kernel size of the elliptic structuring element.

min_area

Minimal area threshold corresponding to the resulting contour(s).

Returns:

Nothing but update the adata_high_res with the domain in domain_key_prefix + “_” + cluster_key.