Page 1 of 1

Uploading .swc file through .hoc

Posted: Sat Apr 02, 2022 2:13 pm
by neuron121!
Hello all,

I have an uploaded neuron in a .swc file, and I'm able to access it through a python file. However, I have all this previous code that runs through NEURON, through a .hoc file. It doesn't read the python file, so I have two options: I can either rewrite all the .hoc code in python, or I can open a .swc file in .hoc code. The latter is much easier, but I can't find the code for it, if anyone has any suggestions. Thank you!

Re: Uploading .swc file through .hoc

Posted: Sat Apr 02, 2022 5:23 pm
by ramcdougal
Roughly speaking the HOC code would look exactly like the Python, but drop the h. part.

That said, HOC and Python are just two different interfaces to the same simulator. You can mix and match.

e.g. from Python, you can run a HOC script called my_script.hoc via:

Code: Select all

h.load_file("my_script.hoc")
and then if that script defined a HOC function called foo, from Python you could simply invoke e.g.

Code: Select all

h.foo(123)
Alternatively from HOC, you can invoke Python via nrnpython and/or by creating a PythonObject:

Code: Select all

oc>objref pyobj
oc>pyobj = new PythonObject()
oc>nrnpython("import math")
	1 
oc>pyobj.math.cos(3.14)
	-0.99999873 

Re: Uploading .swc file through .hoc

Posted: Fri Apr 22, 2022 1:06 pm
by neuron121!
Hello,

Thank you for the reply. I think this code is the most helpful to me.

oc>objref pyobj
oc>pyobj = new PythonObject()
oc>nrnpython("import math")
1
oc>pyobj.math.cos(3.14)
-0.99999873

However, when I run it in the neuron executable or in VS code, the third line always comes out as a 0, indicating that "the python object is not supported". Any suggestions?

Re: Uploading .swc file through .hoc

Posted: Mon Apr 25, 2022 11:46 am
by ted
If you're already familiar with Python, why not stick with Python?

from neuron import h, gui
# or, if you prefer,
# from neuron import h
# h.load_file('stdgui.hoc')
. . . next execute your python code that reads the swc file . . .
# then execute the legacy hoc file that does whatever useful stuff you need
h.load_file('foo.hoc')
# and continue in Python, referring to hoc-defined variables and functions as
# h.variablename and h.functionname()