Page 1 of 1

How to access HOC arrays from Python?

Posted: Wed Dec 04, 2019 8:00 am
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]}
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?

Re: How to access HOC arrays from Python?

Posted: Wed Dec 04, 2019 10:37 am
by ted
The hoc notation
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 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 =

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.

Re: How to access HOC arrays from Python?

Posted: Tue Dec 10, 2019 2:10 pm
by ziemek
Thanks for the help, the way you described hoc's references from Python's code work well for me.