Skip to content

BayesSpace's spatial prior is inert: 0 neighbours found, so spatial_cluster is not spatial #32

Open

Description

@rcannood

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

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.918
matches 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:

- 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_ari stays 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_cluster with 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 published clustering_* number.
  • Drop clustering_ari / clustering_nmi until 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

  1. rcannood commented on Jul 29, 2026

    @rcannood
    MemberAuthor

    Checked all ten datasets now that they are synced locally. Nine of ten have an
    inert prior; only pdac works today.
    And it is not one cause but three, so I was
    wrong to frame this as purely a float-coordinate problem.

    Neighbour counts, replicating .find_neighbors exactly (ST offsets = (0,±1),(±1,0);
    Visium offsets = (±2,0),(±1,±1)):

    dataset assay spots ST offsets (as we call it) Visium offsets ST after round()
    pdac ST 428 427 428 427
    brain Visium 2572 0 2568 0
    fibrosarcoma Visium 2125 0 2123 0
    breast Visium 4744 0 0 4733
    gastrulation seqFISH 5772 0 0 4360
    osteosarcoma Visium 645 0 0 298
    cortex seqFISH+ 521 0 0 480
    hindlimbmuscle Visium 884 0 0 462
    olfactorybulb ST 278 0 0 278
    prostate Visium 268 0 0 0

    Three separate causes

    1. The platform is hardcoded — brain and fibrosarcoma.

    These two are on genuine Visium lattices: col + row has fixed parity, and 2568 of
    2572 / 2123 of 2125 spots have neighbours under Visium offsets. We call BayesSpace
    with platform = "ST" at

    sim_sce <- BayesSpace::spatialPreprocess(sim_sce, n.PCs=7, platform="ST", n.HVGs=2000, log.normalize=FALSE)

    and
    ,
    and on a hex lattice the ST offsets can never land on an occupied spot because of
    that parity. uns$dataset_assay already carries the right value, so this one is
    genuinely 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+) and gastrulation (seqFISH) are imaging-based, not
      lattice-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.
    • four of the six are labelled Visium, yet it is the ST offsets that fire after
      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:

    • the platform hardcoding is separable and worth fixing on its own merits,
      regardless of what is decided about the reference clustering;
    • whatever is decided, it will not land uniformly -- one dataset already works, two
      need only the platform, six need coordinate work of varying soundness, and one
      cannot be fixed at all;
    • so clustering_ari / clustering_nmi are currently not comparable across
      datasets
      : pdac is scored with an active spatial prior and the other nine
      without.

    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_cluster was produced with an
    active prior for pdac specifically. If it was, pdac is self-consistent and the
    other nine are consistently wrong -- which would be the tidier of the possibilities.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions