spatialvi.external.Nolan#
- class spatialvi.external.Nolan(adata, emb_key='X_scVI', spatial_key='spatial', batch_key=None, num_niches=50, **model_kwargs)[source]#
Bases:
objectHigh-level interface to the NOLAN spatial niche model.
NOLAN (NO Label ANalysis) uses self-supervised learning to identify spatial niches without requiring cell type annotations.
- Parameters:
adata¶ (
AnnData) – AnnData object containing spatial transcriptomic data. It must have a low-dimensional embedding inobsm[emb_key]and spatial coordinates inobsm[spatial_key].emb_key¶ (
str) – Key inadata.obsmpointing to a cell embedding. Defaults to"X_scVI".spatial_key¶ (
str) – Key inadata.obsmwith spatial coordinates. Defaults to"spatial".batch_key¶ (
str|None) – Optional key inadata.obsspecifying slide or batch identifiers. If provided, NOLAN will compute a grid size per batch.num_niches¶ (
int) – Dimensionality of the niche representation (i.e. number of niches).**model_kwargs¶ (
Any) – Additional keyword arguments forwarded tonolan.tl.NOLAN.adata (AnnData)
emb_key (str)
spatial_key (str)
batch_key (str | None)
num_niches (int)
model_kwargs (Any)
Notes
NOLAN is distributed separately from spatialvi-tools. To use this wrapper you must install the
nolanpackage (e.g. viapip install nolan). If the dependency is missing, the constructor will still succeed but calls totrain()andpredict()will raiseImportError.Examples
>>> import scanpy as sc >>> from spatialvi.external import Nolan >>> adata = sc.read("spatial_data.h5ad") >>> # Ensure embeddings exist >>> assert "X_scVI" in adata.obsm >>> model = Nolan(adata, num_niches=20) >>> model.train(num_epochs=50) >>> adata = model.predict()
- __init__(adata, emb_key='X_scVI', spatial_key='spatial', batch_key=None, num_niches=50, **model_kwargs)[source]#
Methods
__init__(adata[, emb_key, spatial_key, ...])get_niche_assignments([adata, n_clusters, ...])Cluster cells into discrete niches based on NOLAN embeddings.
predict([adata, batch_size, store_key])Generate niche embeddings for the given AnnData.
train([ckpt_dir, num_epochs])Fit the NOLAN model to the stored AnnData.
- train(ckpt_dir=None, num_epochs=10, **train_kwargs)[source]#
Fit the NOLAN model to the stored AnnData.
- Parameters:
- Return type:
Notes
This method delegates to
nolan.tl.NOLANandnolan.tl.NOLAN.fit. Internally it computes a global crop radius and number of cells in global crops usingnolan.pp.find_grid_size.
- predict(adata=None, batch_size=2048, store_key='X_nolan', **kwargs)[source]#
Generate niche embeddings for the given AnnData.
- Parameters:
adata¶ (
AnnData|None) – The AnnData on which to perform prediction. IfNone, the training AnnData is used.store_key¶ (
str) – Name under which to store the embeddings inadata.obsm.**kwargs¶ (
Any) – Additional keyword arguments forwarded tonolan.tl.NOLAN.predict.adata (AnnData | None)
batch_size (int)
store_key (str)
kwargs (Any)
- Return type:
- Returns:
AnnData with a new
obsm[store_key]containing the predictedniche embeddings.
- get_niche_assignments(adata=None, n_clusters=None, resolution=1.0, store_key='niche_cluster')[source]#
Cluster cells into discrete niches based on NOLAN embeddings.
- Parameters:
adata¶ (
AnnData|None) – AnnData object. If None, uses the training data.n_clusters¶ (
int|None) – Number of clusters. If None, uses Leiden clustering.resolution¶ (
float) – Resolution parameter for Leiden clustering.adata (AnnData | None)
n_clusters (int | None)
resolution (float)
store_key (str)
- Return type:
- Returns:
AnnData with niche cluster assignments in
obs[store_key].