Playing a vector into a linear mechanism from python

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

Moderator: hines

Post Reply
basjanzandt

Playing a vector into a linear mechanism from python

Post by basjanzandt »

I script my simulations from python. I am trying to include a linear mechanism to simulate a voltage clamp pipette with a capacitance.
I have a compartmental cell and a linear mechanism testLC.hoc generated from the LC-builder (for testing purposes consisting of a voltage source (B2), resistor (R1) and cell node (N2) in series). All seems to work fine:

Code: Select all

from neuron import *; from nrn import *; from neuron import gui
cell = generatecell()
xopen('testLC.hoc')         # load the linear mechanism, named LM
stim = h.LM()               # create an instance
stim.N2_loc(.5, cell.soma)  # attach the soma to the resistor
stim.install()              # insert the mechanism in the simulation
stim.R1 = 100              # change the resistor value      
stim.refill()               # update the mechanism
stim.E_B2 = -20             # change the voltage of the source
h.run()                     # run the simulation
This results in the soma being voltage clamped to around -20 mV (minus the drop over the resistor), and I can change this value through stim.E_B2.

However, I would now like to play a variable waveform in this voltage. For a SEClamp, I would use:

Code: Select all

MyWave.play(myclamp._ref_amp,tstim,1,sec=cell.soma
This works for a SEClamp, but to be honest, I don't know why I have to play into ._ref_amp instead of .amp.

Now for my linear mechanism, playing into stim.E_B2 does not seem to have any effect. Neither does playing into stim._b.x[3] to which E_B2 refers. Mimicking the code for the SEClamp by playing into stim._ref_E_B2 throws an error: "Hoc pointer error, E_B2 is not a hoc variable or range variable".

Thanks in advance for your help, it is much appreciated.

(I use NEURON 7.3 on a MAC with python 2.7.8)
ted
Site Admin
Posts: 6289
Joined: Wed May 18, 2005 4:50 pm
Location: Yale University School of Medicine
Contact:

Re: Playing a vector into a linear mechanism from python

Post by ted »

basjanzandt wrote:I am trying to include a linear mechanism to simulate a voltage clamp pipette with a capacitance.
Planning to use an equivalent T circuit for the electrode?

Code: Select all

o---R/2---+---R/2---o
          |
          C
          |
        GND
(fie on proportional fonts inside code blocks! those vertical bars are supposed to line up with the junction between the two resistors in the top line!)
a linear mechanism testLC.hoc generated from the LC-builder
On the LinearCircuit builder's Parameters or Simulate page, click on Source f(t) and select B2. At the top of the voltage source's parameter panel click on the checkbox next to "External Stim Pattern". Bring up the voltage source's parameter panel again and you will see that it contains a message that says something like "External Stim Pattern . . . should play into the alias LinearCircuit[0].Stim". That's your clue. Something like

Code: Select all

MyWave.play(h.LinearCircuit[0]._ref_Stim,tstim,1,sec=cell.soma)
is probably what you need.
For a SEClamp, I would use:

Code: Select all

MyWave.play(myclamp._ref_amp,tstim,1,sec=cell.soma
This works for a SEClamp, but to be honest, I don't know why I have to play into ._ref_amp instead of .amp
For speed, you want the transfer of values from forcing function to driven variable to be as fast as possible. In hoc this is done by using pointer syntax so that the values are passed "by reference". Example: if your forcing function is specified by the vector pair vsrc, tvec, and you want to drive sec.amp1, the statement would be
vsrc.play(&sec.amp1, tvec)
Python doesn't know what to do with pointer syntax, so instead of using an ampersand, you prefix the name of the driven variable with _ref_, which makes a variable name that doesn't gag Python, and the interface between Python and NEURON's computational engine takes care of the messy details.
basjanzandt

Re: Playing a vector into a linear mechanism from python

Post by basjanzandt »

Ted, thanks for your swift and clear reply, and the explanation of using ._ref_.
Indeed, I'm trying to implement an equivalent T-circuit. (There seem to have been tutorials around for this in a previous topic, but the links are broken.)

So now I tried the reference from the LinearCircuit builder. Unfortunately, I still get the same error.

The LC-builder tells me to play into the alias LinearCircuit[0].B2
Playing into this using _ref_ results in the same error as trying to play into the LM from the hoc code:

Code: Select all

Isrc.play(h.LinearCircuit[0]._ref_B2,tstim,1,sec=soma)
TypeError: Hoc pointer error, B2 is not a hoc variable or range variable

as opposed to trying to play into a non-existing variable:

Code: Select all

Isrc.play(h.LinearCircuit[0]._ref_blabla,tstim,1,sec=soma)
AttributeError: 'hoc.HocObject' object has no attribute '_ref_blabla'

Would you have a clue how to fix this?
basjanzandt

Re: Playing a vector into a linear mechanism from python

Post by basjanzandt »

Update:

I got it working by playing into stim._b._ref_x[2] of the Linear Mechanism directly, rather than playing into the defined alias. (2 being the index of the voltage source in the LM.)
Still no clue why playing into the alias doesn't work. Maybe because the target is part of a vector? At least this solves it for me.

Thanks for pointing me in the right direction Ted.
ted
Site Admin
Posts: 6289
Joined: Wed May 18, 2005 4:50 pm
Location: Yale University School of Medicine
Contact:

Re: Playing a vector into a linear mechanism from python

Post by ted »

Playing into the alias via hoc worked fine, but it turned out there was a gap in the Python interface to NEURON's compute engine so that alias variables weren't properly handled. This has now been fixed in the mercurial source code repository so that future versions of NEURON starting with 7.4 (1327:d5bf062d0b56) 2015-04-25 will not have this problem.

I'm glad you were able to implement a workaround. If you get the latest development source code and compile from that, you'll be able to use the alias.

For other readers who may be interested, this file https://www.neuron.yale.edu/ftp/ted/neu ... tifact.zip contains an example of a model of the effects of electrode resistance and capacitance on current clamping of a cell. Unzip it and see the readme.txt for details.
Post Reply