smoother.models.reduction._spcpd

Classes

SpatialLoss

Spatial loss.

CPDecomposition

Tensor CP decomposition.

Functions

_decomposition_sp(target, model[, spatial_loss, ...])

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.Module

Spatial loss.

The spatial smoothing loss on a spatial random variable (num_group x num_spot).

prior

The prior spatial process model, can be one of ‘sma’,’sar’, ‘car’, ‘icar’.

Type:

str

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]

rho

The spatial autocorrelation parameter (for SpatialWeightMatrix.get_inv_cov).

Type:

float

use_sparse

Whether to use sparse inverse covariance matrix in the calculation.

Type:

bool

standardize_cov

Whether to standardize the covariance matrix to have same variance (1) across locations. Only proper covariance can be standardized.

Type:

bool

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

_sanity_check() None

Check whether the spatial loss is defined properly.

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:
  • coords (2D tensor) – Coordinates of spots, num_spot x 2.

  • min_k – Minimum number of k in k-nearest neighbors. k = 0: self.

  • max_k – Maximum number of k in k-nearest neighbors.

  • cov_ind (int) – Index of the covariance matrix to use.

  • return_var (bool) – Whether to return variance stats.

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.Module

Tensor 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)