Use NEURON's native GUI tools with Python!

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

Moderator: hines

Post Reply
ted
Site Admin
Posts: 5369
Joined: Wed May 18, 2005 4:50 pm
Location: Yale University School of Medicine
Contact:

Use NEURON's native GUI tools with Python!

Post by ted » Tue Aug 08, 2017 2:48 pm

While there are many powerful Python packages for creating graphs, such as matplotlib and mayavi, there still isn't any substitute for NEURON's native GUI tools for managing models. The interactive nature of these tools make them far more convenient for development and debugging than any Python graphical library.

And they work perfectly well, as long as all sections are created in hoc. Why is it necessary for all sections to have been created in hoc? Consider this example:

Code: Select all

from neuron import h,gui
h('create soma') # creates soma from hoc
dend = h.Section() # creates dend from Python
h.psection(h.soma) # works nicely
h.psection(dend) # also works nicely
So far so good. Now try

Code: Select all

h('forall print secname()')
and you'll get something like this

Code: Select all

soma
__nrnsec_0x8a7d9b0
That second line is ugly and uninformative because hoc can only discover the internal, computer generated name for the Python object that refers to what you called "dend" in Python. It's also not a useful name because it's likely to be different the next time you start NEURON and execute the same commands.

It would be great if h.Section() had an optional argument that could be used to specify a meaningful hoc name, like

Code: Select all

dend = h.Section(hocname='dend')
but unfortunately that feature doesn't yet exist.

So the use of NEURON's native GUI tools is limited to sections that were created by executing hoc code. But does this mean you actually have to embed hoc statements, like

Code: Select all

h('create axon')
in your Python code?

Nope. Just insert this bit of Python in your program

Code: Select all

def Section(name):
  h("create "+name)
  return h.__getattribute__(name)
(those __ bits are pairs of underscore _ characters). Then you can do stuff like this

Code: Select all

axon = Section('axon')
and now there will be a hoc section called axon, and there will also be a Python variable called axon that refers to the same section. What's nice about this is that
1. it enables use of NEURON's GUI tools (including Model View) to explore and manage your model's properties,
and
2. you don't have to sprinkle hoc statements all over your nice Python program. The only hoc stuff is in the Python procedure called Section.

Many thanks to Robert McDougal for this very useful tip!

Mohamed_Hisham
Posts: 8
Joined: Thu Oct 08, 2015 2:56 pm
Location: Wright State University

Re: Use NEURON's native GUI tools with Python!

Post by Mohamed_Hisham » Wed Aug 09, 2017 5:57 pm

Hello Ted

I used the function , and it is very helpful actually , thank you very much.

Code: Select all

def Section(name):
    h("create "+name)
    return h.__getattribute__(name)
I wanted to know how to extend the previous function to make it work like this one :

Code: Select all

 self.soma = h.Section(name='soma', cell=self)
as the previous line will create a section and associate it with a cell , during a cell class definition. I want to create such a section in a python cell class.
How can I modify the first function to add this attribute of the self belonging cell. so that i have when i print the topology, it appears like this

Code: Select all

h.topology()

    |-|       MyCell[0].soma(0-1)
      `|       MyCell[0].dend(0-1)
Thanks

Post Reply