Arrays Cython缓冲协议:如何检索数据?
我正在尝试在cython中设置缓冲区协议。我声明了一个新类,我在其中设置了两个必要的方法&getbuffer\uuuuuuuuuuuu和&uuuuuu releasebuffer\uuuuArrays Cython缓冲协议:如何检索数据?,arrays,python-2.7,numpy,buffer,cython,Arrays,Python 2.7,Numpy,Buffer,Cython,我正在尝试在cython中设置缓冲区协议。我声明了一个新类,我在其中设置了两个必要的方法&getbuffer\uuuuuuuuuuuu和&uuuuuu releasebuffer\uuuu 仅供参考,我正在使用cython 0.19和Python2.7,下面是cython代码: cimport numpy人民币 #my array类的Cython缓冲协议实现 cdef类P_n阵列: cdef人民币元整净现值 定义(自、输入): self.npy\u ar=inpy\u ar def uu ge
仅供参考,我正在使用cython 0.19和Python2.7,下面是cython代码:
cimport numpy人民币
#my array类的Cython缓冲协议实现
cdef类P_n阵列:
cdef人民币元整净现值
定义(自、输入):
self.npy\u ar=inpy\u ar
def uu getbuffer uu(self,Py_buffer*buffer,int标志):
cdef Py_ssize_t ashape[2]
ashape[0]=自组织形状[0]
ashape[1]=自组织形状[1]
cdef Py_-ssize_t astrides[2]
跨步[0]=自我npy_ar.跨步[0]
跨步[1]=自我npy_ar.跨步[1]
buffer.buf=self.npy\u ar.data
buffer.format='f'
buffer.internal=NULL
buffer.itemsize=self.npy\u ar.itemsize
buffer.len=self.npy\u ar.size*self.npy\u ar.itemsize
buffer.ndim=self.npy_ar.ndim
buffer.obj=self
buffer.readonly=0
buffer.shape=ashape
缓冲步数=跨步数
buffer.suboffsets=NULL
定义释放缓冲区(自,Py缓冲区*缓冲区):
通过
这段代码编译得很好。但我无法正确检索缓冲区数据。参见以下测试,其中:
- 我创建了一个numpy数组
- 用我的缓冲协议类加载它
- 尝试将其检索为numpy数组(只是为了展示我的问题):
谢谢您找到解决方案了吗?我也有类似的问题。我可以做
memoryview(nsa)
但是np.asarray(nsa)
不起作用实际上我现在发现(同一个文件现在更新了),缓冲区和对象的形状必须与
cimport numpy as CNY
# Cython buffer protocol implementation for my array class
cdef class P_NpArray:
cdef CNY.ndarray npy_ar
def __cinit__(self, inpy_ar):
self.npy_ar=inpy_ar
def __getbuffer__(self, Py_buffer *buffer, int flags):
cdef Py_ssize_t ashape[2]
ashape[0]=self.npy_ar.shape[0]
ashape[1]=self.npy_ar.shape[1]
cdef Py_ssize_t astrides[2]
astrides[0]=self.npy_ar.strides[0]
astrides[1]=self.npy_ar.strides[1]
buffer.buf = <void *> self.npy_ar.data
buffer.format = 'f'
buffer.internal = NULL
buffer.itemsize = self.npy_ar.itemsize
buffer.len = self.npy_ar.size*self.npy_ar.itemsize
buffer.ndim = self.npy_ar.ndim
buffer.obj = self
buffer.readonly = 0
buffer.shape = ashape
buffer.strides = astrides
buffer.suboffsets = NULL
def __releasebuffer__(self, Py_buffer *buffer):
pass
>>> import myarray
>>> import numpy as np
>>> ar=np.ones((2,4)) # create a numpy array
>>> ns=myarray.P_NpArray(ar) # declare numpy array as a new numpy-style array
>>> print ns
<myarray.P_NpArray object at 0x7f30f2791c58>
>>> nsa = np.asarray(ns) # Convert back to numpy array. Buffer protocol called here.
/home/tools/local/x86z/lib/python2.7/site-packages/numpy/core/numeric.py:235: RuntimeWarning: Item size computed from the PEP 3118 buffer format string does not match the actual item size.
return array(a, dtype, copy=False, order=order)
>>> print type(nsa) # Output array has the correct type
<type 'numpy.ndarray'>
>>> print "nsa=",nsa
nsa= <myarray.P_NpArray object at 0x7f30f2791c58>
>>> print "nsa.data=", nsa.data
nsa.data= Xy�0
>>> print "nsa.itemsize=",nsa.itemsize
nsa.itemsize= 8
>>> print "nsa.size=",nsa.size # Output array has a WRONG size
nsa.size= 1
>>> print "nsa.shape=",nsa.shape # Output array has a WRONG shape
nsa.shape= ()
>>> np.frombuffer(nsa.data, np.float64) # I can't get a proper read of the data buffer
[ 6.90941928e-310]