h.FInitializeHandler Crashing

When Python is the interpreter, what is a good
design for the interface to the basic NEURON
concepts.

Moderator: hines

Post Reply
bscoventry

h.FInitializeHandler Crashing

Post by bscoventry »

Hi all,

For cluster purposes, I'm currently porting all my MATLAB/NEURON code to Python. I seem to have run into a snag with the h.FInitializeHandler step. I am simply trying to load spike times into a target cell.
Beginning with loading of AMPA, NMDA, and GABA conductances:

Code: Select all

 for iii in range(numE):
        if drvinputsE.any():
            rpE.append(h.Vector())             
            rpE[iii].from_python(drvinputsE)                  #Precalculated spike times
            numspiking = rpE[iii].size()
            aD.append(h.AMPA_LL_IC(0.5))
            aD[iii].tau1 = ampatau1                              #AMPA time constants
            aD[iii].tau = ampatau2
            nctempA = h.NetCon(h.nil,aD[iii],0,1,ampaconduct)
            h.nclistA.append(nctempA)
            bD.append(h.NMDA_LL_IC_2(0.5))
            bD[iii].vdepscale = nmdavdep
            bD[iii].tau1 = nmdatau1
            bD[iii].tau = nmdatau2
            nctempB = h.NetCon(h.nil,bD[iii],0,1,nmdaconduct)
            h.nclistB.append(nctempB)
    for iiii in range(numI):
        if drvIinputs.any():
            rpI.append(h.Vector())
            rpI[iiii].from_python(drvIinputs)
            numspikingI = rpI[iiii].size()
            cD.append(h.ICGABAa(0.5))
            cD[iiii].tau1 = gabatau1
            cD[iiii].tau = gabatau2
            cD[iiii].scale = gscale
            nctempC = h.NetCon(h.nil,cD[iiii],0,1,gabaaconduct)
            h.nclistC.append(nctempC)
            dD.append(h.ICGABAb3(0.5))
            nctempD = h.NetCon(h.nil,dD[iiii],0,1,gababconduct)
            h.nclistD.append(nctempD)
fih2 = h.FInitializeHandler(loadspikes(numE,numI,rpE,rpI,h.nclistA,h.nclistB,h.nclistC,h.nclistD))
The issue arises at fih2 = h.FInitializeHandler(loadspikes(numE,numI,rpE,rpI,h.nclistA,h.nclistB,h.nclistC,h.nclistD))

with loadspikes

Code: Select all

def loadspikes(numE,numI,rpE,rpI,nclistA,nclistB,nclistC,nclistD):
    from neuron import *
    from nrn import *
    import numpy as np
    import pdb
    pdb.set_trace()
    for jj in range(numE):
        if len(rpE)>0:
            numss = len(rpE[jj])
            for ii in range(numss):
                h.nclistA.o(jj).event((rpE[jj].x[ii]+200))
                h.nclistB.o(jj).event((rpE[jj].x[ii]+200))
    for nn in range(numI):
        if len(rpI)>0:
            numsI = len(rpI[jj])
            for mm in range(numsI):
                h.nclistC.o(nn).event((rpI[nn].x[mm]+200))
                h.nclistD.o(nn).event((rpI[nn].x[mm]+200))
Stepping through loadspikes with python debugger ('pbd') works fine. However, after reaching h.FinitializeHandler the whole python terminal crashes. Is there a specific variable which needs to be passed in from load spikes or something I'm not seeing?

Thank you for your time,
Brandon
hines
Site Admin
Posts: 1687
Joined: Wed May 18, 2005 3:32 pm

Re: h.FInitializeHandler Crashing

Post by hines »

You are right.
fih = h.FInitializeHandler(None)
gives a segmentation violation. I'll have to add a check that the arg is a vallid callback. Anyway, I doubt that you had meant to pass the return value of loadspikes into the FInitializeHandler anyway so try

Code: Select all

 fih2 = h.FInitializeHandler((loadspikes, (numE,numI,rpE,rpI,h.nclistA,h.nclistB,h.nclistC,h.nclistD)))
Note that the argtuple of the FInitializeHandler tuple argument (callable, argtuple) will be the constant value using all those variable values when the tuple is argtuple is created. So if you change any of those variable values after calling the h.FInitializeHandler, the eventual call to load_spikes(*argtuple) will use the original values, not the modified values. That is, make sure all those variables have their
proper values before the call to FInitializeHandler.

By the way, the allowed python args for FInitializeHandler have the form (true also for the dozen or so other hoc functions that take python callbacks)
h.FInitializHander(callable)
h.FinitializeHandler((callable, arg1))
h. FInitializaHandler((callable, (arg1, arg2, arg3, ...)))
bscoventry

Re: h.FInitializeHandler Crashing

Post by bscoventry »

Dr. Hines,

Fantastic thank you, I'll give this a shot and report back.

Brandon
bscoventry

Re: h.FInitializeHandler Crashing

Post by bscoventry »

Hi all,

So I tried the fix but now I'm running into another kind of hard to crack error. Running

Code: Select all

fih2 = h.FInitializeHandler(loadspikes, (numE,numI,rpE,rpI,h.nclistA,h.nclistB,h.nclistC,h.nclistD))
Executes just fine. However, something is wrong as h.finitialize throws a HOC runtime error. Running

Code: Select all

fih2 = h.FInitializeHandler(loadspikes)
crashes the system entirely. I know that this should fail, seeing as I have no inputs, but for it to completely crash is a bit strange. Weirdly, loadspikes itself will run with artificially generated inputs using the same methods that my control code generates these inputs with.

My thoughts are that I may be loading spike events in a weird way which causes this strange behavior. Is there a way to check events? The other weird part is that I placed a break point within the load spikes code, which previously would jump into the function when FInitializeHandler is called, but no longer does that. It seems that FInitializeHandler is not even running the program. For reference, here is the load spikes code:

Code: Select all

def loadspikes(numE,numI,rpE,rpI,nclistA,nclistB,nclistC,nclistD):
    from neuron import *
    from nrn import *
    import numpy as np
    import pdb
    pdb.set_trace()
    numE = 1                         #Redundant I know, but for test purposes, sure.
    numI = 1
    for jj in range(numE):
        if len(rpE)>0:
            numss = len(rpE[jj])
            for ii in range(numss):
                h.nclistA.o(jj).event((rpE[jj].x[ii]+200))
                h.nclistB.o(jj).event((rpE[jj].x[ii]+200))
    for nn in range(numI):
        if h.rpI.size()>0:
            numsI = h.rpI[jj].size()
            for mm in range(numsI):
                h.nclistC.o(nn).event((rpI[nn].x[mm]+200))
                h.nclistD.o(nn).event((rpI[nn].x[mm]+200))
hines
Site Admin
Posts: 1687
Joined: Wed May 18, 2005 3:32 pm

Re: h.FInitializeHandler Crashing

Post by hines »

You gave two args to FInitialize instead of a single tuple argument. As mentioned above:

Code: Select all

h. FInitializaHandler((callable, (arg1, arg2, arg3, ...)))
So try

Code: Select all

fih2 = h.FInitializeHandler((loadspikes, (numE,numI,rpE,rpI,h.nclistA,h.nclistB,h.nclistC,h.nclistD)))
The implementation of FInitializeHandler only takes a single hoc PythonObject, so you encode the callable and its args into a single tuple.
I should look into adding more python argument and python return value error checking for python callbacks.
bscoventry

Re: h.FInitializeHandler Crashing

Post by bscoventry »

Perfect, works great now! Thank you for all your help!

Brandon
Post Reply