spatialvi.model.BaseSpatialModel#

class spatialvi.model.BaseSpatialModel(adata, **kwargs)[source]#

Bases: BaseModelClass

Base class for all spatial transcriptomics models.

This class extends scvi-tools’ BaseModelClass with spatial-specific functionality including: - Spatial coordinate handling - Neighbor graph construction - Spatial-aware data loading

Parameters:
  • adata (AnnData) – AnnData object that has been registered via setup_anndata().

  • adata (AnnData)

__init__(adata, **kwargs)[source]#
Parameters:

adata (AnnData)

Methods

__init__(adata, **kwargs)

compute_niche_composition(adata, labels_key)

Compute neighborhood cell type composition.

compute_spatial_neighbors(adata[, ...])

Compute spatial nearest neighbors.

convert_legacy_save(dir_path, output_dir_path)

Converts a legacy saved model (<v0.15.0) to the updated save format.

data_registry(registry_key)

Returns the object in AnnData associated with the key in the data registry.

deregister_manager([adata])

Deregisters the AnnDataManager instance associated with adata.

get_anndata_manager(adata[, required])

Retrieves the AnnDataManager for a given AnnData object.

get_from_registry(adata, registry_key)

Returns the object in AnnData associated with the key in the data registry.

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

Return the latent representation for each cell.

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

Return normalized gene expression.

get_setup_arg(setup_arg)

Returns the string provided to setup of a specific setup_arg.

get_state_registry(registry_key)

Returns the state registry for the AnnDataField registered with this instance.

get_var_names([legacy_mudata_format])

Variable names of input data.

load(dir_path[, adata, accelerator, device, ...])

Instantiate a model from the saved output.

load_registry(dir_path[, prefix])

Return the full registry saved with the model.

register_manager(adata_manager)

Registers an AnnDataManager instance with this model class.

save(dir_path[, prefix, overwrite, ...])

Save the state of the model.

setup_anndata(adata, *args, **kwargs)

Sets up the AnnData object for this model.

to_device(device)

Move the model to the device.

train()

Trains the model.

transfer_fields(adata, **kwargs)

Transfer fields from a model to an AnnData object.

update_setup_method_args(setup_method_args)

Update setup method args.

view_anndata_setup([adata, ...])

Print summary of the setup for the initial AnnData or a given AnnData object.

view_registry([hide_state_registries])

Prints summary of the registry.

view_setup_args(dir_path[, prefix])

Print args used to setup a saved model.

view_setup_method_args()

Prints setup kwargs used to produce a given registry.

Attributes

adata

Data attached to model instance.

adata_manager

Manager instance associated with self.adata.

device

The current device that the module's params are on.

get_normalized_function_name

What the get normalized functions name is

history

Returns computed metrics during training.

is_trained

Whether the model has been trained.

n_neighbors

Number of spatial neighbors.

registry

Data attached to model instance.

run_id

Returns the run id of the model.

run_name

Returns the run name of the model.

spatial_key

Key for spatial coordinates in obsm.

summary_string

Summary string of the model.

test_indices

Observations that are in test set.

train_indices

Observations that are in train set.

validation_indices

Observations that are in validation set.

property spatial_key: str | None#

Key for spatial coordinates in obsm.

property n_neighbors: int | None#

Number of spatial neighbors.

static compute_spatial_neighbors(adata, spatial_key='spatial', n_neighbors=20, index_key='nn_index', dist_key='nn_dist', metric='euclidean')[source]#

Compute spatial nearest neighbors.

Parameters:
  • adata (AnnData) – AnnData object.

  • spatial_key (str) – Key in obsm for spatial coordinates.

  • n_neighbors (int) – Number of neighbors to compute.

  • index_key (str) – Key to store neighbor indices in obsm.

  • dist_key (str) – Key to store neighbor distances in obsm.

  • metric (str) – Distance metric to use.

  • adata (AnnData)

  • spatial_key (str)

  • n_neighbors (int)

  • index_key (str)

  • dist_key (str)

  • metric (str)

Return type:

None

static compute_niche_composition(adata, labels_key, index_key='nn_index', composition_key='niche_composition')[source]#

Compute neighborhood cell type composition.

Parameters:
  • adata (AnnData) – AnnData object.

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

  • index_key (str) – Key in obsm for neighbor indices.

  • composition_key (str) – Key to store composition in obsm.

  • adata (AnnData)

  • labels_key (str)

  • index_key (str)

  • composition_key (str)

Return type:

None

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

Return the latent representation for each cell.

Parameters:
  • adata (AnnData | None) – AnnData object with equivalent structure to initial AnnData.

  • indices (Sequence[int] | None) – Indices of cells to use.

  • give_mean (bool) – Whether to return the mean of the posterior.

  • batch_size (int | None) – Batch size for data loader.

  • adata (AnnData | None)

  • indices (Sequence[int] | None)

  • give_mean (bool)

  • batch_size (int | None)

Return type:

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

Returns:

Latent representation array of shape (n_cells, n_latent).

abstractmethod get_normalized_expression(adata=None, indices=None, transform_batch=None, gene_list=None, library_size=1.0, n_samples=1, batch_size=None, return_mean=True)[source]#

Return normalized gene expression.

Parameters:
Return type:

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

Returns:

Normalized expression array.