Skip to content

Add zero-copy NumPy view for MSChromatogram.get_peaks_struct() - #8825

Merged
timosachsenberg merged 2 commits into
OpenMS:developfrom
Srikanthkn0:feature/mschromatogram-zero-copy-v2
Mar 2, 2026
Merged

timosachsenberg merged 2 commits into
OpenMS:developfrom
Srikanthkn0:feature/mschromatogram-zero-copy-v2

Conversation

@Srikanthkn0

@Srikanthkn0 Srikanthkn0 commented Mar 1, 2026 •

Copy link
Copy Markdown
Contributor

Summary

This PR adds a zero-copy NumPy accessor for chromatogram peak data:

MSChromatogram.get_peaks_struct()

Instead of allocating and copying RT and intensity arrays (as done by get_peaks()), this method exposes a direct NumPy view over the underlying std::vector memory using nanobind.

This eliminates the O(n) copy cost when accessing large chromatograms from Python.

Motivation

For large chromatograms (e.g. SWATH / DIA data), copying peak data into separate NumPy arrays introduces measurable overhead.

The new method:
• Avoids memory allocation
• Avoids copying
• Returns immediately with a view
• Preserves lifetime safety using reference_internal

The existing get_peaks() remains unchanged for backward compatibility.

Benchmark performed on macOS (Apple Silicon), Python 3.14, Release build.
for n in [100, 10_000, 100_000, 1_000_000]:
benchmark(n)

Peaks: 100
get_peaks (copy): 0.000002 s
get_peaks_struct (zero-copy): 0.000001 s

Peaks: 10,000
get_peaks (copy): 0.000009 s
get_peaks_struct (zero-copy): 0.000001 s

Peaks: 100,000
get_peaks (copy): 0.000117 s
get_peaks_struct (zero-copy): 0.000001 s

Peaks: 1,000,000
get_peaks (copy): 0.002011 s
get_peaks_struct (zero-copy): 0.000001 s

Memory

For 1M peaks:
Copy arrays memory: 16,000,000 bytes
Zero-copy array memory: 16,000,000 bytes

Safety

Uses nb::rv_policy::reference_internal
Lifetime safety verified via dedicated test
No segfaults under forced GC
Full pyopenms_tests suite passes

100% tests passed, 0 tests failed

Backward Compatibility

No changes to existing API
get_peaks() remains unchanged
This is an additive improvement

Notes

This implementation mirrors the zero-copy approach already used for peak containers and keeps the Python binding minimal while preserving performance.

Summary by CodeRabbit

  • New Features

    • Retrieve chromatogram peaks as a structured NumPy array (RT, intensity) with zero-copy access, including correct behavior for empty chromatograms.
  • Tests

    • Added unit tests covering zero-copy mutation propagation, memory/lifetime safety, empty-chromatogram behavior, and consistency with existing peak accessors.

@coderabbitai

coderabbitai Bot commented Mar 1, 2026 •

Copy link
Copy Markdown
Contributor

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 0912804 and 767c111.

📒 Files selected for processing (1)
  • src/pyOpenMS/tests/unittests/test_mschromatogram_struct_view.py
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/pyOpenMS/tests/unittests/test_mschromatogram_struct_view.py

📝 Walkthrough

Walkthrough

Adds get_peaks_struct() to MSChromatogram returning a zero-copy (n, 2) NumPy array of [RT, intensity] and adds unit tests verifying zero-copy modification, memory safety, empty-chromatogram behavior, and consistency with get_peaks().

Changes

Cohort / File(s) Summary
MSChromatogram Binding
src/pyOpenMS/bindings/bind_chromatogram.cpp
Adds def("get_peaks_struct", ...) to return a zero-copy (n, 2) double NumPy array representing [RT, intensity]. Handles empty chromatograms (0×2), constructs ndarray from first peak memory, and uses nanobind lifetime management (reference_internal, nb::find(self)). Placed in binding sequence after existing get_peaks.
Unit Tests
src/pyOpenMS/tests/unittests/test_mschromatogram_struct_view.py
Adds tests: zero-copy modification propagation, memory safety across GC (lifetime sharing), empty chromatogram returns (0,2) array, and consistency checks between get_peaks_struct() and get_peaks().

Poem

🐰 I nibble peaks with joyful hop,

arrays aligned without a copy,
RT and height in tidy rows,
memory safe as thistle grows,
bindings snug — a happy hop!

🚥 Pre-merge checks | ✅ 2 | ❌ 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 (2 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 a new zero-copy NumPy view method (get_peaks_struct) to MSChromatogram. It is concise, clear, and directly reflects the primary technical improvement.

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

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Tip

Try Coding Plans. Let us write the prompt for your AI agent so you can ship faster (with fewer bugs).
Share your feedback on Discord.


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.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@src/pyOpenMS/tests/unittests/test_mschromatogram_struct_view.py`:
- Line 21: The test unpacks chrom.get_peaks() into rt_copy and int_copy but only
int_copy is used; replace the unused variable rt_copy with a throwaway name (_)
in the unpacking (i.e., change the assignment where chrom.get_peaks() is called)
so the test uses "_" and int_copy instead of rt_copy and int_copy to silence the
lint warning and make intent clear.

ℹ️ Review info

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 3e94ef6 and 0912804.

📒 Files selected for processing (2)
  • src/pyOpenMS/bindings/bind_chromatogram.cpp
  • src/pyOpenMS/tests/unittests/test_mschromatogram_struct_view.py

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
@timosachsenberg

Copy link
Copy Markdown
Contributor

awesome

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.

2 participants