silx icon indicating copy to clipboard operation
silx copied to clipboard

Use cython const memory view when available to support read only arrays

Open vallsv opened this issue 6 years ago • 8 comments

Here is a feed back from flint.

In the process of fixing numpy deprecation and then using frombuffer instead of fromstring, we saw that the combo min_max do not support read-only array.

Any idea if cython provides decorator to fix it?

In [9]: s = b"\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09"
   ...: data = numpy.frombuffer(s, dtype='uint8', count=5, offset=5)

In [10]: data.flags
Out[10]:
  C_CONTIGUOUS : True
  F_CONTIGUOUS : True
  OWNDATA : False
  WRITEABLE : False
  ALIGNED : True
  WRITEBACKIFCOPY : False
  UPDATEIFCOPY : False

In [11]: from silx.math.combo import min_max

In [12]: min_max(data)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-12-202b7fa8bdc6> in <module>
----> 1 min_max(data)

silx/math/combo.pyx in silx.math.combo.min_max()

silx/math/combo.pyx in silx.math.combo._min_max()

~/miniconda3/envs/blissenv/lib/python3.7/site-packages/silx/math/combo.cpython-37m-x86_64-linux-gnu.so in View.MemoryView.memoryview_cwrapper()

~/miniconda3/envs/blissenv/lib/python3.7/site-packages/silx/math/combo.cpython-37m-x86_64-linux-gnu.so in View.MemoryView.memoryview.__cinit__()

ValueError: buffer source array is read-only

vallsv avatar Aug 13 '19 12:08 vallsv

Looks like it is supported with cython 0.28 https://cython.readthedocs.io/en/latest/src/userguide/memoryviews.html#read-only-views would you like to try?

vallsv avatar Aug 13 '19 12:08 vallsv

Looks like we have no choice but requiring Cython >= 0.28 and using const to define memory views.... Or we use Cython <= 0.27... and drop Python 3.7...

t20100 avatar Aug 28 '19 09:08 t20100

Starting looking at it, there is an issue in cython with usage of const for fused types (https://github.com/cython/cython/issues/1772)

t20100 avatar Aug 28 '19 10:08 t20100

Some possible workarounds:

  • Use ndarray as input type rather than memory views: That allows to support read-only arrays at the expense of a binary dependency on numpy (it's OK as long as we advertise the right version of numpy).. but it makes it more complicated to specify variables of the same type as the input: you need to create a numpy array with the same dtype as the input array instead of using a variable.
  • Copy read-only arrays before processing... i.e., add useless copies...

t20100 avatar Aug 28 '19 11:08 t20100

I looked further into this. In silx we should wait for const fused types to be supported in Cython and then use it everywhere possible, i.e., not only in min_max, but for every function that does not modify the data.

t20100 avatar Aug 30 '19 10:08 t20100

Sounds good. No need to rewrite the full base code if the feature is not available.

vallsv avatar Aug 30 '19 11:08 vallsv

const memoryview will be available in cython 3.0. We "just" have to wait until we move silx minimal supported version of cython to 3.0....

t20100 avatar Sep 10 '19 14:09 t20100

Nice :-)

vallsv avatar Sep 10 '19 17:09 vallsv