I am writing to bring to your attention an issue I encountered while simulating a Purkinje cell using the NEURON simulator with Python code.
Problem Description:
My code is in Python, but some variables and functions of the cell are written in HOC code. While conducting simulations, I observed that if there is an error in the HOC code, the Python code continues to execute without throwing an error. This behaviour can lead to inaccurate results and make debugging more challenging.
Expected Behaviour:
Ideally, when an error occurs in the HOC code during a simulation executed from Python, the execution should halt, and an exception should be raised in the Python environment, allowing the user to identify and address the issue promptly.
Request for Assistance:
I would appreciate any insights or suggestions on how to ensure that errors in the HOC code are properly reported within the Python environment, thereby facilitating debugging and ensuring the accuracy of simulation results.
Thank you in advance for your attention to this matter.
Python Code Continues Execution Despite HOC Error
Moderator: hines
Re: Python Code Continues Execution Despite HOC Error
That is what is supposed to happen. What kind of computer are you using and what is the NEURON version. A short example would be helpful. E.g.when an error occurs in the HOC code during a simulation executed from Python, the execution should halt
Code: Select all
from neuron import h
h('''
proc test() { print 1/0 }
''')
def test1():
print("enter test1")
h.test()
print("leave test1")
test1()
Code: Select all
enter test1
NEURON: division by zero
near line 0
^
test()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 3, in test1
RuntimeError: hocobj_call error: hoc_execerror: division by zero
Re: Python Code Continues Execution Despite HOC Error
Thank you very much for your response.
I am testing this both from a Windows machine and a Linux machine running on Ubuntu. My NEURON version is 8.2.4+, and my Python version is 3.8.5.
I tried the snipped of code you shared and it works as expected. I recreated the problem I am having with the following short example:
In this case, I get the following output:
In my larger code I missed the warning as it was buried by other logs and the simulation ran successfully.
I am testing this both from a Windows machine and a Linux machine running on Ubuntu. My NEURON version is 8.2.4+, and my Python version is 3.8.5.
I tried the snipped of code you shared and it works as expected. I recreated the problem I am having with the following short example:
Code: Select all
from neuron import h
h('''
create d1
create d2
create d3
objref alldend
alldend = new SectionList()
forsec "d*" {alldend.append() }
''')
h('''
func test1() { return a }
''')
print("The next line should throw an error and terminate the execution")
h('forsec alldend{test1()}')
print("The python script is still executing")
Code: Select all
The next line should throw an error and terminate the execution
NEURON: undefined variable a
near line 1
forsec alldend{test1()}
^
test1()
The python script is still executing
Re: Python Code Continues Execution Despite HOC Error
Sorry, h('hoc code') does not raise an exception on a hoc error, it returns 0 on failure and 1 on success. It's behavior is similar toThat is what is supposed to happen.when an error occurs in the HOC code during a simulation executed from Python, the execution should halt
https://nrn.readthedocs.io/en/8.2.3/pyt ... l#execute1
Code: Select all
err = h.execute1("statement")
Code: Select all
for sec in h.alldend:
a_ret = h.test1(sec=sec) # the sec keyword arg ensures the correct currently accessed section in hoc when test1 is called.
Re: Python Code Continues Execution Despite HOC Error
Thank you! This is much clearer now.