smoother.models.reduction ========================= .. py:module:: smoother.models.reduction Submodules ---------- .. toctree:: :maxdepth: 1 /autoapi/smoother/models/reduction/_spae/index /autoapi/smoother/models/reduction/_spanvi/index /autoapi/smoother/models/reduction/_spmultivi/index /autoapi/smoother/models/reduction/_sppca/index /autoapi/smoother/models/reduction/_spvae/index /autoapi/smoother/models/reduction/_utils/index Classes ------- .. autoapisummary:: smoother.models.reduction.SpatialPCA smoother.models.reduction.SpatialAutoEncoder smoother.models.reduction.SpatialVAE smoother.models.reduction.SpatialANVI smoother.models.reduction.SpatialMULTIVI Package Contents ---------------- .. py:class:: SpatialPCA(adata: anndata.AnnData, layer: Optional[str] = None, n_latent: int = 10, spatial_loss: Optional[smoother.SpatialLoss] = None, lambda_spatial_loss=0.1) Bases: :py:obj:`torch.nn.Module` Solving PCA using stochastic gradient descent. .. py:attribute:: n_latent :value: 10 .. py:attribute:: n_feature .. py:attribute:: _data :value: None .. py:attribute:: spatial_loss :value: None .. py:attribute:: l_sp_loss :value: 0.1 .. py:attribute:: U .. py:attribute:: dr_configs .. py:attribute:: dr_logs .. py:method:: load_adata(adata: anndata.AnnData, layer: Optional[str] = None) Load data to project. :param adata: data to project. :type adata: AnnData :param layer: layer to project. If None, use adata.X. :type layer: str .. py:method:: _gram_schmidt(U) Project the PC basis matrix to the feasible space. :param U: PC basis matrix, num_feature x num_pc. :type U: 2D tensor .. py:method:: forward(x) Project x to the lower PC space. :param x: data to project, num_feature x num_sample. :type x: 2D tensor .. py:method:: _init_with_svd(x) Initialize model with svd solution. :param x: data to project, num_feature x num_sample. :type x: 2D tensor .. py:method:: get_latent_representation() Project loaded data to the lower PC space and return the latent representation. .. py:method:: 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. :param lr: The learning rate. :type lr: float :param max_epochs: The maximum number of epochs. :type max_epochs: int :param patience: The patience for early stopping. :type patience: int :param tol: The tolerated convergence error. :type tol: float :param init_with_svd: Whether to initialize with analytical solution calculated using `torch.svd_lowrank()`. :type init_with_svd: bool :param verbose: If True, print out loss while training. :type verbose: bool :param quite: If True, no output printed. :type quite: bool :param clear_logs: If True, clear logs before training. :type clear_logs: bool .. py:method:: from_rna_model(rna_model, st_adata: anndata.AnnData, layer: Optional[str] = None, spatial_loss: Optional[smoother.SpatialLoss] = None, lambda_spatial_loss=0.1) :classmethod: Initialize a spatial model from a pre-trained RNA model. .. py:method:: from_scanpy(adata: anndata.AnnData, layer: Optional[str] = None, spatial_loss: Optional[smoother.SpatialLoss] = None, lambda_spatial_loss=0.1) :classmethod: Initialize a spatial model from a pre-trained RNA model. .. py:class:: SpatialAutoEncoder(adata: anndata.AnnData, layer: Optional[str] = None, spatial_loss: Optional[smoother.losses.SpatialLoss] = 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: Optional[torch.nn.Module] = nn.ReLU, use_bias=True) Bases: :py:obj:`AutoEncoderClass` Generic spatial auto-encoder. .. py:attribute:: setup_args .. py:attribute:: lib_size .. py:attribute:: encoder .. py:attribute:: decoder .. py:method:: encode(x) Project the input to the hidden space. :param x: The input matrix, num_gene x num_spot. :type x: 2D tensor :returns: The hidden embedding, num_spot x num_hidden. :rtype: x_enc (2D tensor) .. py:method:: decode(x_enc) Project the hidden embedding back to the input space. :param x_enc: The hidden embedding, num_spot x num_hidden. :type x_enc: 2D tensor :returns: The decoded matrix, num_gene x num_spot. :rtype: x_dec (2D tensor) .. py:method:: get_recon_loss() Get the reconstruction loss. .. py:method:: get_orth_loss() Get the orthogonality loss (weighted by lambda_orth_loss). .. py:method:: get_sp_loss() Get the spatial loss (weighted by lambda_spatial_loss). .. py:method:: from_rna_model(rna_model, st_adata: anndata.AnnData, layer: Optional[str] = None, spatial_loss: Optional[smoother.losses.SpatialLoss] = None, lambda_spatial_loss=0.1) :classmethod: Initialize a spatial model from a pre-trained RNA model. .. py:class:: SpatialVAE(st_adata: anndata.AnnData, spatial_loss: Optional[smoother.SpatialLoss] = 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: :py:obj:`scvi.model.SCVI` Spatially-aware Variational Autoencoder model. .. py:attribute:: _data_splitter_cls .. py:attribute:: _module_cls .. py:attribute:: module .. py:attribute:: _model_summary_string .. py:attribute:: init_params_ .. py:attribute:: dr_logs .. py:method:: train(max_epochs: int = 400, lr: float = 0.01, accelerator: str = 'auto', devices: Union[int, List[int], str] = 'auto', plan_kwargs: Optional[dict] = None, **kwargs) Trains the model without mini-batch. .. py:method:: from_rna_model(st_adata: anndata.AnnData, sc_model: scvi.model.SCVI, spatial_loss: Optional[smoother.SpatialLoss] = 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) :classmethod: 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. :param st_adata: registed anndata object :param sc_model: pretrained SCVI model .. py:class:: SpatialANVI(st_adata: anndata.AnnData, spatial_loss: Optional[smoother.SpatialLoss] = 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: Optional[lightning.LightningDataModule] = None, **model_kwargs) Bases: :py:obj:`scvi.model.SCANVI` Spatially-aware ANnotation Variational Inference model. .. py:attribute:: _data_splitter_cls .. py:attribute:: _module_cls .. py:attribute:: _training_plan_cls .. py:attribute:: _train_runner_cls .. py:attribute:: module .. py:attribute:: _model_summary_string .. py:attribute:: init_params_ .. py:attribute:: dr_logs .. py:method:: train(max_epochs: Optional[int] = None, accelerator: str = 'auto', devices: Union[int, List[int], str] = 'auto', plan_kwargs: Optional[dict] = None, **trainer_kwargs) Unsupervised training of the spatial model without minibatches. .. py:method:: from_rna_model(st_adata: anndata.AnnData, sc_model: scvi.model.SCANVI, spatial_loss: Optional[smoother.SpatialLoss] = 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) :classmethod: 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. :param st_adata: registed anndata object :param sc_model: pretrained SCANVI model .. py:class:: SpatialMULTIVI(st_adata: anndata.AnnData, spatial_loss: Optional[smoother.SpatialLoss] = None, lambda_spatial_loss: float = 1, sp_loss_as_kl: bool = True, **model_kwargs) Bases: :py:obj:`scvi.model.MULTIVI` Spatially-aware Multimodal Variational Autoencoder model. .. py:attribute:: _data_splitter_cls .. py:attribute:: _module_cls .. py:attribute:: module .. py:attribute:: _model_summary_string .. py:attribute:: init_params_ .. py:attribute:: dr_logs .. py:method:: 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. .. py:method:: from_sc_model(st_adata: mudata.MuData, sc_model: scvi.model.MULTIVI, spatial_loss: Optional[smoother.SpatialLoss] = 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) :classmethod: 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. :param st_adata: registed anndata object :param sc_model: pretrained MULTIVI model