smoother.models.reduction

Submodules

Classes

SpatialPCA

Solving PCA using stochastic gradient descent.

SpatialAutoEncoder

Generic spatial auto-encoder.

SpatialVAE

Spatially-aware Variational Autoencoder model.

SpatialANVI

Spatially-aware ANnotation Variational Inference model.

SpatialMULTIVI

Spatially-aware Multimodal Variational Autoencoder model.

Package Contents

class smoother.models.reduction.SpatialPCA(adata: anndata.AnnData, layer: str | None = None, n_latent: int = 10, spatial_loss: smoother.SpatialLoss | None = None, lambda_spatial_loss=0.1)

Bases: torch.nn.Module

Solving PCA using stochastic gradient descent.

n_latent = 10
n_feature
_data = None
spatial_loss = None
l_sp_loss = 0.1
U
dr_configs
dr_logs
load_adata(adata: anndata.AnnData, layer: str | None = None)

Load data to project.

Parameters:
  • adata (AnnData) – data to project.

  • layer (str) – layer to project. If None, use adata.X.

_gram_schmidt(U)

Project the PC basis matrix to the feasible space.

Parameters:

U (2D tensor) – PC basis matrix, num_feature x num_pc.

forward(x)

Project x to the lower PC space.

Parameters:

x (2D tensor) – data to project, num_feature x num_sample.

_init_with_svd(x)

Initialize model with svd solution.

Parameters:

x (2D tensor) – data to project, num_feature x num_sample.

get_latent_representation()

Project loaded data to the lower PC space and return the latent representation.

reduce(lr=1.0, max_epochs=1000, patience=10, tol=1e-05, init_with_svd=False, verbose=True, quite=False, clear_logs=True)

Reduce the dimension of the expression matrix.

Parameters:
  • lr (float) – The learning rate.

  • max_epochs (int) – The maximum number of epochs.

  • patience (int) – The patience for early stopping.

  • tol (float) – The tolerated convergence error.

  • init_with_svd (bool) – Whether to initialize with analytical solution calculated using torch.svd_lowrank().

  • verbose (bool) – If True, print out loss while training.

  • quite (bool) – If True, no output printed.

  • clear_logs (bool) – If True, clear logs before training.

classmethod from_rna_model(rna_model, st_adata: anndata.AnnData, layer: str | None = None, spatial_loss: smoother.SpatialLoss | None = None, lambda_spatial_loss=0.1)

Initialize a spatial model from a pre-trained RNA model.

classmethod from_scanpy(adata: anndata.AnnData, layer: str | None = None, spatial_loss: smoother.SpatialLoss | None = None, lambda_spatial_loss=0.1)

Initialize a spatial model from a pre-trained RNA model.

class smoother.models.reduction.SpatialAutoEncoder(adata: anndata.AnnData, layer: str | None = None, spatial_loss: smoother.losses.SpatialLoss | None = None, lambda_spatial_loss=0.1, lambda_orth_loss=0.1, recon_loss_mode: Literal['mse', 'poisson'] = 'poisson', n_layers: int = 1, n_hidden: int = 128, n_latent: int = 10, dropout_rate: float = 0.0, use_batch_norm: bool = True, use_activation: bool = True, activation_fn: torch.nn.Module | None = nn.ReLU, use_bias=True)

Bases: AutoEncoderClass

Generic spatial auto-encoder.

setup_args
lib_size
encoder
decoder
encode(x)

Project the input to the hidden space.

Parameters:

x (2D tensor) – The input matrix, num_gene x num_spot.

Returns:

The hidden embedding, num_spot x num_hidden.

Return type:

x_enc (2D tensor)

decode(x_enc)

Project the hidden embedding back to the input space.

Parameters:

x_enc (2D tensor) – The hidden embedding, num_spot x num_hidden.

Returns:

The decoded matrix, num_gene x num_spot.

Return type:

x_dec (2D tensor)

get_recon_loss()

Get the reconstruction loss.

get_orth_loss()

Get the orthogonality loss (weighted by lambda_orth_loss).

get_sp_loss()

Get the spatial loss (weighted by lambda_spatial_loss).

classmethod from_rna_model(rna_model, st_adata: anndata.AnnData, layer: str | None = None, spatial_loss: smoother.losses.SpatialLoss | None = None, lambda_spatial_loss=0.1)

Initialize a spatial model from a pre-trained RNA model.

class smoother.models.reduction.SpatialVAE(st_adata: anndata.AnnData, spatial_loss: smoother.SpatialLoss | None = None, lambda_spatial_loss=0.1, sp_loss_as_kl: bool = False, n_hidden: int = 128, n_latent: int = 10, n_layers: int = 1, dropout_rate: float = 0.0, dispersion: Literal['gene', 'gene-batch', 'gene-label', 'gene-cell'] = 'gene', gene_likelihood: Literal['zinb', 'nb', 'poisson', 'normal'] = 'nb', latent_distribution: Literal['normal', 'ln'] = 'normal', **model_kwargs)

