Dynamically accessing vectors in python (_ref_x)

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

Moderator: hines

Post Reply
cstricker
Posts: 5
Joined: Thu Dec 08, 2005 10:31 pm
Location: JCSMR - ANU, Canberra, ACT, Australia
Contact:

Dynamically accessing vectors in python (_ref_x)

Post by cstricker »

Pythonisti (!)

I am reworking a software bridge that we have created some 10 years ago that allows running code in Neuron being controlled by IGOR which has nice display and printing abilities. The actual bridge is coded in python3 - and has been working fine. However, even though it is in python, it actually mostly passes hoc commands. To future-proof this code base, I thought to have as much as possible in python.

One thing that I cannot get my head around is how to setup the Vectors that allow monitoring of variables during the simulation. Since we run several simulations with recoding sites mostly on the soma, but also on axon and dendrite, I need to have the python statements right - which I don't seem to have. Using the old hoc code, vectors for example for the somatic K conductance were set up like this (simply pythonised string handling):

h('objref soma_gk')
h('soma_gk = new Vector()')
h('soma_gk.record(&gk_kv(0.5)')

I want this to be in elegant python, instead. I know that I can create the respective vector at the soma as

soma_gk = h.Vector().record(soma(0.5)._ref_gk_kv)

The issue is that this statement should be dynamic as within the bridge a python function is called that returns the function name plus inArgs something along

setupVector(soma_gk soma 0.5 gk_kv) // note that we use spaces to separate the inArgs consisting of vector name, section, loc, and variable name

This is then internally taken apart and from the individual elements, I should be able to create a python statement that links the Vector with the variable. Now this needs to be dynamic as sometimes, the location may not be at the soma, but say axon[6] or dend[32], the segment location may be 0.5 or 1.0 (as it is distance dependent in µm) and the variables to watch are either v, gna_na, ina, ik, etc. I also appreciate that I can get the section name plus location again using h.Section(sec=soma) - but the casting around ._ref_x is not trivial.

I appreciate that somehow there is a way to use setpointer() together with some list handling - but I really don't quite get how to cast that statement correctly. No matter what I have tried, I have received run-time errors. I am sure that my inexperience with python may well explain this...

Any code snippet much appreciated.
ramcdougal
Posts: 214
Joined: Fri Nov 28, 2008 3:38 pm
Location: Yale School of Public Health

Re: Dynamically accessing vectors in python (_ref_x)

Post by ramcdougal »

I believe you're looking for Python's getattr function:

You can get a pointer to the membrane potential at the center of the soma via:

Code: Select all

ptr = getattr(soma(0.5), '_ref_v')
(Here, I'm assuming "soma" is a Python Section object... adjust accordingly for HOC variables, which you can also get by name with getattr, if desired.)

In particular, to record variable varname from location x on section,

Code: Select all

def record_vec(section, x, varname):
    return h.Vector().record(getattr(section(x), '_ref_' + varname))
For your example, varname would be "gk_kv"... of course, you could split this further (and recombine later) to separate the mechanism from the variable, if desired.

setpointer is for setting pointers in MOD files, not for setting up recording vectors.

That said, the HOC functionality should continue to work for the foreseeable future, so I wouldn't feel compelled to rewrite things that work unless you want to.
cstricker
Posts: 5
Joined: Thu Dec 08, 2005 10:31 pm
Location: JCSMR - ANU, Canberra, ACT, Australia
Contact:

Re: Dynamically accessing vectors in python (_ref_x)

Post by cstricker »

Thanks Robert

that is exactly what I was after - works like a charm. It is hard for an amateur C programmer to get used to the python language as the notion of a pointer does not really exist - that bridged this gap... they do exist but in disguise. Thanks and cheers Christian.
cstricker
Posts: 5
Joined: Thu Dec 08, 2005 10:31 pm
Location: JCSMR - ANU, Canberra, ACT, Australia
Contact:

Re: Dynamically accessing vectors in python (_ref_x)

Post by cstricker »

On working on this a bit further, in most instances the way to work via getattr() works well, however, if I try to get the vector in a location on either the dendrite or axon, it falls over.

I am loading the morphology from a HOC file where the segments are in the following notation: for example, a location would be at axon[5](0.5) or apic[42](0.5).

In this instance, I cannot get the ptr value via getattr(); I simply get an error

"AttributeError: 'hoc.HocObject' object has no attribute ''axon[5]'".

How do I get the ptr in this situation? I have tried to get via the Section list - and work from there - but could not get it to work. Any bright ideas?

Thanks and cheers

Christian.
ramcdougal
Posts: 214
Joined: Fri Nov 28, 2008 3:38 pm
Location: Yale School of Public Health

Re: Dynamically accessing vectors in python (_ref_x)

Post by ramcdougal »

Think of these things with [] as an array of sections; what that means is that the object you getattr is the array, not any individual section.

i.e. do something like this:

Code: Select all

>>> from neuron import h
>>> h('create axon[6]')
1
>>> getattr(h, 'axon')[5]
axon[5]
>>> sec = getattr(h, 'axon')[5]
>>> ptr = getattr(sec(0.5), '_ref_v')
>>> ptr[0] = 42
>>> h.axon[5].v
42.0
cstricker
Posts: 5
Joined: Thu Dec 08, 2005 10:31 pm
Location: JCSMR - ANU, Canberra, ACT, Australia
Contact:

Re: Dynamically accessing vectors in python (_ref_x)

Post by cstricker »

Thanks Rob - that cleared it up... all working now. Many thanks and cheers Christian.
Post Reply