C/C++ API

Anything that doesn't fit elsewhere.

C/C++ API

Postby stephanmg » Wed Jul 04, 2012 3:13 am

Dear NEURON Users,

i know that there exists the possibility to control NEURON via Python Code. (import neuron)

I would ask for a C/C++ API because i want to execute a NEURON Simulation in my C++ Application.

All the best,
Stephan
stephanmg
 
Posts: 5
Joined: Tue Jul 03, 2012 4:40 am

Re: C/C++ API

Postby hines » Thu Jul 05, 2012 8:59 am

There is not yet a formal API for NEURON but one can use the nrn/src/nrnpython cpp files for examples of how
python uses NEURON functions and data. Python loads NEURON as an extension and gets it initialized via
inithoc.cpp
After that you can execute any hoc statement (e.g. load_file) with hoc_valid_stmt(stmt, 0) as done by
nrnexec in nrnpy_hoc.cpp. The beginning of the file lists many NEURON functions used to retrieve information.
Section, Mechanism, Range Variable interaction is handled in nrnpy_nrn.cpp.
Also, many useful functions are listed in nrn/src/ivoc/oc2iv.h and nrn/src/nrniv/nrnoc2iv.h
hines
Site Admin
 
Posts: 980
Joined: Wed May 18, 2005 3:32 pm

Re: C/C++ API

Postby stephanmg » Tue Aug 07, 2012 7:38 am

Hi there Hines,

i've build NEURON with PYTHON support, therefore using:

import neuron
...
additional python NEURON code.


I'm using Python's functionality for embedding it into it with the high level Python API into C++: http://docs.python.org/extending/embedding.html

Example is:

extern "C" {
#include <Python.h>
}
Py_Initialize();
PyRun_SimpleString("from neuron import h\n");
Py_Finalize();



But this fails with:
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/home/stephan/local/lib/python2.7/site-packages/neuron/__init__.py", line 81, in <module>
import neuron.hoc
ImportError: /home/stephan/neuron/nrn/x86_64/lib/libnrnpython.so.0: undefined symbol: PyExc_ImportError
Error in sys.excepthook:
Traceback (most recent call last):
File "/usr/lib/python2.7/dist-packages/apport_python_hook.py", line 64, in apport_excepthook
from apport.fileutils import likely_packaged, get_recent_crashes
File "/usr/lib/python2.7/dist-packages/apport/__init__.py", line 1, in <module>
from apport.report import Report
File "/usr/lib/python2.7/dist-packages/apport/report.py", line 16, in <module>
from xml.parsers.expat import ExpatError
File "/usr/lib/python2.7/xml/parsers/expat.py", line 4, in <module>
from pyexpat import *
ImportError: /usr/lib/python2.7/lib-dynload/pyexpat.so: undefined symbol: _Py_ZeroStruct

Original exception was:
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/home/stephan/local/lib/python2.7/site-packages/neuron/__init__.py", line 81, in <module>
import neuron.hoc
ImportError: /home/stephan/neuron/nrn/x86_64/lib/libnrnpython.so.0: undefined symbol: PyExc_ImportError
terminate called without an active exception
Aborted (core dumped)



Please note: invoking the Python interpreter in a Bash shell, and import neuron module works.

Let me know if you can suggest anything.

All the best,
Stephan
stephanmg
 
Posts: 5
Joined: Tue Jul 03, 2012 4:40 am

Re: C/C++ API

Postby hines » Wed Aug 08, 2012 9:20 am

I'd start by leaving out the import neuron line and instead compare
import sys
sys.path

It may be that launching python sets up a bit more environment than what you are getting with Py_Initialize.
hines
Site Admin
 
Posts: 980
Joined: Wed May 18, 2005 3:32 pm


Return to Other questions

Who is online

Users browsing this forum: No registered users and 1 guest