Cython缓冲协议:如何检索数据?

时间:2015-03-18 08:39:44

标签: arrays python-2.7 numpy buffer cython

我试图在cython中设置缓冲协议。我声明了一个新类,我在其中设置了两个必要的方法__getbuffer__和__releasebuffer__
仅供我使用Cython0.19和Python2.7,这里是cython代码:

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

此代码编译良好。但是我无法正确检索缓冲区数据 请参阅以下测试:

  • 我创建了一个numpy数组
  • 使用我的缓冲区protocoled类加载它
  • 尝试将其检索为numpy数组(只是为了展示我的问题):
>>> 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]

我四处寻找RuntimeWarning并发现它可能不相关,请参阅PEP 3118 warning when using ctypes array as numpy array http://bugs.python.org/issue10746http://bugs.python.org/issue10744。你觉得怎么样?

显然,缓冲区的形状和大小没有正确传输。所以。我错过了什么?我的缓冲区协议是否正确定义了?

由于

0 个答案:

没有答案