smoother.models.reduction._spvae
Classes
Spatial loss. |
|
Add spatial loss to the latent representation in the VAE model. |
|
Spatially-aware Variational Autoencoder model. |
Functions
|
Freeze parts of network for scArches. |
Module Contents
- class smoother.models.reduction._spvae.SpatialLoss(prior, spatial_weights: List[smoother.weights.SpatialWeightMatrix] = None, rho=1, use_sparse=True, standardize_cov=False)
Bases:
torch.nn.ModuleSpatial loss.
The spatial smoothing loss on a spatial random variable (num_group x num_spot).
- spatial_weights
Spatial weight matrix collection of length num_group or 1. If 1 then all groups will be subject to the same covariance.
- Type:
List[SpatialWeightMatrix]
- standardize_cov
Whether to standardize the covariance matrix to have same variance (1) across locations. Only proper covariance can be standardized.
- Type:
- inv_cov
Inverse covariance (precision) matrix of spatial variables of each group, (num_group or 1) x n x n. If the first dimension has length 1, all groups will have the same covariance structure.
- Type:
3D tensor
- inv_cov_2d_sp
Sparse block diagonal inverse covariance (precision) matrix.
- Type:
2D sparse tensor
- confidences
Relative prior confidence of each group. The higher the confidence, the stronger the smoothing will be. If float, all groups will have the same confidence.
- Type:
1D tensor or float
- estimate_confidence(ref_exp, st_exp, method='lr') None
Estimate the relative confidence for each group.
The covariance matrix will be scaled accordingly.
- Parameters:
ref_exp (2D tensor) – Bulk expression signiture matrix, num_gene x num_group.
st_exp (2D tensor) – Spatial expression matrix, num_gene x num_spot.
method (str) – Method used to estimate variance.
- forward(coefs, normalize=True)
Calculate spatial loss.
- Parameters:
coefs (2D tensor) – Columns of regression coefficients, num_group x num_spot.
- calc_corr_decay_stats(coords, min_k=0, max_k=50, cov_ind=0, return_var=False)
Calculate spatial covariance decay over degree of neighborhoods.
Covariance measured between k-nearest neighbors. If the number of covariance matrices (i.e. self.inv_cov.shape[0]) is larger than 1, use ‘cov_ind’ to select the covariance matrix to use.
- Parameters:
- Returns:
Correlation decay quantiles. var_quantiles_df (pd.DataFrame): Per-spot variance quantiles.
- Return type:
corr_decay_quantiles_df (pd.DataFrame)
- smoother.models.reduction._spvae.set_params_online_update(module, unfrozen, freeze_decoder_first_layer, freeze_batchnorm_encoder, freeze_batchnorm_decoder, freeze_dropout, freeze_expression, freeze_classifier)
Freeze parts of network for scArches.
- class smoother.models.reduction._spvae.SPVAE(n_input: int, spatial_loss: smoother.SpatialLoss | None = None, lambda_spatial_loss=0.1, sp_loss_on: Literal['z', 'mean_z'] = 'mean_z', 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', **model_kwargs)
Bases:
scvi.module.VAEAdd spatial loss to the latent representation in the VAE model.
- Parameters:
spatial_loss – Spatial loss to apply on the latent representation. If None, no spatial loss.
lambda_spatial_loss – Weight of the spatial loss.
sp_loss_on – Whether to apply the spatial loss on the sampled latent representation (‘z’) or on the encoded mean of the latent distribution (‘mean_z’).
- classmethod from_vae(vae_module: scvi.module.VAE, spatial_loss: smoother.SpatialLoss | None = None, lambda_spatial_loss=0.1, sp_loss_on: Literal['z', 'mean_z'] = 'mean_z')
- class smoother.models.reduction._spvae.SpatialVAE(st_adata: anndata.AnnData, spatial_loss: smoother.SpatialLoss | None = None, lambda_spatial_loss=0.1, sp_loss_on: Literal['z', 'mean_z'] = 'mean_z', 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'] = 'nb', latent_distribution: Literal['normal', 'ln'] = 'normal', **model_kwargs)
Bases:
scvi.model.SCVISpatially-aware Variational Autoencoder model.
- _data_splitter_cls
- _module_cls
- 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_on: Literal['z', 'mean_z'] = 'mean_z', 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