spateo.tools.utils
#
Module Contents#
Functions#
|
This function rescale the resolution of the input matrix that represents a spatial domain. For example, if you |
|
|
|
|
|
|
|
Parametric confidence intervals around a correlation coefficient |
|
|
|
|
|
|
|
Compute and return smallest distance. A wrapper for sklearn API |
|
Create a PolyData object from the convex hull constructed with the input data points. |
|
Test if points in p are in hull |
|
Parses a single filtering instruction and returns the equivalent pandas query string. |
|
Filters the AnnData object by spatial coordinates based on the provided instructions list, to be executed |
|
Projects points from an AnnData object onto a specified plane and direction, calculate the distances along this |
|
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.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.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:
- 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