Python中文
首页
教程
问答
标签
搜索
登录
注册
Cython Numpy使用MemoryVi时关于NPY_NO_不推荐使用的_API的警告
回答此问题可获得
20
贡献值,回答如果被采纳可获得
50
分。
<p>我正在将Cython memoryview转换为numpy数组(以便能够在纯Python代码中使用它):</p> <pre><code>from libc.stdlib cimport realloc cimport numpy as np DTYPE = np.float64 ctypedef np.float64_t DTYPE_t cpdef np.ndarray[DTYPE_t] compute(DTYPE_t[:,::1] data): cdef unsigned int Nchannels = data.shape[0] cdef unsigned int Ndata = data.shape[1] cdef DTYPE_t* output = NULL cdef DTYPE_t[::1] mv output = <DTYPE_t*>realloc(output, Ndata*sizeof(output)) if not output: raise MemoryError() mv = <DTYPE_t[:Ndata]>output mv[10:Ndata-10] = 0.0 # various calculations... return np.asarray(mv, dtype=DTYPE, order='C') </code></pre> <p>它会编译,但编译器会发出以下警告:</p> <pre><code>/Users/vlad/anaconda/lib/python2.7/site-packages/numpy/core/include /nump/npy_1_7_deprecated_api.h:15:2: warning: "Using deprecated NumPy API, disable it by #defining NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION" [-W#warnings] </code></pre> <p>我在setup.py中添加了建议的指令:</p> <pre><code>from distutils.core import setup, Extension from Cython.Build import cythonize import numpy filename = 'agents3.pyx' agents_module = Extension( 'Agents', sources = [filename], define_macros = [('NPY_NO_DEPRECATED_API', 'NPY_1_7_API_VERSION')], include_dirs = [numpy.get_include()], ) setup (name = 'Agents', ext_modules = cythonize(agents_module) ) </code></pre> <p>现在它不会编译了,它说:</p> <pre><code>Vlads-MacBook-Pro:program vlad$ python setup.py build_ext --inplace Compiling agents3.pyx because it changed. Cythonizing agents3.pyx running build_ext building 'Agents' extension gcc -fno-strict-aliasing -I/Users/vlad/anaconda/include -arch x86_64 -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -DNPY_NO_DEPRECATED_API=NPY_1_7_API_VERSION -I/Users/vlad/anaconda/lib/python2.7/site-packages/numpy/core/include -I/Users/vlad/anaconda/include/python2.7 -c agents3.c -o build/temp.macosx-10.5-x86_64-2.7/agents3.o agents3.c:2273:52: error: use of undeclared identifier 'NPY_C_CONTIGUOUS' __pyx_t_2 = ((!(PyArray_CHKFLAGS(__pyx_v_self, NPY_C_CONTIGUOUS) != 0)) != 0); ^ agents3.c:2311:52: error: use of undeclared identifier 'NPY_F_CONTIGUOUS' __pyx_t_1 = ((!(PyArray_CHKFLAGS(__pyx_v_self, NPY_F_CONTIGUOUS) != 0)) != 0); ^ agents3.c:2474:42: error: no member named 'descr' in 'struct tagPyArrayObject' __pyx_t_4 = ((PyObject *)__pyx_v_self->descr); ~~~~~~~~~~~~ ^ agents3.c:4026:27: error: no member named 'base' in 'struct tagPyArrayObject' Py_XDECREF(__pyx_v_arr->base); ~~~~~~~~~~~ ^ /Users/vlad/anaconda/include/python2.7/object.h:823:34: note: expanded from macro 'Py_XDECREF' #define Py_XDECREF(op) do { if ((op) == NULL) ; else Py_DECREF(op); } while (0) ^ agents3.c:4026:27: error: no member named 'base' in 'struct tagPyArrayObject' Py_XDECREF(__pyx_v_arr->base); ~~~~~~~~~~~ ^ /Users/vlad/anaconda/include/python2.7/object.h:823:64: note: expanded from macro 'Py_XDECREF' #define Py_XDECREF(op) do { if ((op) == NULL) ; else Py_DECREF(op); } while (0) ^ /Users/vlad/anaconda/include/python2.7/object.h:772:24: note: expanded from macro 'Py_DECREF' --((PyObject*)(op))->ob_refcnt != 0) \ ^ agents3.c:4026:27: error: no member named 'base' in 'struct tagPyArrayObject' Py_XDECREF(__pyx_v_arr->base); ~~~~~~~~~~~ ^ /Users/vlad/anaconda/include/python2.7/object.h:823:64: note: expanded from macro 'Py_XDECREF' #define Py_XDECREF(op) do { if ((op) == NULL) ; else Py_DECREF(op); } while (0) ^ /Users/vlad/anaconda/include/python2.7/object.h:775:34: note: expanded from macro 'Py_DECREF' _Py_Dealloc((PyObject *)(op)); \ ^ /Users/vlad/anaconda/include/python2.7/object.h:762:15: note: expanded from macro '_Py_Dealloc' (*Py_TYPE(op)->tp_dealloc)((PyObject *)(op))) ^ /Users/vlad/anaconda/include/python2.7/object.h:115:47: note: expanded from macro 'Py_TYPE' #define Py_TYPE(ob) (((PyObject*)(ob))->ob_type) ^ agents3.c:4026:27: error: no member named 'base' in 'struct tagPyArrayObject' Py_XDECREF(__pyx_v_arr->base); ~~~~~~~~~~~ ^ /Users/vlad/anaconda/include/python2.7/object.h:823:64: note: expanded from macro 'Py_XDECREF' #define Py_XDECREF(op) do { if ((op) == NULL) ; else Py_DECREF(op); } while (0) ^ /Users/vlad/anaconda/include/python2.7/object.h:775:34: note: expanded from macro 'Py_DECREF' _Py_Dealloc((PyObject *)(op)); \ ^ /Users/vlad/anaconda/include/python2.7/object.h:762:45: note: expanded from macro '_Py_Dealloc' (*Py_TYPE(op)->tp_dealloc)((PyObject *)(op))) ^ agents3.c:4035:16: error: no member named 'base' in 'struct tagPyArrayObject' __pyx_v_arr->base = __pyx_v_baseptr; ~~~~~~~~~~~ ^ agents3.c:4070:30: error: no member named 'base' in 'struct tagPyArrayObject' __pyx_t_1 = ((__pyx_v_arr->base == NULL) != 0); ~~~~~~~~~~~ ^ agents3.c:4093:44: error: no member named 'base' in 'struct tagPyArrayObject' __Pyx_INCREF(((PyObject *)__pyx_v_arr->base)); ~~~~~~~~~~~ ^ agents3.c:1065:37: note: expanded from macro '__Pyx_INCREF' #define __Pyx_INCREF(r) Py_INCREF(r) ^ /Users/vlad/anaconda/include/python2.7/object.h:767:18: note: expanded from macro 'Py_INCREF' ((PyObject*)(op))->ob_refcnt++) ^ agents3.c:4094:41: error: no member named 'base' in 'struct tagPyArrayObject' __pyx_r = ((PyObject *)__pyx_v_arr->base); ~~~~~~~~~~~ ^ 11 errors generated. error: command 'gcc' failed with exit status 1 Vlads-MacBook-Pro:program vlad$ </code></pre> <p>我该怎么办?不推荐的API调用保持原样可以吗?它试图访问<code>base</code>字段——但我没有这样做,这是Cython的错。我只是把一个memoryview转换成一个numpy数组。还有其他更清洁/更安全的方法吗?</p>
0 条评论
分类:
Python问答
请先
登录
后评论
默认排序
时间排序
1 个回答
匿名
1天前
擅长:python、mysql、java
<p>为了进一步的参考,<a href="http://docs.cython.org/src/reference/compilation.html#configuring-the-c-build" rel="noreferrer">cython online docs</a>说这是因为Cython正在使用一个被弃用的Numpy API,目前,这只是一个我们可以忽略的警告。</p>
请先
登录
后评论
针对此问题:
更多的回答
关注
89
关注
收藏
1
收藏,
216
浏览
网友 提问于 2天前
相关Python问题
Django:。是不是“超级用户”字段不起作用
6 回答
Django:'DeleteQuery'对象没有属性'add'
4 回答
Django:'ModelForm'对象没有属性
7 回答
Django:'python manage.py runserver'返回'TypeError:'WindowsPath'类型的对象没有len()
9 回答
Django:'Python管理.pysyncdb'不创建我的架构表
4 回答
Django:'Python管理.py迁移“耗时数小时(和其他奇怪的行为)
9 回答
Django:'readonly'属性在我的ModelForm上不起作用
9 回答
Django:'RegisterEmployeeView'对象没有属性'object'
9 回答
Django:'str'对象没有属性'get'
4 回答
Django:'创建' 不能被指定为Order模型表单中的值,因为它是一个不可编辑的字段
5 回答
Django:“'QuerySet'类型的对象不是JSON可序列化的”
3 回答
Django:“'utf8'编解码器无法解码位置19983中的字节0xe9:无效的连续字节”,加载临时文件时
3 回答
Django:“<…>”需要有一个字段“id”的值,然后才能使用这个manytomy关系
8 回答
Django:“AnonymousUser”对象没有“get_full_name”属性
2 回答
Django:“ascii”编解码器无法解码位置1035中的字节0xc3:序号不在范围内(128)
10 回答
Django:“BaseTable”对象不支持索引
2 回答
Django:“collections.OrderedDict”对象不可调用
1 回答
Django:“Country”对象没有属性“all”
5 回答
Django:“Data”对象没有属性“save”
5 回答
Django:“datetime”类型的对象不是JSON serializab
5 回答