2012-02-26 12 views
11

मैं निम्नलिखित कोड है कि propperly cython को परिवर्तित किया हैNumpy-> Cython रूपांतरण: संकलित करें त्रुटि: कनवर्ट नहीं कर सकता 'npy_intp *' अजगर वस्तु को

from numpy import * 

## returns winning players or [] if undecided. 
def score(board): 
    scores = [] 
    checked = zeros(board.shape) 
    for i in xrange(len(board)): 
     for j in xrange(len(board)): 
      if checked[i,j] == 0 and board[i,j] !=0: 
       ... do stuf 

रूपांतरण पर मेरे प्रयास cython रहे हैं:

import numpy as np 
cimport numpy as np 

@cython.boundscheck(False) 
@cython.wraparound(False) 
@cython.nonecheck(False) 

## returns winning players or [] if undecided. 
def score(np.ndarray[int, ndim=2] board): 
    scores = [] 
    cdef np.ndarray[int, ndim = 2 ] checked 
    checked = np.zeros(board.shape) 
    for i in xrange(len(board)): 
     for j in xrange(len(board)): 
      if checked[i,j] == 0 and board[i,j] !=0: 
       ... do stuf 

लेकिन जब मैं संकलन मैं:

$ python setup.py build_ext --inplace 
running build_ext 
cythoning newgox.pyx to newgox.c 


## returns winning players or [] if undecided. 
def score(np.ndarray[int, ndim=2] board): 
    scores = [] 
    cdef np.ndarray[int, ndim = 2 ] checked 
    checked = np.zeros(board.shape) 
        ^
------------------------------------------------------------ 

newgox.pyx:58:28: Cannot convert 'npy_intp *' to Python object 
building 'newgox' extension 

इसके अलावा, मुझे यकीन है कि यह सही तरीके से फूल के साथ काम करने है नहीं कर रहा हूँ cython में ts:

scores = [] 
if some_stuff_is_true: 
    scores.append(some_integer) 

संपादित करें:

धन्यवाद, कोड अब संकलित लेकिन जब मैं इसे चलाने मैं त्रुटि मिलती है:

ctypedef np.int_t DTYPE_t 
DTYPE = np.int 
: मैं इन दो विकल्पों में बंधे

File "newgox.pyx", line 63, in newgox.score (newgox.c:1710) 
    def score(np.ndarray[np.int, ndim=2] board): 
ValueError: Buffer dtype mismatch, expected 'int object' but got 'long' 

और फिर जारी रखें:

board = zeros((5,5), dtype = DTYPE) 
def score(np.ndarray[DTYPE, ndim=2] board): 

या सिर्फ दोनों घोषणाओं में:

... np.int ... 

यह वही त्रुटि में परिणाम है, लेकिन हस्ताक्षर के साथ:

ValueError: Buffer dtype mismatch, expected 'int' but got 'long' 

धन्यवाद bellamyj, लेकिन आपके द्वारा सुझाए गए कोड को संकलित नहीं होगा,

$ python setup.py build_ext --inplace 
running build_ext 
cythoning newgox.pyx to newgox.c 
building 'newgox' extension 
gcc-4.2 -fno-strict-aliasing -fno-common -dynamic -arch i386 -arch x86_64 -g -O2 -DNDEBUG -g -O3 -I/Library/Frameworks/Python.framework/Versions/2.7/include/python2.7 -c newgox.c -o build/temp.macosx-10.6-intel-2.7/newgox.o 

newgox.c:238:31: error: numpy/arrayobject.h: No such file or directory 
newgox.c:239:31: error: numpy/ufuncobject.h: No such file or directory 
newgox.c:356: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘__pyx_t_5numpy_int8_t’ 
newgox.c:365: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘__pyx_t_5numpy_int16_t’ 
newgox.c:374: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘__pyx_t_5numpy_int32_t’ 

और यह टी जारी है o अन्य सभी प्रकारों को भी समझाएं।

