How can python read from a hoc file templates and procs?

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

Moderator: hines

Post Reply
alexandrapierri
Posts: 21
Joined: Wed Jun 17, 2015 5:31 pm

How can python read from a hoc file templates and procs?

Post by alexandrapierri » Fri Dec 21, 2018 6:01 pm

Hello developers

I want my python script to read from a NEURON hoc script and and I am facing difficulties having python read variables if they are defined within a template or a proc and not on top level. How can python read parameters right (and in general any variable in hoc) if defined within a template or proc?

Here is a section of the hoc code I try to have python read from:
mine=2
begintemplate axoaxoniccell
public NumSoma, NumApical, NumBasal, NumAxon

// create the sections[segments]
NumSoma=1
NumApical=16
NumBasal=0
NumAxon=0

create soma[NumSoma], dend[NumApical]

proc init() {
gid = 1
randi = 2
// morphology
connect_sections()
define_shape()
append_sections()
set_nseg()
get_root()
}

Here is my python script:
import neuron
from neuron import h, gui
h.load_file('cells/class_axoaxoniccell_temp.hoc') //this is the name of the hoc script above
print h.mine
object=h.axoaxoniccell()
param=object.NumApical
print param

And here is the output I get:
2.0
0.0

As shown it reads "mine" right which is defined top level but reads param wrong which in fact should be 1.
Last edited by alexandrapierri on Fri Jan 04, 2019 3:04 pm, edited 4 times in total.

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

Re: How can python read from a hoc file templates and procs?

Post by ted » Tue Dec 25, 2018 5:44 pm

There is nothing wrong with how load_file is reading and executing the hoc file. You had the perfectly reasonable expectation that NumSoma, NumApical, NumBasal, and NumAxon would retain their values after NEURON parsed the code that defines the axoaxoniccell class. But that does not happen--instead, as you discovered, they all become 0 immediately after the template is parsed and the cell class has been created. Why? Once the cell class has been created, NEURON does not need their initial values.

Presumably you have some task in mind, and one way to do it relies on knowing how many sections have a particular base name. If you describe that task, I might be able to suggest a different way to accomplish it.

alexandrapierri
Posts: 21
Joined: Wed Jun 17, 2015 5:31 pm

Re: How can python read from a hoc file templates and procs?

Post by alexandrapierri » Fri Jan 04, 2019 3:02 pm

Thank you Ted.

As a matter of fact, the code I posted is a toy code I use to experiment/understand how Python reads variables, parameters and their values from NEURON. From your response I realize that the idea is that Python reads the type of variables and parameters defined in hoc but expects that they will be instantiated in Python. I haven't encountered any examples in Python-NEURON documentation where a value is assigned to a parameter in hoc and python reads it from hoc. In all examples, Python reads the variable from hoc but then assigns a value to it in Python.

So my first question is how can Python read not just the type of parameters/variables but also their values assigned in hoc? Is that possible?


Here is a more specific example:
The below is taken from Bluepyopt but is a general Python-NEURON communication issue. In the code below I want to have python set nseg on my hoc model. Python doesn't recognize the existence of the public variable called "all" in my hoc model.

Here is the code I ran:
h.load_file('cells/class_axoaxoniccell_temp.hoc')
nrn_sim = ephys.simulators.NrnSimulator() #this is just using NEURON as simulator
morph = ephys.morphologies.NrnFileMorphology('cells/class_axoaxoniccell_temp.hoc')
obj=h.axoaxoniccell_temp()
all=obj.all

Here is the error I get:
AttributeError: 'hoc.HocObject' object has no attribute 'all'

Here is the Python class called, related to the error (ephys.morphologies.NrnFileMorphology):
1. def set_nseg(icell,sim):
2. """Set the nseg of every section"""
3. # from neuron import h
4. iseclist = getattr(sim.neuron.h, 'all') #<------ this line causes the error
5. for section in [x for x in iseclist]:
6. section.push()
7. section.nseg = int((section.L/(0.1*sim.neuron.h.lambda_f(100))+.999)/2)*2 + 1
8. sim.neuron.h.pop_section()

Here is my hoc cell:
begintemplate axoaxoniccell_temp
public all, basal_list, apical_list, soma_list, axon_list, dendrite_list
public .......

