Cython:''NoneType'对象没有属性'shape'

时间:2017-10-23 19:17:43

标签: python numpy cython shape

我正在尝试将我的函数从Python转换为Cython以显着提高其速度。但是,如果我调用它,则会抛出此错误。我将向您展示一个简化的代码,只有有问题的部分。

编译cython文件

%run -i setup.py build_ext --inplace   

Error compiling Cython file:

hh_vers02.pyx:51:25: Compile-time name 'Iext' not defined
hh_vers02.pyx:51:25: Error in compile-time expression: AttributeError: 'NoneType' object has no attribute 'shape'

Cython文件(.pyx)

import numpy as np

def hhModel(Iext):
    DEF numSamples = Iext.shape[0]        # needs to be constant, that's why i used DEF
    cdef float v[numSamples]

    return v

创建“Iext”

# create stimulus vector
def create_stimulus_vector(nA, stimulus_length, zero_length, dt, plotflag):
    "create_stimulus_vector(2.5[nA], 1000[ms], 500[ms], 0.01[step/ms])"
    start           = int(zero_length*1/dt)                      #  5.000
    length          = int(stimulus_length*1/dt+2*start)          # 20.000
    stop            = length-start                               # 15.000  
    stimulus_vector = np.zeros(length)                           # array([ 0.,  0.,  0., ...,  0.,  0.,  0.])
    stimulus_vector[start:stop] = nA                             # array([ 0.,  0.,  1., ...,  1.,  0.,  0.])
    if plotflag:
        plt.plot(np.linspace(0, length*dt/1000, length), stimulus_vector)
        plt.title("One Stimulus Vector")
        plt.ylabel("[nA]")
        plt.xlabel("[s]")
    return stimulus_vector

Iext = create_stimulus_vector(2.5, 1000, 500, 0.01, 1);

我尝试了主要建议here的百万件事,包括以下备选方案,虽然没有改变错误。现在我非常绝望。

cimport numpy as np
DTYPE = np.int
ctypedef np.int_t DTYPE_t

1) def hhModel(np.ndarray Iext):
2) def hhModel(np.ndarray[DTYPE_t, ndim=1] Iext):
3) def hhModel(np.ndarray Iext):
       assert Iext.dtype == DTYPE

重新启动内核后错误仍然存​​在。我在Jupyter Notebook中使用Python 3.6.2和IPython 6.1.0(通过Anaconda安装)。我正在使用Windows 10。

0 个答案:

没有答案