Using nrniv with IPython

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

Moderator: hines

Post Reply
eacheon
Posts: 97
Joined: Wed Jan 18, 2006 2:20 pm

Using nrniv with IPython

Post by eacheon »

Hello,

Here's how we can use nrniv with IPython:

Code: Select all

nrniv -python argv.py
argv.py as:

Code: Select all

#!/usr/bin/python

import sys
sys.argv = ['']

from IPython.Shell import IPythonShellEmbed
ipshell = IPythonShellEmbed()
ipshell()

This does not work with nrngui yet. Maybe IPython embeded shell does not know threads enough to not to block them. I may be able to have a look at the IPython/Shell.py later...

Or it might be something else when embeding python...
emuller
Posts: 15
Joined: Thu Mar 02, 2006 5:26 am
Location: Lausanne

NEURON as a python module

Post by emuller »

Are plans to eventually provide neuron as a python module?

i.e:

ipython -pylab

>>> import nrn
>>> ...

What would be the limitations, if any, in taking this route?

cheers,

Eilif
hines
Site Admin
Posts: 1682
Joined: Wed May 18, 2005 3:32 pm

Post by hines »

There are no specific plans in that regard. However, since NEURON is organized as a collection of shared objects, it seems to me that it would be straightforward to load the NEURON portion from a Python process.
eacheon
Posts: 97
Joined: Wed Jan 18, 2006 2:20 pm

Post by eacheon »

As Micheal pointed out, doNotify() need to be called periodically in a separate thread to make mainmenu reacting to GUI operations.

IPython is doing similar things to GTK, Qt GUIs, but it is said to do it via python binding of those toolkits. Does anyone know of existence of an Interview python binding?
hines
Site Admin
Posts: 1682
Joined: Wed May 18, 2005 3:32 pm

Post by hines »

