Skip to content

fix: LinearResampler right_index clamped to wrong container size - #9127

Merged
timosachsenberg merged 2 commits into
developfrom
fix/linear-resampler-index-bug
Apr 10, 2026
Merged

timosachsenberg merged 2 commits into
developfrom
fix/linear-resampler-index-bug

Conversation

@timosachsenberg

@timosachsenberg timosachsenberg commented Apr 10, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • Fixes wrong bounds clamp in LinearResampler::raster() — right_index was clamped to distance(first, last) - 1 (number of raw points) instead of number_resampled_points - 1, causing intensity to be distributed to completely wrong positions when the resampled grid has more points than the raw data
  • Adds regression test with 3 raw points resampled at fine spacing (101 resampled points) to exercise the failure case

Fixes #9119

Details

The bug was introduced in 2008 (commit 8f00f1b, "Adapted to STL-debug mode") when bounds clamping was added to fix an out-of-bounds access under STL debug mode. It remained latent because the TOPP Resampler tool uses LinearResamplerAlign, which has an independent correct implementation.

The one-line fix changes line 94 in LinearResampler.h:

-        help = distance(first, last) - 1;
+        help = number_resampled_points - 1;

Test plan

  • Existing LinearResampler_test tests continue to pass (intensity conservation with similar raw/resampled counts)
  • New regression test passes: 3 raw points resampled to 101 points — verifies intensity conservation and no extreme values
  • CI green

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes

    • Corrected resampling behavior when producing many more points than the original data, fixing bounds and intensity assignment so resampled spectra conserve intensity.
  • Tests

    • Added a regression test covering extreme upsampling, verifying output size, total intensity conservation, and per-point intensity limits.

The bounds clamp for right_index used distance(first, last) (number of
raw data points) instead of number_resampled_points. When resampled
points far outnumber raw points (e.g. fine spacing over a wide m/z
range), right_index pointed thousands of positions backward in the
resampled grid, producing extreme intensity spikes. Bug was introduced
in 2008 (8f00f1b) but masked because the TOPP Resampler tool uses
LinearResamplerAlign which has an independent correct implementation.

Adds a regression test with 3 raw points resampled at 0.1 spacing
(101 resampled points >> 3 raw points).

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
@coderabbitai

coderabbitai Bot commented Apr 10, 2026 •

Copy link
Copy Markdown
Contributor

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 52a18adf-2fcc-450e-bece-7af76266cddf

📥 Commits

Reviewing files that changed from the base of the PR and between 4d4f091 and 5c995a2.

📒 Files selected for processing (1)
  • src/openms/include/OpenMS/PROCESSING/RESAMPLING/LinearResampler.h
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/openms/include/OpenMS/PROCESSING/RESAMPLING/LinearResampler.h

📝 Walkthrough

Walkthrough

LinearResampler::raster now clamps the right_index using the resampled peak count (number_resampled_points - 1) and handles the last-point case by adding raw intensity directly when left_index == right_index. A regression test was added for scenarios with far more resampled than raw points.

Changes

Cohort / File(s) Summary
Core Fix
src/openms/include/OpenMS/PROCESSING/RESAMPLING/LinearResampler.h
Clamp right_index using number_resampled_points - 1 instead of distance(first, last) - 1. Handle boundary case where left_index == right_index by adding raw intensity to the single target resampled peak; otherwise perform two-sided linear interpolation.
Test Coverage
src/tests/class_tests/openms/source/LinearResampler_test.cpp
Added regression test with 3 raw peaks and spacing 0.1 validating resampled size (101), total intensity conservation (sum == 40.0), and per-point intensity not exceeding max raw intensity (<= 20.0 + 1e-5).

Poem

🐇 I hopped through indices, lost in the night,

A clamp moved misplaced, and peaks grew a fright.
Now edges are snug and intensities keep,
Resampled and true — a rabbit's small leap. 🎉

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 50.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and specifically describes the main fix: a bounds-clamping bug in LinearResampler where right_index was clamped to the wrong container size.
Linked Issues check ✅ Passed The PR directly addresses issue #9119 by fixing the LinearResampler right_index clamping bug that caused incorrect intensity distribution and extreme spikes during resampling with fine rates.
Out of Scope Changes check ✅ Passed All changes are directly scoped to fixing the LinearResampler bug and adding a regression test; no unrelated modifications are present.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/linear-resampler-index-bug

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot 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.

🧹 Nitpick comments (1)
src/tests/class_tests/openms/source/LinearResampler_test.cpp (1)

127-139: Consider switching numeric regression checks to FuzzyDiff.

Lines 127-139 use scalar numeric assertions; a small FuzzyDiff-based reference check would better guard distribution-shape regressions in future changes.

