Skip to content

Make LibSVM a private CMake dependency (#8547, #8558) - #9701

Merged
timosachsenberg merged 2 commits into
OpenMS:developfrom
timosachsenberg:feature/libsvm-private-dep-8547
Jul 2, 2026
Merged

timosachsenberg merged 2 commits into
OpenMS:developfrom
timosachsenberg:feature/libsvm-private-dep-8547

Conversation

@timosachsenberg

@timosachsenberg timosachsenberg commented Jul 2, 2026 •

Copy link
Copy Markdown
Contributor

Description

Makes LibSVM a private CMake dependency of libOpenMS, resolving #8547. The blocker was that the public header FeatureFindingMetabo.h leaked the LIBSVM type struct svm_model (a forward-declared svm_model* member), so LibSVM's headers propagated to every downstream consumer of libOpenMS. That blocker was tracked separately as #8558.

Changes

  • SimpleSVM (which already encapsulates LIBSVM via the Pimpl idiom) gains the API needed to fully wrap LIBSVM usage:
    • loadModel() / saveModel() — load/save a model in LIBSVM's native format.
    • a dense-vector double predict(const std::vector<double>&) const overload that returns a raw class label without probability estimates.
    • Impl::clear_() now resets all training-derived state (nodes_, scaling_, predictor_names_, performance_, log2_*) so a model loaded via loadModel() cannot inherit stale state from a prior setup(); getFeatureWeights() now guards against a loaded model that has no predictor names.
  • FeatureFindingMetabo now holds a std::unique_ptr<SimpleSVM> instead of a raw svm_model*. The struct svm_model; forward declaration and #include "svm.h" are removed from the public header/source. The isotope-pattern legality check builds a std::vector<double> of scaled features and calls SimpleSVM::predict; the prediction math (feature scaling, dense 4-feature vector, == 2.0 legality check) is byte-for-byte unchanged.
  • CMake: LibSVM::LibSVM moved from OPENMS_DEP_LIBRARIES (public) to OPENMS_DEP_PRIVATE_LIBRARIES (private); find_dependency(LIBSVM) moved to the commented-out "private linked libraries" block in OpenMSConfig.cmake.in (consistent with the other private deps; libOpenMS is always built shared).
  • XercesC stays PUBLIC and this is now documented in CMake — its types are genuinely part of the public API (XMLHandler derives from xercesc::DefaultHandler and takes xercesc::Attributes), so it cannot be made private without an API break.

Verification

  • libOpenMS rebuilds cleanly with LibSVM as a private dependency; the LibSVM include path no longer propagates to downstream consumers' compile flags.
  • SimpleSVM_test passes, including a new load/save/predict round-trip test and a lifecycle-reset regression test.
  • FeatureFindingMetabo_test and all 13 TOPP_FeatureFinderMetabo tests pass with byte-identical output (they exercise the real MetaboliteIsoModelNoised5.svm model through the new code path).

Closes #8547
Closes #8558

Checklist

  • Make sure that you are listed in the AUTHORS file
  • Add relevant changes and new features to the CHANGELOG file
  • I have commented my code, particularly in hard-to-understand areas
  • New and existing unit tests pass locally with my changes
  • Updated or added python bindings for changed or new classes (Tick if no updates were necessary.) — no pyOpenMS changes needed; the FeatureFindingMetabo binding only exposes run(), which is unaffected.

🤖 Generated with Claude Code

Summary by CodeRabbit

  • New Features
    • Added SVM model persistence with loadModel()/saveModel() for reuse across sessions.
    • Added dense-vector, single-observation prediction (predict(features)) for models loaded from disk.
  • Bug Fixes
    • Improved isotope-pattern filtering by routing through the updated SVM wrapper while keeping prediction behavior consistent.
    • Prevented stale in-memory training/cross-validation state from being reused after loading or replacing a model.
    • Reduced public exposure of LIBSVM details so downstream consumers no longer need to manage LIBSVM headers.
  • Tests
    • Added/extended tests for save/load, error handling, dense-vector prediction, state reset after loading, and clean cross-validation output.

@coderabbitai

coderabbitai Bot commented Jul 2, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

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: facdffd7-4eb3-45dc-b12a-2b998de48204

📥 Commits

Reviewing files that changed from the base of the PR and between 91ae87c and 80454c2.

📒 Files selected for processing (8)
  • CHANGELOG
  • cmake/OpenMSConfig.cmake.in
  • src/openms/CMakeLists.txt
  • src/openms/include/OpenMS/FEATUREFINDER/FeatureFindingMetabo.h
  • src/openms/include/OpenMS/ML/SVM/SimpleSVM.h
  • src/openms/source/FEATUREFINDER/FeatureFindingMetabo.cpp
  • src/openms/source/ML/SVM/SimpleSVM.cpp
  • src/tests/class_tests/openms/source/SimpleSVM_test.cpp
✅ Files skipped from review due to trivial changes (1)
  • CHANGELOG
🚧 Files skipped from review as they are similar to previous changes (7)
  • cmake/OpenMSConfig.cmake.in
  • src/openms/CMakeLists.txt
  • src/openms/include/OpenMS/FEATUREFINDER/FeatureFindingMetabo.h
  • src/openms/include/OpenMS/ML/SVM/SimpleSVM.h
  • src/tests/class_tests/openms/source/SimpleSVM_test.cpp
  • src/openms/source/ML/SVM/SimpleSVM.cpp
  • src/openms/source/FEATUREFINDER/FeatureFindingMetabo.cpp

📝 Walkthrough

Walkthrough

LibSVM is moved to private linkage. SimpleSVM adds model load/save and dense-vector prediction APIs. FeatureFindingMetabo now owns SimpleSVM instead of exposing svm_model, and tests cover model loading, saving, prediction, and state reset.

Changes

LibSVM Privatization and SimpleSVM Encapsulation

Layer / File(s) Summary
CMake dependency visibility changes
CHANGELOG, cmake/OpenMSConfig.cmake.in, src/openms/CMakeLists.txt
Removes public LibSVM dependency discovery, moves LibSVM::LibSVM into private linkage, and records the dependency visibility change in the changelog.
SimpleSVM load/save and dense prediction
src/openms/include/OpenMS/ML/SVM/SimpleSVM.h, src/openms/source/ML/SVM/SimpleSVM.cpp
Adds model load/save APIs and a dense-vector predict overload, resets training-derived state on clear, and guards feature-weight access for loaded models.
FeatureFindingMetabo uses SimpleSVM
src/openms/include/OpenMS/FEATUREFINDER/FeatureFindingMetabo.h, src/openms/source/FEATUREFINDER/FeatureFindingMetabo.cpp
Replaces raw libsvm ownership with std::unique_ptr<SimpleSVM>, updates model loading and prediction flow, and switches isotope-pattern features to a dense vector.
SimpleSVM persistence tests
src/tests/class_tests/openms/source/SimpleSVM_test.cpp
Adds coverage for load/save error handling, dense-vector prediction, round-trip behavior, and state clearing after reload.

Sequence Diagram(s)

sequenceDiagram
  participant FeatureFindingMetabo
  participant SimpleSVM
  participant LibSVM

  FeatureFindingMetabo->>SimpleSVM: loadModel(model_filename)
  SimpleSVM->>LibSVM: svm_load_model(filename)
  LibSVM-->>SimpleSVM: model pointer
  FeatureFindingMetabo->>SimpleSVM: predict(features)
  SimpleSVM->>LibSVM: svm_predict(model, nodes)
  LibSVM-->>SimpleSVM: label
Loading

Possibly related PRs

  • OpenMS/OpenMS#7892: Extends the same SimpleSVM encapsulation path with model persistence and dense prediction support.
  • OpenMS/OpenMS#9675: Touches SimpleSVM prediction and training behavior in the same code area.

Poem

A bunny hid LibSVM from sight,
Then taught SimpleSVM to save just right.
Dense features hop, models roam,
And FeatureFindingMetabo found a new home.
🐇

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly summarizes the main change: making LibSVM a private dependency.
Linked Issues check ✅ Passed The PR meets #8547 and #8558 by privatizing LibSVM, keeping XercesC public, and refactoring FeatureFindingMetabo onto SimpleSVM with model I/O.
Out of Scope Changes check ✅ Passed The changes shown are all directly tied to the dependency-encapsulation and SimpleSVM refactor work, with no unrelated scope evident.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

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

Actionable comments posted: 1

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

605-605: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Use TEST_TRUE/TEST_FALSE instead of TEST_EQUAL(expr, true/false).

Four new assertions use the TEST_EQUAL(expr, true/false) pattern instead of the preferred boolean macros.

♻️ Proposed fix
-    TEST_EQUAL((label_loaded == 0.0) || (label_loaded == 1.0), true)
+    TEST_TRUE((label_loaded == 0.0) || (label_loaded == 1.0))
-  TEST_EQUAL(svm_reset.getScaling().empty(), false)
+  TEST_FALSE(svm_reset.getScaling().empty())
-  TEST_EQUAL(fw.empty(), false)
+  TEST_FALSE(fw.empty())
-  TEST_EQUAL(svm_reset.getScaling().empty(), true)
+  TEST_TRUE(svm_reset.getScaling().empty())

As per path instructions for src/tests/class_tests/**/*_test.cpp: "Prefer TEST_TRUE(expr) and TEST_FALSE(expr) over TEST_EQUAL(expr, true) and TEST_EQUAL(expr, false) for boolean assertions."

Also applies to: 639-639, 642-642, 650-650

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/tests/class_tests/openms/source/SimpleSVM_test.cpp` at line 605, The
boolean assertions in SimpleSVM_test should use the dedicated boolean macros
instead of TEST_EQUAL(expr, true/false). Update the affected assertions in the
test body around the existing label_loaded checks to use TEST_TRUE or TEST_FALSE
as appropriate, keeping the same conditions but matching the preferred style
used by the test framework.

Source: Path instructions

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@src/openms/source/ML/SVM/SimpleSVM.cpp`:
- Around line 542-554: In SimpleSVM::loadModel, the Exception::ParseError call
is passing its arguments in the wrong order. Update the throw site so the
filename is used as the expression/context argument and the “Could not load SVM
model...” text is used as the message/detail argument. Keep the rest of the
failure handling in loadModel unchanged.

---

Nitpick comments:
In `@src/tests/class_tests/openms/source/SimpleSVM_test.cpp`:
- Line 605: The boolean assertions in SimpleSVM_test should use the dedicated
boolean macros instead of TEST_EQUAL(expr, true/false). Update the affected
assertions in the test body around the existing label_loaded checks to use
TEST_TRUE or TEST_FALSE as appropriate, keeping the same conditions but matching
the preferred style used by the test framework.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 9c5fdaa0-8b1f-4336-9cab-18094828d40a

📥 Commits

Reviewing files that changed from the base of the PR and between 309b88a and 02deb93.

📒 Files selected for processing (8)
  • CHANGELOG
  • cmake/OpenMSConfig.cmake.in
  • src/openms/CMakeLists.txt
  • src/openms/include/OpenMS/FEATUREFINDER/FeatureFindingMetabo.h
  • src/openms/include/OpenMS/ML/SVM/SimpleSVM.h
  • src/openms/source/FEATUREFINDER/FeatureFindingMetabo.cpp
  • src/openms/source/ML/SVM/SimpleSVM.cpp
  • src/tests/class_tests/openms/source/SimpleSVM_test.cpp

Comment thread src/openms/source/ML/SVM/SimpleSVM.cpp Resolved
timosachsenberg and others added 2 commits July 2, 2026 17:42
LIBSVM types are no longer exposed in any public OpenMS header, so
libOpenMS's LibSVM dependency moves from PUBLIC to PRIVATE and downstream
consumers no longer need LibSVM's include paths.

FeatureFindingMetabo previously leaked `struct svm_model` through its
public header. Its isotope-pattern filter now goes through the existing
SimpleSVM wrapper, which encapsulates LIBSVM via the Pimpl idiom:

- SimpleSVM gains loadModel()/saveModel() and a dense-vector predict()
  overload returning a raw class label (no probability estimates).
- SimpleSVM::Impl::clear_() now resets all training-derived state
  (nodes_/scaling_/predictor_names_/performance_/log2_*), so a model loaded
  via loadModel() cannot inherit stale state from a prior setup();
  getFeatureWeights() guards against a loaded model with no predictor names.
- FeatureFindingMetabo holds a std::unique_ptr<SimpleSVM>; the prediction
  math (feature scaling, dense 4-feature vector, ==2.0 legality check) is
  byte-for-byte unchanged.

XercesC stays PUBLIC because its types are part of the public API
(XMLHandler derives from xercesc::DefaultHandler and takes
xercesc::Attributes). The exported OpenMSConfig no longer
find_dependency(LIBSVM): it is now private and libOpenMS is always shared.

Tests: extended SimpleSVM_test (load/save/predict round-trip + lifecycle
reset regression); FeatureFindingMetabo and TOPP FeatureFinderMetabo tests
pass with byte-identical output.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
The ParseError constructor renders its message as `<message> in: <expression>`,
so the filename belongs in the expression slot. The two arguments were swapped,
producing garbled output ("<filename> in: Could not load SVM model from
'<filename>'"). Now: filename as expression, description as message, yielding
"Could not load SVM model in: <filename>".

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@timosachsenberg
timosachsenberg force-pushed the feature/libsvm-private-dep-8547 branch from 91ae87c to 80454c2 Compare July 2, 2026 15:56

@jpfeuffer jpfeuffer 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.

good to go if tests pass.

@timosachsenberg
timosachsenberg merged commit 33ebe49 into OpenMS:develop Jul 2, 2026
38 checks passed
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.

Enhance SimpleSVM wrapper to support model loading and saving Check if some cmake dependencies could be made private

2 participants