neighCount
neighCount ¶
neighCount(
data,
xCoordinate="X_centroid",
yCoordinate="Y_centroid",
zCoordinate=None,
phenotype="phenotype",
method="radius",
radius=30,
knn=10,
imageId="imageid",
subset=None,
verbose=True,
label="neighCount",
maxWorkers=None,
outputDir=None,
streamData=False,
inferenceMode="phenotype_driven",
layer=None,
subsetMarker=None,
sdataTable=None,
)
Count each cell's neighbours by type.
For every cell, find its neighbours and count how many fall into each category,
normalised to proportions. The result — a cell x category matrix — goes to
uns[label], and is what you cluster to get recurrent cellular neighbourhoods:
adata = sp.tl.neighCount(adata, phenotype="phenotype")
adata = sp.tl.cluster(adata, mode="spatial", layer="neighCount", method="kmeans", k=6)
Neighbourhoods are found per image with a BallTree and accumulated with
np.add.at over integer category codes, so there is no Python loop over cells.
Two modes: 'phenotype_driven' counts neighbours by their phenotype label;
'marker_driven' counts them by the markers they express, which lets you build
neighbourhoods before you have called any cell types.
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
|
imageId
|
str
|
Column in |
'imageid'
|
subset
|
str
|
Process only this image. |
None
|
label
|
str
|
Key in |
'neighCount'
|
maxWorkers
|
int
|
Cap on parallel workers. Defaults to |
None
|
inferenceMode
|
str
|
|
'phenotype_driven'
|
layer
|
str
|
Which matrix to read for |
None
|
subsetMarker
|
list
|
Restrict |
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 with the cell x category count matrix in
|
Raises:
| Type | Description |
|---|---|
ValueError
|
When |
Example
# Neighbourhood composition, then recurrent neighbourhoods.
adata = sp.tl.neighCount(adata, phenotype="phenotype", method="radius", radius=30)
adata = sp.tl.cluster(
adata, mode="spatial", layer="neighCount", method="kmeans", k=6,
label="neighbourhood",
)
sp.pl.spatialScatterPlot(adata, colorBy="neighbourhood", s=3)
# Before phenotyping: build neighbourhoods from marker expression.
adata = sp.tl.neighCount(
adata, inferenceMode="marker_driven", layer="rescaled", method="knn", knn=10
)