Page 1 of 1

Required to return current clamp object

Posted: Tue Jul 04, 2017 1:47 pm
by eisinger
I am trying to understand why I must store the output of a Python function (regardless of the name of the variable I use, and regardless of whether I subsequently use that variable) for current clamping. I wasn't sure if the NEURON staff was aware of this problem or not.

The line of interest is here:

Code: Select all

clamp_output = attach_current_clamp(cell)
If I just write

Code: Select all

attach_current_clamp(cell)
, without storing the output of the function into a variable, the code does not work (plot is empty), and yet I don't use clamp_output at all. Why must I use a variable to store the output even without using the output?

Code: Select all

import sys
import numpy
sys.path.append('/Applications/NEURON-7.4/nrn/lib/python')
from neuron import h, gui
from matplotlib import pyplot

#SET UP CELL
class SingleCell(object):
    def __init__(self):
        self.soma = h.Section(name='soma', cell=self)
        self.soma.L = self.soma.diam = 12.6517
        self.all = h.SectionList()
        self.all.wholetree(sec=self.soma)
        self.soma.insert('pas')
        self.soma.e_pas = -65

        for sec in self.all:
            sec.cm = 20
#CURRENT CLAMP
def attach_current_clamp(cell):
    stim = h.IClamp(cell.soma(1))
    stim.delay = 100
    stim.dur = 300
    stim.amp = 0.2
    return stim

cell = SingleCell()

#IF I CALL THIS FUNCTION WITHOUT STORING THE OUTPUT, THEN IT DOES NOT WORK
clamp_output = attach_current_clamp(cell)

#RECORD AND PLOT
soma_v_vec = h.Vector()
t_vec = h.Vector()
soma_v_vec.record(cell.soma(0.5)._ref_v)
t_vec.record(h._ref_t)
h.tstop = 800
h.run()
pyplot.figure(figsize=(8,4))
soma_plot = pyplot.plot(t_vec,soma_v_vec)
pyplot.show()

Re: Required to return current clamp object

Posted: Tue Jul 04, 2017 7:01 pm
by ted
There's no problem at all. NEURON is working exactly as it should. In fact, it's working exactly the same way Python works. Read about reference counting and garbage collection
https://en.wikipedia.org/wiki/Reference_counting
Then do the following two-step experiment and see if the answer to your question becomes clear.

Step 1. Change

Code: Select all

clamp_output = attach_current_clamp(cell)
to

Code: Select all

clamp_output = attach_current_clamp(cell)
print 'IClamp reference count:'
h.allobjects('IClamp')
Now use NEURON to execute your program. How many h.IClamp instances exist?

Step 2. Change

Code: Select all

clamp_output = attach_current_clamp(cell)
to

Code: Select all

attach_current_clamp(cell)
and use NEURON to execute your program again. How many h.IClamp instances exist this time?

By the way, clamp_output seems a rather misleading name for the thing that attach_current_clamp returns. What does Python tell you it is? (hint: at the >>> prompt, enter the command
print clamp_output
or simply
clamp_output
)

Re: Required to return current clamp object

Posted: Thu Jul 06, 2017 10:03 am
by eisinger
Interesting, thanks for addressing my question!

Re: Required to return current clamp object

Posted: Thu Jul 06, 2017 10:55 am
by ted
Thanks for asking it. It raised a couple of important points that may benefit many others.

Re: Required to return current clamp object

Posted: Thu Jul 06, 2017 11:03 am
by eisinger
I hope it will help others. We stumbled across this during the NIH BRAIN Course at U Missouri and we were quite puzzled. Thanks again.

Re: Required to return current clamp object

Posted: Thu Jul 06, 2017 11:35 am
by ted
Ouch. Is there some way to get this answer to the course's organizers and the other participants in the course?

Re: Required to return current clamp object

Posted: Fri Jul 07, 2017 9:35 pm
by eisinger
I have sent this to Dr. Nair, the course director.
Thanks!