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: 6286
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 »

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 you create your new sections with statements of the form

namestring = h.Section(name = 'namestring')

The workaround described below is no longer necessary! Skip to the post in this thread that is dated Fri Sep 27, 2019.

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: 16
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 »

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
ted
Site Admin
Posts: 6286
Joined: Wed May 18, 2005 4:50 pm
Location: Yale University School of Medicine
Contact:

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

Post by ted »

For some time now, all you have to do is specify a name when you use Python to create a section, and you do it like this:
namestring = h.Section(name = 'namestring')


This creates a new section that has the Python name namestring and the hoc name _pysec.namestring. The hoc name can be used in any hoc statement that needs the name of a section, added to a hoc Graph's plotlist, etc..

For example, if you execute
soma = h.Section(name = 'soma')
there will now be a new section called soma in Python, and _pysec.soma in hoc. The hoc name for the membrane potential at the middle of this section will be _pysec.soma.v(0.5). If you use the NEURON Main Menu tool to bring up a new Graph object, then launch that Graph's "Plot what?" tool, click on the tool's "Show" button and select the "Python Sections" item from the list, and you'll see the namestrings in the left panel of the "Plot what?" tool. You can then construct the name of a variable to be plotted in the Graph exactly as you would if the section had been created by a hoc statement--double click on a namestring, then double click on the name of the variable you want to plot, then revise the value of the range parameter if necessary, and finally click on Accept.
Post Reply