fix(SME): degrade gracefully when manual SME has no visiumDir - #98
Conversation
Reported by Dima:
> sme_undirected <- SpaceMarkers(sme, directed=FALSE)
Preparing data from SpaceMarkersExperiment...
Computing optimal parameters...
Error in .apply_sme_filters(sme, ...) :
spatial_params(sme) is NULL and no visiumDir is stored ...
A user who builds an SME by hand (no load10X(), so no visiumDir,
no scalefactors JSON to read) hits a hard stop in
.apply_sme_filters when neither spatial_params(sme) nor
@spacemarkers$params$visiumDir is set. The dispatcher had no way
to recover even though the underlying data.frame methods
(find_pattern_hotspots, calculate_influence) already accept
params = NULL and use sensible defaults (sigma=10, threshold=4).
Replace the stop() with a fallback: build a default optParams
matrix from the user's `sigma` argument (or 10 if NULL) and the
threshold argument, populate it for every pattern column, fill
spatial_params(sme), and emit a `message()` telling the user
defaults were used and that they can set spatial_params(sme)
explicitly for tuned values per pattern.
Test: regression case that builds an SME by hand with no
visiumDir, runs .apply_sme_filters, expects a message (not a
stop) and verifies the populated spatial_params slot has the
expected sigma/threshold values for every pattern.
There was a problem hiding this comment.
Pull request overview
This PR updates the SME-based workflow to avoid a hard failure when spatial_params(sme) is missing and the SME was created manually (so sme@spacemarkers$params$visiumDir is not available to read Visium scalefactors). Instead, it populates default spatial parameters and continues.
Changes:
- In
.apply_sme_filters(), when bothspatial_params(sme)andvisiumDirare missing, create a defaultspatial_paramsmatrix usingsigma(or 10) andthreshold, and emit amessage()instead ofstop(). - Keep the existing “compute optimal parameters” path when
visiumDiris available. - Add a regression test ensuring manual SMEs without
visiumDirget default params and only emit a message.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
R/SpaceMarkers.R |
Implements the graceful fallback to default spatial params for manual SMEs with no visiumDir. |
tests/testthat/test-SME-default-params.R |
Adds a regression test validating the new fallback behavior and message emission. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
There was a problem hiding this comment.
this should work with no error, as sigma is to set to 10, but how important that is to be able to find an optimal sigma? also, can't the spot diameter (looks like the only thing needed for the calc here) be read from the SpatialExperiment object?
|
Looks good. |
Changed default sigma to 20
Changed expected default to 20
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Summary
Reported by @dimalvovs:
```
A user who builds an SME by hand — no `load10X()`, so no `visiumDir` and no scalefactors JSON to read — hits a hard `stop()` even though the underlying data.frame pipeline functions (`find_pattern_hotspots`, `calculate_influence`) already handle `params = NULL` by falling back to `sigma = 10`, `threshold = 4`.
Fix
In `.apply_sme_filters`, when both `spatial_params(sme)` and `visiumDir` are missing, build a default `optParams` matrix from the user's `sigma` argument (or 10) and `threshold` argument, populate it for every pattern column, attach it to `spatial_params(sme)`, and emit an informative `message()` rather than a fatal error. Users who want tuned values per pattern can still set `spatial_params(sme)` explicitly before calling.
Test
Regression case in `tests/testthat/test-SME-default-params.R` that:
5 PASS, 0 FAIL.
🤖 Generated with Claude Code