Cython:创建数组会抛出“常量表达式中不允许". [英] Cython: creating an array throws "not allowed in a constant expression"
问题描述
我尝试将复杂的Python函数重写为Cython,以大幅度提高它的速度,但遇到以下问题:在使用
I try to rewrite a complex function form Python to Cython to speed it up massively and I encounter the following problem: while compiling my function hh_vers_vector.pyx using
setup(
ext_modules=cythonize("hh_vers_vector.pyx"),
)
它会引发以下错误
cdef int numSamples = len(Iext);
# initial values
cdef float v[numSamples]
^
------------------------------------------------------------
hh_vers_vector.pyx:47:27: Not allowed in a constant expression
但是,如果我将"numSamples"作为数字输入该函数,则没有问题.我不明白,因为我认为len(Iext)也会返回数字10.000.那么,为什么Cython的表达式有问题?
If, however, I give "numSamples" as a number into the function, there is no problem. I don't understand that, because I thought that len(Iext) will return a number as 10.000 as well. So why does Cython have a problem with this expression?
cdef float v[numSamples] # ERROR
cdef float v[10000] # NO ERROR
到目前为止,我的功能是这样的:
My function looks like this so far:
from math import exp
import time
def hhModel(*params, Iext, float dt, int Vref):
## Unwrap params argument: these variables are going to be optimized
cdef float ENa = params[0]
cdef float EK = params[1]
cdef float EL = params[2]
cdef float GNa = params[3]
cdef float GK = params[4]
cdef float GL = params[5]
## Input paramters
# I : a list containing external current steps, your stimulus vector [nA/1000]
# dt : a crazy time parameter [ms]
# Vref : reference potential [mV]
# T : Total simulation time in [ms]
def alphaM(float v, float vr): return 0.1 * (v-vr-25) / ( 1 - exp(-(v-vr-25)/10) )
def betaM(float v, float vr): return 4 * exp(-(v-vr)/18)
def alphaH(float v, float vr): return 0.07 * exp(-(v-vr)/20)
def betaH(float v, float vr): return 1 / ( 1 + exp( -(v-vr-30)/10 ) )
def alphaN(float v, float vr): return 0.01 * (v-vr-10) / ( 1 - exp(-(v-vr-10)/10) )
def betaN(float v, float vr): return 0.125 * exp(-(v-vr)/80)
## steady-state values and time constants of m,h,n
def m_infty(float v, float vr): return alphaM(v,vr) / ( alphaM(v,vr) + betaM(v,vr) )
def h_infty(float v, float vr): return alphaH(v,vr) / ( alphaH(v,vr) + betaH(v,vr) )
def n_infty(float v, float vr): return alphaN(v,vr) / ( alphaN(v,vr) + betaN(v,vr) )
## parameters
cdef float Cm, gK, gL, INa, IK, IL, dv_dt, dm_dt, dh_dt, dn_dt, aM, bM, aH, bH, aN, bN
cdef float Smemb = 4000 # [um^2] surface area of the membrane
cdef float Cmemb = 1 # [uF/cm^2] membrane capacitance density
Cm = Cmemb * Smemb * 1e-8 # [uF] membrane capacitance
gNa = GNa * Smemb * 1e-8 # Na conductance [mS]
gK = GK * Smemb * 1e-8 # K conductance [mS]
gL = GL * Smemb * 1e-8 # leak conductance [mS]
######### HERE IS THE PROBLEM ##############
cdef int numSamples = len(Iext);
######### HERE IS THE PROBLEM ##############
# initial values
cdef float v[numSamples]
cdef float m[numSamples]
cdef float h[numSamples]
cdef float n[numSamples]
v[0] = Vref # initial membrane potential
m[0] = m_infty(v[0], Vref) # initial m
h[0] = h_infty(v[0], Vref) # initial h
n[0] = n_infty(v[0], Vref) # initial n
## calculate membrane response step-by-step
for j in range(0, numSamples-1):
# ionic currents: g[mS] * V[mV] = I[uA]
INa = gNa * m[j]*m[j]*m[j] * h[j] * (ENa-v[j])
IK = gK * n[j]*n[j]*n[j]*n[j] * (EK-v[j])
IL = gL * (EL-v[j])
# derivatives
# I[uA] / C[uF] * dt[ms] = dv[mV]
dv_dt = ( INa + IK + IL + Iext[j]*1e-3) / Cm;
aM = 0.1 * (v[j]-Vref-25) / ( 1 - exp(-(v[j]-Vref-25)/10))
bM = 4 * exp(-(v[j]-Vref)/18)
aH = 0.07 * exp(-(v[j]-Vref)/20)
bH = 1 / ( 1 + exp( -(v[j]-Vref-30)/10 ) )
aN = 0.01 * (v[j]-Vref-10) / ( 1 - exp(-(v[j]-Vref-10)/10) )
bN = 0.125 * exp(-(v[j]-Vref)/80)
dm_dt = (1-m[j])* aM - m[j]*bM
dh_dt = (1-h[j])* aH - h[j]*bH
dn_dt = (1-n[j])* aN - n[j]*bN
# calculate next step
v[j+1] = (v[j] + dv_dt * dt)
m[j+1] = (m[j] + dm_dt * dt)
h[j+1] = (h[j] + dh_dt * dt)
n[j+1] = (n[j] + dn_dt * dt)
return v
顾名思义,Cython中的
推荐答案
cdef
用于C定义.因此,代码
cdef
in Cython is for C definitions, as the name implies. Therefore the code
cdef float v[10000]
被翻译成以下C代码
float v[10000];
表示大小为10k的 static 浮点数组,在编译时定义..
Meaning a static float array of size 10k, defined at compile time.
另一方面,
cdef float v[numSamples]
将翻译为C代码
int numSamples = <..>;
float v[numSamples];
这是尝试编译包含可变数量元素的静态数组,该数组不是有效的C.因此是常量表达式错误".
This is an attempt to compile a static array containing a variable number of elements, which is not valid C. Hence the 'constant expression error'.
要么使用python列表存储浮点数,要么通过malloc
/free
动态分配C数组.
Either use a python list to store the floats, or dynamically allocate C arrays via malloc
/free
.
这篇关于Cython:创建数组会抛出“常量表达式中不允许".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!