Skip to content

Add automatic K selection (CellCharter's ClusterAutoK) for the cellcharter niche flavor #1244

Description

@selmanozleyen

Context

sq.gr.calculate_niche(flavor="cellcharter", ...) fits a GMM with a fixed, user-supplied n_components. CellCharter's own ClusterAutoK instead searches a range of K, refits the model max_runs times per K, scores stability via Fowlkes-Mallows similarity between labelings at K/K-1/K+1, and picks the K with highest stability (best_k, plus a peaks attribute for runner-up candidates). Squidpy's _get_GMM_clusters currently skips this -- its own docstring notes it's "simplified... without stability analysis."

Proposal

Port ClusterAutoK-style automatic K selection, with an input signature that mirrors CellCharter's own API rather than a magic-string trigger -- i.e. take a n_clusters range plus the same knobs CellCharter exposes (max_runs, convergence_tol, similarity_function, model_params), so behavior and defaults line up with the original.

Open question: integrate this as an option inside calculate_niche vs. a separate standalone function (e.g. sq.gr.cluster_auto_k) that returns best_k/stability curve/peaks, to be fed into calculate_niche afterward. Leaning toward separate/composable, since it's a stochastic multi-run search (many GMM refits across a K range) rather than a single deterministic fit, and bolting it into calculate_niche's already-branchy per-flavor validation adds complexity. Not settled yet -- needs discussion.

Dependency

Blocked on / should follow #1192 (refactor of calculate_niche into separate functions) -- final placement of auto-K selection depends on how that refactor shakes out.

cc: @sarajimenez

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions