Python Code Continues Execution Despite HOC Error

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

Moderator: hines

Post Reply
EleBern
Posts: 3
Joined: Sun Jan 15, 2023 10:40 am

Python Code Continues Execution Despite HOC Error

Post by EleBern »

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.
hines
Site Admin
Posts: 1691
Joined: Wed May 18, 2005 3:32 pm

Re: Python Code Continues Execution Despite HOC Error

Post by hines »

when an error occurs in the HOC code during a simulation executed from Python, the execution should halt
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.

Code: Select all

from neuron import h
h('''
proc test() { print 1/0 }
''')
def test1():
    print("enter test1")
    h.test()
    print("leave test1")

test1()
The output I get is:

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
EleBern
Posts: 3
Joined: Sun Jan 15, 2023 10:40 am

Re: Python Code Continues Execution Despite HOC Error

Post by EleBern »

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:

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")
In this case, I get the following output:

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 
In my larger code I missed the warning as it was buried by other logs and the simulation ran successfully.
hines
Site Admin
Posts: 1691
Joined: Wed May 18, 2005 3:32 pm

Re: Python Code Continues Execution Despite HOC Error

Post by hines »

when an error occurs in the HOC code during a simulation executed from Python, the execution should halt
That is what is supposed to happen.
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 to
https://nrn.readthedocs.io/en/8.2.3/pyt ... l#execute1

Code: Select all

err = h.execute1("statement")
A more pythonic call to test1 which would raise an error (if any) is

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.
 
although for your implementation of test1, the keyword arg is not needed as test1 makes no implicit use of the currently accessed section.
EleBern
Posts: 3
Joined: Sun Jan 15, 2023 10:40 am

Re: Python Code Continues Execution Despite HOC Error

Post by EleBern »

Thank you! This is much clearer now.
Post Reply