Java arrays to/from NumPy arrays?
Any possibility or interest in passing large arrays of numbers between Java arrays and NumPy arrays? Since Cython is already used, this may be trivial to add to the conversion methods.
To do this efficiently, pyjnius would need to use the numpy C API. I'm not sure pyjnius would be willing to use such a dependency.
It could however be a third Python library; does pyjnius have a stable enough API that it could use to access or create Java arrays?
I'm not sure pyjnius would be willing to use such a dependency
then the question is, if I implement support in pyjnius do I send a PR or not?
Any updated on this?
One could use ctypes to call into NumPy C API without requiring NumPy to be present, so it should be easy to implement.
I will have a look into that, thanks for the hint!
This is the solution I came up with:
- Use ctypes to get the address of the
numpyarray data. - Pass address to Java class that uses
Unsafeto access the data. This has certain problems with lifetime and garbage collection of objects but for me it is good enough.
I obtain the array data address like this:
np_img = ...
ct_pt = ctypes.cast( np_img.ctypes.data_as(ctypes.POINTER(ctypes.c_float)), ctypes.c_void_p ).value
Has anyone explored using memoryviews in PyJNIus? This would also be a valid approach for working with NumPy arrays that would avoid a NumPy dependency.