spateo.tools.coarse_align

Functions

procrustes(→ Tuple[float, numpy.ndarray, dict])

A port of MATLAB's procrustes function to Numpy.

AffineTrans(→ Tuple[numpy.ndarray, numpy.ndarray, ...)

Translate the x/y coordinates of data points by the translating the centroid to the origin. Then data will be

pca_align(→ Tuple[numpy.ndarray, numpy.ndarray])

Use pca to rotate a coordinate matrix to reveal the largest variance on each dimension.

align_slices_pca(→ None)

Coarsely align the slices based on the major axis, identified via PCA

Module Contents

spateo.tools.coarse_align.procrustes(X: numpy.ndarray, Y: numpy.ndarray, scaling: bool = True, reflection: str = 'best') Tuple[float, numpy.ndarray, dict][source]

A port of MATLAB’s procrustes function to Numpy.

This function will need to be rewritten just with scipy.spatial.procrustes and scipy.linalg.orthogonal_procrustes later.

Procrustes analysis determines a linear transformation (translation, reflection, orthogonal rotation and scaling) of the points in Y to best conform them to the points in matrix X, using the sum of squared errors as the goodness of fit criterion.

d, Z, [tform] = procrustes(X, Y)

Parameters:
X

matrices of target and input coordinates. they must have equal numbers of points (rows), but Y may have fewer dimensions (columns) than X. scaling: if False, the scaling component of the transformation is forced to 1

Y

matrices of target and input coordinates. they must have equal numbers of points (rows), but Y may have fewer dimensions (columns) than X. scaling: if False, the scaling component of the transformation is forced to 1

reflection

if ‘best’ (default), the transformation solution may or may not include a reflection component, depending on which fits the data best. setting reflection to True or False forces a solution with reflection or no reflection respectively.

Returns:

the residual sum of squared errors, normalized according to a measure of the scale of X,

((X - X.mean(0))**2).sum()

Z: the matrix of transformed Y-values tform: a dict specifying the rotation, translation and scaling that maps X –> Y

Return type:

d

spateo.tools.coarse_align.AffineTrans(x: numpy.ndarray, y: numpy.ndarray, centroid_x: float, centroid_y: float, theta: Tuple[None, float], R: Tuple[None, numpy.ndarray]) Tuple[numpy.ndarray, numpy.ndarray, numpy.ndarray][source]

Translate the x/y coordinates of data points by the translating the centroid to the origin. Then data will be rotated with angle theta.

Parameters:
x

x coordinates for the data points (bins). 1D np.array.

y

y coordinates for the data points (bins). 1D np.array.

centroid_x

x coordinates for the centroid of data points (bins).

centroid_y

y coordinates for the centroid of data points (bins).

theta

the angle of rotation. Unit is is in np.pi (so 90 degree is np.pi / 2 and value is defined in the clockwise direction.

R

the rotation matrix. If R is provided, theta will be ignored.

Returns:

The translation matrix used in affine transformation. T_r: The rotation matrix used in affine transformation. trans_xy_coord: The matrix that stores the translated and rotated coordinates.

Return type:

T_t

spateo.tools.coarse_align.pca_align(X: numpy.ndarray) Tuple[numpy.ndarray, numpy.ndarray][source]

Use pca to rotate a coordinate matrix to reveal the largest variance on each dimension.

This can be used to correct, for example, embryo slices to the right orientation.

Parameters:
X

The input coordinate matrix.

Returns:

The rotated coordinate matrix that has the major variances on each dimension. R: The rotation matrix that was used to convert the input X matrix to output Y matrix.

Return type:

Y

spateo.tools.coarse_align.align_slices_pca(adata: anndata.AnnData, spatial_key: str = 'spatial', inplace: bool = False, result_key: Tuple[None, str] = None) None[source]

Coarsely align the slices based on the major axis, identified via PCA

Parameters:
adata

the input adata object that contains the spatial key in .obsm.

spatial_key

the key in .obsm that points to the spatial information.

inplace

whether the spatial coordinates will be inplace updated or a new key `spatial_.

result_key

when inplace is False, this points to the key in .obsm that stores the corrected spatial coordinates.

Returns:

Nothing but updates the spatial coordinates either inplace or with the result_key key based on the major axis identified via PCA.