mantispy.pp.harmony#
- mantispy.pp.harmony(adata, batch_key='Metadata_Batch', use_rep='X_pca', key_added='X_harmony', max_iter=20, seed=0, copy=False, **harmony_kwargs)#
Correct an embedding for batch with Harmony.
Harmony ranked in the top three in every scenario of the batch-correction benchmark of Arevalo et al. [2024], as did Seurat RPCA. It is the last step of the JUMP profiling recipe for compound and ORF profiles. It iterates soft clustering and per-cluster linear correction on an embedding, so it writes a corrected
obsmand leavesXunchanged.- Parameters:
adata (
AnnData) – Object holding the embedding to correct.batch_key (
str(default:'Metadata_Batch')) –obscolumn naming the nuisance grouping, such as the batch, plate or imaging site.use_rep (
str(default:'X_pca')) – Embedding to correct, normallysc.pp.pca’s output.key_added (
str(default:'X_harmony')) –obsmkey for the corrected embedding.max_iter (
int(default:20)) – Harmony iterations.seed (
int(default:0)) – Seed, passed through asrandom_state.copy (
bool(default:False)) – Return a modified copy instead of writing in place.harmony_kwargs (
Any) – Passed toharmonypy.run_harmony, for exampletheta,nclustorsigma.
- Return type:
- Returns:
None, or the modified copy. Writesobsm[key_added].- Raises:
ImportError – If
harmonypyis not installed.KeyError – If
use_repis not inobsm, orbatch_keyis not anobscolumn.ValueError – If
batch_keyhas a single level, leaving nothing to correct for.
Notes
Requires
harmonypy:pip install 'mantispy[harmony]'.harmonypy can report convergence and return the embedding unchanged; this wrapper warns when it does. What decides this is separability rather than size: it corrected the embedding of 50 640 JUMP TARGET2 wells across ten imaging sites, and returns the input untouched when the batches occupy disjoint regions of the embedding, because every soft cluster then holds a single batch.
Check the result with more than one metric. On those JUMP wells Harmony moved the site centroids 36% closer together (mean separation 300 to 192, with unchanged overall spread) but lowered iLISI from 2.01 to 1.02, so the sites moved together globally while neighborhoods stayed site-pure. Batch metrics often disagree like this, which is why
evaluate_correction()reports several and takes amap_key.