COSIE.downstream_analysis.cluster_and_visualize_superpixel

cluster_and_visualize_superpixel(final_embeddings, data_dict, n_clusters, mode='joint', defined_labels=None, vis_basis='spatial', random_state=0, colormap=None, swap_xy=False, invert_x=False, invert_y=False, offset=False, save_path=None, dpi=300, remove_title=False, remove_legend=False, remove_spine=False, figscale=35)[source]

Perform clustering on superpixel embeddings across multiple tissue sections and visualize the results.

Supports three clustering modes:

  • ‘joint’: All sections’ embeddings are clustered together.

  • ‘independent’: Each section is clustered independently.

  • ‘defined’: Uses user-specified cluster labels.

Parameters

final_embeddingsdict

Dictionary of {section_id: np.ndarray} representing cell embeddings.

data_dictdict

Dictionary of {modality: list of AnnData}, where each AnnData contains spatial coordinates.

n_clustersint

Number of clusters to generate.

modestr, default “joint”

Clustering mode: “joint”, “independent”, or “defined”.

defined_labelsdict or None

Required if mode is “defined”. A dictionary of {section_id: np.ndarray of cluster labels}.

vis_basisstr, default “spatial”

Key in obsm indicating spatial coordinates.

random_stateint, default 0

Random seed for KMeans clustering.

colormapstr or list or None

Color map used to assign RGB colors to clusters.

swap_xybool, default False

Whether to swap x and y coordinates.

invert_xbool, default False

Whether to flip the image horizontally.

invert_ybool, default False

Whether to flip the image vertically.

offsetbool, default False

Whether to shift coordinates to (0, 0).

save_pathstr or None, default None

If specified, saves the figure(s) with this filename prefix.

dpiint, default 300

DPI for the saved figure.

remove_titlebool, default False

Whether to remove figure title.

remove_legendbool, default False

Whether to remove cluster legend.

remove_spinebool, default False

Whether to remove axis borders.

figscaleint, default 35

Controls image figure size.

Returns

cluster_labelsdict

Dictionary of {section_id: np.ndarray of cluster labels}.