Goal
Transfer cell labels from a reference AnnData to a query AnnData using a shared latent space such as X_glue, then write predicted labels and uncertainty scores back to the query object. Keep this as one skill: the notebook's plots are optional presentation on top of the same transfer job, not a separate capability.
Quick Workflow
- Confirm the query and reference expose the same embedding basis in
.obsm, or fall back toX. - Confirm the reference has the target label column in
.obs. - Train
ov.utils.weighted_knn_trainer(...)on the reference embedding. - Transfer labels with
ov.utils.weighted_knn_transfer(...). - Store the first label column and uncertainty column in
query_adata.obs. - Plot on an existing basis for smoke validation; use
mdeonly ifpymdeis available.
Interface Summary
weighted_knn_trainer(train_adata, train_adata_emb, n_neighbors=50)builds the reference KNN model.weighted_knn_transfer(query_adata, query_adata_emb, ref_adata_obs, label_keys, knn_model, threshold=1, pred_unknown=False, mode='package')returns prediction and uncertainty DataFrames.embedding(adata, basis, ..., show=None, save=None, return_fig=None, ...)plots any existing basis.mde(data, device=None, **kwargs)is optional; it depends onpymdeand is only for visualization.
Branch Selection
- Use
mode='package'for transfer. The current source only implements this branch;mode='paper'is documented in the docstring but raises in the live code. - Leave
pred_unknown=Falsefor the default label-assignment path. - Set
pred_unknown=Trueonly when you want threshold-basedUnknownlabels. - Use
mdeonly as an optional visual branch. Ifpymdeis missing, skip it and plot onX_glueor another precomputed basis instead.
Input Contract
train_adataandquery_adatamust expose the same latent basis name in.obsm, or useX.ref_adata_obs[label_keys]must exist; the function selects columns whose names start withlabel_keys.- If you want one label column, make the prefix unique.
- The notebook's
transf_celltypeandtransf_celltype_uncare the reuse convention for storing outputs on the query object.
Minimal Execution Patterns
Core transfer:
knn = ov.utils.weighted_knn_trainer(train_adata=rna, train_adata_emb="X_glue", n_neighbors=15)
labels, uncert = ov.utils.weighted_knn_transfer(
query_adata=atac,
query_adata_emb="X_glue",
ref_adata_obs=rna.obs,
label_keys="major_celltype",
knn_model=knn,
mode="package",
)
atac.obs["transf_celltype"] = labels.loc[atac.obs.index, "major_celltype"]
atac.obs["transf_celltype_unc"] = uncert.loc[atac.obs.index, "major_celltype"]
Optional smoke plot:
ov.utils.embedding(
atac,
basis="X_glue",
color="transf_celltype",
show=False,
)
Validation
labelsanduncertmust both be DataFrames indexed byquery_adata.obs_names.- The expected label column should appear in both outputs.
query_adata.obs["transf_celltype"]andquery_adata.obs["transf_celltype_unc"]should exist after writeback.- If you call
mde, verifypymdeis installed first; otherwise skip it. - For plots, confirm the basis you pass actually exists in
.obsm.
Resource Map
- Read
references/integration-method-selection.mdfor skill boundary and branch selection. - Read
references/source-notebook-map.mdto map notebook cells to the reusable workflow. - Read
references/source-grounding.mdfor inspected signatures and source behavior. - Read
references/compatibility.mdfor known runtime caveats.