how to add a current clamp in Python?

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

Moderator: hines

Post Reply
cassiehu
Posts: 25
Joined: Tue Sep 06, 2022 10:41 am

how to add a current clamp in Python?

Post by cassiehu »

I want to put a current clamp of a special form instead of a constant on a neuron. The change of the value of current should be like in the following picture.
https://pic.imgdb.cn/item/638b3d2d16f2c2beb1f72805.png
ted
Site Admin
Posts: 6101
Joined: Wed May 18, 2005 4:50 pm
Location: Yale University School of Medicine
Contact:

Re: how to add a current clamp in Python?

Post by ted »

The Vector class's play() method can be used to force the current delivered by an IClamp to follow an arbitrary waveform. However, if the waveform can be described by a function or algorithm, it might be easier to implement the function or algorithm in NMODL. The resulting point process would also be easier to use, and it would automatically work properly with NEURON's adaptive integrators. I mention this because the waveform in figure you provided looks like it could be described by an algorithm like this:

Code: Select all

Initialization: at t == 0 set current i to 0
During a simulation:
at each time step
  if an event with weight w arrives
    i = i * exp(-dt/tau)
    i = i + w
  else
    i = i * exp(-dt/tau)
If this is what you have in mind, let me know and I will be glad to implement it for you.
cassiehu
Posts: 25
Joined: Tue Sep 06, 2022 10:41 am

Re: how to add a current clamp in Python?

Post by cassiehu »

Thanks for your reply very much. You are right, the form of the curret trully is regular. We can treat it as an exponential form. Then could you introduce the method of the exponential form current to express it correctly in NEURON+ Python.
cassiehu
Posts: 25
Joined: Tue Sep 06, 2022 10:41 am

Re: how to add a current clamp in Python?

Post by cassiehu »

I noticed that we can write in a mod file, is it right? I am still learning how to describe mechanisms with NMODL.
ted
Site Admin
Posts: 6101
Joined: Wed May 18, 2005 4:50 pm
Location: Yale University School of Medicine
Contact:

Re: how to add a current clamp in Python?

Post by ted »

I am still learning how to describe mechanisms with NMODL.
Then it will save your time and mine if I just implement something that matches your needs. As bad as most user-written hoc or Python code may be, the sad truth is that most user-written NMODL code is even worse because of NMODL's peculiarities and the fact that very few people do enough programming in NMODL to develop any real competence. That may sound harsh, but reality is often harsh.

Here are two key questions.

First, are you sure that ExpSyn wouldn't do what you need? The current generated by an ExpSyn is
i = g * (v - e)
where v is local membrane potential, e is the ExpSyn's reversal potential, and g decays exponentially with time. If (v - e) is large and fluctuations of v are small, the time course of i can approximate a monoexponential decay very closely.

Second, is this thing that you want supposed to represent synaptic current that enters a cell through ion channels in the membrane, or is it supposed to represent a current that is injected into the cell through an electrode? Your answer is important because the sign convention for positive current flow depends on it.
cassiehu
Posts: 25
Joined: Tue Sep 06, 2022 10:41 am

Re: how to add a current clamp in Python?

Post by cassiehu »

OK, I totally understand. For the first question, we are not sure wether "If (v - e) is large and fluctuations of v are small" which means this expression may not be absolutely correct under some circumstances. However, it seems to be appropriate for the form that I need, so I can try it firstly.

For the second question, it should represent a current that is injected into the cell through an electrode because it is an input current stimulus acting on the sensory neuron of C.elegans.

I am really looking forward to your reply and to see how you address this problem. Thanks for your reply very much!
ted
Site Admin
Posts: 6101
Joined: Wed May 18, 2005 4:50 pm
Location: Yale University School of Medicine
Contact:

Re: how to add a current clamp in Python?

Post by ted »

it should represent a current that is injected into the cell through an electrode
So this really is to be similar to an IClamp. The name I chose for this particular mechanism is ExpIClamp because it delivers a current that decays monoexponentially with time. Its source code is in a file called expiclamp.mod.

The source code for ExpIClamp is below. In the NEURON block note that the current generated by an ExpIClamp is declared to be an ELECTRODE_CURRENT. A depolarizing ELECTRODE_CURRENT has positive sign. The alternative would be an "ionic current", which would be declared in a NONSPECIFIC_CURRENT statement if its effect on ionic concentration is to be ignored, or in a USEION statement so that it can affect the concentration of a particular ion.

Code: Select all

