nichesphere.coloc.compute_sc_spatial_knn_coloc_matrix#
- nichesphere.coloc.compute_sc_spatial_knn_coloc_matrix(adata, cluster_col, k=5)#
Compute a kNN-based co-localization probability matrix between cell type pairs.
For each cell, the k nearest spatial neighbors are identified. The co-localization matrix counts how often each cluster appears in the neighborhood of each other cluster, then normalizes counts to obtain interaction probabilities.
- Parameters:
adata (anndata.AnnData) – Spatial AnnData object. Must contain cell centroid coordinates in
adata.obsm['spatial']and a categorical cluster annotation column inadata.obs.cluster_col (str) – Name of the categorical column in
adata.obscontaining cluster labels.k (int, default 5) – Number of nearest spatial neighbors to consider per cell (excluding self).
- Returns:
Symmetric co-localization probability matrix of shape (n_clusters, n_clusters). Entry (i, j) is the probability of finding cluster j in the k-nearest neighborhood of cluster i, normalized so that all entries sum to 1.
- Return type:
pd.DataFrame
Notes
The
cluster_colcolumn must be of dtypecategory; cluster order followsadata.obs[cluster_col].cat.categories.Examples
>>> coloc = compute_sc_spatial_knn_coloc_matrix(adata, cluster_col='cell_type', k=10) >>> sns.heatmap(coloc, cmap='Blues')