spatialSimilarityLookup
spatialSimilarityLookup ¶
spatialSimilarityLookup(
data,
roiColumn,
xCoordinate="X_centroid",
yCoordinate="Y_centroid",
zCoordinate=None,
similarityThreshold=0.5,
roiSubset=None,
method="radius",
radius=30,
knn=10,
imageId="imageid",
layer="raw",
log=True,
subset=None,
label="spatialSimilarityLookup",
reuseSimilarityMatrix=None,
verbose=True,
outputDir=None,
streamData=False,
n_jobs=-1,
sdataTable=None,
)
Find regions whose neighbourhood resembles a reference ROI.
The neighbourhood-weighted expression (spatial lag) is computed for every cell,
the median lag vector of the reference ROI becomes the query, and every cell is
scored by its Euclidean similarity to that query. Cells scoring at or above
similarityThreshold are labelled as matching.
Draw the reference ROI first — with
sp.helpers.addROI_omero or
sp.pl.addRoiScatter — and pass the column it
wrote as roiColumn. Cells labelled 'Other' are treated as outside every ROI.
Scores land in layers[label], and one boolean obs column per ROI is written
as label + "_" + <roi name>. The similarity kernel is Numba-compiled and the
per-image work runs through joblib.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
(AnnData | SpatialData | str, required)
|
The cell table. An |
required |
roiColumn
|
(str, required)
|
Column in |
required |
xCoordinate
|
str
|
Column in |
'X_centroid'
|
yCoordinate
|
str
|
Column in |
'Y_centroid'
|
zCoordinate
|
str
|
Column in |
None
|
similarityThreshold
|
float
|
Score at or above which a cell counts as a match. Higher is stricter. |
0.5
|
roiSubset
|
str | list
|
Use only these ROIs as queries. Defaults to every value of |
None
|
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'
|
layer
|
str
|
Which matrix the spatial lag is computed from. |
'raw'
|
log
|
bool
|
Apply |
True
|
subset
|
str
|
Process only this image. |
None
|
label
|
str
|
Key for the outputs: |
'spatialSimilarityLookup'
|
reuseSimilarityMatrix
|
str
|
Name of a layer holding scores from a previous run. Supplying it skips the spatial-lag computation, which is the expensive part — use it to re-threshold without recomputing. |
None
|
n_jobs
|
int
|
Workers for the per-image computation, 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 with |
Raises:
| Type | Description |
|---|---|
ValueError
|
When no cells overlap between the result and |
Example
# Draw a reference region, then look for it everywhere.
adata = sp.pl.addRoiScatter(adata, marker="ECAD", roiName="tumour", label="roi")
adata = sp.tl.spatialSimilarityLookup(
adata, roiColumn="roi", radius=30, similarityThreshold=0.5
)
sp.pl.spatialScatterPlot(adata, colorBy="spatialSimilarityLookup_tumour", s=3)
# Try a stricter threshold without recomputing the spatial lag.
adata = sp.tl.spatialSimilarityLookup(
adata,
roiColumn="roi",
similarityThreshold=0.8,
reuseSimilarityMatrix="spatialSimilarityLookup",
label="strictLookup",
)