COMMENT
An event-driven current source that delivers an electrode current 
which decays monoexponentially with time.
Arrival of an event with weight w 
instantaneously perturbs the current by w nA.
For example, a single event with weight 0.1 
will elicit a single current pulse with peak amplitude 0.1 nA.

Developed by NTC based on ExpSyn.
ENDCOMMENT

NEURON {
        POINT_PROCESS ExpIClamp
        RANGE tau, i
        ELECTRODE_CURRENT i
}

UNITS {
        (nA) = (nanoamp)
}

PARAMETER {
        tau = 0.1 (ms) <1e-9,1e9>
}

ASSIGNED {
        i (nA)
}

STATE {
        ii (nA)
}

INITIAL {
        ii=0
}

BREAKPOINT {
        SOLVE state METHOD cnexp
        i = ii
}

DERIVATIVE state {
        ii' = -ii/tau
}

NET_RECEIVE(weight (nA)) {
        ii = ii + weight
}
cassiehu
Posts: 25
Joined: Tue Sep 06, 2022 10:41 am

Re: how to add a current clamp in Python?

Post by cassiehu »

Thank you very much!!!
I have learned your method while I still has a question about NET_RECEIVE module. I have known the information in the following picture from the book <Expanding NEURON’s Repertoire of Mechanisms with NMODL> that for synaptic model building the NET_RECEIVE module can be called by NetCon(). Then in this mod file, what should I write in Python code to give a value to weight. Is h.ExpIClamp.weight=2 a correct expression? Like in simple IClamp.

Secondly, if I want the current to have two decays which is shown in the following picture. What should I write in mod file, because its logic is like:
for t in [0,50ms]:
ii=exp(-t)+weight_1
for t in (50,100ms]:
ii=exp(-t)+weight_2(smaller than weight_1)
https://pic.imgdb.cn/item/63ca1669be43e0d30e46fea7.png
https://pic.imgdb.cn/item/638b3d2d16f2c2beb1f72805.png
https://pic.imgdb.cn/item/63ca169ebe43e0d30e477451.png
ted
Site Admin
Posts: 6101
Joined: Wed May 18, 2005 4:50 pm
Location: Yale University School of Medicine
Contact:

Re: how to add a current clamp in Python?

Post by ted »

in this mod file, what should I write in Python code to give a value to weight
Nothing. Do not change the NMODL code. The weight associated with an event is specified by the weight parameter of the NetCon that (1) detected the presynaptic spike and (2) delivers the event to the synaptic target mechanism. The value of a NetCon's weight parameter can be specified when the NetCon is created, or after the NetCon has been created. In the file initexpiclamp.hoc you will see that the weight was specified after the NetCon was created. The Python syntax for assigning weight to a NetCon is identical. See the documentation of the NetCon class in the Programmer's Reference.
if I want the current to have two decays
then come up with a set of ODEs or a kinetic scheme that will produce such a time course, and I'll show you how to implement those equations properly in NMODL. Do not try to use crude hacks, like logic statements (conditional statements or "if" statements) to switch between one time constant and another--the resulting code will not work properly.
cassiehu
Posts: 25
Joined: Tue Sep 06, 2022 10:41 am

Re: how to add a current clamp in Python?

Post by cassiehu »

and I'll show you how to implement those equations properly in NMODL.
Hi, thanks for your reply. I am really looking forward to your method about how to write in NMODL. Could you please tell me again? Thanks
ted
Site Admin
Posts: 6101
Joined: Wed May 18, 2005 4:50 pm
Location: Yale University School of Medicine
Contact:

Re: how to add a current clamp in Python?

Post by ted »

if I want the current to have two decays
then come up with a set of ODEs or a kinetic scheme that will produce such a time course, and I'll show you how to implement those equations properly in NMODL.
cassiehu
Posts: 25
Joined: Tue Sep 06, 2022 10:41 am

Re: how to add a current clamp in Python?

Post by cassiehu »

Hi, sorry that I am not fully understanding.
This is the mod file I tried and you said that do not use "if else" expression, then what should I write?
Plus, this stimuli has nothing to do with the synapse which means it does not have relationship with weight. So I changed a little, the number can just be a constant.

Code: Select all

COMMENT
An event-driven current source that delivers an electrode current which decays monoexponentially with time.
Arrival of an event with weight w instantaneously perturbs the current by w nA.
For example, a single event with weight 0.1 will elicit a single current pulse with peak amplitude 0.1 nA.

Developed by NTC based on ExpSyn.
ENDCOMMENT

