Skip to content

fix(SME): degrade gracefully when manual SME has no visiumDir - #98

Merged
atuldeshpande merged 9 commits into
mainfrom
fix/sme-default-spatial-params
May 29, 2026
Merged

fix(SME): degrade gracefully when manual SME has no visiumDir#98
atuldeshpande merged 9 commits into
mainfrom
fix/sme-default-spatial-params

Conversation

@atuldeshpande

Copy link
Copy Markdown
Member

Summary

Reported by @dimalvovs:

```

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 in
sme@spacemarkers$params$visiumDir.
```

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:

  • Builds an SME by hand with no visiumDir
  • Calls `.apply_sme_filters`
  • Asserts a `message()` (not a `stop()`) and verifies the resulting `spatial_params` slot has `sigmaOpt = 10`, `threshOpt = 4` for every pattern

5 PASS, 0 FAIL.

🤖 Generated with Claude Code

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.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 both spatial_params(sme) and visiumDir are missing, create a default spatial_params matrix using sigma (or 10) and threshold, and emit a message() instead of stop().
  • Keep the existing “compute optimal parameters” path when visiumDir is available.
  • Add a regression test ensuring manual SMEs without visiumDir get 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.

Comment thread tests/testthat/test-SME-default-params.R Outdated

@dimalvovs dimalvovs left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

@atuldeshpande

Copy link
Copy Markdown
Member Author

Looks good.

Changed default sigma to 20
Changed expected default to 20
@dimalvovs
dimalvovs self-requested a review May 29, 2026 14:58
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
@atuldeshpande
atuldeshpande merged commit 903b42b into main May 29, 2026
4 checks passed
@atuldeshpande
atuldeshpande deleted the fix/sme-default-spatial-params branch May 29, 2026 15:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants