smoother.models.reduction._spcpd
Classes
Spatial loss. |
|
Tensor CP decomposition. |
Functions
|
Module Contents
- class smoother.models.reduction._spcpd.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)
- class smoother.models.reduction._spcpd.CPDecomposition(shape_tensor, dim_hidden, nonneg=True)
Bases:
torch.nn.ModuleTensor CP decomposition.
- forward()
- get_loss_fn()
- init_with_tensorly(target)
Initialize tensor decomposition using tensorly.
- decomposition_sp(target, spatial_loss: smoother.losses.SpatialLoss = None, lambda_spatial_loss=0.1, lr=0.001, max_epochs=1000, patience=10, init_with_tensorly=False, verbose=True, quite=False)
- smoother.models.reduction._spcpd._decomposition_sp(target, model, spatial_loss: smoother.losses.SpatialLoss = None, lambda_spatial_loss=0.1, lr=0.001, max_epochs=1000, patience=10, init_with_tensorly=True, verbose=True, quite=False, return_model=False)