So the question is how can I have my Python class access public variable "all" within my hoc template in line 4?

thank you,
Alexandra

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

Re: How can python read from a hoc file templates and procs?

Post by ted » Fri Jan 04, 2019 5:25 pm

It seems that you have accumulated some misconceptions. Try to forget everything you think you know about the relationship between hoc, Python, and NEURON. Here are some true statements.

Any variable that has been created by executing hoc statements can be accessed by Python, and any variable that has been created by executing Python statements can be accessed by hoc. How? Read the relevant parts of the Programmer's Reference, especially
Python Accessing Hoc https://www.neuron.yale.edu/neuron/stat ... essing-hoc
Hoc Accessing Python https://www.neuron.yale.edu/neuron/stat ... ing-python

Example:
Suppose you have a Python script myprog.py that contains the statements
from neuron import h
h.load_file("somefile.hoc")
where somefile.hoc is a file that contains one or more hoc statements.
Executing myprog.py will make Python use the hoc interpreter to execute the code in somefile.hoc. Any hoc variables that are created by statements in somefile.hoc can then be accessed by Python. For example, suppose somefile.hoc contains the following hoc statements:
width = 3
height = 4
func area() {
return $1*$2
}
Then if you use Python to execute myprog.py, you will find that Python knows that the value of h.widht is 3.0, h.height is 4.0, and h.area(h.width, h.height) returns the value 12.0

Now suppose you have another hoc file called pyrcelldef.hoc, and pyrcelldef.hoc contains a hoc template that defines a cell class called Pyr. Then if you use Python to execute this script
fron neuron import h
h.load_file("pyrcelldef.hoc")
newpyr = h.Pyr()
you will have a new instance of the Pyr class that has the Python name newpyr. Furthermore, any of the names that are declared "public" in the Pyr template will also be accessible to Python. For example if the Pyr template declares that soma is public, then the Python name for the soma that belongs to newpyr will be newpyr.soma

alexandrapierri
Posts: 21
Joined: Wed Jun 17, 2015 5:31 pm

Re: How can python read from a hoc file templates and procs?

Post by alexandrapierri » Thu Jan 10, 2019 3:27 pm

Thank you Ted.
I now understand that "newpyr = h.Pyr()" creates a new instance of "Pyr" and this explains the zero values I was getting in the first example I posted. However if we go back to my first example listed again below, how can I have python print h.NumApical?
"print h.axoaxoniccell_temp.NumApical", as shown in the code below doesn't do it.

This is a section of the hoc code I try to have python read from:
begintemplate axoaxoniccell
public NumSoma, NumApical, NumBasal, NumAxon

// create the sections[segments]
NumSoma=1
NumApical=16
NumBasal=0
NumAxon=0

This is my python script:
import neuron
from neuron import h, gui
h.load_file('cells/class_axoaxoniccell_temp.hoc') //this is the name of the hoc script above
print=h.axoaxoniccell_temp.NumApical

Output:
0

Thank you again.

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

Re: How can python read from a hoc file templates and procs?

Post by ted » Thu Jan 10, 2019 4:28 pm

I guess my post from
Tue Dec 25, 2018 5:44 pm
wasn't sufficiently explicit. Let me try again.

The assignment statements
NumSoma=1
NumApical=16
NumBasal=0
NumAxon=0
inside the axoaxoniccell template are not contained in a proc or func that belongs to the template.
Therefore they are executed only once: when the template is first encountered by the interpreter. These four variables have the assigned values only during the interpreter's first pass through the template. After the cell class has been defined, the variables do not persist, so they cannot retain those values. Consequently their values will be 0 when you create an instance of the axoaxoniccell class. If that isn't what you want, then move the assignment statements into the template's proc init().

alexandrapierri
Posts: 21
Joined: Wed Jun 17, 2015 5:31 pm

Re: How can python read from a hoc file templates and procs?

Post by alexandrapierri » Thu Jan 10, 2019 5:06 pm

Ok, understood now thank you.

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

Re: How can python read from a hoc file templates and procs?

Post by ted » Thu Jan 10, 2019 5:12 pm

You're welcome. Sorry I didn't make it clear enough the first time.

Post Reply