spatialDistance
spatialDistance ¶
spatialDistance(
data,
xCoordinate: str = "X_centroid",
yCoordinate: str = "Y_centroid",
zCoordinate: str = None,
phenotype: str = "phenotype",
subset: str = None,
imageId: str = "imageid",
label: str = "spatial_distance",
verbose: bool = True,
maxWorkers: int = None,
outputDir: str = None,
streamData: bool = False,
sdataTable: str = None,
) -> Union[None, AnnData]
Distance from every cell to the nearest cell of each phenotype.
For each image independently, a BallTree is built per phenotype and queried
with every cell, giving a cell x phenotype matrix of nearest-neighbour
distances. The result is written to uns[label], indexed like obs so it can
be joined back or plotted directly.
Distances are in the units of the coordinate columns — pixels for a typical mcmicro quantification. A cell's distance to its own phenotype is the distance to the nearest other cell of that type, not zero.
Plot the result with
sp.pl.spatialDistanceHeatmap or
sp.pl.spatialDistanceDistribution.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
(AnnData | SpatialData | str, required)
|
The cell table. An |
required |
xCoordinate
|
str
|
Column in |
'X_centroid'
|
yCoordinate
|
str
|
Column in |
'Y_centroid'
|
zCoordinate
|
str
|
Column in |
None
|
phenotype
|
str
|
Column in |
'phenotype'
|
subset
|
str
|
Process only this image. |
None
|
imageId
|
str
|
Column in |
'imageid'
|
label
|
str
|
Key in |
'spatial_distance'
|
maxWorkers
|
int
|
Cap on parallel workers. Defaults to |
None
|
verbose
|
bool
|
Print progress messages. |
True
|
outputDir
|
str
|
Directory to write the updated object to. With |
None
|
streamData
|
bool
|
Run out of core against an |
False
|
sdataTable
|
str
|
Which |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
adata |
AnnData | SpatialData | None
|
The updated object when |
Example
adata = sp.tl.spatialDistance(adata, phenotype="phenotype")
adata.uns["spatial_distance"].head()
# Mean distance from each cell type to every other.
sp.pl.spatialDistanceHeatmap(adata, phenotype="phenotype")
# Out of core, on a file too large to load.
sp.tl.spatialDistance("big.h5ad", phenotype="phenotype", streamData=True)