a memory + CPU problem when using mpl_connect?


I am writing a small module to easily load images and interact with
them. I now face a memory usage +CPU problem, which may in fact be the
result of a normal behaviour of matplotlib (but more certainly something
I am not doing right), but I really like to be sure here because if
confirmed, it would mean that this module is almost not usable for large

Below is a (very simplified) example of what I am doing, that script
showing a similar behaviour than the real full module.

Questions and problems:
1/ when I load the image the first time (first imshow command in the
script), I see that, although the amount of data is very small,
ipython/matplotlib already uses more than 40 Mb of memory!
Is that normal?

2/ When I now use the mouse_move event, it can go up to 150 Mb of memory
usage!! Again: is that normal?

3/ When I do successive cycles of connect, disconnect, imshow (reloading
the figure), the memory usage seems to grow slightly, but more
importantly the capturing event (writing the data intensity on the top
right corner of the figure) gets REALLY SLOW.. Try a series of 10 imshow
with the same data..

So maybe I should "clean" some structure somewhere to allow a fast
interaction, or maybe the code below is rubish. But so far I haven't
found a cure, and this is very annoying to say the least.

Thanks for any tip on this problem.

P.S.: and for the sake of tempering the questions above (certainly
caused by my ignorance): matplotlib + numpy is just an amazing
P.P.S: set up for me is:

Suse 10.1
matplotlib version 0.87.7
numerix numpy 1.0.2.dev3491
Python 2.4.2 (#1, May 2 2006, 08:13:46)
IPython 0.7.4.svn.r2010 -- An enhanced Interactive Python.

## ===============================
## Below is a small script to illustrate memory and CPU pbs
## It loads some rand data, with coordinates given in xy
## and the mouse event allows to write the intensity of
## the image in the top right corner of the figure
## ===============================

import numpy as num
import matplotlib as mpl
import matplotlib.pylab as plab

data = num.random.rand(200,200)
xy = [num.arange(0.,20,.1), num.arange(0.,20,.1)]

fig = plab.figure()
canvas = fig.canvas
plab.imshow(data, extent=[0.,20.,0.,20.])
ftext = plab.figtext(0.9,0.9,"")

def whichpix_inframe(coord) :
   indw = num.zeros(2, num.int32)
   if len(coord) == 2 :
      indw[0] = num.sort(xy[0]).searchsorted(coord[0])
      indw[1] = num.sort(xy[1]).searchsorted(coord[1])
   return indw

def mouse_move(event) :
   if event.inaxes :
     ax = event.inaxes

id = canvas.mpl_connect('motion_notify_event', mouse_move)

## then we can go on and cycle through the next 3 lines...: it shows the
CPU+memory pb getting worse
# canvas.mpl_disconnect(id)
# plab.imshow(data, extent=[0.,20.,0.,20.])
# id = canvas.mpl_connect('motion_notify_event', mouse_move)

Hi Eric,


I am writing a small module to easily load images and interact with

Sorry for getting to this thread late - back from a quick holiday now.
I have written an image browser module that does what it sounds like
you're trying to do (and works with 3-D image stacks as well). It's
still quite rudimentary, and I hadn't really planned on sharing it in
its current state, but it may be that you can find some useful ideas
in the code. The image browser is a matplotlib canvas embedded in a wx
window, and I use the WxAgg backend - I've no idea how that will
change things from your setup. I run it interactively from ipython
-pylab as shown below.

Usage is pretty simple:

In [1]: import pyvis
In [2]: pv = pyvis.pyvis()
In [3]: pv.AddImg(arange(10000).reshape(100,100), 'my gradient')

then play around with the menus. More than one image can be loaded at
a time. I haven't had a close look at the memory usage, but it has
been working adequately for quite large image stacks
(1024x1024x250x8-bit). Feel free to use the code as you like.


pyvis.py (17.1 KB)


On 30/12/06, Eric Emsellem <emsellem@...419...> wrote:
AJC McMorland, PhD Student
Physiology, University of Auckland