spatialCooccurrence
spatialCooccurrence ¶
spatialCooccurrence(
data,
xCoordinate="X_centroid",
yCoordinate="Y_centroid",
zCoordinate=None,
phenotype="phenotype",
method="radius",
radius=30,
knn=10,
permutation=1000,
imageId="imageid",
subset=None,
verbose=True,
label="spatialCooccurrence",
maxWorkers=-1,
streamData=False,
outputDir=None,
sdataTable=None,
)
Test cell-type adjacency against a permutation null.
Neighbourhoods are found once per image with a BallTree, and every
(cell, neighbour) pair is reduced to an integer key so the counts are a single
np.bincount. The phenotype labels are then shuffled permutation times — the
shuffle and the recount run in a Numba-compiled kernel — and each observed count
is turned into a z-score against that null, then into a two-sided p-value.
The reported score is not the raw count: counts are divided by the number of cells of the source type and rescaled so each row's largest value is 1, then signed by whether the pair was observed more or less often than expected. So a score near +1 means strong attraction, near -1 strong avoidance.
The result written to uns[label] is a long table with columns phenotype,
neighbour_phenotype, one score column per image, and one pvalue_<image>
column per image. Plot it with
sp.pl.spatialCooccurrence or
sp.pl.spatialInteractionNetwork.
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'
|
method
|
str
|
How neighbourhoods are defined. |
'radius'
|
radius
|
float
|
Neighbourhood radius in the units of the coordinate columns, normally
pixels. Used when |
30
|
knn
|
int
|
Number of nearest neighbours. Used when |
10
|
permutation
|
int
|
Number of label shuffles behind each p-value. 1000 is a reasonable default; drop it to 100 while exploring, since runtime is linear in it. |
1000
|
imageId
|
str
|
Column in |
'imageid'
|
subset
|
str
|
Process only this image. |
None
|
label
|
str
|
Key in |
'spatialCooccurrence'
|
maxWorkers
|
int
|
Workers for the permutation loop, passed to |
-1
|
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.spatialCooccurrence(
adata, phenotype="phenotype", method="radius", radius=30, permutation=1000
)
sp.pl.spatialCooccurrence(adata)
# Nearest-neighbour definition instead of a fixed radius.
adata = sp.tl.spatialCooccurrence(adata, method="knn", knn=10)
# Only significant pairs, as a network.
sp.pl.spatialInteractionNetwork(adata, pVal=0.01)