Section object created in python partially existing in hoc

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

Moderator: hines

Post Reply
romain.caze

Section object created in python partially existing in hoc

Post by romain.caze »

Dear all,

One can wrap a section object within a python class

Code: Select all

from neuron import h, nrn
class NewSection(nrn.Section):
    """
    Should wrap a NEURON section
    """
    def __init__(self, name="fault"):
        """Initialize the section
        """
        nrn.Section.__init__(self, name=name)
It seems to work beautifully. For instance one could type

Code: Select all

newSec = NewSection(name='something')
newSec.L = 123.4
h.psection(sec=newSec)
And have a meaningful output. Or

Code: Select all

for sec in h.allsec(): 
    print sec.name()
And see the name of the section called "something". In other it seems to work, however, when you type

Code: Select all

h.something
or

Code: Select all

h("print something.L")
This raise an exception. For instance the first one raise

Code: Select all

AttributeError: 'hoc.HocObject' object has no attribute 'something'
And the second one:

Code: Select all

NEURON: syntax error
near line 0
print newSec.L
             ^
This seems to indicate that the section created in python is in fact not existing in the h namespace despite its listing with h.allsec().

Is it a "bug" or a "feature"? For my usage it is a bug because I cannot create a cell model using Python and then type cellmodel.dend10.L to access the section length.

Best,
hines
Site Admin
Posts: 1687
Joined: Wed May 18, 2005 3:32 pm

Re: Section object created in python partially existing in h

Post by hines »

If cellmodel is an instance of a python class

Code: Select all

from neuron import h
class CellModel():
  def __init__ (self):
    self.dend10 = h.Section(name='foo', cell=self)

cellmodel = CellModel()
cellmodel.dend10.L = 10
and

Code: Select all

>>> h.topology()

|-|       <__main__.CellModel instance at 0x7f6a441ca518>.foo(0-1)

1.0
>>> 
Python Section is good at wrapping a HOC section but to hoc, Python Sections are more or less anonymous unless you wrap them in a HOC SectionRef or
get them onto the hoc sectionstack. Unfortunately hoc sections predated the addition of the concept of a hoc object and all section manipulation was carried out
via the hoc section stack. Thus in the hoc world, the only way hoc statements can use python sections is for python to make sure the proper section is on the section stack. eg.
cellmodel.dend10.push()
h('print secname()')
h.pop_section()
or (to alleviate the incessant pushing and popping), every hoc function called from python can take a sec=.. arg as in
h.secname(sec=cellmodel.dend10)

There is a minor advantage to creating all cell classes in hoc and everything else in python because there is a complete interoperability between hoc and python objects. In addition hoc cell
classes have the property that if the cell goes out of existence, so do all the sections the cell created. This happens for python cell objects only if no one else holds a reference to the contained
sections.
Post Reply