newgox.c:978: error: expected ‘)’ before ‘*’ token 
newgox.c:979: error: expected ‘)’ before ‘*’ token 
newgox.c:980: error: expected ‘)’ before ‘*’ token 
newgox.c:983: error: expected declaration specifiers or ‘...’ before ‘PyArrayObject’ 
newgox.c:984: error: expected declaration specifiers or ‘...’ before ‘PyArrayObject’ 
newgox.c:985: error: expected ‘)’ before ‘*’ token 
newgox.c:987: error: ‘__pyx_t_5numpy_int32_t’ undeclared here (not in a function) 
newgox.c: In function ‘__pyx_pf_6newgox_7score’: 
newgox.c:1638: error: ‘PyArrayObject’ undeclared (first use in this function) 
newgox.c:1638: error: (Each undeclared identifier is reported only once 
newgox.c:1638: error: for each function it appears in.) 
newgox.c:1638: error: ‘__pyx_v_checked’ undeclared (first use in this function) 
newgox.c:1659: error: ‘__pyx_t_5’ undeclared (first use in this function) 
newgox.c:1721: error: expected expression before ‘)’ token 
newgox.c:1723: error: expected expression before ‘)’ token 
newgox.c:1747: error: expected expression before ‘)’ token 
newgox.c:1766: error: expected expression before ‘)’ token 
newgox.c:1813: error: expected expression before ‘)’ token 
newgox.c:1846: error: expected expression before ‘)’ token 
newgox.c:1846: error: too many arguments to function ‘__pyx_f_6newgox_check_life’ 
newgox.c:2009: error: expected expression before ‘)’ token 
newgox.c:2009: warning: assignment makes pointer from integer without a cast 
newgox.c:2012: error: expected expression before ‘)’ token 
newgox.c:2032: error: expected expression before ‘)’ token 
newgox.c: At top level: 
newgox.c:2088: error: expected declaration specifiers or ‘...’ before ‘PyArrayObject’ 
newgox.c: In function ‘__pyx_f_6newgox_check_life’: 
newgox.c:2124: error: ‘__pyx_v_board’ undeclared (first use in this function) 
newgox.c:2160: error: ‘PyArrayObject’ undeclared (first use in this function) 
newgox.c:2160: error: expected expression before ‘)’ token 
newgox.c:2160: error: too many arguments to function ‘__pyx_f_6newgox_liberty’ 
newgox.c:2420: error: too many arguments to function ‘__pyx_f_6newgox_check_life’ 
newgox.c: At top level: 
newgox.c:2583: error: expected declaration specifiers or ‘...’ before ‘PyArrayObject’ 
newgox.c: In function ‘__pyx_f_6newgox_liberty’: 
newgox.c:2610: error: ‘__pyx_v_board’ undeclared (first use in this function) 
newgox.c: At top level: 
newgox.c:2859: error: expected ‘)’ before ‘*’ token 
newgox.c: In function ‘__pyx_pf_5numpy_7ndarray___getbuffer__’: 
newgox.c:2999: error: ‘PyArray_Descr’ undeclared (first use in this function) 
newgox.c:2999: error: ‘__pyx_v_descr’ undeclared (first use in this function) 
newgox.c:3062: error: ‘PyArrayObject’ undeclared (first use in this function) 
newgox.c:3062: error: expected expression before ‘)’ token 
newgox.c:3071: error: ‘npy_intp’ undeclared (first use in this function) 
newgox.c:3114: error: expected expression before ‘)’ token 
newgox.c:3114: error: ‘NPY_C_CONTIGUOUS’ undeclared (first use in this function) 
newgox.c:3154: error: expected expression before ‘)’ token 
newgox.c:3154: error: ‘NPY_F_CONTIGUOUS’ undeclared (first use in this function) 
newgox.c:3184: error: expected expression before ‘)’ token 
newgox.c:3184: warning: assignment makes pointer from integer without a cast 
newgox.c:3240: error: expected expression before ‘)’ token 
newgox.c:3240: error: subscripted value is neither array nor pointer 
newgox.c:3249: error: expected expression before ‘)’ token 
newgox.c:3249: error: subscripted value is neither array nor pointer 
newgox.c:3262: error: expected expression before ‘)’ token 
newgox.c:3271: error: expected expression before ‘)’ token 
newgox.c:3291: error: expected expression before ‘)’ token 

और कुछ और:

है तो मुझे इस बताता है।

सूचना यह कहते हैं:

newgox.c:2160: error: too many arguments to function ‘__pyx_f_6newgox_liberty’ 
newgox.c:2420: error: too many arguments to function ‘__pyx_f_6newgox_check_life’ 

लेकिन स्कोर से मेरे कॉल/1 check_life के लिए है:

life, newly_checked = check_life(i,j,board,[]) 

और यह इस तरह प्राप्त होता है:

# helper functions of score/1 
cdef check_life(int i, int j, np.ndarray[int, ndim=2] board, checked): 
    ... 
    return life, checked 

