Skip to content

[pyOpenMS][TEST] Prove numpy peak-data getters survive owner GC (#9460) - #9544

Merged
timosachsenberg merged 1 commit into
OpenMS:developfrom
timosachsenberg:test/9460-numpy-getter-lifetime
Jun 17, 2026
Merged

timosachsenberg merged 1 commit into
OpenMS:developfrom
timosachsenberg:test/9460-numpy-getter-lifetime

Conversation

@timosachsenberg

@timosachsenberg timosachsenberg commented Jun 15, 2026 •

Copy link
Copy Markdown
Contributor

What

Adds lifetime tests (numpy importorskip) for the numpy peak-data getters:

  • MSSpectrum.get_peaks() → (mz, intensity)
  • MSExperiment.get2DPeakData() → (rt, mz, intensity)
  • MSExperiment.get2DPeakDataLong() → (rt, mz, intensity)

Each test snapshots the returned arrays while the source is still alive, then dels it + gc.collect(), then asserts the arrays are byte-identical. Snapshot-before-delete keeps the assertion non-vacuous.

Why

Closes the "numpy getters" part of the §2 (pyOpenMS nanobind rewrite) [AUTO] zero-copy task in the OpenMS 3.6.0 pre-release testing plan (#9460):

For each export (get_peaks_struct, numpy getters, spectra_to_arrow, …): snapshot contents → del the source object → gc.collect() → re-read every value. Pass: bytes match the snapshot.

These getters return numpy arrays whose buffer is owned by an nb::capsule (delete[]), independent of the C++ source — so they're safe today, but the issue flags the "risk is a future view-returning edit." This pins the contract: if a getter is ever changed to return a view into the source buffer, the test fails after the source is GC'd.

test_spectrum_zerocopy.py already covers get_peaks_struct(), and #9543 (companion PR) covers the Arrow exporters; this covers the get_peaks()/get2DPeakData() getters the issue's coding-agent prompt names explicitly.

Test plan

Tests only — no production code changed. Verified against the built nanobind bindings two ways: direct script (All numpy-getter lifetime tests passed!) and pytest (3 passed). numpy importorskip keeps it optional; the spectrum/experiment/kernel modules used are unaffected by the unrelated stale-build _pyopenms_analysis warning.

Part of #9460. Follows #9524 … #9543.

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Tests
    • Added tests to verify the stability and validity of numpy arrays returned from peak data getter methods under garbage collection scenarios.

…MS#9460)

MSSpectrum.get_peaks() and MSExperiment.get2DPeakData()/get2DPeakDataLong()
return their peak data as numpy arrays whose buffer is owned by an nb::capsule
(delete[]), independent of the C++ source. They are safe by construction today;
this pins that so a future change to a view-returning implementation (sharing
the source buffer) is caught.

test_spectrum_zerocopy.py already covers get_peaks_struct(); these cover the
get_peaks()/get2DPeakData() getters named in the issue. Each test snapshots the
arrays WHILE the source is alive, then `del`s it + gc.collect(), then asserts the
arrays are byte-identical -- snapshot-before-delete keeps it non-vacuous.

Complements the Arrow zero-copy lifetime test. Verified against the built
nanobind bindings (3 passed).

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@coderabbitai

coderabbitai Bot commented Jun 15, 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: 998e99c4-501f-4d68-9dd8-cc1b598f698c

📥 Commits

Reviewing files that changed from the base of the PR and between aacd941 and bb9fbbb.

📒 Files selected for processing (1)
  • src/pyOpenMS/tests/unittests/test_numpy_getter_lifetime.py

📝 Walkthrough

Walkthrough

A new pytest module is added that verifies the buffer lifetime contract for nanobind numpy wrappers: MSSpectrum.get_peaks(), MSExperiment.get2DPeakData(), and get2DPeakDataLong(). Tests snapshot returned arrays, delete the source C++ object, force garbage collection, and assert array contents remain intact.

Changes

numpy getter buffer lifetime tests

Layer / File(s) Summary
Module docs and test fixture helpers
src/pyOpenMS/tests/unittests/test_numpy_getter_lifetime.py
Module docstring describes the ownership contract being tested. _make_spectrum() and _make_experiment() construct MSSpectrum and MSExperiment instances with known RT, MS level, and numpy-typed peak arrays.
Lifetime assertion tests and __main__ entrypoint
src/pyOpenMS/tests/unittests/test_numpy_getter_lifetime.py
Three pytest functions call the peak-data getters, copy the returned arrays as pre-deletion snapshots, delete the owning C++ object, call gc.collect(), and assert the copied arrays equal the snapshots. A __main__ block allows direct execution of all three tests.

🐇 A spectrum deleted, its peaks still in view,
The buffer lives on when the owner is through.
We snapshot, we collect, we gc away,
And numpy arrays faithfully stay!
Ownership proved with a hop and a cheer~ 🥕

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.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 accurately describes the main change: adding tests to verify that numpy peak-data getters survive garbage collection of their source objects, which is the primary purpose of the test module added in this PR.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

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

✨ 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 and usage tips.

@github-actions

Copy link
Copy Markdown
Contributor

@timosachsenberg The following names might be missing from the AUTHORS file:

  • Timo Sachsenberg

Please consider applying the following patch:

--- AUTHORS	2026-06-15 18:15:54.170206220 +0000
+++ AUTHORS.new	2026-06-15 18:15:59.553664334 +0000
@@ -23,8 +23,8 @@
  - Chirag Dave
  - Chris Bauer
  - Chris Bielow
- - Christie Mathews
  - Christian Ehrlich
+ - Christie Mathews
  - Clemens Groepl
  - Cornelia Friedle
  - Daniel Jameson

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.

1 participant