3

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). I think this is more general to Python and not specifically to the software NEURON, thus I put it here on Stackoverflow.

The line of interest is here:

clamp_output = attach_current_clamp(cell)

If I just write 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 cannot I not just call the function? Why must I use a variable to store the output even without using the output?

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()
CodeGuy
  • 28,427
  • 76
  • 200
  • 317

1 Answers1

3

This is a NEURON+Python specific bug/feature. It has to do with Python garbage collection and the way NEURON implements the Python-HOC interface.

When there are no more references to a NEURON object (e.g. the IClamp) from within Python or HOC, the object is removed from NEURON.

Saving the IClamp as a property of the cell averts the problem in the same way as saving the result, so that could be an option for you:

# In __init__:
self.IClamps = []

# In attach_current_clamp:
stim.amp = 0.2
cell.IClamps.append(stim)
#return stim
Justas
  • 5,718
  • 2
  • 34
  • 36
  • Would you be able to make a reproducible example outside of Neuron? or it seems specific to Neuron? – CodeGuy Jun 24 '17 at 15:22
  • 2
    Not sure if I understand. The problem is specific to the Python package "neuron" that implements the interface between NEURON HOC and Python. You would not need to save references in plain python (i.e. outside of neuron). – Justas Jun 24 '17 at 18:36
  • 1
    Thank you. That was my questions. – CodeGuy Jun 24 '17 at 18:37