NEURON {
        POINT_PROCESS ExpIClamp
        RANGE tau_1,tau_2, i
        ELECTRODE_CURRENT i
}

UNITS {
        (nA) = (nanoamp)
}

PARAMETER {
        tau_1 = 0.2 (ms) <1e-9,1e9>
        tau_2 = 0.1 (ms) <1e-9,1e9>
}

ASSIGNED {
        i (nA)
}

STATE {
        i_1 (nA)
        i_2 (nA)
}

INITIAL {
        i_1=0
        i_2=0
}

BREAKPOINT {
        SOLVE state METHOD cnexp
        if (t>=0(ms) && t<200(ms) {
                i = i_1 + 30
        }    else   {
                i=i_2+15
        }
}

DERIVATIVE state {
        i_1' = -i_1/tau_1
        i_2' = -i_2/tau_2
}
ted
Site Admin
Posts: 6101
Joined: Wed May 18, 2005 4:50 pm
Location: Yale University School of Medicine
Contact:

Re: how to add a current clamp in Python?

Post by ted »

The new code still uses a conditional statement. The INITIAL block sets both i_1 and i_2 to 0, so both of these variables will stay equal to 0 forever. This mechanism will deliver 30 nA for the first 30 ms, after which it will deliver 15 nA forever. That's definitely not what you want.

Maybe I don't really understand what you actually want.

This figure https://pic.imgdb.cn/item/638b3d2d16f2c2beb1f72805.png looks like the synaptic current generated by an ordinary ExpSyn that is driven by two events, where the weight associated with the first event is bigger than the weight associated with the second event. Both of the current transients appear to decay monoexponentially with identical time constants. This can be done with a single ExpSyn that is driven by events from two NetStims, and you wouldn't have to do anything with NMODL. Here's how.

Code: Select all

# assumes the section of interest is called dend
syn = h.ExpSyn(dend(0.5)) # assumes synapse is in middle of dend

# NetStim that drives the first activation of syn
ns1 = h.NetStim(dend(0.5))
# statements that specify number and timing of ns1's events
# for example, to make ns1 generate one event at t = 10 ms
ns1.number = 1
ns1.start = 10 # time of event in ms
# NetCon that conveys ns1's events to syn
nc1 = h.NetCon(ns1, syn)
nc1.weight = 0.001 # syn's peak conductance change in response to a single event from nc1

# NetStim that drives the second activation of syn
ns2 = h.NetStim(dend(0.5))
# statements that specify number and timing of ns2's events
# to make ns2 generate one event at t = 210 ms
ns2.number = 1
ns2.start = 210 # time of event in ms
# NetCon that conveys ns2's events to syn
nc2 = h.NetCon(ns2, syn)
nc2.weight = 0.0005 # syn's peak conductance change in response to a single event from nc2
But what if you want the first and second conductance changes to decay with different time constants? Then you still wouldn't have to do anything with NMODL. Instead, you'll need two ExpSyns, each with its own time constant. Here's how, assuming that you want the synaptic inputs to be at dend(0.5) (the middle of dend) and the time constants of the two conductance changes are 5 and 10 ms, respectively.

Code: Select all

# assumes the section of interest is called dend
syn1 = h.ExpSyn(dend(0.5)) # syn1 is in middle of dend
syn1.tau = 5 # and its conductance decays with a time constant of 5 ms

ns1 = h.NetStim(dend(0.5)) # NetStim that drives syn1
ns1.number = 1
ns1.start = 10 # time of event in ms
nc1 = h.NetCon(ns1, syn1) # conveys ns1's events to syn1
nc1.weight = 0.001 # syn1's peak conductance change when single event arrives from nc1

syn2 = h.ExpSyn(dend(0.5)) # syn2 is also in middle of dend
syn2.tau = 10 # but its conductance decays with a time constant of 10 ms

ns2 = h.NetStim(dend(0.5)) # NetStim that drives syn2
ns2.number = 1
ns2.start = 210 # time of event in ms
nc2 = h.NetCon(ns2, syn2) # convey ns2's events to syn2
nc2.weight = 0.0005 # syn2's peak conductance change when single event arrives from nc2
Let me know if this is, or isn't, what you wanted.
cassiehu
Posts: 25
Joined: Tue Sep 06, 2022 10:41 am

Re: how to add a current clamp in Python?

Post by cassiehu »

Yes! That is what I want. In the past, I do not know about h.NetStim() and its role. While I talked with other person who is also using NEURON, he recommended me this method, too. Thank you for your help really much!!!
Post Reply