As per coding guidelines, "Use FuzzyDiff for numeric comparisons in tests; keep test data small and use whitelists for unstable lines".

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/tests/class_tests/openms/source/LinearResampler_test.cpp` around lines
127 - 139, The test uses scalar assertions TEST_REAL_SIMILAR and TEST_EQUAL on
the spectrum intensities (variables/spec elements like spec, sum and
spec[i].getIntensity()), so replace these with FuzzyDiff-based checks: compute
the vector of intensities (or the summed value) and compare it to a small
reference vector/value via FuzzyDiff (or FuzzyDiff::compare) with an appropriate
epsilon, and mark any inherently unstable lines with the whitelist mechanism;
update the assertions that check total intensity and per-point maxima to use
FuzzyDiff comparisons instead of TEST_REAL_SIMILAR/TEST_EQUAL to better detect
distribution-shape regressions.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@src/tests/class_tests/openms/source/LinearResampler_test.cpp`:
- Around line 127-139: The test uses scalar assertions TEST_REAL_SIMILAR and
TEST_EQUAL on the spectrum intensities (variables/spec elements like spec, sum
and spec[i].getIntensity()), so replace these with FuzzyDiff-based checks:
compute the vector of intensities (or the summed value) and compare it to a
small reference vector/value via FuzzyDiff (or FuzzyDiff::compare) with an
appropriate epsilon, and mark any inherently unstable lines with the whitelist
mechanism; update the assertions that check total intensity and per-point maxima
to use FuzzyDiff comparisons instead of TEST_REAL_SIMILAR/TEST_EQUAL to better
detect distribution-shape regressions.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 820fcdb1-485a-478a-83f4-52e281eff48d

📥 Commits

Reviewing files that changed from the base of the PR and between 9c29275 and 4d4f091.

📒 Files selected for processing (2)
  • src/openms/include/OpenMS/PROCESSING/RESAMPLING/LinearResampler.h
  • src/tests/class_tests/openms/source/LinearResampler_test.cpp

…s intensity

When a raw data point falls exactly on the last resampled grid point,
left_index == right_index and both interpolation distances become 0,
silently dropping the point's entire intensity.  Add a guard that
assigns the full intensity directly to the single bin in this case.

Fixes the LinearResampler_test regression introduced in 4d4f091.
@timosachsenberg
timosachsenberg merged commit 4bf1984 into develop Apr 10, 2026
36 checks passed
@timosachsenberg
timosachsenberg deleted the fix/linear-resampler-index-bug branch April 10, 2026 16:42
github-actions Bot pushed a commit that referenced this pull request Apr 11, 2026
Add missing changelog entries for commits merged after the last sync:
- #9127: Fix LinearResampler right_index bounds clamping (intensity spike bug)
- #9126: CometAdapter Bruker .d DDA-PASEF input support
- #9121: PeptideDataBaseSearchFI runtime search diagnostics and tolerance estimation
- #9122: PeptideDataBaseSearchFI automatic mass accuracy calibration (two-pass search)
- #9123: PeptideDataBaseSearchFI protein-level FDR (picked-protein)
- #9129: OpenSwathWorkflow Bruker DIA MS2 frame aggregation, denoising, and streaming

Co-authored-by: Copilot <[email protected]>
timosachsenberg pushed a commit that referenced this pull request Apr 11, 2026
Add missing changelog entries for commits merged after the last sync:
- #9127: Fix LinearResampler right_index bounds clamping (intensity spike bug)
- #9126: CometAdapter Bruker .d DDA-PASEF input support
- #9121: PeptideDataBaseSearchFI runtime search diagnostics and tolerance estimation
- #9122: PeptideDataBaseSearchFI automatic mass accuracy calibration (two-pass search)
- #9123: PeptideDataBaseSearchFI protein-level FDR (picked-protein)
- #9129: OpenSwathWorkflow Bruker DIA MS2 frame aggregation, denoising, and streaming

Co-authored-by: GitHub Copilot <[email protected]>
Co-authored-by: Copilot <[email protected]>
@github-actions github-actions Bot mentioned this pull request Apr 12, 2026
@coderabbitai coderabbitai Bot mentioned this pull request Apr 15, 2026
4 of 5 tasks
@timosachsenberg timosachsenberg mentioned this pull request Jun 8, 2026
timosachsenberg added a commit that referenced this pull request Jun 16, 2026
…dge cases (#9460) (#9526)

Adds regression guards for the folded LinearResampler spike fix (#9127). The
area-preserving raster() distributes each raw point's intensity onto its two
grid neighbours and divides by the grid spacing (never by the raw-point gap),
so a lone spike or coincident (zero-width interval) raw points must conserve
total intensity, never be amplified into a spurious larger peak, and never
produce NaN/Inf. The existing sections never exercised these degenerate inputs.

Three [EXTRA] sections on raster():
- single data point (start_pos == end_pos): one output point carrying all intensity
- isolated off-grid spike: area preserved, split across the two neighbours,
  not amplified, and not leaked to the far ends of the spectrum
- coincident raw m/z (zero-width interval): total intensity conserved, all finite

Verified: builds clean and the test runs PASSED.

Co-authored-by: Claude Opus 4.8 <[email protected]>
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.

Python Bindings for LinearResampler

1 participant