spateo.alignment.utils

Functions

_iteration(n, progress_name[, verbose, start_n, ...])

downsampling(→ Union[List[anndata.AnnData], ...)

generate_label_transfer_prior(cat1, cat2[, ...])

group_pca(→ None)

Perform PCA on a concatenated set of AnnData objects and store the results back in each individual AnnData.

get_optimal_mapping_relationship(X, Y, pi[, keep_all])

mapping_aligned_coords(→ Tuple[dict, dict])

Optimal mapping coordinates between X and Y.

mapping_center_coords(→ dict)

Optimal mapping coordinates between X and Y based on intermediate coordinates.

get_labels_based_on_coords(→ pandas.DataFrame)

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

solve_RT_by_correspondence(...)

Solve for the rotation matrix R and translation vector t that best align the points in X to the points in Y.

rigid_transformation(adata, spatial_key, key_added[, ...])

split_slice(adata, spatial_key[, split_num, axis])

tps_deformation(adata, spatial_key, key_added[, ...])

Module Contents

spateo.alignment.utils._iteration(n: int, progress_name: str, verbose: bool = True, start_n: int = 0, indent_level=1)[source]
spateo.alignment.utils.downsampling(models: List[anndata.AnnData] | anndata.AnnData, n_sampling: int | None = 2000, sampling_method: str = 'trn', spatial_key: str = 'spatial') List[anndata.AnnData] | anndata.AnnData[source]
spateo.alignment.utils.generate_label_transfer_prior(cat1, cat2, positive_pairs=None, negative_pairs=None)[source]
spateo.alignment.utils.group_pca(adatas: List[anndata.AnnData], batch_key: str = 'batch', pca_key: str = 'X_pca', use_hvg: bool = True, hvg_key: str = 'highly_variable', **args) None[source]

Perform PCA on a concatenated set of AnnData objects and store the results back in each individual AnnData.

Parameters:

adatasList[AnnData]

A list of AnnData objects to be concatenated and processed.

batch_keystr, optional

The key to distinguish different batches in the concatenated AnnData object (default is ‘batch’).

pca_keystr, optional

The key under which to store the PCA results in each AnnData’s .obsm attribute (default is ‘X_pca’).

use_hvgbool, optional

Whether to perform PCA using only highly variable genes (default is True).

hvg_keystr, optional

The key under which highly variable genes are marked in .var (default is ‘highly_variable’).

**args

Additional arguments to pass to sc.tl.pca.

Raises:

ValueError:

If the specified batch_key already exists in any of the input AnnData objects. If no highly variable genes are found when use_hvg is True.

spateo.alignment.utils.get_optimal_mapping_relationship(X: numpy.ndarray, Y: numpy.ndarray, pi: numpy.ndarray, keep_all: bool = False)[source]
spateo.alignment.utils.mapping_aligned_coords(X: numpy.ndarray, Y: numpy.ndarray, pi: numpy.ndarray, keep_all: bool = False) Tuple[dict, dict][source]

Optimal mapping coordinates between X and Y.

Parameters:
X

Aligned spatial coordinates.

Y

Aligned spatial coordinates.

pi

Mapping between the two layers output by PASTE.

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:

Two dicts of mapping_X, mapping_Y, pi_index, pi_value.

mapping_X is X coordinates aligned with Y coordinates. mapping_Y is the Y coordinate aligned with X coordinates. pi_index is index between optimal mapping points in the pi matrix. pi_value is the value of optimal mapping points.

spateo.alignment.utils.mapping_center_coords(modelA: anndata.AnnData, modelB: anndata.AnnData, center_key: str) dict[source]

Optimal mapping coordinates between X and Y based on intermediate coordinates.

Parameters:
modelA

modelA aligned with center model.

modelB

modelB aligned with center model.

center_key

The key in .uns that corresponds to the alignment info between modelA/modelB and center model.

Returns:

A dict of raw_X, raw_Y, mapping_X, mapping_Y, pi_value.

raw_X is the raw X coordinates. raw_Y is the raw Y coordinates. mapping_X is the Y coordinates aligned with X coordinates. mapping_Y is the X coordinates aligned with Y coordinates. pi_value is the value of optimal mapping points.

spateo.alignment.utils.get_labels_based_on_coords(model: anndata.AnnData, coords: numpy.ndarray, labels_key: str | List[str], spatial_key: str = 'align_spatial') pandas.DataFrame[source]

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

spateo.alignment.utils.solve_RT_by_correspondence(X: numpy.ndarray, Y: numpy.ndarray, return_scale: bool = False) Tuple[numpy.ndarray, numpy.ndarray] | Tuple[numpy.ndarray, numpy.ndarray, float][source]

Solve for the rotation matrix R and translation vector t that best align the points in X to the points in Y.

Parameters:
X np.ndarray

Source points, shape (N, D).

Y np.ndarray

Target points, shape (N, D).

return_scale bool, optional

Whether to return the scale factor. Defaults to False.

Returns:

If return_scale is False, returns the rotation matrix R and translation vector t. If return_scale is True, also returns the scale factor s.

Return type:

Union[Tuple[np.ndarray, np.ndarray], Tuple[np.ndarray, np.ndarray, float]]

spateo.alignment.utils.rigid_transformation(adata, spatial_key, key_added, theta=None, translation=None, inplace=True)[source]
spateo.alignment.utils.split_slice(adata, spatial_key, split_num=5, axis=2)[source]
spateo.alignment.utils.tps_deformation(adata, spatial_key, key_added, grid_num=2, tps_noise_scale=25, add_corner_points=True, alpha=0.1, inplace=True)[source]