spateo.tools.utils ================== .. py:module:: spateo.tools.utils Functions --------- .. autoapisummary:: spateo.tools.utils.rescaling spateo.tools.utils.get_mapper spateo.tools.utils.update_dict spateo.tools.utils.flatten spateo.tools.utils.compute_corr_ci spateo.tools.utils.calc_1nd_moment spateo.tools.utils.affine_transform spateo.tools.utils.gen_rotation_2d spateo.tools.utils.compute_smallest_distance spateo.tools.utils.polyhull spateo.tools.utils.in_hull spateo.tools.utils.parse_instruction spateo.tools.utils.filter_adata_spatial spateo.tools.utils.create_new_coordinate Module Contents --------------- .. py:function:: rescaling(mat: Union[numpy.ndarray, scipy.sparse.spmatrix], new_shape: Union[List, Tuple]) -> Union[numpy.ndarray, scipy.sparse.spmatrix] 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`. :param mat: The input matrix of the spatial domain (or an image). :param 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. :rtype: res .. py:function:: get_mapper(smoothed=True) .. py:function:: update_dict(dict1, dict2) .. py:function:: flatten(arr) .. py:function:: compute_corr_ci(r: float, n: int, confidence: float = 95, decimals: int = 2, alternative: Literal['two-sided', 'less', 'greater'] = 'two-sided') Parametric confidence intervals around a correlation coefficient :param r: Correlation coefficient :param n: Length of x vector and y vector (the vectors used to compute the correlation) :param confidence: Confidence level, as a percent (so 95 = 95% confidence interval). Must be between 0 and 100. :param decimals: Number of rounded decimals :param alternative: Defines the alternative hypothesis, or tail for the correlation coefficient. Must be one of "two-sided" (default), "greater" or "less" :returns: Confidence interval :rtype: ci .. py:function:: calc_1nd_moment(X, W, normalize_W=True) .. py:function:: affine_transform(X, A, b) .. py:function:: gen_rotation_2d(degree: float) .. py:function:: compute_smallest_distance(coords: numpy.ndarray, leaf_size: int = 40, sample_num=None, use_unique_coords=True) -> float 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 :rtype: float .. py:function:: polyhull(x: numpy.ndarray, y: numpy.ndarray, z: numpy.ndarray) -> pyvista.PolyData 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. :param x: x coordinates of the data points. :param y: y coordinates of the data points. :param z: z coordinates of the data points. :returns: a PolyData object generated with the convex hull constructed based on the input data points. :rtype: poly .. py:function:: in_hull(p: numpy.ndarray, hull: Tuple[scipy.spatial.Delaunay, numpy.ndarray]) -> numpy.ndarray Test if points in `p` are in `hull` :param p: a `N x K` coordinates of `N` points in `K` dimensions :param hull: either a scipy.spatial.Delaunay object or the `MxK` array of the coordinates of `M` points in `K` :param dimensions for which Delaunay triangulation will be computed.: :returns: A numpy array with boolean values indicating whether the input points is in the convex hull. :rtype: res .. py:function:: parse_instruction(instruction: str, axis_map: Optional[Dict[str, str]] = None) Parses a single filtering instruction and returns the equivalent pandas query string. :param 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)". :param 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. :rtype: query .. py:function:: filter_adata_spatial(adata: anndata.AnnData, coords_key: str, instructions: List[str], col_alias_map: Optional[Dict[str, str]] = None) Filters the AnnData object by spatial coordinates based on the provided instructions list, to be executed sequentially. :param adata: AnnData object containing spatial coordinates in .obsm :param coords_key: Key in .obsm containing spatial coordinates :param 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)". :param 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 :rtype: adata .. py:function:: create_new_coordinate(adata: anndata.AnnData, position_key: str = 'spatial', plane: Literal['xy', 'yz', 'xz', '-xy', '-yz', '-xz'] = 'xy') 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. :param adata: AnnData object containing spatial coordinates in .obsm :param position_key: Key in .obsm containing spatial coordinates. Defaults to "spatial". :param 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 :rtype: adata