Enable/Disable NetStim objects at run time

Anything that doesn't fit elsewhere.
Post Reply
raghu.sesha
Posts: 1
Joined: Wed Nov 18, 2020 8:41 pm

Enable/Disable NetStim objects at run time

Post by raghu.sesha »

I have a simple two cell NEURON model with the first cell receiving an external stimuli in the form of NetStim.
I have two NetStim objects, one with a .start set to 0 and another with .start set to 50. When I run the model, I see two sets of spikes, one starting from 0ms and another from 50ms as expected.

Code: Select all


from neuron import h
import neuron
import numpy as np
import matplotlib.pyplot as plt


class HHNeuron():
    def __init__(self):
        self.soma = h.Section()
        self.ap_dend = h.Section()
        self.soma.L = 30
        self.soma.diam = 30
        self.soma.nseg = 1
        self.soma.insert('hh')
        self.ap_dend.L = 500
        self.ap_dend.diam = 2
        self.ap_dend.nseg = 23
        self.ap_dend.insert('hh')
        self.ap_dend.gnabar_hh = 0.012
        self.ap_dend.gkbar_hh = 0.0036
        self.ap_dend.gl_hh = 0.00003
        self.ap_dend.connect(self.soma,1,0)
        self.esyn = h.Exp2Syn(0.5,sec=self.ap_dend)
        self.esyn.tau1 = 0.5
        self.esyn.tau2 = 1.0
        self.esyn.e = 0

tstop = 100
v_init = -65.0
hh_neuron = [HHNeuron() for i in range(2)]

stim1 = h.NetStim(0.5)
stim1.interval = 2
stim1.number = 20
stim1.start = 0
stim1.noise = 0
stim2 = h.NetStim(0.5)
stim2.interval = 2
stim2.number = 20
stim2.start = 50
stim2.noise = 0
nclist = []
nclist.append(h.NetCon(stim1, hh_neuron[0].esyn, 0.0, 0, 0.02))
nclist.append(h.NetCon(stim2, hh_neuron[0].esyn, 0.0, 0, 0.02))
nclist.append(h.NetCon(hh_neuron[0].soma(0.5)._ref_v, hh_neuron[1].esyn,10,1,0.02))

tv = h.Vector()
vs0 = h.Vector()
vs1 = h.Vector()
tv.record(h._ref_t)
vs0.record(hh_neuron[0].soma(0.5)._ref_v)
vs1.record(hh_neuron[1].soma(0.5)._ref_v)

h.finitialize(v_init)
h.fcurrent()
while h.t < tstop:
    neuron.run(h.t+0.05)

ax = plt.subplot()
ax.plot(tv.as_numpy(),vs0.as_numpy())
ax.plot(tv.as_numpy(),vs1.as_numpy())
plt.xlabel('Time(ms)')
plt.ylabel('Potential (mV)')
plt.savefig('test.png')
plt.show()
Now, my requirement is to decide at run time whether I want to turn on the second NetStim or not. So, at t=40ms, I call the function set_ees_input to decide if the NetStim2 should be enabled. For this, I initialize the .start of NetStim2 to -1 in the beginning and then set it to 50 in the function (which is called at t=40ms). However, with this, I don't see the second set of spikes at t=50 ms

Code: Select all

from neuron import h
import neuron
import numpy as np
import matplotlib.pyplot as plt

def set_ees_input(nowt):
    stim2.start = nowt+10

class HHNeuron():
    def __init__(self):
        self.soma = h.Section()
        self.ap_dend = h.Section()
        self.soma.L = 30
        self.soma.diam = 30
        self.soma.nseg = 1
        self.soma.insert('hh')
        self.ap_dend.L = 500
        self.ap_dend.diam = 2
        self.ap_dend.nseg = 23
        self.ap_dend.insert('hh')
        self.ap_dend.gnabar_hh = 0.012
        self.ap_dend.gkbar_hh = 0.0036
        self.ap_dend.gl_hh = 0.00003
        self.ap_dend.connect(self.soma,1,0)
        self.esyn = h.Exp2Syn(0.5,sec=self.ap_dend)
        self.esyn.tau1 = 0.5
        self.esyn.tau2 = 1.0
        self.esyn.e = 0

tstop = 100
v_init = -65.0
hh_neuron = [HHNeuron() for i in range(2)]

stim1 = h.NetStim(0.5)
stim1.interval = 2
stim1.number = 20
stim1.start = 0
stim1.noise = 0
stim2 = h.NetStim(0.5)
stim2.interval = 2
stim2.number = 20
stim2.start = -1
stim2.noise = 0
nclist = []
nclist.append(h.NetCon(stim1, hh_neuron[0].esyn, 0.0, 0, 0.02))
nclist.append(h.NetCon(stim2, hh_neuron[0].esyn, 0.0, 0, 0.02))
nclist.append(h.NetCon(hh_neuron[0].soma(0.5)._ref_v, hh_neuron[1].esyn,10,1,0.02))

