How to access HOC arrays from Python?

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

Moderator: hines

Post Reply
ziemek
Posts: 43
Joined: Thu May 23, 2019 8:02 am
Location: Warsaw, Poland
Contact:

How to access HOC arrays from Python?

Post by ziemek »

I have a Migliore cell morphology which is created based on the following compartments (written in HOC):

Code: Select all

public soma, axon, dendrite, apical_dend, user5

{create axon[1]}
{create soma[1]}
{create dendrite[52]}
{create apical_dend[70]}
{create user5[49]}
which are then used to build a morphology based on pt3dadd() HOC function, eg.:

Code: Select all

{access dendrite[0]}
{pt3dclear()}
{pt3dadd(0,0,0,7.491)}
{pt3dadd(-12.97,0.03,-0.87,3.15)}
{pt3dadd(-13.24,-0.65,-0.87,3.15)}
I want to create a Python-based model, but in the same time load and utilize this morphology written in HOC.

So specificaly - how can I access (and preferably create a similar list of references) to all dendrites and apica_dend HOC arrays from Python?
ted
Site Admin
Posts: 5795
Joined: Wed May 18, 2005 4:50 pm
Location: Yale University School of Medicine
Contact:

Re: How to access HOC arrays from Python?

Post by ted »

The hoc notation
sectionname[i]
does not really mean that there is an "array" of sections. Instead, treat the [i] as part of the section's name.

Except for the very simplest cases (no more than half a dozen sections or so) It is probably more effort than it is worth to re-implement a hoc-specified model cell in Python. Even after you think you have accomplished the task, you then have to prove that you didn't break anything when you built your Python implementation, which means verifying that the branched topology of the model and the properties of all the section are identical to those of the original hoc model.

It is usually better just to let the hoc code do the hard work of setting up the model cell, e.g. by

h.load_file("set_up_cell.hoc") # assuming set_up_cell.hoc contains the hoc statements
# that set up the model cell

That creates the variables and data structures that represent the model cell inside NEURON's computational engine. Then you can use Python to deal directly with those variables and data structures. After all, if the hoc name of a section is foo, then h.foo is a Python name for the same section.

"But I don't want to have to type h. in front of every section's name."

Then create a Python alilas for each section that you want to deal with. Execute the Python statement

foo = h.foo

and that makes the Python name "foo" refer directly to the section whose hoc name is "foo".

"Must I do this for every hoc section?"

Only for those that you want to call from Python without having to type h. as part of the name. But that kind of task doesn't come up very often. More often you want to do something to a subset of sections, e.g. a dendritic subtree or part of an axon. And there are powerful methods for creating Python lists of sections; read about subtree, wholetree, and other topology-related methods under the general topic of Topology in the Programmer's Reference. After you create such a list, you can iterate over its contents and do whatever you like to each of the sections that it contains.
ziemek
Posts: 43
Joined: Thu May 23, 2019 8:02 am
Location: Warsaw, Poland
Contact:

Re: How to access HOC arrays from Python?

Post by ziemek »

Thanks for the help, the way you described hoc's references from Python's code work well for me.
Post Reply