Bases: scvi.model.SCVI

Spatially-aware Variational Autoencoder model.

_data_splitter_cls
_module_cls
module
_model_summary_string
init_params_
dr_logs
train(max_epochs: int = 400, lr: float = 0.01, accelerator: str = 'auto', devices: int | List[int] | str = 'auto', plan_kwargs: dict | None = None, **kwargs)

Trains the model without mini-batch.

classmethod from_rna_model(st_adata: anndata.AnnData, sc_model: scvi.model.SCVI, spatial_loss: smoother.SpatialLoss | None = None, lambda_spatial_loss=0.1, sp_loss_as_kl: bool = False, unfrozen: bool = False, freeze_dropout: bool = False, freeze_expression: bool = True, freeze_decoder_first_layer: bool = True, freeze_batchnorm_encoder: bool = True, freeze_batchnorm_decoder: bool = False, freeze_classifier: bool = True, **spvae_kwargs)

Alternate constructor for exploiting a pre-trained model on RNA-seq data.

Note that because of the dropout layer, even though the new instance is initialized with the same parameters as the pre-trained model, new_instance.get_latent_representation() may not return the same latent representation as the pre-trained model.

Parameters:
  • st_adata – registed anndata object

  • sc_model – pretrained SCVI model

class smoother.models.reduction.SpatialANVI(st_adata: anndata.AnnData, spatial_loss: smoother.SpatialLoss | None = None, lambda_spatial_loss=0.1, sp_loss_as_kl: bool = False, n_hidden: int = 128, n_latent: int = 10, n_layers: int = 1, dropout_rate: float = 0.1, dispersion: Literal['gene', 'gene-batch', 'gene-label', 'gene-cell'] = 'gene', gene_likelihood: Literal['zinb', 'nb', 'poisson'] = 'nb', latent_distribution: Literal['normal', 'ln'] = 'normal', use_observed_lib_size: bool = True, linear_classifier: bool = False, datamodule: lightning.LightningDataModule | None = None, **model_kwargs)

Bases: scvi.model.SCANVI

Spatially-aware ANnotation Variational Inference model.

_data_splitter_cls
_module_cls
_training_plan_cls
_train_runner_cls
module
_model_summary_string
init_params_
dr_logs
train(max_epochs: int | None = None, accelerator: str = 'auto', devices: int | List[int] | str = 'auto', plan_kwargs: dict | None = None, **trainer_kwargs)

Unsupervised training of the spatial model without minibatches.

classmethod from_rna_model(st_adata: anndata.AnnData, sc_model: scvi.model.SCANVI, spatial_loss: smoother.SpatialLoss | None = None, lambda_spatial_loss=0.1, sp_loss_as_kl: bool = False, unfrozen: bool = False, freeze_dropout: bool = False, freeze_expression: bool = True, freeze_decoder_first_layer: bool = True, freeze_batchnorm_encoder: bool = True, freeze_batchnorm_decoder: bool = False, freeze_classifier: bool = True, **spanvae_kwargs)

Alternate constructor for exploiting a pre-trained model on RNA-seq data.

Note that because of the dropout layer, even though the new instance is initialized with the same parameters as the pre-trained model, new_instance.get_latent_representation() may not return the same latent representation as the pre-trained model.

Parameters:
  • st_adata – registed anndata object

  • sc_model – pretrained SCANVI model

class smoother.models.reduction.SpatialMULTIVI(st_adata: anndata.AnnData, spatial_loss: smoother.SpatialLoss | None = None, lambda_spatial_loss: float = 1, sp_loss_as_kl: bool = True, **model_kwargs)

Bases: scvi.model.MULTIVI

Spatially-aware Multimodal Variational Autoencoder model.

_data_splitter_cls
_module_cls
module
_model_summary_string
init_params_
dr_logs
train(max_epochs: int = 500, lr: float = 0.0001, accelerator: str = 'auto', devices='auto', weight_decay: float = 0.001, eps: float = 1e-08, n_steps_kl_warmup=None, n_epochs_kl_warmup=50, adversarial_mixing=False, datasplitter_kwargs=None, plan_kwargs=None, **kwargs)

Train the model without mini-batch.

classmethod from_sc_model(st_adata: mudata.MuData, sc_model: scvi.model.MULTIVI, spatial_loss: smoother.SpatialLoss | None = None, lambda_spatial_loss: float = 1, sp_loss_as_kl: bool = True, unfrozen: bool = False, freeze_dropout: bool = False, freeze_expression: bool = True, freeze_decoder_first_layer: bool = True, freeze_batchnorm_encoder: bool = True, freeze_batchnorm_decoder: bool = False, freeze_classifier: bool = True, **spmultivae_kwargs)

Alternate constructor for exploiting a pre-trained model on non-spatial data.

Note that because of the dropout layer, even though the new instance is initialized with the same parameters as the pre-trained model, new_instance.get_latent_representation() may not return the same latent representation as the pre-trained model.

Parameters:
  • st_adata – registed anndata object

  • sc_model – pretrained MULTIVI model