nrnpython+matplotlib gui thread hangs

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

Moderator: hines

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

nrnpython+matplotlib gui thread hangs

Post by samnemo »

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.

Run sequence:

nrnpython("execfile('')") // see code below

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
    import gtk

# Thread class that executes processing
class NRNThread(Thread):
    """NRNThread Class."""
    def __init__(self,sleepdt=0.1):
        """Init NRNThread Class."""
        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/
                for manager in Gcf.get_all_fig_managers():
                figManager =  Gcf.get_active()
                if figManager is not None:

        # 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
Posts: 219
Joined: Fri Nov 28, 2008 3:38 pm
Location: Yale School of Public Health

Re: nrnpython+matplotlib gui thread hangs

Post by ramcdougal »

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("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)
for (x = 0; x < 3; x += 0.1) {
    g.line(x, x * x)
To make both of these interactive, we use the wrapper file

Code: Select all

from neuron import h, gui
and run via "python -i" (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