Repository navigation
Add ModifiedSincSmoother for profile data smoothing - #8217
Conversation
📝 WalkthroughWalkthroughIntroduces a new sinc-based smoothing component ModifiedSincSmoother with MS1/MS2 variants, boundary handling via weighted linear regression, vector and in-place container filtering, batch filtering with progress logging, helper parameter conversions, Python bindings, build integration, and comprehensive unit tests. Changes
Sequence Diagram(s)sequenceDiagram
autonumber
actor User
participant App as Client Code
participant MSS as ModifiedSincSmoother
participant Kern as Kernel Builder
participant LR as LinearRegression (boundary)
participant C as Containers (MSSpectrum / MSChromatogram / Mobilogram)
User->>App: Construct MSS(isMS1, degree, m)
App->>MSS: new ModifiedSincSmoother(...)
MSS->>Kern: makeKernel(isMS1, degree, m)
Kern-->>MSS: kernel, fit_weights
User->>App: filter(container)
App->>MSS: filter(C)
MSS->>LR: extendData(..., fit_weights)
LR-->>MSS: boundary-extended data
MSS->>MSS: smoothExceptBoundaries(data, kernel)
MSS-->>App: in-place intensities updated (non-negative)
sequenceDiagram
autonumber
actor User
participant App as Client Code
participant MSS as ModifiedSincSmoother
participant PM as PeakMap
participant PL as ProgressLogger
User->>App: filterExperiment(PeakMap)
App->>MSS: filterExperiment(PM)
MSS->>PL: startProgress(total entities)
loop For each spectrum/chromatogram/mobilogram
MSS->>MSS: extend boundaries + smooth
MSS-->>PM: write intensities (positions unchanged)
MSS->>PL: setProgress(i)
end
MSS->>PL: endProgress()
MSS-->>App: done
Estimated code review effort🎯 4 (Complex) | ⏱️ ~60–90 minutes Poem
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
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. Comment |
There was a problem hiding this comment.
Actionable comments posted: 11
🧹 Nitpick comments (12)
src/openms/include/OpenMS/PROCESSING/SMOOTHING/ModifiedSincSmoother.h (2)
11-16: Drop unused include to reduce compile timeDefaultParamHandler isn’t used in this header.
-#include <OpenMS/DATASTRUCTURES/DefaultParamHandler.h>
124-141: Input validation gaps in static helpers
- bandwidthToM: may return m below the constructor’s minimum; clamp or document min(m).
- noiseGainToM: missing guard for noiseGain <= 0 (division by zero).
Example for noiseGainToM:
int noiseGainToM(bool isMS1, int degree, double noiseGain);And in .cpp:
+ if (noiseGain <= 0.0) { + throw std::invalid_argument("noiseGain must be > 0"); + }Optionally clamp both helpers to the same mMin rule as the ctor.
src/openms/include/OpenMS/PROCESSING/SMOOTHING/sources.cmake (1)
11-12: Minor: trim trailing whitespace and keep list sortedRemove the trailing space after the filename; consider keeping this list alphabetically sorted for easier diffing.
-ModifiedSincSmoother.h +ModifiedSincSmoother.hsrc/openms/source/PROCESSING/SMOOTHING/sources.cmake (1)
11-12: Minor: consider alphabetical orderingOptional, but keeping sources_list alphabetized reduces merge conflicts.
src/pyOpenMS/pxds/ModifiedSincSmoother.pxd (1)
1-15: Expose missing overloads to keep Python API parityC++ provides filter(MSChromatogram&) and filter(Mobilogram&); exposing them in PyOpenMS improves usability. Add imports and methods with wrap-docs. Also consider adding the static helpers if they’re intended for end users.
from ProgressLogger cimport * from MSExperiment cimport * from MSSpectrum cimport * +from MSChromatogram cimport * +from Mobilogram cimport * cdef extern from "<OpenMS/PROCESSING/SMOOTHING/ModifiedSincSmoother.h>" namespace "OpenMS": cdef cppclass ModifiedSincSmoother(ProgressLogger): # wrap-inherits: # ProgressLogger ModifiedSincSmoother(bool isMS1, int degree, int m) except + nogil void filter(MSSpectrum & spectrum) except + nogil # wrap-doc:Smoothes an MSSpectrum containing profile data + void filter(MSChromatogram & chromatogram) except + nogil # wrap-doc:Smoothes an MSChromatogram containing profile data + void filter(Mobilogram & mobilogram) except + nogil # wrap-doc:Smoothes a Mobilogram containing profile data void filterExperiment(MSExperiment & exp) except + nogil # wrap-doc:Smoothes an MSExperiment containing profile datasrc/tests/class_tests/openms/source/ModifiedSincSmoother_test.cpp (5)
22-23: Fix START_TEST name (drop template marker).Use START_TEST(ModifiedSincSmoother, "$Id$") to match ClassTest conventions.
-START_TEST(ModifiedSincSmoother<D>, "$Id$") +START_TEST(ModifiedSincSmoother, "$Id$")
40-59: Avoid brittle golden numbers for smooth(); assert properties instead.Hard-coding exact outputs for a numeric smoother is fragile. Prefer property-based checks (size preserved, variance reduced, bounded max deviation).
- for (size_t i = 0; i < data.size(); ++i) - { - TEST_REAL_SIMILAR(output[i], expected[i]) - } + TEST_EQUAL(output.size(), data.size()) + // variance should not increase for low-pass smoothing + auto variance = [](const std::vector<double>& v){ + double m = std::accumulate(v.begin(), v.end(), 0.0) / v.size(); + double s = 0.0; for (double x : v) s += (x - m)*(x - m); + return s / v.size(); + }; + TEST(variance(output) <= variance(data))
61-76: Sum-conservation assertion is too strict.Boundary extension + clamping can break exact sum equality. Use a tolerance or relax to “approximately equal”.
- TEST_REAL_SIMILAR(sum_input, sum_output) + TEST(abs(sum_input - sum_output) < 1e-6 * std::max(1.0, abs(sum_input)))
107-121: bandwidthToM: remove magic expected values; test monotonicity and type of exception.Exact M depends on empirical constants; assert relationships instead. Also expect InvalidParameter from OpenMS once source switches to OpenMS exceptions.
- // Test bandwidth conversion for MS2 - TEST_EQUAL(ModifiedSincSmoother::bandwidthToM(false, 4, 0.1), 30) - TEST_EQUAL(ModifiedSincSmoother::bandwidthToM(false, 6, 0.1), 32) - - // Test bandwidth conversion for MS1 - TEST_EQUAL(ModifiedSincSmoother::bandwidthToM(true, 4, 0.1), 10) - TEST_EQUAL(ModifiedSincSmoother::bandwidthToM(true, 6, 0.1), 12) + // Monotonic with degree for fixed bandwidth + const int ms2_d4 = ModifiedSincSmoother::bandwidthToM(false, 4, 0.1); + const int ms2_d6 = ModifiedSincSmoother::bandwidthToM(false, 6, 0.1); + TEST(ms2_d6 > ms2_d4) + const int ms1_d4 = ModifiedSincSmoother::bandwidthToM(true, 4, 0.1); + const int ms1_d6 = ModifiedSincSmoother::bandwidthToM(true, 6, 0.1); + TEST(ms1_d6 > ms1_d4) + // MSn should require larger M than MS1 for same (degree, bandwidth) + TEST(ms2_d4 > ms1_d4) // Test invalid bandwidth - should throw - TEST_EXCEPTION(Exception::InvalidParameter, ModifiedSincSmoother::bandwidthToM(false, 4, 0.0)) - TEST_EXCEPTION(Exception::InvalidParameter, ModifiedSincSmoother::bandwidthToM(false, 4, 0.5)) + TEST_EXCEPTION(Exception::InvalidParameter, ModifiedSincSmoother::bandwidthToM(false, 4, 0.0)) + TEST_EXCEPTION(Exception::InvalidParameter, ModifiedSincSmoother::bandwidthToM(false, 4, 0.5))
123-133: Add invalid-input coverage for noiseGainToM once validated in source.Guarding noiseGain<=0 avoids division by zero; add a negative/zero case test after source change.
int m_ms2 = ModifiedSincSmoother::noiseGainToM(false, 4, 0.8); int m_ms1 = ModifiedSincSmoother::noiseGainToM(true, 4, 0.8); @@ TEST(m_ms2 != m_ms1) // Should be different for MS1 vs MS2 + // invalid input + TEST_EXCEPTION(Exception::InvalidParameter, ModifiedSincSmoother::noiseGainToM(false, 4, 0.0))src/openms/source/PROCESSING/SMOOTHING/ModifiedSincSmoother.cpp (2)
34-41: Pass degree_ explicitly to extendData to avoid relying on defaults.Be explicit and future-proof.
- std::vector<double> extended = extendData(data, radius); + std::vector<double> extended = extendData(data, radius, degree_);
24-32: Minor: prefer member flags consistently.Use isMS1_ for consistency (already correct elsewhere); current usage mixes param and member but is harmless.
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (7)
src/openms/include/OpenMS/PROCESSING/SMOOTHING/ModifiedSincSmoother.h(1 hunks)src/openms/include/OpenMS/PROCESSING/SMOOTHING/sources.cmake(1 hunks)src/openms/source/PROCESSING/SMOOTHING/ModifiedSincSmoother.cpp(1 hunks)src/openms/source/PROCESSING/SMOOTHING/sources.cmake(1 hunks)src/pyOpenMS/pxds/ModifiedSincSmoother.pxd(1 hunks)src/tests/class_tests/openms/executables.cmake(1 hunks)src/tests/class_tests/openms/source/ModifiedSincSmoother_test.cpp(1 hunks)
🧰 Additional context used
📓 Path-based instructions (3)
src/tests/class_tests/openms/**/*.cpp
📄 CodeRabbit inference engine (.github/copilot-instructions.md)
Write unit tests for new functionality
Files:
src/tests/class_tests/openms/source/ModifiedSincSmoother_test.cpp
src/openms/**/*.{cpp,h,hpp,cc,cxx}
📄 CodeRabbit inference engine (.github/copilot-instructions.md)
src/openms/**/*.{cpp,h,hpp,cc,cxx}: Follow the existing C++ coding conventions in the codebase
Use the established naming patterns for classes, methods, and variables
Use OpenMS data structures (e.g., MSExperiment, FeatureMap, PeptideIdentification)
Follow the established error handling patterns
Utilize OpenMS logging mechanisms
Be mindful of memory usage when processing large datasets
Consider algorithmic complexity for data processing operations
Use appropriate OpenMS containers and algorithms
Files:
src/openms/source/PROCESSING/SMOOTHING/ModifiedSincSmoother.cppsrc/openms/include/OpenMS/PROCESSING/SMOOTHING/ModifiedSincSmoother.h
src/openms/include/OpenMS/**/*.h
📄 CodeRabbit inference engine (.github/copilot-instructions.md)
Add Doxygen comments for new public methods and classes
Files:
src/openms/include/OpenMS/PROCESSING/SMOOTHING/ModifiedSincSmoother.h
🧠 Learnings (4)
📚 Learning: 2025-08-05T12:43:11.681Z
Learnt from: CR
PR: OpenMS/OpenMS#0
File: .github/copilot-instructions.md:0-0
Timestamp: 2025-08-05T12:43:11.681Z
Learning: Applies to src/pyOpenMS/**/*.h : Add Doxygen comments for new public methods and classes in pyOpenMS bindings
Applied to files:
src/pyOpenMS/pxds/ModifiedSincSmoother.pxd
📚 Learning: 2025-08-05T12:43:11.681Z
Learnt from: CR
PR: OpenMS/OpenMS#0
File: .github/copilot-instructions.md:0-0
Timestamp: 2025-08-05T12:43:11.681Z
Learning: Applies to src/tests/class_tests/openms/**/*.cpp : Write unit tests for new functionality
Applied to files:
src/tests/class_tests/openms/source/ModifiedSincSmoother_test.cppsrc/tests/class_tests/openms/executables.cmake
📚 Learning: 2025-08-05T12:43:11.681Z
Learnt from: CR
PR: OpenMS/OpenMS#0
File: .github/copilot-instructions.md:0-0
Timestamp: 2025-08-05T12:43:11.681Z
Learning: Applies to src/tests/class_tests/openms/*Test.cpp : Follow naming convention: `ClassNameTest.cpp` for `ClassName`
Applied to files:
src/tests/class_tests/openms/source/ModifiedSincSmoother_test.cppsrc/tests/class_tests/openms/executables.cmake
📚 Learning: 2025-09-03T12:58:20.032Z
Learnt from: timosachsenberg
PR: OpenMS/OpenMS#8177
File: src/tests/topp/CMakeLists.txt:1857-1865
Timestamp: 2025-09-03T12:58:20.032Z
Learning: OpenMS tests: When adding new TOPP tests in src/tests/topp/CMakeLists.txt that compare outputs via FuzzyDiff, always add set_tests_properties("<tool>_<n>_out<m>" PROPERTIES DEPENDS "<tool>_<n>") to avoid flakiness under parallel CTest runs.
Applied to files:
src/tests/class_tests/openms/source/ModifiedSincSmoother_test.cppsrc/tests/class_tests/openms/executables.cmake
🧬 Code graph analysis (3)
src/tests/class_tests/openms/source/ModifiedSincSmoother_test.cpp (3)
src/openms/source/PROCESSING/SMOOTHING/ModifiedSincSmoother.cpp (17)
ModifiedSincSmoother(16-32)smooth(34-41)smooth(34-34)bandwidthToM(63-73)bandwidthToM(63-63)noiseGainToM(75-83)noiseGainToM(75-75)savitzkyGolayBandwidth(85-88)savitzkyGolayBandwidth(85-85)filter(196-223)filter(196-196)filter(225-247)filter(225-225)filter(249-271)filter(249-249)filterExperiment(273-288)filterExperiment(273-273)src/openms/include/OpenMS/PROCESSING/SMOOTHING/ModifiedSincSmoother.h (1)
OpenMS(20-348)src/openms/source/CONCEPT/Exception.cpp (1)
InvalidParameter(230-233)
src/openms/source/PROCESSING/SMOOTHING/ModifiedSincSmoother.cpp (1)
src/openms/include/OpenMS/PROCESSING/SMOOTHING/ModifiedSincSmoother.h (1)
sqr(346-346)
src/openms/include/OpenMS/PROCESSING/SMOOTHING/ModifiedSincSmoother.h (1)
src/openms/source/PROCESSING/SMOOTHING/ModifiedSincSmoother.cpp (27)
ModifiedSincSmoother(16-32)smooth(34-41)smooth(34-34)smoothExceptBoundaries(43-61)smoothExceptBoundaries(43-43)bandwidthToM(63-73)bandwidthToM(63-63)noiseGainToM(75-83)noiseGainToM(75-75)savitzkyGolayBandwidth(85-88)savitzkyGolayBandwidth(85-85)filter(196-223)filter(196-196)filter(225-247)filter(225-225)filter(249-271)filter(249-249)filterExperiment(273-288)filterExperiment(273-273)makeKernel(90-132)makeKernel(90-90)getCoefficients(134-138)getCoefficients(134-134)makeFitWeights(140-157)makeFitWeights(140-140)extendData(159-194)extendData(159-159)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (2)
- GitHub Check: build-and-test (macos-13, xcode, 14.2)
- GitHub Check: build-and-test (windows-2022, cl.exe, 14.36)
🔇 Additional comments (6)
src/tests/class_tests/openms/executables.cmake (1)
325-325: Added test registration looks correctName matches the new class test and placement within filtering_executables_list is fine.
src/tests/class_tests/openms/source/ModifiedSincSmoother_test.cpp (1)
290-309: Constructor exception type must align with OpenMS patterns.Tests expect Exception::InvalidParameter; ensure the implementation throws OpenMS exceptions (not std::invalid_argument). See source comments.
src/openms/source/PROCESSING/SMOOTHING/ModifiedSincSmoother.cpp (4)
196-223: Filter methods look solid.Positions preserved, sizes conserved, intensities clamped non-negative. Good.
Also applies to: 225-247, 249-271
34-61: Convolution core is correct and safe on indices.Symmetric application with guarded bounds; OK.
90-132: Kernel normalization and construction are reasonable.Unity DC normalization with symmetric accounting; OK.
159-179: Initialize LinearRegression before first use.linreg is default-constructed; ensure sums are zeroed before accumulation for left extension (symmetry with right side).
- LinearRegression linreg; + LinearRegression linreg; + linreg.clear(); const int fitLength = std::min(static_cast<int>(fit_weights_.size()), static_cast<int>(N)); // left extension using forward pointsLikely an incorrect or invalid review comment.
| void filter(MSSpectrum& spectrum); | ||
|
|
There was a problem hiding this comment.
Implementation uses setPosition on Peak1D — replace with setMZ or write intensity only
Peak1D/ChromatogramPeak/MobilityPeak1D don’t provide setPosition; current .cpp will not compile. Easiest: keep copy-swap but only set intensities (positions are already preserved by the copy).
- out_it->setPosition(in_it->getPosition());
- out_it->setIntensity(std::max(0.0, smoothed[i]));
+ out_it->setIntensity(std::max(0.0, smoothed[i]));Apply the same change to the MSChromatogram and Mobilogram overloads.
Committable suggestion skipped: line range outside the PR's diff.
🤖 Prompt for AI Agents
In src/openms/include/OpenMS/PROCESSING/SMOOTHING/ModifiedSincSmoother.h around
lines 171-172, the current implementation in the .cpp uses Peak1D::setPosition
(which does not exist) — replace that approach by preserving peak positions and
only assigning intensities (or use setMZ for mz-based peaks) when doing the
copy-swap; update the corresponding MSChromatogram and Mobilogram overloads
similarly so they do not call setPosition but either call setMZ where
appropriate or simply copy the intensity value into the existing peak/point to
keep positions unchanged.
| #include <OpenMS/PROCESSING/SMOOTHING/ModifiedSincSmoother.h> | ||
|
|
||
| #include <OpenMS/KERNEL/MSSpectrum.h> | ||
| #include <OpenMS/KERNEL/MSChromatogram.h> | ||
| #include <OpenMS/KERNEL/Mobilogram.h> | ||
| #include <OpenMS/KERNEL/MSExperiment.h> | ||
|
|
||
| #include <algorithm> | ||
| #include <cmath> | ||
| #include <numeric> | ||
| #include <utility> | ||
|
|
There was a problem hiding this comment.
🛠️ Refactor suggestion
Include OpenMS Exception header (switch to OpenMS exceptions).
Use OpenMS::Exception types per project conventions.
#include <OpenMS/PROCESSING/SMOOTHING/ModifiedSincSmoother.h>
+#include <OpenMS/CONCEPT/Exception.h>
#include <OpenMS/KERNEL/MSSpectrum.h>📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| #include <OpenMS/PROCESSING/SMOOTHING/ModifiedSincSmoother.h> | |
| #include <OpenMS/KERNEL/MSSpectrum.h> | |
| #include <OpenMS/KERNEL/MSChromatogram.h> | |
| #include <OpenMS/KERNEL/Mobilogram.h> | |
| #include <OpenMS/KERNEL/MSExperiment.h> | |
| #include <algorithm> | |
| #include <cmath> | |
| #include <numeric> | |
| #include <utility> | |
| #include <OpenMS/PROCESSING/SMOOTHING/ModifiedSincSmoother.h> | |
| #include <OpenMS/CONCEPT/Exception.h> | |
| #include <OpenMS/KERNEL/MSSpectrum.h> | |
| #include <OpenMS/KERNEL/MSChromatogram.h> | |
| #include <OpenMS/KERNEL/Mobilogram.h> | |
| #include <OpenMS/KERNEL/MSExperiment.h> | |
| #include <algorithm> | |
| #include <cmath> | |
| #include <numeric> | |
| #include <utility> |
🤖 Prompt for AI Agents
In src/openms/source/PROCESSING/SMOOTHING/ModifiedSincSmoother.cpp lines 1-12,
the file currently lacks the OpenMS exception header and should use
OpenMS::Exception types per project conventions; add the include for
<OpenMS/CONCEPT/Exception.h> at the top and update any thrown or caught
exceptions in this translation unit to use OpenMS::Exception (or the appropriate
derived OpenMS::Exception::IllegalArgument/InvalidValue/etc.) instead of
std::runtime_error or other non-OpenMS types, adjusting message construction
accordingly so logs and error handling follow project conventions.
| if (degree < 2 || degree > 10 || (degree % 2) != 0) | ||
| { | ||
| throw std::invalid_argument("Invalid degree; must be even and between 2 and 10."); | ||
| } | ||
|
|
||
| const int mMin = isMS1 ? degree / 2 + 1 : degree / 2 + 2; | ||
| if (m < mMin) | ||
| { | ||
| throw std::invalid_argument("Invalid kernel half-width m; too small for the given degree."); | ||
| } | ||
|
|
There was a problem hiding this comment.
🛠️ Refactor suggestion
Use Exception::InvalidParameter instead of std::invalid_argument.
Aligns with OpenMS error-handling patterns and the unit tests.
- if (degree < 2 || degree > 10 || (degree % 2) != 0)
- {
- throw std::invalid_argument("Invalid degree; must be even and between 2 and 10.");
- }
+ if (degree < 2 || degree > 10 || (degree % 2) != 0)
+ {
+ throw Exception::InvalidParameter(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION,
+ "Invalid degree; must be even and between 2 and 10.");
+ }
@@
- if (m < mMin)
- {
- throw std::invalid_argument("Invalid kernel half-width m; too small for the given degree.");
- }
+ if (m < mMin)
+ {
+ throw Exception::InvalidParameter(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION,
+ "Invalid kernel half-width m; too small for the given degree.");
+ }📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| if (degree < 2 || degree > 10 || (degree % 2) != 0) | |
| { | |
| throw std::invalid_argument("Invalid degree; must be even and between 2 and 10."); | |
| } | |
| const int mMin = isMS1 ? degree / 2 + 1 : degree / 2 + 2; | |
| if (m < mMin) | |
| { | |
| throw std::invalid_argument("Invalid kernel half-width m; too small for the given degree."); | |
| } | |
| if (degree < 2 || degree > 10 || (degree % 2) != 0) | |
| { | |
| throw Exception::InvalidParameter(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION, | |
| "Invalid degree; must be even and between 2 and 10."); | |
| } | |
| const int mMin = isMS1 ? degree / 2 + 1 : degree / 2 + 2; | |
| if (m < mMin) | |
| { | |
| throw Exception::InvalidParameter(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION, | |
| "Invalid kernel half-width m; too small for the given degree."); | |
| } |
🤖 Prompt for AI Agents
In src/openms/source/PROCESSING/SMOOTHING/ModifiedSincSmoother.cpp around lines
19-29, replace the throws of std::invalid_argument with OpenMS-style
Exception::InvalidParameter (e.g. throw Exception::InvalidParameter(...)) and
ensure the message names the offending parameter ("degree" or "m"); also add or
confirm inclusion of the Exception header (OpenMS/CONCEPT/Exception.h) at the
top if not already present so the code compiles and matches OpenMS
error-handling and unit tests.
| void ModifiedSincSmoother::LinearRegression::calculate() | ||
| { | ||
| double denom = sum_x2 - (sum_x * sum_x) / sum_weights; | ||
| slope = (sum_xy - sum_x * sum_y / sum_weights) / denom; | ||
| if (std::isnan(slope)) slope = 0; | ||
| offset = (sum_y - slope * sum_x) / sum_weights; | ||
| calculated = true; | ||
| } |
There was a problem hiding this comment.
Guard against zero denominator and non-finite slope in LinearRegression.
Prevents NaN/Inf propagation on short/degenerate windows.
- double denom = sum_x2 - (sum_x * sum_x) / sum_weights;
- slope = (sum_xy - sum_x * sum_y / sum_weights) / denom;
- if (std::isnan(slope)) slope = 0;
- offset = (sum_y - slope * sum_x) / sum_weights;
+ if (sum_weights <= 0) { slope = 0; offset = 0; calculated = true; return; }
+ const double denom = sum_x2 - (sum_x * sum_x) / sum_weights;
+ if (std::abs(denom) < 1e-12) {
+ slope = 0;
+ } else {
+ slope = (sum_xy - sum_x * sum_y / sum_weights) / denom;
+ if (!std::isfinite(slope)) slope = 0;
+ }
+ offset = (sum_y - slope * sum_x) / sum_weights;📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| void ModifiedSincSmoother::LinearRegression::calculate() | |
| { | |
| double denom = sum_x2 - (sum_x * sum_x) / sum_weights; | |
| slope = (sum_xy - sum_x * sum_y / sum_weights) / denom; | |
| if (std::isnan(slope)) slope = 0; | |
| offset = (sum_y - slope * sum_x) / sum_weights; | |
| calculated = true; | |
| } | |
| void ModifiedSincSmoother::LinearRegression::calculate() | |
| { | |
| if (sum_weights <= 0) { slope = 0; offset = 0; calculated = true; return; } | |
| const double denom = sum_x2 - (sum_x * sum_x) / sum_weights; | |
| if (std::abs(denom) < 1e-12) { | |
| slope = 0; | |
| } else { | |
| slope = (sum_xy - sum_x * sum_y / sum_weights) / denom; | |
| if (!std::isfinite(slope)) slope = 0; | |
| } | |
| offset = (sum_y - slope * sum_x) / sum_weights; | |
| calculated = true; | |
| } |
🤖 Prompt for AI Agents
In src/openms/source/PROCESSING/SMOOTHING/ModifiedSincSmoother.cpp around lines
307 to 314, the LinearRegression::calculate() routine can produce NaN/Inf when
denom or sum_weights are zero (or near-zero) on short/degenerate windows; update
the method to (1) check sum_weights and denom against a small epsilon (e.g.
1e-12) before dividing, (2) if sum_weights is too small set slope and offset to
0 and mark calculated, (3) if denom is too small set slope to 0 (avoid
division), (4) after computing slope check std::isfinite(slope) and if not set
slope to 0, and finally compute offset only when sum_weights is safe (otherwise
set to 0) and set calculated = true.
| START_SECTION((short input, expect safe behavior)) | ||
| { | ||
| int degree = 4; | ||
| int m = 3; | ||
| std::vector<double> data = {1.0, 2.0, 3.0}; | ||
|
|
||
| ModifiedSincSmoother smoother(false, degree, m); | ||
| std::vector<double> output = smoother.smooth(data); | ||
|
|
||
| TEST_EQUAL(output.size(), data.size()) | ||
| } | ||
| END_SECTION |
There was a problem hiding this comment.
Invalid m for MS2: test currently expects success where constructor should throw.
For MS2, mMin = degree/2 + 2 → degree=4 ⇒ mMin=4. Set m=4 to avoid false failures.
- int m = 3;
+ int m = 4;📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| START_SECTION((short input, expect safe behavior)) | |
| { | |
| int degree = 4; | |
| int m = 3; | |
| std::vector<double> data = {1.0, 2.0, 3.0}; | |
| ModifiedSincSmoother smoother(false, degree, m); | |
| std::vector<double> output = smoother.smooth(data); | |
| TEST_EQUAL(output.size(), data.size()) | |
| } | |
| END_SECTION | |
| START_SECTION((short input, expect safe behavior)) | |
| { | |
| int degree = 4; | |
| int m = 4; | |
| std::vector<double> data = {1.0, 2.0, 3.0}; | |
| ModifiedSincSmoother smoother(false, degree, m); | |
| std::vector<double> output = smoother.smooth(data); | |
| TEST_EQUAL(output.size(), data.size()) | |
| } | |
| END_SECTION |
🤖 Prompt for AI Agents
In src/tests/class_tests/openms/source/ModifiedSincSmoother_test.cpp around
lines 78 to 89, the test uses m = 3 which is below the minimum for MS2 when
degree = 4 (mMin = degree/2 + 2 = 4), so the constructor should throw; update
the test to use m = 4 (or any >=4) so the constructor succeeds and the test
validates size, ensuring the test no longer expects success with an invalid m.
|
I made your branch to compile and added the missing features/doc. But some tests fail @blackangel2512
|
There was a problem hiding this comment.
Actionable comments posted: 6
♻️ Duplicate comments (1)
src/openms/include/OpenMS/PROCESSING/SMOOTHING/ModifiedSincSmoother.h (1)
6-7: Authors header added correctly.Matches the prior suggestion; nothing further.
🧹 Nitpick comments (14)
src/openms/include/OpenMS/PROCESSING/SMOOTHING/sources.cmake (1)
11-11: Trim trailing whitespace; optional: keep header list ordering stable.Remove the trailing space after the filename. Consider keeping the list alphabetically sorted like the surrounding entries.
-ModifiedSincSmoother.h +ModifiedSincSmoother.hsrc/openms/source/PROCESSING/SMOOTHING/sources.cmake (1)
11-11: LGTM: source added to build.Optional: keep entries alphabetically sorted for diff hygiene.
src/pyOpenMS/pxds/ModifiedSincSmoother.pxd (2)
1-3: Avoid wildcard cimports to reduce symbol bleed and compile time.-from ProgressLogger cimport * -from MSExperiment cimport * -from MSSpectrum cimport * +from ProgressLogger cimport ProgressLogger +from MSExperiment cimport MSExperiment +from MSSpectrum cimport MSSpectrum
11-14: Add constructor doc; consider exposing remaining overloads for parity.
- Add a wrap-doc for the ctor.
- Optional: expose filter(MSChromatogram&) and filter(Mobilogram&) to match C++ API.
- ModifiedSincSmoother(bool isMS1, int degree, int m) except + nogil + ModifiedSincSmoother(bool isMS1, int degree, int m) except + nogil # wrap-doc:Create a ModifiedSincSmoother. isMS1 toggles MS1/MS2 tuning; degree must be even in [2,10]; m is kernel half-width. void filter(MSSpectrum & spectrum) except + nogil # wrap-doc:Smoothes an MSSpectrum containing profile data void filterExperiment(MSExperiment & exp) except + nogil # wrap-doc:Smoothes an MSExperiment containing profile data + # Optional parity with C++ API: + # void filter(MSChromatogram & chromatogram) except + nogil # wrap-doc:Smoothes an MSChromatogram containing profile data + # void filter(Mobilogram & mobilogram) except + nogil # wrap-doc:Smoothes a Mobilogram containing profile datasrc/tests/class_tests/openms/source/ModifiedSincSmoother_test.cpp (2)
22-22: Use canonical test name.Drop the template marker to match existing convention: START_TEST(ModifiedSincSmoother, ...).
-START_TEST(ModifiedSincSmoother<D>, "$Id$") +START_TEST(ModifiedSincSmoother, "$Id$")
290-310: Avoid leaks and double-allocation in “valid parameters” checks.
TEST_NOT_EQUAL(new ..., ms_nullPointer)leaks the created object;delete new ...;deletes a different instance.- TEST_NOT_EQUAL(new ModifiedSincSmoother(false, 4, 4), ms_nullPointer) - delete new ModifiedSincSmoother(false, 4, 4); - TEST_NOT_EQUAL(new ModifiedSincSmoother(true, 6, 4), ms_nullPointer) - delete new ModifiedSincSmoother(true, 6, 4); + { + ModifiedSincSmoother ok1(false, 4, 4); + ModifiedSincSmoother ok2(true, 6, 4); + // construction succeeded if we reached here + TEST_EQUAL(true, true) + }src/openms/source/PROCESSING/SMOOTHING/ModifiedSincSmoother.cpp (1)
98-111: Avoid non-portable M_PI; use OpenMS constants.MSVC doesn’t guarantee M_PI without defines. Use Constants::PI.
+#include <OpenMS/CONCEPT/Constants.h> @@ - const double sinc_arg = M_PI * 0.5 * (isMS1 ? degree + 2 : degree + 4) * x; + const double sinc_arg = Constants::PI * 0.5 * (isMS1 ? degree + 2 : degree + 4) * x; @@ - k += coeffs[j] * x * std::sin((j + 1) * M_PI * x); + k += coeffs[j] * x * std::sin((j + 1) * Constants::PI * x); @@ - k += coeffs[j] * x * std::sin((2 * static_cast<int>(j) + nu) * M_PI * x); + k += coeffs[j] * x * std::sin((2 * static_cast<int>(j) + nu) * Constants::PI * x); @@ - weights[static_cast<size_t>(p)] = sqr(std::cos(0.5 * M_PI / (first_zero * beta) * p)); + weights[static_cast<size_t>(p)] = sqr(std::cos(0.5 * Constants::PI / (first_zero * beta) * p));If
OpenMS/CONCEPT/Constants.hdiffers, replace with the project’s canonical constants header.Also applies to: 154-155
src/openms/include/OpenMS/PROCESSING/SMOOTHING/ModifiedSincSmoother.h (7)
274-277: Fix Doxygen: remove non-existent @PARAM degree in extendData.The method signature has no degree parameter; Doxygen will warn.
Apply:
- @param degree polynomial degree (affects fit length)
142-156: Clarify/guard savitzkyGolayBandwidth domain.For small m/degree the denominator can approach 0 or negative; add precondition and throws.
Apply doc change:
@return equivalent Savitzky-Golay bandwidth + @throws std::invalid_argument if m <= 0 or the internal denominator <= 0Suggested guard in .cpp:
const double denom = 6.352 * (m + 0.5) / (degree + 1.379) - (0.513 + 0.316 * degree) / (m + 0.5); if (m <= 0 || denom <= 0.0) throw std::invalid_argument("Invalid (degree,m) for SG bandwidth."); return 1.0 / denom;
94-95: Const‑correct API for non-logging operations.These methods don’t mutate state; marking them const improves usability and thread-safety.
Apply:
- std::vector<double> smooth(const std::vector<double>& data); + std::vector<double> smooth(const std::vector<double>& data) const; - std::vector<double> smoothExceptBoundaries(const std::vector<double>& data); + std::vector<double> smoothExceptBoundaries(const std::vector<double>& data) const; - void filter(MSSpectrum& spectrum); + void filter(MSSpectrum& spectrum) const; - void filter(MSChromatogram& chromatogram); + void filter(MSChromatogram& chromatogram) const; - void filter(Mobilogram& mobilogram); + void filter(Mobilogram& mobilogram) const;Then add const to definitions in the .cpp accordingly.
Also applies to: 109-110, 171-171, 184-184, 197-197
11-11: Drop unused include to lighten the header.DefaultParamHandler isn’t used here.
Apply:
-#include <OpenMS/DATASTRUCTURES/DefaultParamHandler.h>
214-224: Minor: member comments are great; consider defaulted dtor explicitly.Not required, but adding a defaulted virtual destructor can be clearer when inheriting from ProgressLogger.
Example:
public: virtual ~ModifiedSincSmoother() override = default;
226-238: Replace M_PI with OpenMS::Constants::PI (or a local constexpr) for portabilityModifiedSincSmoother.cpp uses M_PI at lines 98, 106, 111, 154 — M_PI requires _USE_MATH_DEFINES on MSVC; replace with OpenMS::Constants::PI (or add constexpr double PI = OpenMS::Constants::PI) to ensure portability and match repo usage.
75-79: Use OpenMS Exception types instead of std::invalid_argumentRepo-wide convention is to throw OpenMS::Exception variants; ModifiedSincSmoother currently uses std::invalid_argument — align implementation and docs.
- Replace throws in src/openms/source/PROCESSING/SMOOTHING/ModifiedSincSmoother.cpp:
- lines ~21 and ~27 -> throw Exception::IllegalArgument(...);
- line ~67 -> throw Exception::InvalidValue(...).
- Update @throws in src/openms/include/OpenMS/PROCESSING/SMOOTHING/ModifiedSincSmoother.h to document the OpenMS::Exception types.
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (7)
src/openms/include/OpenMS/PROCESSING/SMOOTHING/ModifiedSincSmoother.h(1 hunks)src/openms/include/OpenMS/PROCESSING/SMOOTHING/sources.cmake(1 hunks)src/openms/source/PROCESSING/SMOOTHING/ModifiedSincSmoother.cpp(1 hunks)src/openms/source/PROCESSING/SMOOTHING/sources.cmake(1 hunks)src/pyOpenMS/pxds/ModifiedSincSmoother.pxd(1 hunks)src/tests/class_tests/openms/executables.cmake(1 hunks)src/tests/class_tests/openms/source/ModifiedSincSmoother_test.cpp(1 hunks)
🧰 Additional context used
📓 Path-based instructions (3)
src/openms/**/*.{cpp,h,hpp,cc,cxx}
📄 CodeRabbit inference engine (.github/copilot-instructions.md)
src/openms/**/*.{cpp,h,hpp,cc,cxx}: Follow the existing C++ coding conventions in the codebase
Use the established naming patterns for classes, methods, and variables
Use OpenMS data structures (e.g., MSExperiment, FeatureMap, PeptideIdentification)
Follow the established error handling patterns
Utilize OpenMS logging mechanisms
Be mindful of memory usage when processing large datasets
Consider algorithmic complexity for data processing operations
Use appropriate OpenMS containers and algorithms
Files:
src/openms/include/OpenMS/PROCESSING/SMOOTHING/ModifiedSincSmoother.hsrc/openms/source/PROCESSING/SMOOTHING/ModifiedSincSmoother.cpp
src/openms/include/OpenMS/**/*.h
📄 CodeRabbit inference engine (.github/copilot-instructions.md)
Add Doxygen comments for new public methods and classes
Files:
src/openms/include/OpenMS/PROCESSING/SMOOTHING/ModifiedSincSmoother.h
src/tests/class_tests/openms/**/*.cpp
📄 CodeRabbit inference engine (.github/copilot-instructions.md)
Write unit tests for new functionality
Files:
src/tests/class_tests/openms/source/ModifiedSincSmoother_test.cpp
🧠 Learnings (4)
📚 Learning: 2025-09-03T12:58:20.032Z
Learnt from: timosachsenberg
PR: OpenMS/OpenMS#8177
File: src/tests/topp/CMakeLists.txt:1857-1865
Timestamp: 2025-09-03T12:58:20.032Z
Learning: OpenMS tests: When adding new TOPP tests in src/tests/topp/CMakeLists.txt that compare outputs via FuzzyDiff, always add set_tests_properties("<tool>_<n>_out<m>" PROPERTIES DEPENDS "<tool>_<n>") to avoid flakiness under parallel CTest runs.
Applied to files:
src/tests/class_tests/openms/executables.cmake
📚 Learning: 2025-08-05T12:43:11.681Z
Learnt from: CR
PR: OpenMS/OpenMS#0
File: .github/copilot-instructions.md:0-0
Timestamp: 2025-08-05T12:43:11.681Z
Learning: Applies to src/tests/class_tests/openms/**/*.cpp : Write unit tests for new functionality
Applied to files:
src/tests/class_tests/openms/executables.cmakesrc/tests/class_tests/openms/source/ModifiedSincSmoother_test.cpp
📚 Learning: 2025-08-05T12:43:11.681Z
Learnt from: CR
PR: OpenMS/OpenMS#0
File: .github/copilot-instructions.md:0-0
Timestamp: 2025-08-05T12:43:11.681Z
Learning: Applies to src/tests/class_tests/openms/*Test.cpp : Follow naming convention: `ClassNameTest.cpp` for `ClassName`
Applied to files:
src/tests/class_tests/openms/executables.cmakesrc/tests/class_tests/openms/source/ModifiedSincSmoother_test.cpp
📚 Learning: 2025-08-05T12:43:11.681Z
Learnt from: CR
PR: OpenMS/OpenMS#0
File: .github/copilot-instructions.md:0-0
Timestamp: 2025-08-05T12:43:11.681Z
Learning: Applies to src/pyOpenMS/**/*.h : Add Doxygen comments for new public methods and classes in pyOpenMS bindings
Applied to files:
src/pyOpenMS/pxds/ModifiedSincSmoother.pxd
🧬 Code graph analysis (3)
src/openms/include/OpenMS/PROCESSING/SMOOTHING/ModifiedSincSmoother.h (1)
src/openms/source/PROCESSING/SMOOTHING/ModifiedSincSmoother.cpp (27)
ModifiedSincSmoother(16-32)smooth(34-41)smooth(34-34)smoothExceptBoundaries(43-61)smoothExceptBoundaries(43-43)bandwidthToM(63-73)bandwidthToM(63-63)noiseGainToM(75-83)noiseGainToM(75-75)savitzkyGolayBandwidth(85-88)savitzkyGolayBandwidth(85-85)filter(187-214)filter(187-187)filter(216-238)filter(216-216)filter(240-262)filter(240-240)filterExperiment(264-279)filterExperiment(264-264)makeKernel(90-132)makeKernel(90-90)getCoefficients(134-138)getCoefficients(134-134)makeFitWeights(140-157)makeFitWeights(140-140)extendData(159-185)extendData(159-159)
src/tests/class_tests/openms/source/ModifiedSincSmoother_test.cpp (3)
src/openms/source/PROCESSING/SMOOTHING/ModifiedSincSmoother.cpp (17)
ModifiedSincSmoother(16-32)smooth(34-41)smooth(34-34)bandwidthToM(63-73)bandwidthToM(63-63)noiseGainToM(75-83)noiseGainToM(75-75)savitzkyGolayBandwidth(85-88)savitzkyGolayBandwidth(85-85)filter(187-214)filter(187-187)filter(216-238)filter(216-216)filter(240-262)filter(240-240)filterExperiment(264-279)filterExperiment(264-264)src/openms/include/OpenMS/PROCESSING/SMOOTHING/ModifiedSincSmoother.h (1)
OpenMS(20-348)src/openms/source/CONCEPT/Exception.cpp (1)
InvalidParameter(230-233)
src/openms/source/PROCESSING/SMOOTHING/ModifiedSincSmoother.cpp (1)
src/openms/include/OpenMS/PROCESSING/SMOOTHING/ModifiedSincSmoother.h (1)
sqr(346-346)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (6)
- GitHub Check: build-and-test (ubuntu-22.04, g++, 11)
- GitHub Check: build-and-test (ubuntu-22.04, clang++, 15)
- GitHub Check: build-and-test (macos-13, xcode, 14.2)
- GitHub Check: build-and-test (windows-2022, cl.exe, 14.36)
- GitHub Check: build-and-test (macos-14, xcode, 15.0.1)
- GitHub Check: cppcheck-test
🔇 Additional comments (3)
src/tests/class_tests/openms/executables.cmake (1)
325-325: LGTM: test target added.Entry integrates the new unit test into the filtering suite.
src/tests/class_tests/openms/source/ModifiedSincSmoother_test.cpp (1)
107-116: Replace brittle exact-value assertions in bandwidthToM test with property-based checksHard-coded expected values (30/32/10/12) are brittle and may not match the implementation; assert general properties (monotonicity, degree effect) instead.
File: src/tests/class_tests/openms/source/ModifiedSincSmoother_test.cpp Lines: 107-116
- // Test bandwidth conversion for MS2 - TEST_EQUAL(ModifiedSincSmoother::bandwidthToM(false, 4, 0.1), 30) - TEST_EQUAL(ModifiedSincSmoother::bandwidthToM(false, 6, 0.1), 32) - - // Test bandwidth conversion for MS1 - TEST_EQUAL(ModifiedSincSmoother::bandwidthToM(true, 4, 0.1), 10) - TEST_EQUAL(ModifiedSincSmoother::bandwidthToM(true, 6, 0.1), 12) + // Monotonic in bandwidth (larger bandwidth -> smaller m) + TEST_EQUAL(ModifiedSincSmoother::bandwidthToM(false, 6, 0.10) > ModifiedSincSmoother::bandwidthToM(false, 6, 0.20), true) + TEST_EQUAL(ModifiedSincSmoother::bandwidthToM(true, 6, 0.10) > ModifiedSincSmoother::bandwidthToM(true, 6, 0.20), true) + // Degree effect (larger degree -> larger m for fixed bandwidth) + TEST_EQUAL(ModifiedSincSmoother::bandwidthToM(false, 6, 0.10) > ModifiedSincSmoother::bandwidthToM(false, 4, 0.10), true) + TEST_EQUAL(ModifiedSincSmoother::bandwidthToM(true, 6, 0.10) > ModifiedSincSmoother::bandwidthToM(true, 4, 0.10), true)If hard-coded constants are intended, update the implementation formulas accordingly.
src/pyOpenMS/pxds/ModifiedSincSmoother.pxd (1)
1-15: Binding wired: ModifiedSincSmoother included in pyOpenMS buildsrc/pyOpenMS/pxds/ModifiedSincSmoother.pxd is present; src/pyOpenMS/create_cpp_extension.py globs the pxds directory (pxd_files = glob.glob(PYOPENMS_SRC_DIR + "/pxds/.pxd")) and src/pyOpenMS/CMakeLists.txt drives the compile_pxds / generated _pyopenms.pyx flow — no additional .pyx wrapper required.
| /** | ||
| @brief Convert desired noise gain to optimal parameter m. | ||
|
|
||
| Computes the kernel half-width m that achieves the specified noise gain | ||
| factor for the given filter configuration. | ||
|
|
||
| @param isMS1 true for MS1 mode, false for MS2 mode | ||
| @param degree polynomial degree used in the filter | ||
| @param noiseGain desired noise amplification factor (typically < 1) | ||
| @return corresponding kernel half-width parameter m | ||
|
|
||
| @note noiseGain = 1 means noise unchanged, < 1 means noise reduction | ||
| @note Formula based on empirical characterization of filter response | ||
| */ | ||
| static int noiseGainToM(bool isMS1, int degree, double noiseGain); | ||
|
|
There was a problem hiding this comment.
Validate noiseGainToM input and document throws.
Division by zero for noiseGain <= 0 leads to undefined behavior.
Apply doc change:
@param noiseGain desired noise amplification factor (typically < 1)
@return corresponding kernel half-width parameter m
-
- @note noiseGain = 1 means noise unchanged, < 1 means noise reduction
- @note Formula based on empirical characterization of filter response
+ @throws std::invalid_argument if noiseGain <= 0 or not finite
+ @note noiseGain = 1 means noise unchanged; < 1 means noise reduction
+ @note Formula based on empirical characterization of filter responseAnd in the .cpp, add upfront:
if (!std::isfinite(noiseGain) || noiseGain <= 0.0)
throw std::invalid_argument("Invalid noiseGain; must be > 0 and finite.");🤖 Prompt for AI Agents
In src/openms/include/OpenMS/PROCESSING/SMOOTHING/ModifiedSincSmoother.h around
lines 126 to 141, the noiseGainToM declaration lacks validation and
documentation of exceptions for non-positive or non-finite noiseGain; update the
docblock to state that noiseGain must be finite and > 0 and that the function
throws std::invalid_argument on invalid input, and in the corresponding .cpp
implementation add an upfront check that tests std::isfinite(noiseGain) and
noiseGain > 0 and throws std::invalid_argument with a clear message when the
check fails.
| if (degree < 2 || degree > 10 || (degree % 2) != 0) | ||
| { | ||
| throw std::invalid_argument("Invalid degree; must be even and between 2 and 10."); | ||
| } | ||
|
|
||
| const int mMin = isMS1 ? degree / 2 + 1 : degree / 2 + 2; | ||
| if (m < mMin) | ||
| { | ||
| throw std::invalid_argument("Invalid kernel half-width m; too small for the given degree."); | ||
| } |
There was a problem hiding this comment.
Throw OpenMS exceptions to match project patterns and tests.
Replace std::invalid_argument with Exception::InvalidParameter for constructor validation.
+#include <OpenMS/CONCEPT/Exception.h>
@@
- if (degree < 2 || degree > 10 || (degree % 2) != 0)
- {
- throw std::invalid_argument("Invalid degree; must be even and between 2 and 10.");
- }
+ if (degree < 2 || degree > 10 || (degree % 2) != 0)
+ {
+ throw Exception::InvalidParameter(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION,
+ "Invalid degree; must be even and between 2 and 10.");
+ }
@@
- if (m < mMin)
- {
- throw std::invalid_argument("Invalid kernel half-width m; too small for the given degree.");
- }
+ if (m < mMin)
+ {
+ throw Exception::InvalidParameter(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION,
+ "Invalid kernel half-width m; too small for the given degree.");
+ }📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| if (degree < 2 || degree > 10 || (degree % 2) != 0) | |
| { | |
| throw std::invalid_argument("Invalid degree; must be even and between 2 and 10."); | |
| } | |
| const int mMin = isMS1 ? degree / 2 + 1 : degree / 2 + 2; | |
| if (m < mMin) | |
| { | |
| throw std::invalid_argument("Invalid kernel half-width m; too small for the given degree."); | |
| } | |
| if (degree < 2 || degree > 10 || (degree % 2) != 0) | |
| { | |
| throw Exception::InvalidParameter(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION, | |
| "Invalid degree; must be even and between 2 and 10."); | |
| } | |
| const int mMin = isMS1 ? degree / 2 + 1 : degree / 2 + 2; | |
| if (m < mMin) | |
| { | |
| throw Exception::InvalidParameter(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION, | |
| "Invalid kernel half-width m; too small for the given degree."); | |
| } |
🤖 Prompt for AI Agents
In src/openms/source/PROCESSING/SMOOTHING/ModifiedSincSmoother.cpp around lines
19 to 28, replace the two throws of std::invalid_argument with OpenMS-style
exceptions by throwing Exception::InvalidParameter with the same explanatory
messages (i.e., use Exception::InvalidParameter("Invalid degree; must be even
and between 2 and 10.") and Exception::InvalidParameter("Invalid kernel
half-width m; too small for the given degree.")); ensure the correct OpenMS
Exception header is included and the exception class is referenced in the proper
namespace if needed.
| int ModifiedSincSmoother::bandwidthToM(bool isMS1, int degree, double bandwidth) | ||
| { | ||
| if (bandwidth <= 0.0 || bandwidth >= 0.5) | ||
| { | ||
| throw std::invalid_argument("Invalid bandwidth value. Expected in (0, 0.5)."); | ||
| } | ||
| const double radius = isMS1 | ||
| ? (0.27037 + 0.24920 * degree) / bandwidth - 1.0 | ||
| : (0.74548 + 0.24943 * degree) / bandwidth - 1.0; | ||
| return static_cast<int>(std::round(radius)); | ||
| } |
There was a problem hiding this comment.
Use OpenMS exception type; keep parameter domain consistent.
Also update the throw in bandwidthToM to InvalidParameter.
- if (bandwidth <= 0.0 || bandwidth >= 0.5)
- {
- throw std::invalid_argument("Invalid bandwidth value. Expected in (0, 0.5).");
- }
+ if (bandwidth <= 0.0 || bandwidth >= 0.5)
+ {
+ throw Exception::InvalidParameter(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION,
+ "Invalid bandwidth value. Expected in (0, 0.5).");
+ }📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| int ModifiedSincSmoother::bandwidthToM(bool isMS1, int degree, double bandwidth) | |
| { | |
| if (bandwidth <= 0.0 || bandwidth >= 0.5) | |
| { | |
| throw std::invalid_argument("Invalid bandwidth value. Expected in (0, 0.5)."); | |
| } | |
| const double radius = isMS1 | |
| ? (0.27037 + 0.24920 * degree) / bandwidth - 1.0 | |
| : (0.74548 + 0.24943 * degree) / bandwidth - 1.0; | |
| return static_cast<int>(std::round(radius)); | |
| } | |
| int ModifiedSincSmoother::bandwidthToM(bool isMS1, int degree, double bandwidth) | |
| { | |
| if (bandwidth <= 0.0 || bandwidth >= 0.5) | |
| { | |
| throw Exception::InvalidParameter(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION, | |
| "Invalid bandwidth value. Expected in (0, 0.5)."); | |
| } | |
| const double radius = isMS1 | |
| ? (0.27037 + 0.24920 * degree) / bandwidth - 1.0 | |
| : (0.74548 + 0.24943 * degree) / bandwidth - 1.0; | |
| return static_cast<int>(std::round(radius)); | |
| } |
🤖 Prompt for AI Agents
In src/openms/source/PROCESSING/SMOOTHING/ModifiedSincSmoother.cpp around lines
63 to 73, the function currently throws std::invalid_argument for out-of-range
bandwidth; replace this with the OpenMS exception type
Exception::InvalidParameter and keep the same parameter-domain check (bandwidth
<= 0.0 || bandwidth >= 0.5). Change the throw to something like
Exception::InvalidParameter(__FILE__, __LINE__, "Invalid bandwidth value.
Expected in (0, 0.5)."); and add #include <OpenMS/CONCEPT/Exception.h> at the
top of the file if it isn’t already included.
| int ModifiedSincSmoother::noiseGainToM(bool isMS1, int degree, double noiseGain) | ||
| { | ||
| const double invNG2 = 1.0 / (noiseGain * noiseGain); | ||
| const double e = -2.5 - 0.8 * degree; | ||
| const double m = isMS1 | ||
| ? (-1.0 + invNG2 * (0.543 + 0.4974 * degree) + 0.47 * std::pow(invNG2, e)) | ||
| : (-1.0 + invNG2 * (1.494 + 0.4965 * degree) + 0.52 * std::pow(invNG2, e)); | ||
| return static_cast<int>(std::round(m)); | ||
| } |
There was a problem hiding this comment.
Guard division by zero in noiseGainToM and align exception type.
noiseGain <= 0 causes division by zero; negative values are nonsensical.
- int ModifiedSincSmoother::noiseGainToM(bool isMS1, int degree, double noiseGain)
+ int ModifiedSincSmoother::noiseGainToM(bool isMS1, int degree, double noiseGain)
{
+ if (noiseGain <= 0.0)
+ {
+ throw Exception::InvalidParameter(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION,
+ "Invalid noiseGain value. Expected > 0.");
+ }
const double invNG2 = 1.0 / (noiseGain * noiseGain);📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| int ModifiedSincSmoother::noiseGainToM(bool isMS1, int degree, double noiseGain) | |
| { | |
| const double invNG2 = 1.0 / (noiseGain * noiseGain); | |
| const double e = -2.5 - 0.8 * degree; | |
| const double m = isMS1 | |
| ? (-1.0 + invNG2 * (0.543 + 0.4974 * degree) + 0.47 * std::pow(invNG2, e)) | |
| : (-1.0 + invNG2 * (1.494 + 0.4965 * degree) + 0.52 * std::pow(invNG2, e)); | |
| return static_cast<int>(std::round(m)); | |
| } | |
| int ModifiedSincSmoother::noiseGainToM(bool isMS1, int degree, double noiseGain) | |
| { | |
| if (noiseGain <= 0.0) | |
| { | |
| throw Exception::InvalidParameter(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION, | |
| "Invalid noiseGain value. Expected > 0."); | |
| } | |
| const double invNG2 = 1.0 / (noiseGain * noiseGain); | |
| const double e = -2.5 - 0.8 * degree; | |
| const double m = isMS1 | |
| ? (-1.0 + invNG2 * (0.543 + 0.4974 * degree) + 0.47 * std::pow(invNG2, e)) | |
| : (-1.0 + invNG2 * (1.494 + 0.4965 * degree) + 0.52 * std::pow(invNG2, e)); | |
| return static_cast<int>(std::round(m)); | |
| } |
🤖 Prompt for AI Agents
In src/openms/source/PROCESSING/SMOOTHING/ModifiedSincSmoother.cpp around lines
75 to 83, guard against division by zero and invalid negative noiseGain by
validating that noiseGain > 0 at the start of noiseGainToM; if not, throw a
std::invalid_argument with a clear message (e.g. "noiseGain must be > 0"), then
proceed with the existing computations using invNG2 = 1.0/(noiseGain*noiseGain)
and return the rounded int as before so callers get a defined exception type for
invalid inputs.
| void ModifiedSincSmoother::LinearRegression::calculate() | ||
| { | ||
| double denom = sum_x2 - (sum_x * sum_x) / sum_weights; | ||
| slope = (sum_xy - sum_x * sum_y / sum_weights) / denom; | ||
| if (std::isnan(slope)) slope = 0; | ||
| offset = (sum_y - slope * sum_x) / sum_weights; | ||
| calculated = true; | ||
| } |
There was a problem hiding this comment.
Harden LinearRegression against degenerate cases (zero/near-zero weights).
Current code may yield inf/nan and propagate. Handle small denominators and empty weights.
+#include <limits>
@@
- double denom = sum_x2 - (sum_x * sum_x) / sum_weights;
- slope = (sum_xy - sum_x * sum_y / sum_weights) / denom;
- if (std::isnan(slope)) slope = 0;
- offset = (sum_y - slope * sum_x) / sum_weights;
+ if (sum_weights <= std::numeric_limits<double>::epsilon())
+ {
+ slope = 0.0;
+ offset = 0.0;
+ calculated = true;
+ return;
+ }
+ const double denom = sum_x2 - (sum_x * sum_x) / sum_weights;
+ if (std::fabs(denom) <= std::numeric_limits<double>::epsilon())
+ {
+ slope = 0.0;
+ offset = sum_y / sum_weights;
+ calculated = true;
+ return;
+ }
+ slope = (sum_xy - sum_x * sum_y / sum_weights) / denom;
+ offset = (sum_y - slope * sum_x) / sum_weights;📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| void ModifiedSincSmoother::LinearRegression::calculate() | |
| { | |
| double denom = sum_x2 - (sum_x * sum_x) / sum_weights; | |
| slope = (sum_xy - sum_x * sum_y / sum_weights) / denom; | |
| if (std::isnan(slope)) slope = 0; | |
| offset = (sum_y - slope * sum_x) / sum_weights; | |
| calculated = true; | |
| } | |
| #include <limits> | |
| void ModifiedSincSmoother::LinearRegression::calculate() | |
| { | |
| if (sum_weights <= std::numeric_limits<double>::epsilon()) | |
| { | |
| slope = 0.0; | |
| offset = 0.0; | |
| calculated = true; | |
| return; | |
| } | |
| const double denom = sum_x2 - (sum_x * sum_x) / sum_weights; | |
| if (std::fabs(denom) <= std::numeric_limits<double>::epsilon()) | |
| { | |
| slope = 0.0; | |
| offset = sum_y / sum_weights; | |
| calculated = true; | |
| return; | |
| } | |
| slope = (sum_xy - sum_x * sum_y / sum_weights) / denom; | |
| offset = (sum_y - slope * sum_x) / sum_weights; | |
| calculated = true; | |
| } |
🤖 Prompt for AI Agents
In src/openms/source/PROCESSING/SMOOTHING/ModifiedSincSmoother.cpp around lines
298 to 305, the LinearRegression::calculate() does not guard against degenerate
cases (zero or near-zero sum_weights and tiny denominator) which can produce
inf/nan; add checks: if sum_weights is <= a small epsilon (e.g. 1e-12) treat as
no data and set slope=0, offset=0 (or another safe default) and mark
calculated=true; otherwise compute denom and if fabs(denom) <= epsilon treat
slope=0 and compute offset as sum_y / sum_weights (or offset=0 if that is
unreliable), else compute slope normally; ensure you use the same epsilon for
both checks and still set calculated=true.
| START_SECTION((short input, expect safe behavior)) | ||
| { | ||
| int degree = 4; | ||
| int m = 3; | ||
| std::vector<double> data = {1.0, 2.0, 3.0}; | ||
|
|
||
| ModifiedSincSmoother smoother(false, degree, m); | ||
| std::vector<double> output = smoother.smooth(data); | ||
|
|
There was a problem hiding this comment.
Invalid ctor params in test (m too small for MS2).
For degree=4 and MS2 (isMS1=false), m must be >= degree/2 + 2 = 4. m=3 will throw.
- int degree = 4;
- int m = 3;
+ int degree = 4;
+ int m = 4;📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| START_SECTION((short input, expect safe behavior)) | |
| { | |
| int degree = 4; | |
| int m = 3; | |
| std::vector<double> data = {1.0, 2.0, 3.0}; | |
| ModifiedSincSmoother smoother(false, degree, m); | |
| std::vector<double> output = smoother.smooth(data); | |
| START_SECTION((short input, expect safe behavior)) | |
| { | |
| int degree = 4; | |
| int m = 4; | |
| std::vector<double> data = {1.0, 2.0, 3.0}; | |
| ModifiedSincSmoother smoother(false, degree, m); | |
| std::vector<double> output = smoother.smooth(data); |
🤖 Prompt for AI Agents
In src/tests/class_tests/openms/source/ModifiedSincSmoother_test.cpp around
lines 78 to 86, the test constructs ModifiedSincSmoother with degree=4 and
isMS1=false but uses m=3 which is too small for MS2 (requires m >= degree/2 + 2
= 4) and will throw; update the test to use m >= 4 (e.g., set m=4) or
alternatively set isMS1=true if you intend to keep m=3 so the constructor does
not throw.
Add CORRECTION_DATA tables and getCoefficients() computation using kappa = a + b / (c - m)^3 formula from Schmid et al. supplementary material. Fixes passband ripple for degree >= 6 (MS) and >= 4 (MS1). Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
Match OpenMS exception conventions. Tests already expected this type. Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
Prevents division by zero when noiseGain is zero or negative. Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
Task 5: Use MS1 mode in short-input test (MS2 requires m >= 4). Task 10: Fix new/delete mismatch in constructor tests, remove unused sum_y2 from LinearRegression, fix extendData doxygen. Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
Follow GaussFilter/SavitzkyGolayFilter pattern: register is_ms1, degree, m as parameters with defaults and constraints. Add default constructor and updateMembers_() for INI file / TOPP tool support. Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
Expose all public methods including smooth(), all filter() overloads, and static helper methods (bandwidthToM, noiseGainToM, savitzkyGolayBandwidth). Add DefaultParamHandler integration for parameter access from Python. Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
Fix wrong bandwidthToM expected values (30/32/10/12 → 16/21/12/17). Add exact noiseGainToM and savitzkyGolayBandwidth values from Java ref. Add MS1 exact reference vector. Add numerical container filter checks. Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
73abdc3 to
dd51fcc
Compare
- Update QPX output filenames to quantms.* scheme (#8974) - Add GenericWrapper removal (BREAKING) (#8981) - Add ModifiedSincSmoother new algorithm (#8217) - Add experimental BrukerTimsFile/BRUKER_TDF format support (#8975) Co-authored-by: Copilot <[email protected]>
- Update QPX output filenames to quantms.* scheme (#8974) - Add GenericWrapper removal (BREAKING) (#8981) - Add ModifiedSincSmoother new algorithm (#8217) - Add experimental BrukerTimsFile/BRUKER_TDF format support (#8975) Co-authored-by: Copilot <[email protected]> Co-authored-by: Copilot <[email protected]>
Description
Adds a new
ModifiedSincSmootherclass implementing the modified sinc (MS/MS1) filter from:The filter provides superior stopband suppression (
-70 dB) compared to traditional Savitzky-Golay filters (-13 dB), with two variants:isMS1=false): wider kernel (α=4), best stopband suppressionisMS1=true): narrower kernel (α=2), faster computationFeatures
DefaultParamHandlerintegration for INI file / TOPP tool supportbandwidthToM(),noiseGainToM(),savitzkyGolayBandwidth()Implementation verified against
Checklist