mantispy.tl.nn_moa_classify#

mantispy.tl.nn_moa_classify(adata, moa_key='Metadata_MOA', metric='cosine', scheme='nsc', compound_key='Metadata_Compound', batch_key='Metadata_Batch', use_rep=None, key_added='moa', copy=False)[source]#

Leave-one-out nearest-neighbor mechanism assignment.

Parameters:
adata AnnData

Profiles to classify, one row per treatment or per well.

moa_key str (default: 'Metadata_MOA')

obs column holding the known mechanism.

metric str (default: 'cosine')

Similarity between profiles, "cosine" or "pearson".

scheme str (default: 'nsc')

"nn" allows any neighbor, which is usually optimistic because a compound can match itself at another dose. "nsc" (not-same-compound) excludes neighbors of the same compound, as in the published BBBC021 benchmark. "nscb" also excludes neighbors from the same batch, so a batch effect cannot produce the match.

compound_key str (default: 'Metadata_Compound')

obs column read for the nsc and nscb exclusions.

batch_key str (default: 'Metadata_Batch')

obs column read for the nscb exclusion.

use_rep str | None (default: None)

Classify obsm[use_rep] instead of X.

key_added str (default: 'moa')

Name for the outputs.

copy bool (default: False)

Return a modified copy instead of mutating in place.

Return type:

AnnData | None

Returns:

None, or the modified copy. Writes obs[key_added + "_predicted"], a summary at uns["mantispy"][key_added] (accuracy, n_classified, n_excluded, scheme) and a tidy true/predicted/count table at uns["mantispy"][key_added + "_confusion"].

Raises:
  • ValueError – scheme is not one of SCHEMES.

  • KeyError – obs has no moa_key, or no column that the chosen scheme excludes neighbors on.

Notes

A row with no admissible neighbor, for example under "nscb" on a single batch, is left unclassified and counted in n_excluded. Chance level is 1/n_classes only when the classes are balanced; otherwise compare against the largest class’s share.

A profile with no mechanism on file is neither scored nor used as a neighbor, so the accuracy does not depend on the annotated fraction.