spateo.tools.three_dims_align#

Module Contents#

Functions#

rigid_transform_2D(→ numpy.ndarray)

Compute optimal transformation based on the two sets of 2D points and apply the transformation to other points.

rigid_transform_3D(→ numpy.ndarray)

Compute optimal transformation based on the two sets of 3D points and apply the transformation to other points.

models_align(→ List[anndata.AnnData])

Align spatial coordinates of models.

models_align_ref(→ Tuple[List[anndata.AnnData], ...)

Align the spatial coordinates of one model list through the affine transformation matrix obtained from another model list.

models_center_align(→ Tuple[anndata.AnnData, ...)

Align spatial coordinates of a list of models to a center model.

models_center_align_ref(→ Tuple[anndata.AnnData, ...)

Align the spatial coordinates of one model list to the central model through the affine transformation matrix obtained from another model list.

get_align_labels(→ pandas.DataFrame)

Obtain the label information in anndata.obs[key] corresponding to the align_X coordinate.

spateo.tools.three_dims_align.rigid_transform_2D(coords: numpy.ndarray, coords_refA: numpy.ndarray, coords_refB: numpy.ndarray) numpy.ndarray[source]#

Compute optimal transformation based on the two sets of 2D points and apply the transformation to other points.

Parameters
coords

2D coordinate matrix needed to be transformed.

coords_refA

Referential 2D coordinate matrix before transformation.

coords_refB

Referential 2D coordinate matrix after transformation.

Returns

The 2D coordinate matrix after transformation

spateo.tools.three_dims_align.rigid_transform_3D(coords: numpy.ndarray, coords_refA: numpy.ndarray, coords_refB: numpy.ndarray) numpy.ndarray[source]#

Compute optimal transformation based on the two sets of 3D points and apply the transformation to other points.

Parameters
coords

3D coordinate matrix needed to be transformed.

coords_refA

Referential 3D coordinate matrix before transformation.

coords_refB

Referential 3D coordinate matrix after transformation.

Returns

The 3D coordinate matrix after transformation

spateo.tools.three_dims_align.models_align(models: List[anndata.AnnData], layer: str = 'X', genes: Optional[Union[list, numpy.ndarray]] = None, spatial_key: str = 'spatial', key_added: str = 'align_spatial', mapping_key_added: str = 'models_align', alpha: float = 0.1, numItermax: int = 200, numItermaxEmd: int = 100000, dtype: str = 'float32', device: str = 'cpu', keep_all: bool = False, **kwargs) List[anndata.AnnData][source]#

Align spatial coordinates of models.

Parameters
models

List of models (AnnData Object).

layer

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

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.

mapping_key_added

.uns key under which to add the alignment info.

alpha

Alignment tuning parameter. Note: 0 <= alpha <= 1.

When alpha = 0 only the gene expression data is taken into account, while when alpha =1 only the spatial coordinates are taken into account.

numItermax

Max number of iterations for cg during FGW-OT.

numItermaxEmd

Max number of iterations for emd during FGW-OT.

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

keep_all

Whether to retain all the optimal relationships obtained only based on the pi matrix, If keep_all is False, the optimal relationships obtained based on the pi matrix and the nearest coordinates.

**kwargs

Additional parameters that will be passed to pairwise_align function.

Returns

List of models (AnnData Object) after alignment.

spateo.tools.three_dims_align.models_align_ref(models: List[anndata.AnnData], models_ref: Optional[List[anndata.AnnData]] = None, n_sampling: Optional[int] = 2000, sampling_method: str = 'trn', layer: str = 'X', genes: Optional[Union[list, numpy.ndarray]] = None, spatial_key: str = 'spatial', key_added: str = 'align_spatial', mapping_key_added: str = 'models_align', alpha: float = 0.1, numItermax: int = 200, numItermaxEmd: int = 100000, dtype: str = 'float32', device: str = 'cpu', **kwargs) Tuple[List[anndata.AnnData], List[anndata.AnnData]][source]#

Align the spatial coordinates of one model list through the affine transformation matrix obtained from another model list.

Parameters
models

List of models (AnnData Object).

models_ref

Another list of models (AnnData Object).

n_sampling

When models_ref is None, new data containing n_sampling coordinate points will be automatically generated for alignment.

sampling_method

The method to sample data points, can be one of ["trn", "kmeans", "random"].

layer

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

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.

mapping_key_added

.uns key under which to add the alignment info.

alpha

Alignment tuning parameter. Note: 0 <= alpha <= 1.

When alpha = 0 only the gene expression data is taken into account, while when alpha =1 only the spatial coordinates are taken into account.

numItermax

Max number of iterations for cg during FGW-OT.

numItermaxEmd

Max number of iterations for emd during FGW-OT.

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'

**kwargs

Additional parameters that will be passed to pairwise_align function.

Returns

List of models (AnnData Object) after alignment. align_models_ref: List of models_ref (AnnData Object) after alignment.

Return type

align_models

spateo.tools.three_dims_align.models_center_align(init_center_model: anndata.AnnData, models: List[anndata.AnnData], layer: str = 'X', genes: Optional[Union[list, numpy.ndarray]] = None, spatial_key: str = 'spatial', key_added: str = 'align_spatial', mapping_key_added: str = 'models_align', lmbda: Optional[numpy.ndarray] = None, alpha: float = 0.1, n_components: int = 15, threshold: float = 0.001, max_iter: int = 10, numItermax: int = 200, numItermaxEmd: int = 100000, dissimilarity: str = 'kl', norm: bool = False, random_seed: Optional[int] = None, pis_init: Optional[List[numpy.ndarray]] = None, distributions: Optional[List[numpy.ndarray]] = None, dtype: str = 'float32', device: str = 'cpu', keep_all: bool = False) Tuple[anndata.AnnData, List[anndata.AnnData]][source]#

Align spatial coordinates of a list of models to a center model.

Parameters
init_center_model

AnnData object to use as the initialization for center alignment; Make sure to include gene expression and spatial information.

models

List of AnnData objects to use in the center alignment.

layer

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

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.

mapping_key_added

.uns key under which to add the alignment info.

lmbda

List of probability weights assigned to each slice; If None, use uniform weights.

alpha

Alignment tuning parameter. Note: 0 <= alpha <= 1.

When alpha = 0 only the gene expression data is taken into account, while when alpha =1 only the spatial coordinates are taken into account.

n_components

Number of components in NMF decomposition.

threshold

Threshold for convergence of W and H during NMF decomposition.

max_iter

Maximum number of iterations for our center alignment algorithm.

numItermax

Max number of iterations for cg during FGW-OT.

numItermaxEmd

Max number of iterations for emd during FGW-OT.

dissimilarity

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

norm

If norm = True, scales spatial distances such that neighboring spots are at distance 1.

Otherwise, spatial distances remain unchanged.

random_seed

Set random seed for reproducibility.

pis_init

Initial list of mappings between A and models to solver.

Otherwise, default will automatically calculate mappings.

distributions

Distributions of spots for each slice. Otherwise, default is uniform.

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

keep_all

Whether to retain all the optimal relationships obtained only based on the pi matrix, If keep_all is False, the optimal relationships obtained based on the pi matrix and the nearest coordinates.

Returns

The center model. align_models: List of models (AnnData Object) after alignment.

Return type

new_center_model

spateo.tools.three_dims_align.models_center_align_ref(init_center_model: anndata.AnnData, models: List[anndata.AnnData], models_ref: Optional[List[anndata.AnnData]] = None, n_sampling: Optional[int] = 1000, sampling_method: str = 'trn', layer: str = 'X', genes: Optional[Union[list, numpy.ndarray]] = None, spatial_key: str = 'spatial', key_added: str = 'align_spatial', mapping_key_added: str = 'models_align', lmbda: Optional[numpy.ndarray] = None, alpha: float = 0.1, n_components: int = 15, threshold: float = 0.001, max_iter: int = 10, numItermax: int = 200, numItermaxEmd: int = 100000, dissimilarity: str = 'kl', norm: bool = False, random_seed: Optional[int] = None, pis_init: Optional[List[numpy.ndarray]] = None, distributions: Optional[List[numpy.ndarray]] = None, dtype: str = 'float32', device: str = 'cpu') Tuple[anndata.AnnData, List[anndata.AnnData], List[anndata.AnnData]][source]#

Align the spatial coordinates of one model list to the central model through the affine transformation matrix obtained from another model list.

Parameters
init_center_model

AnnData object to use as the initialization for center alignment; Make sure to include gene expression and spatial information.

models

List of AnnData objects to use in the center alignment.

models_ref

List of AnnData objects with a small number of coordinates.

n_sampling

When models_ref is None, new data containing n_sampling coordinate points will be automatically generated for alignment.

sampling_method

The method to sample data points, can be one of [“trn”, “kmeans”, “random”].

layer

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

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.

mapping_key_added

.uns key under which to add the alignment info.

lmbda

List of probability weights assigned to each slice; If None, use uniform weights.

alpha

Alignment tuning parameter. Note: 0 <= alpha <= 1.

When alpha = 0 only the gene expression data is taken into account, while when alpha =1 only the spatial coordinates are taken into account.

n_components

Number of components in NMF decomposition.

threshold

Threshold for convergence of W and H during NMF decomposition.

max_iter

Maximum number of iterations for our center alignment algorithm.

numItermax

Max number of iterations for cg during FGW-OT.

numItermaxEmd

Max number of iterations for emd during FGW-OT.

dissimilarity

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

norm

If norm = True, scales spatial distances such that neighboring spots are at distance 1.

Otherwise, spatial distances remain unchanged.

random_seed

Set random seed for reproducibility.

pis_init

Initial list of mappings between A and models to solver.

Otherwise, default will automatically calculate mappings.

distributions

Distributions of spots for each slice. Otherwise, default is uniform.

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'

Returns

The center model. align_models: List of models (AnnData Object) after alignment. align_models_ref: List of models_ref (AnnData Object) after alignment.

Return type

new_center_model

spateo.tools.three_dims_align.get_align_labels(model: anndata.AnnData, align_X: numpy.ndarray, key: Union[str, List[str]], spatial_key: str = 'align_spatial') pandas.DataFrame[source]#

Obtain the label information in anndata.obs[key] corresponding to the align_X coordinate.