spatialvi.external.DestVI#

class spatialvi.external.DestVI(st_adata, sc_model, **kwargs)[source]#

Bases: object

Wrapper for scvi-tools DestVI model.

DestVI performs multi-resolution spatial deconvolution, estimating both cell type proportions and continuous sub-cell-type variation within spatial transcriptomics spots.

This is a thin wrapper around the scvi-tools implementation that provides a consistent interface with spatialvi-tools.

Parameters:
  • st_adata (AnnData) – Spatial transcriptomics AnnData.

  • sc_model – Trained CondSCVI model on reference single-cell data.

  • **kwargs – Additional keyword arguments for scvi.model.DestVI.

  • st_adata (AnnData)

Examples

>>> import spatialvi
>>> # First train CondSCVI on reference
>>> sc_adata = spatialvi.data.synthetic_scrna()
>>> DestVI.setup_anndata(sc_adata, labels_key="cell_type")
>>> sc_model = spatialvi.external.CondSCVI(sc_adata)
>>> sc_model.train()
>>> # Then train DestVI on spatial
>>> st_adata = spatialvi.data.synthetic_spatial()
>>> model = DestVI.from_rna_model(st_adata, sc_model)
>>> model.train()
>>> proportions = model.get_proportions()
__init__(st_adata, sc_model, **kwargs)[source]#
Parameters:

st_adata (AnnData)

Methods

__init__(st_adata, sc_model, **kwargs)

from_rna_model(st_adata, sc_model, **kwargs)

Create DestVI from a trained CondSCVI model.

get_gamma([adata, indices, batch_size])

Get sub-cell-type variation (gamma) per cell type.

get_latent_representation([adata, indices, ...])

Get latent representation.

get_proportions([adata, indices, ...])

Get estimated cell type proportions.

get_scale_for_ct(cell_type[, adata, ...])

Get cell type-specific expression scale.

load(dir_path[, adata])

Load model from disk.

save(dir_path, **kwargs)

Save model to disk.

setup_anndata(adata[, layer, labels_key])

Setup AnnData for DestVI (reference single-cell data).

train([max_epochs, lr, accelerator, devices])

Train the model.

classmethod setup_anndata(adata, layer=None, labels_key=None, **kwargs)[source]#

Setup AnnData for DestVI (reference single-cell data).

Parameters:
  • adata (AnnData) – AnnData object (single-cell reference).

  • layer (str | None) – Layer to use for expression data.

  • labels_key (str | None) – Key for cell type labels in obs.

  • **kwargs – Additional keyword arguments.

  • adata (AnnData)

  • layer (str | None)

  • labels_key (str | None)

Return type:

None

classmethod from_rna_model(st_adata, sc_model, **kwargs)[source]#

Create DestVI from a trained CondSCVI model.

Parameters:
  • st_adata (AnnData) – Spatial transcriptomics AnnData.

  • sc_model – Trained CondSCVI model.

  • **kwargs – Additional keyword arguments.

  • st_adata (AnnData)

Return type:

DestVI

Returns:

DestVI model instance.

train(max_epochs=2500, lr=0.001, accelerator='auto', devices='auto', **kwargs)[source]#

Train the model.

Parameters:
  • max_epochs (int) – Maximum number of epochs.

  • lr (float) – Learning rate.

  • accelerator (str) – Accelerator to use.

  • devices (int | str) – Devices to use.

  • **kwargs – Additional keyword arguments for training.

  • max_epochs (int)

  • lr (float)

  • accelerator (str)

  • devices (int | str)

Return type:

None

get_proportions(adata=None, indices=None, batch_size=None, return_dataframe=True)[source]#

Get estimated cell type proportions.

Parameters:
Return type:

ndarray[tuple[Any, ...], dtype[TypeVar(_ScalarT, bound= generic)]] | DataFrame

Returns:

Cell type proportions.

get_gamma(adata=None, indices=None, batch_size=None)[source]#

Get sub-cell-type variation (gamma) per cell type.

Parameters:
Return type:

dict[str, ndarray[tuple[Any, ...], dtype[TypeVar(_ScalarT, bound= generic)]]]

Returns:

Dictionary mapping cell types to gamma arrays.

get_scale_for_ct(cell_type, adata=None, indices=None, batch_size=None)[source]#

Get cell type-specific expression scale.

Parameters:
Return type:

ndarray[tuple[Any, ...], dtype[TypeVar(_ScalarT, bound= generic)]]

Returns:

Expression scale array for the cell type.

get_latent_representation(adata=None, indices=None, batch_size=None)[source]#

Get latent representation.

Parameters:
Return type:

ndarray[tuple[Any, ...], dtype[TypeVar(_ScalarT, bound= generic)]]

Returns:

Latent representation array.

save(dir_path, **kwargs)[source]#

Save model to disk.

Return type:

None

Parameters:

dir_path (str)

classmethod load(dir_path, adata=None, **kwargs)[source]#

Load model from disk.

Return type:

DestVI

Parameters: