Repository navigation
Description
BayesSpace::spatialCluster() finds no neighbours at all on the shipped test
dataset, so its spatial prior contributes nothing and spatial_cluster is being
produced by what is effectively a non-spatial mixture model. It prints this on
every run:
Neighbors were identified for 0 out of 278 spots.
Reproducing
source("src/helpers/utils.R")
adata <- anndataR::read_h5ad("resources_test/spatialsimbench_mobnew/dataset_sp.h5ad")
generate_sim_spatialCluster(adata, adata)
#> Neighbors were identified for 0 out of 278 spots.
#> Fitting model...Why
BayesSpace:::.find_neighbors locates neighbours with an exact-equality join
on the coordinates -- it adds ±1 to each spot's position and looks for a spot
sitting exactly there:
neighbor.positions$x.pos <- neighbor.positions$array_col + x.offset # ±1 steps
neighbors <- merge(as.data.frame(neighbor.positions), as.data.frame(spot.positions),
by.x = c("x.pos", "y.pos"),
by.y = c("array_col", "array_row"), all.x = TRUE)We hand it col/row unchanged in
task_spatial_simulators/src/helpers/utils.R
Lines 275 to 276 in 5e9da04
| sim_adata$obs["array_col"] <- sim_adata$obs["col"] | |
| sim_adata$obs["array_row"] <- sim_adata$obs["row"] |
and those are continuous:
16.918, 18.017, 20.075. 16.918 + 1 = 17.918matches no spot, every join misses, and the neighbour graph comes out empty. The
gamma = 2 spatial smoothing then multiplies nothing.
The spots really are on an integer lattice -- those values are lattice positions
carrying sub-0.02 jitter, so a round() recovers it:
| coordinates handed to BayesSpace | spots with ≥1 neighbour |
|---|---|
col / row as stored |
0 of 278 |
round(col) / round(row) |
278 of 278 |
The API spec is arguably where this starts:
task_spatial_simulators/src/api/file_dataset_sp.yaml
Lines 21 to 28 in 5e9da04
| - type: double | |
| name: col | |
| description: Column index of the cell in the spatial grid. | |
| required: true | |
| - type: double | |
| name: row | |
| description: Row index of the cell in the spatial grid. | |
| required: true |
documents both columns as "index of the cell in the spatial grid", i.e. lattice
indices, while the data carries physical coordinates.
What it costs
20 runs per condition on MOBNEW, the same call the pipeline makes, with only the
coordinates changed:
| coordinates | ARI vs reference | ARI run vs run | spatial coherence |
|---|---|---|---|
| as stored (prior inert) | 0.935 | 0.909 | 0.552 |
| rounded (prior active) | 0.628 | 0.679 | 0.628 |
("spatial coherence" = fraction of a spot's 6 nearest neighbours carrying the same
label; the reference clustering's own value is 0.553.)
So switching the prior on does what it says -- the result gets more spatially
coherent -- but agreement with the stored reference drops sharply.
Why this is not a one-line fix
The reference clustering's own spatial coherence is 0.553, essentially identical to
the un-smoothed result (0.552) and well below the smoothed one (0.628). That is
consistent with the stored spatial_cluster having been produced the same way, with
the same inert prior, upstream in SpatialSimBench.
If that is right, rounding the coordinates in generate_sim_spatialCluster() alone
would make every method score worse, because it would compare a genuinely
spatial clustering against a reference that is not one. It would also make the
run-to-run noise worse before it gets better -- agreement between two independent
runs falls from 0.91 to 0.68 once the Potts prior starts contributing.
Options
- Leave it, and document it.
clustering_aristays a comparison of two
non-spatial clusterings. Cheap, honest, but the metric is not measuring what its
name says. - Round the coordinates on our side only. One line, and wrong on its own for
the reason above. - Round on both sides, regenerating the reference
spatial_clusterwith an
active prior. Correct, but the reference comes from the source data via
process_datasets/convert, so this reaches upstream of this repository and
changes every publishedclustering_*number. - Drop
clustering_ari/clustering_nmiuntil the above is settled, so the
benchmark does not report a spatial metric that is not spatial.
I lean toward the third, with the fourth as the interim if regenerating the
reference turns out to be slow -- but it is a bigger call than a bug fix and worth
agreeing before anyone writes code.
Still to check
Confirmed on MOBNEW only, the sole dataset in resources_test. The other nine need
the same check: if any of them ship integer coordinates then their prior is
active, and their clustering_* scores are not comparable to MOBNEW's. That the
reference was produced with an inert prior is inference from the coherence match
above, not something traced through SpatialSimBench's own code.
Context: found while measuring #30, which is about the same metric but a separate
concern (it removes a duplicate BayesSpace run; it does not touch this).
Activity
rcannood commentedon Jul 29, 2026
Checked all ten datasets now that they are synced locally. Nine of ten have an
inert prior; only
pdacworks today. And it is not one cause but three, so I waswrong to frame this as purely a float-coordinate problem.
Neighbour counts, replicating
.find_neighborsexactly (ST offsets =(0,±1),(±1,0);Visium offsets =
(±2,0),(±1,±1)):round()Three separate causes
1. The platform is hardcoded —
brainandfibrosarcoma.These two are on genuine Visium lattices:
col + rowhas fixed parity, and 2568 of2572 / 2123 of 2125 spots have neighbours under Visium offsets. We call BayesSpace
with
platform = "ST"attask_spatial_simulators/src/helpers/utils.R
Line 281 in 5e9da04
and
task_spatial_simulators/src/helpers/utils.R
Line 290 in 5e9da04
and on a hex lattice the ST offsets can never land on an occupied spot because of
that parity.
uns$dataset_assayalready carries the right value, so this one isgenuinely a small fix -- pass the platform through instead of hardcoding it.
2. Continuous coordinates --
breast,gastrulation,osteosarcoma,cortex,hindlimbmuscle,olfactorybulb.The original finding. Rounding recovers a neighbour graph for all six, though how
much varies a lot (4733 of 4744 for breast, 462 of 884 for hindlimbmuscle).
Two caveats I would not want to gloss over:
cortex(seqFISH+) andgastrulation(seqFISH) are imaging-based, notlattice-based. Their spots are individual cells at arbitrary positions, so
rounding them onto a grid invents a lattice that does not exist. BayesSpace's
neighbour model may simply be the wrong tool for these two.
rounding. So rounding produces a graph, not necessarily the correct one. The
true array geometry needs establishing before trusting it.
3. Coordinates are pixel positions --
prostate.268 spots spread over col 1686–2839, row 1327–2659. Nothing recovers this: raw,
rounded and rank-indexed all give ≤4 spots with neighbours. The array indices are
not in the file, so this one cannot be fixed from the data as shipped.
What this changes
The options in the issue body still stand, but the scope is clearer:
platformhardcoding is separable and worth fixing on its own merits,regardless of what is decided about the reference clustering;
need only the platform, six need coordinate work of varying soundness, and one
cannot be fixed at all;
clustering_ari/clustering_nmiare currently not comparable acrossdatasets:
pdacis scored with an active spatial prior and the other ninewithout.
The last point is the one I would weigh most heavily. It holds today, independently
of anything upstream.
Still untested: whether the stored reference
spatial_clusterwas produced with anactive prior for
pdacspecifically. If it was,pdacis self-consistent and theother nine are consistently wrong -- which would be the tidier of the possibilities.