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: object

High-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 in obsm[emb_key] and spatial coordinates in obsm[spatial_key].

  • emb_key (str) – Key in adata.obsm pointing to a cell embedding. Defaults to "X_scVI".

  • spatial_key (str) – Key in adata.obsm with spatial coordinates. Defaults to "spatial".

  • batch_key (str | None) – Optional key in adata.obs specifying 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 to nolan.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 nolan package (e.g. via pip install nolan). If the dependency is missing, the constructor will still succeed but calls to train() and predict() will raise ImportError.

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]#
Parameters:
Return type:

None

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:
  • ckpt_dir (str | None) – Directory in which model checkpoints will be stored. If None, checkpoints are not saved.

  • num_epochs (int) – Number of training epochs.

  • **train_kwargs (Any) – Additional keyword arguments forwarded to the training method.

  • ckpt_dir (str | None)

  • num_epochs (int)

  • train_kwargs (Any)

Return type:

None

Notes

This method delegates to nolan.tl.NOLAN and nolan.tl.NOLAN.fit. Internally it computes a global crop radius and number of cells in global crops using nolan.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. If None, the training AnnData is used.

  • batch_size (int) – Batch size used during prediction.

  • store_key (str) – Name under which to store the embeddings in adata.obsm.

  • **kwargs (Any) – Additional keyword arguments forwarded to nolan.tl.NOLAN.predict.

  • adata (AnnData | None)

  • batch_size (int)

  • store_key (str)

  • kwargs (Any)

Return type:

AnnData

Returns:

  • AnnData with a new obsm[store_key] containing the predicted

  • niche 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.

  • store_key (str) – Key to store cluster assignments in obs.

  • adata (AnnData | None)

  • n_clusters (int | None)

  • resolution (float)

  • store_key (str)

Return type:

AnnData

Returns:

AnnData with niche cluster assignments in obs[store_key].