Page 1 of 1

Using nrniv with IPython

Posted: Thu Sep 28, 2006 12:16 am
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...

NEURON as a python module

Posted: Mon Oct 02, 2006 5:25 am
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

Posted: Tue Oct 03, 2006 7:28 am
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.

Posted: Wed May 09, 2007 3:41 pm
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?

Posted: Wed May 09, 2007 4:38 pm
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]$

Posted: Wed May 09, 2007 11:30 pm
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'

Posted: Thu May 10, 2007 3:16 am
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'

Re: NEURON as a python module

Posted: Fri May 25, 2007 2:14 pm
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?

Re: NEURON as a python module

Posted: Fri Jun 08, 2007 3:14 am
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 !

Posted: Fri Jun 08, 2007 3:29 am
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.

Posted: Mon Sep 17, 2007 7:46 am
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.