Skip to content
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

avoid copying data to Python but using numpy::frombuffer #717

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

rijobro
Copy link
Contributor

@rijobro rijobro commented Jun 12, 2020

Just an idea.

We can use numpy.frombuffer() to share data between C++ and python (e.g., for ProjDataInMemory). I think all that needs to be done is pass the address of begin to the python level. No idea how to do that with your wrapper, though, @evgueni-ovtchinnikov. Any ideas?

@rijobro rijobro marked this pull request as draft June 12, 2020 13:01
Comment on lines +732 to +735
buffer = ?
try_calling(pystir.cSTIR_AcquisitionDataInMemory_get_buffer\
(self.handle, buffer))
array = numpy.frombuffer(buffer, dtype=numpy.float32)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not see how this can possibly work. However you define buffer, pystir.cSTIR_AcquisitionDataInMemory_get_buffer can only give you an address in memory, but numpy.frombuffer needs to know the size of memory represented by the buffer to be able to handle that memory as numpy array data.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I haven't done it here, but you can give the number of elements with count:

numpy.frombuffer(buffer, dtype=float, count=-1, offset=0)

And obviously we know how many elements our sinogram contains.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem is that the first argument to frombuffer should be of type buffer_like (see here). I have no idea how to do this, but the address to the first float in C++ somehow needs to be converted to this buffer_like object in Python. Searching online, it looks like it's possible with SWIG, but I'm not sure how to do it in SIRF's setup.

@KrisThielemans KrisThielemans changed the title get from buffer avoid copying data to Python but using numpy::frombuffer Oct 28, 2021
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