Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays Cython缓冲协议:如何检索数据?_Arrays_Python 2.7_Numpy_Buffer_Cython - Fatal编程技术网

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

Arrays 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中设置缓冲区协议。我声明了一个新类,我在其中设置了两个必要的方法&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 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数组(只是为了展示我的问题):
导入myarray >>>将numpy作为np导入 >>>ar=np.ones((2,4))#创建一个numpy数组 >>>ns=myarray.P_NpArray(ar)#将numpy数组声明为新的numpy样式数组 >>>打印ns >>>nsa=np.asarray(ns)#转换回numpy数组。这里调用了缓冲协议。 /home/tools/local/x86z/lib/python2.7/site packages/numpy/core/numeric.py:235:RuntimeWarning:从PEP 3118缓冲区格式字符串计算的项目大小与实际项目大小不匹配。 返回数组(a,数据类型,copy=False,order=order) >>>打印类型(nsa)#输出数组的类型正确 >>>打印“nsa=”,nsa nsa= >>>打印“nsa.data=”,nsa.data nsa.data=Xy�0 >>>打印“nsa.itemsize=”,nsa.itemsize nsa.itemsize=8 >>>打印“nsa.size=”,nsa.size#输出数组大小错误 nsa.size=1 >>>打印“nsa.shape=”,nsa.shape#输出数组的形状错误 nsa.shape=() >>>np.frombuffer(nsa.data,np.float64)#我无法正确读取数据缓冲区 [6.90941928e-310] 我四处寻找RuntimeWarning,发现它可能与see和see无关。你觉得怎么样

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


谢谢

您找到解决方案了吗?我也有类似的问题。我可以做
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]