spateo.alignment.methods.morpho#

Module Contents#

Functions#

con_K(→ Union[numpy.ndarray, torch.Tensor])

con_K constructs the Squared Exponential (SE) kernel, where K(i,j)=k(X_i,Y_j)=exp(-beta*||X_i-Y_j||^2).

get_P(→ Tuple[Any, Any, Any])

Calculating the generating probability matrix P.

get_P_chunk(→ Union[numpy.ndarray, torch.Tensor])

Calculating the generating probability matrix P.

BA_align(→ Tuple[Optional[Tuple[anndata.AnnData, ...)

_summary_

spateo.alignment.methods.morpho.con_K(X: numpy.ndarray | torch.Tensor, Y: numpy.ndarray | torch.Tensor, beta: int | float = 0.01, use_chunk: bool = False) numpy.ndarray | torch.Tensor[source]#

con_K constructs the Squared Exponential (SE) kernel, where K(i,j)=k(X_i,Y_j)=exp(-beta*||X_i-Y_j||^2).

Parameters:
X

The first vector Xinmathbb{R}^{N imes d}

Y

The second vector Xinmathbb{R}^{M imes d}

beta

The length-scale of the SE kernel.

use_chunk bool, optional

Whether to use chunk to reduce the GPU memory usage. Note that if set to ``True’’ it will slow down the calculation. Defaults to False.

Returns:

The kernel Kinmathbb{R}^{N imes M}

Return type:

K

spateo.alignment.methods.morpho.get_P(XnAHat: numpy.ndarray | torch.Tensor, XnB: numpy.ndarray | torch.Tensor, sigma2: int | float | numpy.ndarray | torch.Tensor, beta2: int | float | numpy.ndarray | torch.Tensor, alpha: numpy.ndarray | torch.Tensor, gamma: float | numpy.ndarray | torch.Tensor, Sigma: numpy.ndarray | torch.Tensor, GeneDistMat: numpy.ndarray | torch.Tensor, SpatialDistMat: numpy.ndarray | torch.Tensor, samples_s: List[float] | None = None, outlier_variance: float = None) Tuple[Any, Any, Any][source]#

Calculating the generating probability matrix P.

Parameters:
XAHat

Current spatial coordinate of sample A. Shape: N x D.

XnB

spatial coordinate of sample B (reference sample). Shape: M x D.

sigma2

The spatial coordinate noise.

beta2

The gene expression noise.

alpha

A vector that encoding each probability generated by the spots of sample A. Shape: N x 1.

gamma

Inlier proportion of sample A.

Sigma

The posterior covariance matrix of Gaussian process. Shape: N x N or N x 1.

GeneDistMat

The gene expression distance matrix between sample A and sample B. Shape: N x M.

SpatialDistMat

The spatial coordinate distance matrix between sample A and sample B. Shape: N x M.

samples_s

The space size of each sample. Area size for 2D samples and volume size for 3D samples.

Returns:

Generating probability matrix P. Shape: N x M.

Return type:

P

spateo.alignment.methods.morpho.get_P_chunk(XnAHat: numpy.ndarray | torch.Tensor, XnB: numpy.ndarray | torch.Tensor, X_A: numpy.ndarray | torch.Tensor, X_B: numpy.ndarray | torch.Tensor, sigma2: int | float | numpy.ndarray | torch.Tensor, beta2: int | float | numpy.ndarray | torch.Tensor, alpha: numpy.ndarray | torch.Tensor, gamma: float | numpy.ndarray | torch.Tensor, Sigma: numpy.ndarray | torch.Tensor, samples_s: List[float] | None = None, outlier_variance: float = None, chunk_size: int = 1000, dissimilarity: str = 'kl') numpy.ndarray | torch.Tensor[source]#

Calculating the generating probability matrix P.

Parameters:
XAHat

Current spatial coordinate of sample A. Shape

spateo.alignment.methods.morpho.BA_align(sampleA: anndata.AnnData, sampleB: anndata.AnnData, genes: List | torch.Tensor | None = None, spatial_key: str = 'spatial', key_added: str = 'align_spatial', iter_key_added: str | None = 'iter_spatial', vecfld_key_added: str | None = 'VecFld_morpho', layer: str = 'X', dissimilarity: str = 'kl', keep_size: bool = False, max_iter: int = 200, lambdaVF: int | float = 100.0, beta: int | float = 0.01, K: int | float = 15, normalize_c: bool = True, normalize_g: bool = True, select_high_exp_genes: bool | float | int = False, dtype: str = 'float32', device: str = 'cpu', inplace: bool = True, verbose: bool = True, nn_init: bool = True, SVI_mode: bool = True, batch_size: int = 1000, partial_robust_level: float = 25) Tuple[Tuple[anndata.AnnData, anndata.AnnData] | None, numpy.ndarray, numpy.ndarray][source]#

_summary_

Parameters:
sampleA

Sample A that acts as reference.

sampleB

Sample B that performs alignment.

genes

Genes used for calculation. If None, use all common genes for calculation.

spatial_key

The key in .obsm that corresponds to the raw spatial coordinate.

key_added

.obsm key under which to add the aligned spatial coordinate.

iter_key_added

.uns key under which to add the result of each iteration of the iterative process. If iter_key_added is None, the results are not saved.

vecfld_key_added

The key that will be used for the vector field key in .uns. If vecfld_key_added is None, the results are not saved.

layer

If 'X', uses .X to calculate dissimilarity between spots, otherwise uses the representation given by .layers[layer].

dissimilarity

Expression dissimilarity measure: 'kl' or 'euclidean'.

small_variance

When approximating the assignment matrix, if True, we use small sigma2 (0.001) rather than the infered sigma2

max_iter

Max number of iterations for morpho alignment.

lambdaVF

Hyperparameter that controls the non-rigid distortion degree. Smaller means more flexibility.

beta

The length-scale of the SE kernel. Higher means more flexibility.

K

The number of sparse inducing points used for Nystr ̈om approximation. Smaller means faster but less accurate.

normalize_c

Whether to normalize spatial coordinates.

normalize_g

Whether to normalize gene expression. If dissimilarity == 'kl', normalize_g must be False.

select_high_exp_genes

Whether to select genes with high differences in gene expression.

samples_s

The space size of each sample. Area size for 2D samples and volume size for 3D samples.

dtype

The floating-point number type. Only float32 and float64.

device

Equipment used to run the program. You can also set the specified GPU for running. E.g.: '0'.

inplace

Whether to copy adata or modify it inplace.

verbose

If True, print progress updates.

nn_init

If True, use nearest neighbor matching to initialize the alignment.

SVI_mode

Whether to use stochastic variational inferential (SVI) optimization strategy.

batch_size

The size of the mini-batch of SVI. If set smaller, the calculation will be faster, but it will affect the accuracy, and vice versa. If not set, it is automatically set to one-tenth of the data size.

partial_robust_level

The robust level of partial alignment. The larger the value, the more robust the alignment to partial cases is. Recommended setting from 1 to 50.