Skip to content

Updates for v2 buffer #128

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open

Conversation

henrypinkard
Copy link
Member

@henrypinkard henrypinkard commented Feb 22, 2025

This PR provides updates to the SWIG wrapper to enable use of the v2 buffer, once merged in micro-manager/mmCoreAndDevices#570. These changes should provide the same performance benefits described there.

I tried to keep these changes minimal. In particular one difference from my changes to java SWIG wrapper is that I did not make higher level classes analogous to TaggedImagePointer and LazyJSONObject. Ad advantage to doing something like this is that unlike Java, the pymmcore has the additional opportunity for creating direct, zero-copy views into the v2 buffer. For example, here's how to use the SWIG wrapped objects for pointer-based image handling now:

core.enableV2Buffer(True)
core.snapImage()

# This does returns just a SWIG-wrapped object, but performs no copy
buffer_data_pointer = core.getImagePointer()
# Analogous versions for sequences: popNextImagePointer(), etc

# get the image pixels -- this copies data
pixels = buffer_data_pointer.getData()

# get metadata
md = pymmcore.Metadata()
buffer_data_pointer.getMetadata(md)

# allow the memory holding the pointer to be recycled
buffer_data_pointer.release()

An additional wrapping object should be able to avoid the copy in pixels = buffer_data_pointer.getData(), and instead return some numpy-array like object that is read-only and has a release method (or handles this automatically when garbage collected -- though that is probably less reliable).

I'm not sure what the most desirable way to handle this is, so I'll defer to you @marktsuchida @tlambert03

The functions needed to implement it are already enabled:

address = buffer_data_pointer.getDataPointer() # the memory address of the image  (or other) data
size = buffer_data_pointer.getSizeBytes() # the number of bytes of image (or other) data

Tests for this new API can be found here:

I'm not sure how the pymmcore version numbering works so I'll leave that to you.

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