np.asarray fails for memoryview

46 views
Skip to first unread message

Rok Roskar

unread,
Aug 8, 2016, 11:31:33 AM8/8/16
to cython-users
Hello, 

I have encountered some strange behavior using numpy.asarray with a memoryview object that I can't explain. Here's a brief example using the cython magic in a jupyter notebook -- I simply make a function that expects two struct array buffers and returns them. One has two ints and the other a long and an int: 

cdef struct S1:
    int iGroup
    int iOrder

cdef struct S2:
    long iGroup
    int iOrder
 
def test_struct(S1[:] s1, S2[:] s2):
    return s1, s2


Now I make two arrays in python to pass to this function: 

dt1 = np.dtype([('iGroup', 'i4'), ('iOrder', 'i4')], align=True)
dt2 = np.dtype([('iGroup', 'i8'), ('iOrder', 'i4')], align=True)

a = np.zeros(10, dtype=dt1)
b = np.zeros(10, dtype=dt2)

x, y = test_struct(a,b)

print x
print y

<MemoryView of 'ndarray' object>
<MemoryView of 'ndarray' object>


Both are successfully returned as MemoryView objects. Now I want to turn them into a numpy array:

np.asarray(x)

array([(0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0),
       (0, 0), (0, 0)], 
      dtype=[('iGroup', '<i4'), ('iOrder', '<i4')])


np.asarray(y)

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-204-ca5459515bfd> in <module>()
----> 1 np.asarray(y)

/Users/rok/miniconda/lib/python2.7/site-packages/numpy/core/numeric.pyc in asarray(a, dtype, order)
    480 
    481     """
--> 482     return array(a, dtype, copy=False, order=order)
    483 
    484 def asanyarray(a, dtype=None, order=None):

TypeError: expected a readable buffer object


What am I missing here? Why does this second struct not work? Any tips would be greatly appreciated!


Rok



Reply all
Reply to author
Forward
0 new messages