spateo.tools.utils#

Module Contents#

Functions#

rescaling(→ Union[numpy.ndarray, scipy.sparse.spmatrix])

This function rescale the resolution of the input matrix that represents a spatial domain. For example, if you

get_mapper([smoothed])

update_dict(dict1, dict2)

flatten(arr)

compute_corr_ci(r, n[, confidence, decimals, alternative])

Parametric confidence intervals around a correlation coefficient

calc_1nd_moment(X, W[, normalize_W])

affine_transform(X, A, b)

gen_rotation_2d(degree)

compute_smallest_distance(→ float)

Compute and return smallest distance. A wrapper for sklearn API

polyhull(→ pyvista.PolyData)

Create a PolyData object from the convex hull constructed with the input data points.

in_hull(→ numpy.ndarray)

Test if points in p are in hull

parse_instruction(instruction[, axis_map])

Parses a single filtering instruction and returns the equivalent pandas query string.

filter_adata_spatial(adata, coords_key, instructions)

Filters the AnnData object by spatial coordinates based on the provided instructions list, to be executed

create_new_coordinate(adata[, position_key, plane])

Projects points from an AnnData object onto a specified plane and direction, calculate the distances along this

project_and_calculate_distance(row, cols, min_point, ...)

Project a point onto a line with a slope of either 1 or -1, and calculate the distance along the new line for

spateo.tools.utils.rescaling(mat: numpy.ndarray | scipy.sparse.spmatrix, new_shape: List | Tuple) numpy.ndarray | scipy.sparse.spmatrix[source]#

This function rescale the resolution of the input matrix that represents a spatial domain. For example, if you want to decrease the resolution of a matrix by a factor of 2, the new_shape will be mat.shape / 2.

Parameters:
mat

The input matrix of the spatial domain (or an image).

new_shape

The rescaled shape of the spatial domain, each dimension must be an factorial of the original dimension.

Returns:

the spatial resolution rescaled matrix.

Return type:

res

spateo.tools.utils.get_mapper(smoothed=True)[source]#
spateo.tools.utils.update_dict(dict1, dict2)[source]#
spateo.tools.utils.flatten(arr)[source]#
spateo.tools.utils.compute_corr_ci(r: float, n: int, confidence: float = 95, decimals: int = 2, alternative: Literal[two - sided, less, greater] = 'two-sided')[source]#

Parametric confidence intervals around a correlation coefficient

Parameters:
r

Correlation coefficient

n

Length of x vector and y vector (the vectors used to compute the correlation)

confidence

Confidence level, as a percent (so 95 = 95% confidence interval). Must be between 0 and 100.

decimals

Number of rounded decimals

alternative

Defines the alternative hypothesis, or tail for the correlation coefficient. Must be one of “two-sided” (default), “greater” or “less”

Returns:

Confidence interval

Return type:

ci

spateo.tools.utils.calc_1nd_moment(X, W, normalize_W=True)[source]#
spateo.tools.utils.affine_transform(X, A, b)[source]#
spateo.tools.utils.gen_rotation_2d(degree: float)[source]#
spateo.tools.utils.compute_smallest_distance(coords: numpy.ndarray, leaf_size: int = 40, sample_num=None, use_unique_coords=True) float[source]#

Compute and return smallest distance. A wrapper for sklearn API :param coords: NxM matrix. N is the number of data points and M is the dimension of each point’s feature. :param leaf_size: Leaf size parameter for building Kd-tree, by default 40. :type leaf_size: int, optional :param sample_num: The number of cells to be sampled. :param use_unique_coords: Whether to remove duplicate coordinates

Returns:

min_dist – the minimum distance between points

Return type:

float

spateo.tools.utils.polyhull(x: numpy.ndarray, y: numpy.ndarray, z: numpy.ndarray) pyvista.PolyData[source]#

Create a PolyData object from the convex hull constructed with the input data points.

scipy’s ConvexHull to be 500X faster than using vtkDelaunay3D and vtkDataSetSurfaceFilter because you skip the expensive 3D tesselation of the volume.