tv = h.Vector()
vs0 = h.Vector()
vs1 = h.Vector()
tv.record(h._ref_t)
vs0.record(hh_neuron[0].soma(0.5)._ref_v)
vs1.record(hh_neuron[1].soma(0.5)._ref_v)

h.finitialize(v_init)
h.fcurrent()
stim2_set = False
while h.t < tstop:
    if stim2_set == False and h.t > 40.0:
        set_ees_input(40.0)
        stim2_set = True
    neuron.run(h.t+0.05)

ax = plt.subplot()
ax.plot(tv.as_numpy(),vs0.as_numpy())
ax.plot(tv.as_numpy(),vs1.as_numpy())
plt.xlabel('Time(ms)')
plt.ylabel('Potential (mV)')
plt.savefig('test.png')
plt.show()
I also tried creating the NetStim2 object itself when the function is called (instead of setting the .start to -1 and then changing it). This too, resulted in no spikes seen at t=50ms.

Code: Select all

from neuron import h
import neuron
import numpy as np
import matplotlib.pyplot as plt

def set_ees_input(nowt):
    global nclist
    stim2 = h.NetStim(0.5)
    stim2.interval = 2
    stim2.number = 20
    stim2.start = nowt+10
    stim2.noise = 0
    nclist.append(h.NetCon(stim2, hh_neuron[0].esyn, 0.0, 0, 0.02))


class HHNeuron():
    def __init__(self):
        self.soma = h.Section()
        self.ap_dend = h.Section()
        self.soma.L = 30
        self.soma.diam = 30
        self.soma.nseg = 1
        self.soma.insert('hh')
        self.ap_dend.L = 500
        self.ap_dend.diam = 2
        self.ap_dend.nseg = 23
        self.ap_dend.insert('hh')
        self.ap_dend.gnabar_hh = 0.012
        self.ap_dend.gkbar_hh = 0.0036
        self.ap_dend.gl_hh = 0.00003
        self.ap_dend.connect(self.soma,1,0)
        self.esyn = h.Exp2Syn(0.5,sec=self.ap_dend)
        self.esyn.tau1 = 0.5
        self.esyn.tau2 = 1.0
        self.esyn.e = 0

tstop = 100
v_init = -65.0
hh_neuron = [HHNeuron() for i in range(2)]

stim1 = h.NetStim(0.5)
stim1.interval = 2
stim1.number = 20
stim1.start = 0
stim1.noise = 0

nclist = []
nclist.append(h.NetCon(stim1, hh_neuron[0].esyn, 0.0, 0, 0.02))
nclist.append(h.NetCon(hh_neuron[0].soma(0.5)._ref_v, hh_neuron[1].esyn,10,1,0.02))

tv = h.Vector()
vs0 = h.Vector()
vs1 = h.Vector()
tv.record(h._ref_t)
vs0.record(hh_neuron[0].soma(0.5)._ref_v)
vs1.record(hh_neuron[1].soma(0.5)._ref_v)

h.finitialize(v_init)
h.fcurrent()
stim2_set = False
while h.t < tstop:
    if stim2_set == False and h.t > 40.0:
        set_ees_input(40.0)
        stim2_set = True
    neuron.run(h.t+0.05)

ax = plt.subplot()
ax.plot(tv.as_numpy(),vs0.as_numpy())
ax.plot(tv.as_numpy(),vs1.as_numpy())
plt.xlabel('Time(ms)')
plt.ylabel('Potential (mV)')
plt.savefig('test.png')
plt.show()
Question: How do I setup the NetStims that I provide to my NEURON model so that I can enable/disable them at run time (or change properties such as .interval at run time)
ted
Site Admin
Posts: 5895
Joined: Wed May 18, 2005 4:50 pm
Location: Yale University School of Medicine
Contact:

Re: Enable/Disable NetStim objects at run time

Post by ted »

Good question.

Before dealing with it--

Instead of
from neuron import h
import neuron
do this:
from neuron import h
h.load_file("stdgui.hoc")

import h gives Python access to everything built into the NEURON executable (the program that is launched by executing
nrniv
at the command line).

Probably the single most important thing that h.load_file("stdgui.hoc") does: it makes NEURON's standard run library (which is not part of the executable) available.

Your homebrew code for simulation flow control

Code: Select all

h.finitialize(v_init)
h.fcurrent()
while h.t < tstop:
    neuron.run(h.t+0.05)
works but requires more code than necessary. The standard run library's h.run() does initialization and launches a simulation much more compactly:

h.v_init = v_init
h.run()

Now for your question.

Don't set start to values < 0.

Best way to "Disable" a NetStim is set its number to 0.

To change any NetStim parameter you like at run time, you could try an FInitializeHandler with type set to 3.
Post Reply