2014-09-11 6 views
23

Ich Umwandlung eines Cython memoryview zu einem numpy Array (können es in reinen Python-Code verwenden):Cython Numpy über NPY_NO_DEPRECATED_API Warnung, wenn MemoryView mit

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') 

Es kompiliert, aber der Compiler gibt die folgende Warnung:

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) 
) 

No:

/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] 

ich die vorgeschlagene Richtlinie in setup.py hinzugefügt W würde es nicht kompilieren, es sagt:

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$ 

Was soll ich tun? Ist es in Ordnung, den veralteten API-Aufruf so zu lassen, wie er ist? Es versucht, auf das base Feld zuzugreifen - aber ich tue es nicht, es ist Cythons Schuld. Ich habe einfach eine Speicheransicht in ein numpliges Array umgewandelt. Gibt es einen anderen, saubereren/sichereren Weg?

+2

FWIW, ich bekomme die gleichen Warnungen. Es hat noch nichts gebrochen, deshalb ignoriere ich sie jetzt. – perimosocordiae

Antwort

20

Nur für eine weitere Referenz, cython online docs sagt, dies ist, weil Cython eine veraltete Numpy-API verwendet, und vorerst ist es nur eine Warnung, die wir ignorieren können.

+1

Danke! Ich habe deine Antwort gewählt, weil es nicht nur deine Vermutung ist, sondern du Beweise gebracht hast. :) – Amenhotep

+0

OK, aber die Warnung wird auf stdout geschrieben, es ist also lästig zu filtern. Kann man Cython so konfigurieren, dass keine Warnungen gemeldet werden? – Arthur

+0

@Arthur Wenn Sie Cython Magic in Ipython verwenden, können Sie '%% cython --compile-args = -w' verwenden, um Warnungen zu deaktivieren. – towr

0

Ich bekomme auch die gleichen Warnungen, und ich würde sagen, es ist normal.

Mit der numpy C API müssen Sie eine Zeile vor das C-Skript setzen, wenn Sie diese Warnung nicht mögen, aber alles was es tut, ist dem Compiler zu sagen, die "veraltete" Nachricht zu ignorieren - es scheint zu funktionieren egal wie.

Ich vermute, der Cython-Compiler legt diese Codezeile nicht, wenn es den C-Code generiert, und ich denke nicht, dass das wichtig ist.

1

Angenommen, man möchte die Verwarnungswarnung ausblenden, kann das folgende Compiler-Flag mit clang implementiert werden: extra_compile_args = ['- Wno- # warnings'].

Für gcc, extra_compile_args = ['- Wno-cpp'] erreicht das gleiche.

Natürlich verbirgt dies auch andere Präprozessor-Direktivenwarnungen.