Parameters:
x

x coordinates of the data points.

y

y coordinates of the data points.

z

z coordinates of the data points.

Returns:

a PolyData object generated with the convex hull constructed based on the input data points.

Return type:

poly

spateo.tools.utils.in_hull(p: numpy.ndarray, hull: Tuple[scipy.spatial.Delaunay, numpy.ndarray]) numpy.ndarray[source]#

Test if points in p are in hull

Parameters:
p

a N x K coordinates of N points in K dimensions

hull

either a scipy.spatial.Delaunay object or the MxK array of the coordinates of M points in K

computed. dimensions for which Delaunay triangulation will be

Returns:

A numpy array with boolean values indicating whether the input points is in the convex hull.

Return type:

res

spateo.tools.utils.parse_instruction(instruction: str, axis_map: Dict[str, str] | None = None)[source]#

Parses a single filtering instruction and returns the equivalent pandas query string.

Parameters:
instruction

Filtering condition, in a form similar to the following: “x less than 950 and z less than or equal to 350”. This is equivalent to ((x < 950) & (z <= 350)). Here, x is the name of one dataframe column and z is the name of another. For negation, use “not (x less than 950)”.

axis_map

In the case that an alias can be used for the dataframe column names (e.g. “x-axis” -> “x”), this dictionary maps these optional aliases to column names.

Returns:

The equivalent pandas query string.

Return type:

query

spateo.tools.utils.filter_adata_spatial(adata: anndata.AnnData, coords_key: str, instructions: List[str], col_alias_map: Dict[str, str] | None = None)[source]#

Filters the AnnData object by spatial coordinates based on the provided instructions list, to be executed sequentially.

Parameters:
adata

AnnData object containing spatial coordinates in .obsm

coords_key

Key in .obsm containing spatial coordinates

instructions

List of filtering instructions, in a form similar to the following: “x less than 950 and z less than or equal to 350”. This is equivalent to ((x < 950) & (z <= 350)). Here, x is the name of one dataframe column and z is the name of another. For negation, use “not (x less than 950)”.

col_alias_dict

In the case that an alias can be used for the dataframe column names (e.g. “x-axis” is used to refer to the dataframe column “x”), this dictionary maps these optional aliases to column names.

Returns:

Filtered AnnData object

Return type:

adata

spateo.tools.utils.create_new_coordinate(adata: anndata.AnnData, position_key: str = 'spatial', plane: Literal[xy, yz, xz, -xy, -yz, -xz] = 'xy')[source]#

Projects points from an AnnData object onto a specified plane and direction, calculate the distances along this projection, and add the results to the AnnData object.

Parameters:
adata

AnnData object containing spatial coordinates in .obsm

position_key

Key in .obsm containing spatial coordinates. Defaults to “spatial”.

plane

Plane to project points onto. Must be one of “xy”, “yz”, “xz”, “-xy”, “-yz”, “-xz”. The “-” prefix indicates that the direction along the first axis is reversed (i.e. instead of starting from the minimum value, it starts from the maximum value). Defaults to “xy”.

Returns:

AnnData object with new column added to .obs

Return type:

adata

spateo.tools.utils.project_and_calculate_distance(row: pandas.Series, cols: list, min_point: pandas.Series, max_point: pandas.Series, reverse: bool = False)[source]#

Project a point onto a line with a slope of either 1 or -1, and calculate the distance along the new line for the projected point to establish its coordinate.

Parameters:
row

A row from the dataframe that contains the spatial coordinates

cols

The list of two columns that define the plane, e.g. [“X”, “Y”]

min_point

The minimum point that defines the “start” of the new coordinate axis

max_point

The maximum point that defines the “end” of the new coordinate axis

reverse

Flag that indicates whether the projection starts from the minimum along the x-axis or the maximum along the x-axis (or y-axis or z-axis, depending on which axis is used as the reference).

Returns:

The distance along the new coordinate axis for the projected point

Return type:

distance