mantispy.tl.cluster#
- mantispy.tl.cluster(adata, use_rep='X_pca', method='hierarchical', linkage='average', metric='correlation', distance_cut=None, n_clusters=None, criterion='silhouette', resolution=1.0, key_added='cluster', copy=False, *, stability_window=None)[source]#
Cluster the profiles and store the labels, with the linkage tree for a dendrogram.
- Parameters:
- adata
AnnData Profiles to cluster, normally one consensus profile per perturbation from
consensus().- use_rep
str|None(default:'X_pca') Cluster
obsm[use_rep](an embedding such aspca()writes) instead ofX, orNoneforX.- method
str(default:'hierarchical') "hierarchical"(the default) builds a linkage tree with scipy;"leiden"delegates toscanpy.tl.leiden()on the neighbors graph and stores no tree.- linkage
str(default:'average') The scipy linkage method for
method="hierarchical", for example"average","complete"or"ward".- metric
str(default:'correlation') The scipy pairwise distance for
method="hierarchical"."correlation"is1 - Pearsonbetween profiles.- distance_cut
float|None(default:None) Cut the tree at this height. Mutually exclusive with
n_clusters;method="hierarchical"only.- n_clusters
int|None(default:None) Cut the tree into this many clusters. Mutually exclusive with
distance_cut;method="hierarchical"only.- criterion
Literal['silhouette','stability'] (default:'silhouette') Which score picks the automatic cut for
method="hierarchical":"silhouette"(the default) keeps the cut with the best silhouette,"stability"keeps the cut whose cluster membership is most stable across nearby heights. Ignored whendistance_cutorn_clustersis given, or whenmethod != "hierarchical".- resolution
float(default:1.0) Passed to
scanpy.tl.leiden()formethod="leiden".- key_added
str(default:'cluster') obscolumn the labels are written to.- copy
bool(default:False) Return a modified copy instead of mutating in place.
- stability_window
tuple[float,float] |None(default:None) Optional
(low, high)height band thecriterion="stability"sweep is restricted to, in the same height units asdistance_cut(formetric="correlation", height is1 - correlation, so the correlation window 0.4 to 0.7 is(0.3, 0.6)).None(the default) sweeps the full height range. Whenever provided it is validated for shape; it is only applied to thecriterion="stability"automatic cut.
- adata
- Return type:
- Returns:
None, or the modified copy. Writes categorical cluster labels toobs[key_added]. Formethod="hierarchical"it also writes the linkage matrix touns["mantispy"][key_added + "_linkage"]and, touns["mantispy"][key_added], a summary withn_clusters,distance_cut,metric,linkage,silhouette,stabilityand thelabelsthe tree’s leaves carry, in the object’s row order, sodendrogram()can label them.silhouetteis set only when the automaticcriterion="silhouette"cut ran.stabilityis set when thecriterion="stability"auto-cut selects an in-range cut, and staysnanon the degenerate fallback (a flat tree or no in-range cut). The score that did not run staysnan.- Raises:
ValueError –
methodis not one ofMETHODS, bothdistance_cutandn_clustersare given,criterionis not"silhouette"or"stability",stability_windowis not a length-2(low, high)pair of numbers withlow < highor does not overlap the tree’s height range, or the object has fewer than two rows to cluster.
Notes
With neither
distance_cutnorn_clustersthe granularity is chosen automatically. Withcriterion="silhouette"the tree is cut into 2 tomin(n_obs - 1, 25)clusters and the cut with the best silhouette is kept. Withcriterion="stability"a grid of cut heights is swept and the height whose cluster membership recurs most at neighboring heights is kept, the way Rohban 2017 cut their dendrogram. Passstability_windowto restrict that sweep to a height band, so the wide plateau of a few huge clusters near the top of the tree cannot trivially win and collapse the cut; it only affectscriterion="stability". Within a window the criterion favors the finest perfectly-stable cut, since the exact-membership stability saturates to 1.0 across a stable plateau. With a window the sweep is bounded at both ends by the window rather than by themin(n_obs - 1, 25)cluster ceiling, so a windowed stability cut may return more than 25 clusters; only the two-or-more-clusters floor is kept, so set the window low edge above the near-singleton region of the tree or a near-singleton cut can be selected. Rank clusters by the biology they recover rather than trusting the count, since neither score sees biology.