2017-07-17 63 views
-1

我正在嘗試編寫一個cython函數,該函數可以從使用c gsl庫來計算spearman相關性和使用t分佈的相應p值的python腳本調用。我不成功.pyx文件如下:如何在cython中使用gsl

import numpy as np 
import numpy as np 

def spearmanr(cdef double v1, cdef double v2, cdef int N): 

    cdef extern from "gsl/gsl_statistics_double.h": 
     double gsl_stats_spearman(double data1[],size_t stride1,double data2[],size_t stride2, size_t n) 

    cdef int strides = 1 
    cdef int n = N 

    cdef double r = gsl_stats_spearmanr(v1,strides,v2,strides,n) 

    cdef double tstat=r*((n-2)/(1-r**2))**0.5 

    cdef extern from "gsl/gsl_ranhist.h": 
     double gsl_cdf_tdist_Q(double x, double nu) 

    cdef double nu = N %% Do I need to Type Cast? 
    cdef double pval=gsl_cdf_tdist_Q(stat,nu) 

    return r,pal 

當我嘗試編譯此我得到以下錯誤:

running build_ext 
cythoning spear_coxen.pyx to spear_coxen.c 

Error compiling Cython file: 
------------------------------------------------------------ 
... 
import numpy as np 
cimport numpy as np 

def spermanr(cdef double v1, cdef double v2, cdef int N): 
     ^
------------------------------------------------------------ 

spear_coxen.pyx:4:13: Expected an identifier, found 'cdef' 

Error compiling Cython file: 
------------------------------------------------------------ 
... 
import numpy as np 
cimport numpy as np 

def spermanr(cdef double v1, cdef double v2, cdef int N): 
        ^
------------------------------------------------------------ 

spear_coxen.pyx:4:25: Expected ')', found 'v1' 
building 'spear_r' extension 
gcc -pthread -fno-strict-aliasing -g -O2 -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -fPIC -I/usr/local/Anaconda2-2.5.0/envs/python27/include/python2.7 -c spear_coxen.c -o build/temp.linux-x86_64-2.7/spear_coxen.o 
spear_coxen.c:1:2: error: #error Do not use this file, it is the result of a failed Cython compilation. 
error: command 'gcc' failed with exit status 1 

我真的不知道,如果任何的,這是正確的語法,因爲我可以沒有發現搜索沒有產生什麼,但微不足道的例子。如果有人能提供幫助,應該非常感激。謝謝

回答

0

你不需要把cdef放在你的函數參數中。即

def spearmanr(cdef double v1, cdef double v2, cdef int N): 

應該僅僅是:

cpdef spearmanr(double v1, double v2, int N): 

並移動函數聲明上方的GSL的聲明,而不是裏面的吧。然後它可以訪問你聲明的函數,例如

cdef extern from "gsl/gsl_statistics_double.h": 

    double gsl_stats_spearman(double data1[],size_t stride1,double data2[],size_t stride2, size_t n) 

與其他extern一樣。

而且你的聲明看起來不正確的(我不是在尋找在GSL文檔),但如果double data1[]你的意思是這是一個數組應該double[:,:] data1或爲NumPy的memoryview矢量double[:] data1。讓我知道,如果它不這樣做後編譯data1data2變量聲明爲...