Section stack overflow

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

Moderator: hines

Post Reply
ssalgado
Posts: 6
Joined: Mon Apr 28, 2014 11:48 am

Section stack overflow

Post by ssalgado »

Hi. I'm running a simulation to see how some parameters affect the output of a neuron. I wrote the simulation code in python under a def statement simulation().
However, when I run the simulation multiple times on the same script I get a section stack overflow error. I've tried unsuccessfully to delete the created section or something like that.
I create the neuron soma with:

Code: Select all

soma = h.Section()
    h.pt3dclear()
    h.pt3dadd(-5,0,0,10)
    h.pt3dadd(5,0,0,10)
    soma.nseg = 1
    soma.Ra = 200
    soma.insert('pas')
And then 4 dendrites with:

Code: Select all

dend = [h.Section() for i in range(ndend)]
    for i in dend:
        i.push()
        i.connect(soma, 0, 0)
        i.nseg = npoints
        i.Ra = 200
        i.insert('pas')
        i.insert('ds')
        i.e_pas = -70
        i.g_pas = 0.00004
        
I think every time I call the function simulation(), new sections are created, so when I run it multiple times I get the stack overflow. Is there any way to clear the sections to prevent this? I've been looking other post and tried delete_section() with no results. I also tried soma = None, dend = None and so on. In addition, I tried to h.pop_section() after the statements that create every section, but my simulation gives wrong results.
Any advice?
Thanks.
ramcdougal
Posts: 267
Joined: Fri Nov 28, 2008 3:38 pm
Location: Yale School of Public Health

Re: Section stack overflow

Post by ramcdougal »

Your problem doesn't lie in creating the sections; it lies in "i.push()".

You're pushing "i" onto the section stack without ever popping anything from the section stack, so the stack grows and grows.

In my opinion, one should never use Section.push. In your specific case, none of your code accesses the section stack. If you were to use a function that did depend on the section stack, like h.pt3dadd, you can explicitly set the section for that line with the sec= keyword argument, e.g. h.pt3dadd(1, 2, 3, 4, sec=soma).
oren
Posts: 55
Joined: Fri Mar 22, 2013 1:03 am

Re: Section stack overflow

Post by oren »

Just add a h.pop_section() after you finish with the section you pushed [push()] and the stack overflow will disappear

Code: Select all

dend = [h.Section() for i in range(ndend)]
    for i in dend:
        i.push()
        i.connect(soma, 0, 0)
        i.nseg = npoints
        i.Ra = 200
        i.insert('pas')
        i.insert('ds')
        i.e_pas = -70
        i.g_pas = 0.00004
        h.pop_section()
ted
Site Admin
Posts: 6289
Joined: Wed May 18, 2005 4:50 pm
Location: Yale University School of Medicine
Contact:

Re: Section stack overflow

Post by ted »

oren wrote:Just add a h.pop_section() after you finish with the section you pushed [push()] and the stack overflow will disappear
True, but none of the statements after i.push() get any benefit out of the fact that i was pushed onto the section stack. The best fix is to omit i.push() in the first place.
ssalgado
Posts: 6
Joined: Mon Apr 28, 2014 11:48 am

Re: Section stack overflow

Post by ssalgado »

Well thanks for the replies. I've already tried the solutions mentioned above. I don't get a stack overflow, but the voltage traces are far from what I expected. I'll share the piece of code that creates the neuron and gives the right voltage traces.

Code: Select all

Generacion de la SAC
    #Soma
    soma = h.Section()
    soma.push()
    h.pt3dclear()
    h.pt3dadd(-5,0,0,10, sec=soma)
    h.pt3dadd(5,0,0,10, sec=soma)
    soma.nseg = 1
    soma.Ra = 200
    soma.insert('pas')
        
    #Dendritas
    dend = [h.Section() for i in range(ndend)]
    Isyn = []
    for i in dend:
        i.push()
        i.connect(soma, 0, 0)
        i.nseg = npoints
        i.Ra = 200
        i.insert('pas')
        i.insert('ds')
        i.e_pas = -70
        i.g_pas = 0.00004
                
    Isyn=[h.ContSynapse(ra, sec=dend[nd]) for (nd,ra) in zip(numdend,raux)]
        
    #Orden espacial de las dendritas
    for i in range(ndend):
        h.pt3dclear()
        theta = 2*np.pi*i/float(ndend)
        h.pt3dadd(0,0,0,diam_max,sec=dend[i])
        h.pt3dadd(ldend*np.cos(theta),ldend*np.sin(theta),0,diam_min,sec=dend[i])
        
    #Agregar las conexiones postsinápticas
    syn = [h.Section() for i in range(2)]
    GABAsyn = []
    for i in syn:
        i.push()
        i.nseg = 1
        i.Ra = 200
        i.insert('pas')  
        i.e_pas = -60
        i.g_pas = 0.00004
                
    GABAsyn = [h.GABAsyn(0.9, sec=dend[i]) for i in [0,2]]
        
    h.setpointer(dend[0](0.9)._ref_v, 'vpre', GABAsyn[0])
    h.setpointer(dend[2](0.9)._ref_v, 'vpre', GABAsyn[1])
Ok this code works perfectly but has the stack overflow issue. So I tried:
First, add h.pop_section() after creating the section. Result, no stack overflow, wrong voltage traces.
Then I avoided completely the section.push() and used the keyword sec=secname in h.pt3dadd. Same results.

So, I'm open to suggestions. Does anybody know where can I read about how does the section stack works? The documentation I've found so far doesn't really explains how the thing is working.
Thanks :)
ramcdougal
Posts: 267
Joined: Fri Nov 28, 2008 3:38 pm
Location: Yale School of Public Health

Re: Section stack overflow

Post by ramcdougal »

Replace the first h.pt3dclear() with h.pt3dclear(sec=soma) and the second with h.pt3dclear(sec=i), then you should be able to remove the "push" calls.

The section stack is what provides HOC it's ability to say things like

Code: Select all

soma {
    L = 10
    nseg = 11
}
It's used exactly when something depends on the currently accessed section.

The section stack is not a very Pythonic concept. For clearer code that's free of section stack errors and unambiguous about what sections are being operated on, never use sec.push or h.pop_section; always specify a sec= instead.
ssalgado
Posts: 6
Joined: Mon Apr 28, 2014 11:48 am

Re: Section stack overflow

Post by ssalgado »

Great, specifying sec=soma worked smoothly.
Also, the article regarding pythonic was quite interesting. I'm new to programming and I realize that is a little hard for me to think in a non-pythonic way, so I get a hard time understanding HOC code.
Thanks :)
ted
Site Admin
Posts: 6289
Joined: Wed May 18, 2005 4:50 pm
Location: Yale University School of Medicine
Contact:

Re: Section stack overflow

Post by ted »

ssalgado wrote:little hard for me to think in a non-pythonic way
Cognitive inflexibility is a common sign of encephalopathy. Looks like BASIC isn't the only programming language that can cause brain damage. One hears about "wet brain" in alcoholics and cognitive impairment in potheads. Could the popularity of Python be producing a generation of victims of "snake brain syndrome"?
Post Reply