Python object as argument of hoc function

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

Moderator: hines

Post Reply
Nin
Posts: 41
Joined: Thu May 03, 2007 4:04 pm
Location: Institute of Science and Technology (IST Austria)
Contact:

Python object as argument of hoc function

Post by Nin » Thu Sep 29, 2016 10:26 am

While accessing a python object (h.Section) is not a problem with this code:

Code: Select all

objref py, mygraph
strdef cellname

py = new PythonObject()
mygraph = new Graph(0)

mygraph.size(0, tstop, -80, 40)
sprint(cellname, "%s", "py.mycell[0].soma.v")
graphList[0].append( mygraph )

mygraph.addexpr(cellname, 9,1 , 0.8, 0.9, 2)

i wonder how can one set the Python object as argument of a hoc function.

ramcdougal
Posts: 138
Joined: Fri Nov 28, 2008 3:38 pm
Location: Yale School of Medicine

Re: Python object as argument of hoc function

Post by ramcdougal » Thu Sep 29, 2016 12:00 pm

Object arguments (doesn't matter if HOC objects or Python objects) to HOC functions are accessed using positional notation with $o instead of just $. e.g. $o1 instead of $1.

A complete example, where for laziness I define the HOC function via h():

Code: Select all

from neuron import h

# declare a HOC function that just invokes a Python object's method
h('proc printit() {$o1.go()}')

class Foo:
    def go(self):
        print 'hello world'

f = Foo()

print 'now invoking the HOC function'

h.printit(f)

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

Re: Python object as argument of hoc function

Post by ted » Thu Sep 29, 2016 12:12 pm

hoc procs and funcs can accept objrefs as arguments. In the body of the proc or func, the name of the objref will be of the form
$oi
where i refers to the position of the objref in the argument list. Remember that the items in the argument list are numbered starting with 1.
Example: define func foo as

Code: Select all

func foo() {
  print $o1, $s2, " ", $3
  return 2
}
then execute

Code: Select all

objref bla
foo(bla, "hello", PI)
and the result is

Code: Select all

NULLobject hello 3.1415927
              2
Does that help?

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

Re: Python object as argument of hoc function

Post by ted » Thu Sep 29, 2016 12:45 pm

Unfortunately, sections created by statements of the form
name = h.Section()
end up with strange hoc names that are difficult to use in hoc statements or with the InterViews-based GUI.
This is why it can be better to define the properties of the cell with hoc code (at least the creation of sections). Then every section will have a valid, user-defined, meaningful section name and can be easily accessed from both hoc and Python.

Example:
Suppose a cell's properties are specified completely by a CellBuilder's ses file or by a hoc file. In Python just do
h.load_file(name_of_ses_or_hoc_file)
Now suppose you're particularly interested in what's going on at the 0.8 location in a section whose hoc name is dend.
Just do this in Python
dend = h.dend
and you can refer to any of this section's properties in either hoc or Python.
dend.nseg and h.dend.nseg are the section's discretization parameter
dend(0.8).v is the same variable as h.dend(0.8).v, but easier to type.
Ditto for dend(0.8).diam and h.dend(0.8).diam.

And you can do
g = h.Graph(0)
g.view(...)
g.addvar('dend.v(0.8)')

If you prefer your graph labels to remind you of the Python names for what is plotted, you could
g.addvar('py.dend(0.8).v')
assuming that you have already told hoc that py is an instance of the PythonObject class
i.e. by doing
h("objref py")
h("py = new PythonObject()")

Nin
Posts: 41
Joined: Thu May 03, 2007 4:04 pm
Location: Institute of Science and Technology (IST Austria)
Contact:

Re: Python object as argument of hoc function

Post by Nin » Thu Sep 29, 2016 4:25 pm

Thank you Ted and ramcdougal for your responses.
ted wrote:Unfortunately, sections created by statements of the form
name = h.Section()
end up with strange hoc names that are difficult to use in hoc statements or with the InterViews-based GUI.
Definitely, I like defining cell objects from a Python Class that calls a h.load_file('morphologytemplate.hoc'). Call me silly, but I like seeing the results of the simulations with the Interviews GUI... matplotlib is for final results :P

Post Reply