ValueError continuous memory view and 0-size numpy array
See original GitHub issueA ValueError is raised if one gives a numpy array with shape (2, 0, 1)
to such Cython functions with a 3d memoryview as argument:
cimport numpy as np
import numpy as np
np.import_array()
cpdef myfunc3d(np.float64_t[:, :, ::1] arr):
if arr.size > 0:
print(arr[0, 0, 0])
else:
print('size == 0')
The code to get the error:
import numpy as np
arr = np.ones((2, 0, 1))
print(arr.flags)
myfunc3d(arr)
which gives:
C_CONTIGUOUS : True
F_CONTIGUOUS : True
OWNDATA : True
WRITEABLE : True
ALIGNED : True
UPDATEIFCOPY : False
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-7-81a397764d36> in <module>()
1 arr = np.ones((2, 0, 1))
2 print(arr.flags)
----> 3 myfunc3d(arr)
_cython_magic_5dfab2d7f2a0e837d79c56566c9147b4.pyx in _cython_magic_5dfab2d7f2a0e837d79c56566c9147b4.myfunc3d()
ValueError: Buffer and memoryview are not contiguous in the same dimension.
In [ ]:
Strangely, no error is raised for a shape (1, 0, 1)
!
Issue Analytics
- State:
- Created 6 years ago
- Reactions:1
- Comments:5 (3 by maintainers)
Top Results From Across the Web
ValueError: When changing to a larger dtype, its size must be ...
You are trying to create a view on an array with an incompatible memory layout, where the output dtype has an itemsize that...
Read more >Look Ma, No For-Loops: Array Programming With NumPy
Technical Detail: The smaller-sized array or scalar is not literally stretched in memory: it is the computation itself that is repeated. This extends...
Read more >numpy.ascontiguousarray — NumPy v1.24 Manual
Return a contiguous array (ndim >= 1) in memory (C order). Parameters: ... Reference object to allow the creation of arrays which are...
Read more >Typed Memoryviews — Cython 3.0.0a11 documentation
Typed Memoryviews¶. Typed memoryviews allow efficient access to memory buffers, such as those underlying NumPy arrays, without incurring any Python overhead ...
Read more >Arithmetic on Cython Typed Memoryviews - Google Groups
But converting the memory view back to an np.array is slower than tortoises ... ValueError: Buffer and memoryview are not contiguous in the...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I ran into this too. Here’s another minimal example to reproduce this:
The problem is that
__Pyx_ValidateAndInit_memviewslice
does not correctly realize that a 0-size array is always C-contiguous.This crops up in the wild, too. A search for the error message reveals many instances where people inadvertently passed 0-size arrays to cython-implemented functions and got confusing error messages. E.g.: https://stackoverflow.com/questions/46470492/buffer-and-memoryview-are-not-contiguous-in-the-same-dimension-in-python
Yes, that’s a bug. It should behave like NumPy. PR welcome. Tests can go into
tests/memoryview/memslice.pyx
, there are several contiguity tests already.