Spatial niche detection
easydecon includes simple spatial niche utilities that cluster local composition profiles derived from posterior-like matrices.
Public functions:
detect_spatial_niches_from_posteriorsdetect_niches_from_easydecon_resultsummarize_niche_compositionsplot_niche_compositions
Inputs
detect_spatial_niches_from_posteriors accepts either a pandas DataFrame or an
EasyDeconResult-like object. If a result object has posterior_df=None, pass
use_assignment_if_no_posterior=True to use assignment_df instead.
Rows are aligned to table.obs.index. Spatial coordinates are read from
table.obsm["spatial"], with a fallback to table.obs[["x", "y"]] when those
columns exist.
Smoothing and clustering
n_neighborsNumber of spatial nearest neighbors for optional local averaging.
smoothIf
True, cluster neighborhood-smoothed profiles.n_nichesFixed number of niche clusters when
auto_n_niches=False.auto_n_niches,n_niches_min,n_niches_max,selection_metricOptional k selection using
silhouetteorinertia.random_stateSeed passed to k-means.
add_to_obsIf
True, writes labels totable.obs[niches_column].
Example
niches, smoothed, diagnostics = ed.detect_niches_from_easydecon_result(
sdata,
result,
n_neighbors=6,
auto_n_niches=True,
n_niches_min=2,
n_niches_max=8,
return_diagnostics=True,
)
composition = ed.summarize_niche_compositions(smoothed, niches)
fig, ax = ed.plot_niche_compositions(smoothed, niches)
niches is a DataFrame with one categorical label column. smoothed is the
matrix used for clustering. Niche IDs are categorical labels and do not imply
an ordering.
Spatial niche clustering is exploratory. It depends on posterior quality, coordinate quality, neighborhood size, and the chosen number of clusters.