I should mention that there is a development branch for NEURON + Python at https://facets-vm3.kip.uni-heidelberg.d ... etsetcall/
http://neuralensemble.kip.uni-heidelber ... getsetcall
where changeset 17 contains a first pass at allowing NEURON to be an extension to Python. There are instructions in the log message about how to build and some
tentative discussion about how to put the
doNotify into a loop timer so InterViews can handle its window events.
The loop timer I have been experimenting with is (if you don't have an hh.ses file handy, comment that out and uncomment the nrngui.hoc load statement)

Code: Select all

[hines@localhost npy]$ cat testext.py
import sys
sys.path.append("/home/hines/lib64/python")
import hoc
h = hoc.HocObject()
#h('load_file("nrngui.hoc")')
h('load_file("hh.ses")')

import threading
import time
def f() :
  #h.doEvents()
  h.doNotify()
  #print "timer"

class LoopTimer(threading.Thread) :
  """
  a Timer that calls f every interval
  """
  def __init__(self, interval, fun) :
    """
    @param interval: time in seconds between call to fun()
    @param fun: the function to call on timer update
    """
    self.interval = interval
    self.fun = fun
    threading.Thread.__init__(self)
    self.setDaemon(True)

  def run(self) :
    while True:
      self.fun()
      time.sleep(self.interval)

timer = LoopTimer(.05, f)
timer.start()
print "hello"
[hines@localhost npy]$
eacheon
Posts: 97
Joined: Wed Jan 18, 2006 2:20 pm

Post by eacheon »

I had difficulty building r17:

gcc version 4.1.2 20061115 (prerelease) (Debian 4.1.1-21)

Code: Select all

/bin/bash ../../libtool --tag=CXX   --mode=compile g++ -DHAVE_CONFIG_H -I. -I../.. -I../../src/oc -I../../src/parallel -I../../src/nrnjava -I../../src/nrncvode -I../../src/ivos -I../../src/sundials -I. -I../../src/nrniv -I../../src/ivoc -I../../src/nrnoc -I../../src/oc -I/usr/include/python2.4 -I/usr/nrn59/iv/include  -DOOP=1 -DCABLE=1 -DUSECVODE=1 -DUSEMATRIX=1 -DUSEBBS=1   -g -O2 -MT nrnpy_hoc.lo -MD -MP -MF .deps/nrnpy_hoc.Tpo -c -o nrnpy_hoc.lo nrnpy_hoc.cpp
 g++ -DHAVE_CONFIG_H -I. -I../.. -I../../src/oc -I../../src/parallel -I../../src/nrnjava -I../../src/nrncvode -I../../src/ivos -I../../src/sundials -I. -I../../src/nrniv -I../../src/ivoc -I../../src/nrnoc -I../../src/oc -I/usr/include/python2.4 -I/usr/nrn59/iv/include -DOOP=1 -DCABLE=1 -DUSECVODE=1 -DUSEMATRIX=1 -DUSEBBS=1 -g -O2 -MT nrnpy_hoc.lo -MD -MP -MF .deps/nrnpy_hoc.Tpo -c nrnpy_hoc.cpp  -fPIC -DPIC -o .libs/nrnpy_hoc.o
nrnpy_hoc.cpp: In function 'PyObject* hocobj_getattro(PyHocObject*, PyObject*)':
nrnpy_hoc.cpp:499: error: jump to case label
nrnpy_hoc.cpp:490: error:   crosses initialization of 'PyHocObject* po'
nrnpy_hoc.cpp: In function 'int hocobj_setattro(PyHocObject*, PyObject*, PyObject*)':
nrnpy_hoc.cpp:587: error: jump to case label
nrnpy_hoc.cpp:574: error:   crosses initialization of 'Object** op'
nrnpy_hoc.cpp: In function 'int hocobj_setitem(PyObject*, int, PyObject*)':
nrnpy_hoc.cpp:699: error: jump to case label
nrnpy_hoc.cpp:686: error:   crosses initialization of 'Object** op'
eacheon
Posts: 97
Joined: Wed Jan 18, 2006 2:20 pm

Post by eacheon »

Seems r1729 of NEURON fixed this.
eacheon wrote:I had difficulty building r17:

gcc version 4.1.2 20061115 (prerelease) (Debian 4.1.1-21)

Code: Select all

/bin/bash ../../libtool --tag=CXX   --mode=compile g++ -DHAVE_CONFIG_H -I. -I../.. -I../../src/oc -I../../src/parallel -I../../src/nrnjava -I../../src/nrncvode -I../../src/ivos -I../../src/sundials -I. -I../../src/nrniv -I../../src/ivoc -I../../src/nrnoc -I../../src/oc -I/usr/include/python2.4 -I/usr/nrn59/iv/include  -DOOP=1 -DCABLE=1 -DUSECVODE=1 -DUSEMATRIX=1 -DUSEBBS=1   -g -O2 -MT nrnpy_hoc.lo -MD -MP -MF .deps/nrnpy_hoc.Tpo -c -o nrnpy_hoc.lo nrnpy_hoc.cpp
 g++ -DHAVE_CONFIG_H -I. -I../.. -I../../src/oc -I../../src/parallel -I../../src/nrnjava -I../../src/nrncvode -I../../src/ivos -I../../src/sundials -I. -I../../src/nrniv -I../../src/ivoc -I../../src/nrnoc -I../../src/oc -I/usr/include/python2.4 -I/usr/nrn59/iv/include -DOOP=1 -DCABLE=1 -DUSECVODE=1 -DUSEMATRIX=1 -DUSEBBS=1 -g -O2 -MT nrnpy_hoc.lo -MD -MP -MF .deps/nrnpy_hoc.Tpo -c nrnpy_hoc.cpp  -fPIC -DPIC -o .libs/nrnpy_hoc.o
nrnpy_hoc.cpp: In function 'PyObject* hocobj_getattro(PyHocObject*, PyObject*)':
nrnpy_hoc.cpp:499: error: jump to case label
nrnpy_hoc.cpp:490: error:   crosses initialization of 'PyHocObject* po'
nrnpy_hoc.cpp: In function 'int hocobj_setattro(PyHocObject*, PyObject*, PyObject*)':
nrnpy_hoc.cpp:587: error: jump to case label
nrnpy_hoc.cpp:574: error:   crosses initialization of 'Object** op'
nrnpy_hoc.cpp: In function 'int hocobj_setitem(PyObject*, int, PyObject*)':
nrnpy_hoc.cpp:699: error: jump to case label
nrnpy_hoc.cpp:686: error:   crosses initialization of 'Object** op'
eacheon
Posts: 97
Joined: Wed Jan 18, 2006 2:20 pm

Re: NEURON as a python module

Post by eacheon »

emuller wrote:Are plans to eventually provide neuron as a python module?

i.e:

ipython -pylab

>>> import nrn
>>> ...

What would be the limitations, if any, in taking this route?

cheers,

Eilif
would an nrn.load_dll(path_to_so) be provided so external mechanisms can be loaded?
emuller
Posts: 15
Joined: Thu Mar 02, 2006 5:26 am
Location: Lausanne

Re: NEURON as a python module

Post by emuller »

eacheon wrote: would an nrn.load_dll(path_to_so) be provided so external mechanisms can be loaded?
This sounds like a good idea to me, however I must admit I don't know the specifics of NEURON's mechanism loader. We did have some problems getting pynest to dynamically load external mechanisms, but we were trying to allow NEST/SLI calls to load mechanisms. A python call should be much less problematic to implement ... and the hoc dll loader should probably be disabled when using hoc as a python module ... with a message "please use nrn.load_mechanism(path_to_so)"

I won't have time to look at this for the next little while, but please check out
http://neuralensemble.org/trac/nrnpy and contribute !
emuller
Posts: 15
Joined: Thu Mar 02, 2006 5:26 am
Location: Lausanne

Post by emuller »

The big question in my mind is: how can I get python to load a dll which wasn't built for python ... I guess something from ctypes would help here.

in neuron.py we could define something like:

Code: Select all

def load_mechanism(dll_file):
    import ctypes
    dll = ctypes.CDLL(dll_file)
    hoc.register_py_dll(dll)
then register_py_dll should do much the same thing as already done in hoc NEURON:

I assume find a certain indentifier in the dll and hook it in somewhere in NEURON's symbol table.
hines
Site Admin
Posts: 1682
Joined: Wed May 18, 2005 3:32 pm

Post by hines »

By referring to 'dll' I assume you are talking about the mswin version. Because some people have been experiencing difficulty on new machines with loading the old style nrnmech.dll (see
https://www.neuron.yale.edu/phpBB2/viewtopic.php?t=750
11 Sep 2007 message by hines) I've switched to standard mswin dlls. As a side effect of this change there are now cygnrniv.dll and cygIVhines-3.dll which should be importable into python (and ipython) as well as the nrnmech.dll.
Post Reply