pyjnius icon indicating copy to clipboard operation
pyjnius copied to clipboard

Java arrays to/from NumPy arrays?

Open maedoc opened this issue 10 years ago • 7 comments

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.

--- Want to back this issue? **[Post a bounty on it!](https://www.bountysource.com/issues/21214795-java-arrays-to-from-numpy-arrays?utm_campaign=plugin&utm_content=tracker%2F77133&utm_medium=issues&utm_source=github)** We accept bounties via [Bountysource](https://www.bountysource.com/?utm_campaign=plugin&utm_content=tracker%2F77133&utm_medium=issues&utm_source=github).

maedoc avatar Jun 09 '15 13:06 maedoc

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?

remram44 avatar Jun 09 '15 13:06 remram44

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?

maedoc avatar Oct 11 '15 13:10 maedoc

Any updated on this?

hanslovsky avatar Mar 13 '17 12:03 hanslovsky

One could use ctypes to call into NumPy C API without requiring NumPy to be present, so it should be easy to implement.

maedoc avatar Mar 13 '17 12:03 maedoc

I will have a look into that, thanks for the hint!

hanslovsky avatar Mar 13 '17 12:03 hanslovsky

This is the solution I came up with:

  • Use ctypes to get the address of the numpy array data.
  • Pass address to Java class that uses Unsafe to 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

hanslovsky avatar Mar 13 '17 20:03 hanslovsky

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.

jakirkham avatar Jun 19 '18 21:06 jakirkham