spatialtis_core
Contents
spatialtis_core
#
Subpackages#
Submodules#
Package Contents#
- class spatialtis_core.CellCombs(types: List[str], order: bool = False)#
Profile cell-cell interaction using permutation test
- Parameters
types – All the type of cells in your research
order – bool (False); If False, A->B and A<-B is the same
- bootstrap(types: List[str], neighbors: spatialtis_core.types.Neighbors, labels: spatialtis_core.types.Labels, times: int = 1000, pval: float = 0.05, method: str = 'pval') List[Tuple[str, str, float]] #
Bootstrap functions
1.0 means association, -1.0 means avoidance, 0.0 means insignificance.
- Parameters
types – The type of all the cells
neighbors – List of neighbors
labels – List of labels
times – How many times to perform bootstrap
pval – The threshold of p-value
method – ‘pval’ or ‘zscore’
- Returns
List of tuples, eg.(‘a’, ‘b’, 1.0), the type a and type b has a relationship as association
- spatialtis_core.bbox_neighbors(bbox: List[spatialtis_core.types.BoundingBox], labels: spatialtis_core.types.Labels, expand: float = - 1.0, scale: float = 1.3) List[List[int]] #
Get neighbors for each bounding box
- Parameters
bbox – A list of bounding box
labels – Integer to label your bounding box
expand – Expand the bounding box to search for neighbors
scale – Scale the bounding box to search for neighbors
- Returns
A list of neighbors
- spatialtis_core.comb_bootstrap(exp_matrix: numpy.ndarray, markers: List[str], neighbors: spatialtis_core.types.Neighbors, labels: spatialtis_core.types.Labels, pval: float = 0.05, times: int = 1000) List[Tuple[str, str, float]] #
Bootstrap between two types
If you want to test co-localization between protein X and Y, first determine if the cell is X-positive and/or Y-positive. True is considered as positive and will be counted.
- Parameters
exp_matrix – The expression matrix, each row should be a marker
markers – Match to the row of exp_matrix
neighbors – List of neighbors
labels – List of labels
pval – The threshold of p-value
times – How many times to perform bootstrap
- Returns
The significance between markers List of (marker1, marker2, p-value)
- spatialtis_core.dumps_points_wkt(points: spatialtis_core.types.Points) List[str] #
Dumps points into wkt format
- Parameters
points – A list of 2D points
- Returns
A list of wkt string
- spatialtis_core.dumps_polygons_wkt(polygons: List[spatialtis_core.types.Points]) List[str] #
Dumps points into wkt format
- Parameters
polygons – A list of polygons
- Returns
A list of wkt string
- spatialtis_core.fast_corr(data1: numpy.ndarray, data2: numpy.ndarray, method: Optional[str] = 'pearson') numpy.ndarray #
Parallel pairwise correlation
Compute pairwise (combination with replacement) correlation between two 2D array with same shape. Faster than scipy’s implementation.
- Parameters
data1 – 2D Array
data2 – 2D Array
method – “pearson” or “spearman”
- spatialtis_core.getis_ord(points: spatialtis_core.types.Points, bbox: spatialtis_core.types.BoundingBox, search_level: int = 3, quad: Optional[Tuple[int, int]] = None, rect_side: Optional[Tuple[float, float]] = None, pval: float = 0.05, min_cells: int = 10) List[bool] #
Getis-ord analysis to find hot cells
- Parameters
points – A list of points
bbox – The bounding box
search_level – The level of outer-ring to search for
quad – eg.(X, Y) Use X * Y grid to perform analysis
rect_side – eg.(X, Y) Use X * Y rectangle to perform analysis
pval – The threshold for p-value
min_cells – The minimum number of cells to perform analysis
- Returns
A list of bool
- spatialtis_core.multipoints_bbox(points_collections: Sequence[spatialtis_core.types.Points]) Sequence[spatialtis_core.types.BoundingBox] #
A utility function to return minimum bounding box list of polygons
- Parameters
points_collections – List of 2d points collections
- Returns
A list of bounding box (minx, miny, maxx, maxy)
- spatialtis_core.multipolygons_area(points_collections: Sequence[spatialtis_core.types.Points]) Sequence[float] #
Calculate the area of polygons using shoelace formula
- Parameters
points_collections – List of 2d points collections, represents a list of polygons
- Returns
The area of the polygons in list
- spatialtis_core.neighbor_components(neighbors: spatialtis_core.types.Neighbors, labels: spatialtis_core.types.Labels, types: List[str])#
Compute the number of different cells at neighbors
- Parameters
neighbors – The neighbors dict
labels – Integer to label points
types – A list of types match to points
- Returns
can be used to construct dataframe
- Return type
(header, data)
- spatialtis_core.points_bbox(points: spatialtis_core.types.Points) spatialtis_core.types.BoundingBox #
Return minimum bounding box of points
- Parameters
points – A list of points
- Returns
The bounding box (minx, miny, maxx, maxy) or (minx, miny, minz, maxx, maxy, maxz)
- spatialtis_core.points_neighbors(points: spatialtis_core.types.Points, labels: spatialtis_core.types.Labels, r: Optional[float] = None, k: Optional[int] = None, method: str = 'kdtree') List[List[int]] #
Get neighbors for each points
When search with KD-tree, you can use either r or k. If r = 5, it will search for all points within 5; If k = 5, it will search for the 5 nearest neighbors; If combined, r = 5 and k = 5, it will search for at most 5 neighbors within 5;
When search with Delaunay triangulation, there is no parameter.
The return list follow the order of labels. For example, if serach for points of [100, 101, 102], the result may look like [[100, 102], [101, 102], [102, 100, 101]]
- Parameters
points – A list of points
labels – Integer to labels your points
r – Radius range to search for neighbors
k – Number of nearest neighbors
method – “kdtree” or “delaunay”
- Returns
A list of neighbors
- spatialtis_core.points_shapes(polygons: List[spatialtis_core.types.Points], method: str = 'convex', concavity: float = 1.5) spatialtis_core.types.Points #
Acquire multipoints (shapes) that describe the points
- Parameters
polygons – A list of polygons
method – “convex” or “concave”
concavity – Determine the concavity in concave hull
- Returns
A list of points
- spatialtis_core.polygons_area(points: spatialtis_core.types.Points) float #
Calculate the area of polygons using shoelace formula
- Parameters
points – A list of points represents a polygon
- Returns
The area of the polygon
- spatialtis_core.reads_wkt_points(wkt_strings: List[str]) spatialtis_core.types.Points #
Reads wkt points into python object
- Parameters
wkt_strings – A list of wkt string represents points
- Returns
A list of 2D points
- spatialtis_core.reads_wkt_polygons(wkt_strings: List[str]) List[spatialtis_core.types.Points] #
Reads wkt points into python object
- Parameters
wkt_strings – A list of wkt string represents polygons
- Returns
A list of 2D polygons
- spatialtis_core.spatial_autocorr(x: numpy.ndarray, neighbors: spatialtis_core.types.Neighbors, labels: spatialtis_core.types.Labels, two_tailed: bool = True, pval: float = 0.05, method: str = 'moran_i') List[Tuple[float, float]] #
Compute spatial auto-correlation value for a 2D array in parallel
The p-value is under the assumption of normal distribution Return is tuples of (spatial_autocorr value, p value)
- Parameters
x – Gene expression matrix, each row is the expression of a gene
neighbors – A list of neighbors
labels – A list of labels
two_tailed – Determine the p value
pval – The p-value threshold
method – “moran_i” or “geary_c”
- Returns
A list of (value, p_value)
- spatialtis_core.spatial_distribution_pattern(points_collections: List[spatialtis_core.types.Points], bbox: spatialtis_core.types.BoundingBox, method: str = 'id', r: Optional[float] = None, resample: int = 1000, quad: Optional[Tuple[int, int]] = None, rect_side: Optional[Tuple[float, float]] = None, pval: float = 0.05, min_cells: int = 10, dims: int = 2) List[Tuple[float, float, int]] #
Compute the distribution index and determine the pattern for different cells in a ROI in parallel
If data is 3D, only method=”id” is supported
- Parameters
points_collections – A list of list of points
bbox – The bounding box
method – “id” for index of dispersion, “morisita” for morisita index, “clark_evans” for clark evans’ index
r – If method == “id”; The sample windows’ radius
resample – If method == “id”; The number of sampling times
quad – If method == “morisita”; eg.(X, Y) Use X * Y grid to perform analysis
rect_side – If method == “morisita”; eg.(X, Y) Use X * Y rectangle to perform analysis
pval – The threshold for p-value
min_cells – The minimum number of cells to perform analysis
dims – The dimension of data
- Returns
A list of (index_value, p_value, pattern)
- spatialtis_core.spatial_entropy(points_collections: List[spatialtis_core.types.Points], types_collections: List[List[str]], method: str = 'leibovici', d: Optional[float] = None, cut: int = 3, dims: int = 2) List[float] #
Compute spatial entropy value of multiple ROIs in parallel
- Parameters
points_collections – A list of list of points
types_collections – A list of list of types
bbox – The bounding box
method – “leibovici” or “altieri”
d – If method == “leibovici”; The distance threshold to determine co-occurrence
cut – If method == “altieri”; The distance interval to determine co-occurrence
dims – The dimension of data
- Returns
A list of spatial entropy
- spatialtis_core.spatial_weight(neighbors: types.Neighbors, labels: types.Labels) scipy.sparse.csr_matrix #
Build a neighbors sparse matrix from neighbors data
- Parameters
neighbors – List of neighbors
labels – List of neighbors
- Returns
A scipy sparse matrix in csr format
- spatialtis_core.Labels#
- spatialtis_core.Neighbors#