Cython: creating an array throws “not allowed in a

2020-04-16 03:42发布

问题:

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

it throws the following error

    cdef int numSamples = len(Iext);

    # initial values
    cdef float v[numSamples]
                      ^
    ------------------------------------------------------------

    hh_vers_vector.pyx:47:27: Not allowed in a constant expression

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

回答1:

cdef in Cython is for C definitions, as the name implies. Therefore the code

cdef float v[10000]

is translated to the following C code

float v[10000];

Meaning a static float array of size 10k, defined at compile time.

This, on the other hand

cdef float v[numSamples]

Would translate to the C code

int numSamples = <..>;
float v[numSamples];

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

Either use a python list to store the floats, or dynamically allocate C arrays via malloc/free.