Use a numpy.flatiter as a 1D-memoryview

14 views
Skip to first unread message

Julien Jerphanion

unread,
Jun 16, 2022, 5:37:02 PM6/16/22
to cython-users
Hi,

I am interested to use a numpy.flatiter in Cython via a memoryview.

Yet I get the following error at runtime:

stringsource:658: in View.MemoryView.memoryview_cwrapper
    ???
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

>   ???
E   TypeError: a bytes-like object is required, not 'numpy.flatiter'


I guess such an error is thrown because numpy.flatiter does not implement the buffer protocol.

Is it possible to define a wrapper around such an iterator so that's it is the case?

For the full context, see: https://github.com/scikit-learn/scikit-learn/pull/23585/files#diff-abc7f758c572e98ab4d40c8f89cb4549d6d63708391e6828b8b58b2bf1d04f71R246-R318

Thank you a lot!

Best,
Julien.

da-woods

unread,
Jun 17, 2022, 2:57:57 AM6/17/22
to cython...@googlegroups.com
I don't think this is possible - a general flatiter can't define the buffer protocol because it's 1D but the spacing between adjacent elements isn't uniform.

Options include:
* the Numpy pxd files contain wrappers for flatiter https://github.com/cython/cython/blob/master/Cython/Includes/numpy/__init__.pxd (including some macro functions) that you could use to access it efficiently.
* Instead iterate over multi-dimensional memoryviews (you could use a fused function to generate versions for between 0 and 7 dimensions, which I think is the upper limit that Cython memoryviews support)
* Maybe look at defining a Numpy ufunc instead to handle the varying number of dimensions.
* Use np.ravel (which makes a copy only if needed ... I dunno if that's a problem for your specific use)

David
--

---
You received this message because you are subscribed to the Google Groups "cython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cython-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/cython-users/4aa7cb6b-1f32-4657-a579-39d5eff12467n%40googlegroups.com.


da-woods

unread,
Jun 17, 2022, 12:44:18 PM6/17/22
to cython...@googlegroups.com
One further comment: it looks from your code like you're requiring a C contiguous array so I think ravel should work and always avoid the copy.
Reply all
Reply to author
Forward
Message has been deleted
0 new messages