nrnpython+matplotlib gui thread hangs

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

Moderator: hines

Post Reply
samnemo
Posts: 14
Joined: Sun Dec 12, 2010 1:14 pm

nrnpython+matplotlib gui thread hangs

Post by samnemo » Sat Dec 14, 2013 11:15 am

I'm having some issues using matplotlib plots from the hoc side with nrnpython
due to threading issues. The problem is that the matplotlib gui thread seems
to prevent response from the hoc interpreter. Previously, the code sequence
below was effective in dealing with this issue (similar to ipython in that there's
a separate loop for graphics). Right now, when running with python 2.7.3
and NEURON 7.4 (978:41e073aceb37) 2013-10-30, the interpreter hangs. If any
python gurus have suggestions on how to fix this problem, please let me know.
Thanks

Run sequence:

nrniv
nrnpython("execfile('nrnivthread.py')") // see nrnivthread.py code below
nrnpython("plot([5,1,3])")

Note that this warning comes up :
QCoreApplication::sendPostedEvents: Cannot send posted events for objects in another thread

Code: Select all

# load with execfile()
# from neuron import h # h should be present in the global namespace

import time
from threading import *
from pylab import *
from matplotlib._pylab_helpers import Gcf
hasgtk=True
try:
    import gtk
except:
    hasgtk=False

# Thread class that executes processing
class NRNThread(Thread):
    """NRNThread Class."""
    def __init__(self,sleepdt=0.1):
        """Init NRNThread Class."""
        Thread.__init__(self)
        self._want_abort = 0
        self.sleepdt = sleepdt
    def run(self):
        from neuron import h
        """Run NRNThread."""
        # code executing in the new thread
        while True:
            if self._want_abort:
                return "DONE"
            h.doNotify()  # specific for handling hoc graphics
            if hasgtk:
                # messing with code from : (/usr/lib/pymodules/python2.6/matplotlib/backends/backend_gtk.py:66)
                for manager in Gcf.get_all_fig_managers():
                    manager.window.show()
                figManager =  Gcf.get_active()
                if figManager is not None:
                    figManager.canvas.draw() 
                    figManager.canvas.flush_events()
            time.sleep(self.sleepdt)

        # Here's where the result would be returned 
    def abort(self):
        """abort NRNThread."""
        # Method for use by main thread to signal an abort
        self._want_abort = 1

bg = NRNThread() # these 2 lines properly belong in the calling routines
bg.start()  # start is part of base class Thread

ramcdougal
Posts: 153
Joined: Fri Nov 28, 2008 3:38 pm
Location: Yale School of Medicine

Re: nrnpython+matplotlib gui thread hangs

Post by ramcdougal » Tue Dec 17, 2013 7:47 pm

As of 7.4 (978), Python threads do not execute when the HOC interpreter is waiting for input.

To do interactive matplotlib and interviews graphics simultaneously from HOC, simply run a two-line python wrapper.

For example, test.hoc, below plots y = x ^ 2 in both Interviews and matplotlib windows:

Code: Select all

nrnpython("import matplotlib")
nrnpython("matplotlib.interactive(True)")
nrnpython("from matplotlib import pyplot")
nrnpython("import numpy")
nrnpython("pyplot.plot(numpy.linspace(0, 3), numpy.linspace(0, 3) ** 2)")
objref g
g = new Graph()
g.size(0, 3, 0, 9)
g.beginline()
for (x = 0; x < 3; x += 0.1) {
    g.line(x, x * x)
}
g.flush()
To make both of these interactive, we use the wrapper file test.py:

Code: Select all

from neuron import h, gui
h.load_file('test.hoc')
and run via "python -i test.py" (The -i causes python to give us a prompt instead of exiting after the script terminates. This approximates the behavior of nrniv -python.)

Post Reply