अंत में, क्या किस तरह का डेटा प्रकार "i4" है जिसका आप उपयोग करते हैं?

उत्तर

14

मुझे लगता है कि साइथन में tuple unpacking काम करता है जैसे यह पायथन में करता है। आपको (board.shape[0], board.shape[1]) का उपयोग करके checked का आकार निर्दिष्ट करना होगा।

आपको checked के डेटाटाइप को भी निर्दिष्ट करने की आवश्यकता है। डिफ़ॉल्ट रूप से np.zerosfloat64 देता है। घोषित प्रकार के साथ संगत होने के लिए आपको int32 पर इसे बदलने की आवश्यकता है।

यहां एक संशोधित संस्करण है जो चलाना चाहिए।

def score(np.ndarray[int, ndim=2] board): 

    cdef np.ndarray[np.int32_t, ndim = 2] checked 
    checked = np.zeros((board.shape[0], board.shape[1]), dtype='i4') 

    for i in xrange(len(board)): 
     for j in xrange(len(board)): 
      if checked[i,j] == 0 and board[i,j] !=0: 
       ... do stuff 

अपने प्रश्न के दूसरे भाग का उत्तर देने के लिए - हाँ आप साइथन कार्यों के भीतर सूचियों में शामिल हो सकते हैं। हालांकि, जैसा कि मैं समझता हूं कि इसमें पायथन फ़ंक्शन कॉल शामिल हैं, इसलिए यह पायथन के भीतर समान संचालन से अधिक तेज़ नहीं होगा।

अद्यतन 1:

मेरा मानना ​​है कि उन त्रुटियों रहे हैं, क्योंकि संकलक NumPy हेडर फाइल नहीं मिल रहा। उन फ़ाइलों के पथ को शामिल करने के लिए अपना setup.py अपडेट करें। पथ np.get_include() फ़ंक्शन द्वारा आउटपुट होना चाहिए।

import numpy as np 
from distutils.core import setup 
from distutils.extension import Extension 
from Cython.Distutils import build_ext 

setup(
     cmdclass={'build_ext': build_ext}, 
     ext_modules = [ 
       Extension("new_gox", 
         ["new_gox.pyx"], 
         include_dirs=[np.get_include()]) 
        ] 
    ) 

dtype i4 एक 32 बिट पूर्णांक है। यह np.int32 के बराबर है।

मुझे यकीन नहीं है कि 'बहुत अधिक तर्क' त्रुटियों के साथ क्या चल रहा है। यदि आप उस फ़ंक्शन को पायथन के भीतर से कॉल कर रहे हैं, तो इसे def या cpdef के रूप में घोषित करने की आवश्यकता है। cdef के साथ घोषित कार्यों को केवल सिथॉन से ही बुलाया जा सकता है। साइथन डॉक्स में यह here वर्णित है।

ctypedef np.npy_intp SIZE_t    # Type for indices and counters 

स्रोत:

1

np.intp के रूप में (शून्य *)

इस तरह अपने प्रकार को परिभाषित ही है

NPY_INTP == The enumeration value for a signed integer type which is the same size as a (void *) pointer. This is the type used by all arrays of indices.

cython में उदाहरण के लिए

तो ...

शीर्षलेख फ़ाइल में संरचना एचटी फ़ाइल:

struct Cudamat_Struct1 { 
    float * dataHost; 
    float * dataDevice; 
    int onDevice; 
    int onHost; 
    (void *) shape; 
    int size; 
    int isTrans; // 0 or 1 
    int ownsData; 

}; 

cython file.pxd फ़ाइल में:

cdef extern from "file.h": 
    cdef cppclass c_Cudamat_Struct "Cudamat_Struct": 
     float *dataHost 
     float *dataDevice 
     bint onDevice 
     bint onHost 
     SIZE_t * shape 
     int size 
     bint isTrans 
     bint ownsData 

अंत में cython समारोह में:

cdef pycublas_thrust_2d(): 
    cdef c_Cudamat_Struct1* mat = new c_Cudamat_Struct1() 

    cdef int N = 10 

    cdef np.ndarray[ndim=2, dtype=F32t] arrHost = np.ones((1,10),dtype=F32) 

    cdef int size = 10 

    cdef F32t[:,:] arrHost_ = arrHost 

    cdef F32t * arrHostPtr = &arrHost[0,0] 

    mat.dataHost = arrHostPtr 
    mat.shape = arrHost.shape 
    mat.size = size 
संबंधित मुद्दे