Workaround for 0.87 WXAgg Bug

Onsi,

Thank you very much

This work around worked for me. I haven't tested everything but I can make a plot successfully now with WXAgg.

Have you reported this bug?

Sam

···

On 26 Feb, 2006, at 17:31, Onsi Fakhouri wrote:

Hi Samuel,

I was looking through the matplotlib-users mailing list archive and came across your 2006-02-25 post. I've been having the same problem and after prodding around, seem to have come up with a temporary workaround.

If you look in the
/Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/site-packages/matplotlib/backends/backend_wxagg.py

file you'll see that there are two methods that can be used to run convert_agg_to_wx_bitmap. By default matplotlib tries to import _wxagg and use the _wxagg.convert_agg_to_wx_bitmap version of this method.

This version is a compiled C++ shared library. The C++ code is a little cryptic, but it basically looks like the code is failing to allocate memory for the bitmap object.

Fortunately, backend_wxagg.py includes a python alternative called _py_convert_agg_to_wx_bitmap. This uses python code to perform the agg->wx.bitmap conversion which, being written in python and not in C++, is slower (the speed's fine in my opinion) -- but actually works.

So for, now, a temporary solution until the C++ issue is resolved is to get backend_wxagg.py to use the python versions of the convert method. To do this replace the following code in backend_wxagg.py at line 228:

# try to load the WXAgg accelerator

try:
    import _wxagg
except ImportError:
    _wxagg = None

# if it's present, use it
_use_accelerator(True)

with this:

# try to load the WXAgg accelerator
try:
    import _wxagg
except ImportError:
    _wxagg = None
#It imported without any problems --- but it doesn't actually work!
_wxagg=None

# if it's present, use it
_use_accelerator(True)

Let me know if this helps. It seems to have done the trick for me. But I haven't tested it too extensively.

Take care,

Onsi

**********************************************************************
Samuel M. Smith Ph.D.
2966 Fort Hill Road
Eagle Mountain, Utah 84043
801-768-2768 voice
801-768-2769 fax
**********************************************************************
"The greatest source of failure and unhappiness in the world is
giving up what we want most for what we want at the moment"
**********************************************************************

Sorry I'm so late in replying. I've been looking into this problem today but haven't been able to reproduce it myself. Could you or Onsi please send me a short script that causes the MemoryError, along information about your versions of Python, wxPython, and matplotlib?

As an aside, you can disable the accelerator without editing backend_wxagg.py:

     import matplotlib.backends.backend_wxagg
     matplotlib.backends.backend_wxagg._use_accelerator(False)

Ken

···

On Feb 27, 2006, at 11:19 AM, Samuel M. Smith wrote:

This work around worked for me. I haven't tested everything but I can make a plot successfully now with WXAgg.

Hi Ken,

Thanks for the tip. It takes very little to get this bug on my system:

>>> import pylab

>>> pylab.plot([1,2])
[<matplotlib.lines.Line2D instance at 0x32b29e0>]

>>> pylab.show()
Traceback (most recent call last):
   File "/Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/site-packages/matplotlib/backends/backend_wx.py", line 1048, in _onPaint
     self.draw(repaint=False)
   File "/Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/site-packages/matplotlib/backends/backend_wxagg.py", line 62, in draw
     self.bitmap = _convert_agg_to_wx_bitmap(self.get_renderer(), None)
MemoryError: _wxagg.convert_agg_to_wx_bitmap(): could not create the wx.Bitmap
Traceback (most recent call last):
   File "<stdin>", line 1, in ?
   File "/Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/site-packages/matplotlib/backends/backend_wx.py", line 1193, in show
     figwin.canvas.draw()
   File "/Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/site-packages/matplotlib/backends/backend_wxagg.py", line 62, in draw
     self.bitmap = _convert_agg_to_wx_bitmap(self.get_renderer(), None)
MemoryError: _wxagg.convert_agg_to_wx_bitmap(): could not create the wx.Bitmap

The problem is in the _wxagg.so library which was compiled during install. Looking through the _wxagg.cpp code it looks like

wxPyConstructObject(bitmap, _T("wxBitmap"),1); (line 129)

is returning null. Ken, if you want me to try something let me know -- my guess is I'm using a different version of either the AGG library or wx.

As an aside, does the cpp implementation run much faster than the python implementation?

Onsi

···

On Mar 10, 2006, at 11:45 AM, Ken McIvor wrote:

On Feb 27, 2006, at 11:19 AM, Samuel M. Smith wrote:

This work around worked for me. I haven't tested everything but I can make a plot successfully now with WXAgg.

Sorry I'm so late in replying. I've been looking into this problem today but haven't been able to reproduce it myself. Could you or Onsi please send me a short script that causes the MemoryError, along information about your versions of Python, wxPython, and matplotlib?

As an aside, you can disable the accelerator without editing backend_wxagg.py:

    import matplotlib.backends.backend_wxagg
    matplotlib.backends.backend_wxagg._use_accelerator(False)

Ken

-------------------------------------------------------
This SF.Net email is sponsored by xPML, a groundbreaking scripting language
that extends applications into web and mobile media. Attend the live webcast
and join the prime developer group breaking into this new coding territory!
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=110944&bid=241720&dat=121642
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

Ken, if you want me to try something let me know -- my guess is I'm using a different version of either the AGG library or wx.

I'm afraid that I'm at a loss right now. Knowing exactly what version of MacOS, Python, wxPython, and matplotlib you're using might help.

If you have MacOS 10.4, Python 2.3, and wxPython 2.6.2.1, then you could see if you have the same problem with my build of matplotlib:

  http://agni.phys.iit.edu/~kmcivor/downloads/matplotlib-0.87.1-py2.3-macosx-10.4-ppc.egg

As an aside, does the cpp implementation run much faster than the python implementation?

You probably won't notice a difference unless you're doing animations. If you are, then it provides a big speedup under Linux but much less of a speedup on a Mac. I think the difference is due to the way wxWidgets is implemented on the two platforms.

Ken

···

On Mar 10, 2006, at 2:01 PM, Onsi Fakhouri wrote:

I am using the SVN build of matplotlib last week.

I have included console dumps below.

The first is using pythonw the second using ipython

pythonw anim.py
loaded rc file /Users/smithsm/.matplotlib/matplotlibrc
matplotlib version 0.87.1
verbose.level helpful
interactive is False
platform is darwin
numerix numpy 0.9.5.2006
font search path ['/Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/site-packages/matplotlib/mpl-data']
$HOME=/Users/smithsm
CONFIGDIR=/Users/smithsm/.matplotlib
loaded ttfcache file /Users/smithsm/.matplotlib/ttffont.cache
matplotlib data path /Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/site-packages/matplotlib/mpl-data
backend WXAgg version 2.6.2.1
Traceback (most recent call last):
   File "/Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/site-packages/matplotlib/backends/backend_wx.py", line 1048, in _onPaint
     self.draw(repaint=False)
   File "/Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/site-packages/matplotlib/backends/backend_wxagg.py", line 62, in draw
     self.bitmap = _convert_agg_to_wx_bitmap(self.get_renderer(), None)
MemoryError: _wxagg.convert_agg_to_wx_bitmap(): could not create the wx.Bitmap
Traceback (most recent call last):
   File "anim.py", line 27, in ?
     line, = plot(x,sin(x))
   File "/Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/site-packages/matplotlib/pylab.py", line 2127, in plot
     b = ishold()
   File "/Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/site-packages/matplotlib/pylab.py", line 967, in ishold
     return gca().ishold()
   File "/Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/site-packages/matplotlib/pylab.py", line 918, in gca
     ax = gcf().gca(**kwargs)
   File "/Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/site-packages/matplotlib/pylab.py", line 928, in gcf
     return figure()
   File "/Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/site-packages/matplotlib/pylab.py", line 898, in figure
     draw_if_interactive()
   File "/Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/site-packages/matplotlib/backends/backend_wx.py", line 1172, in draw_if_interactive
     figManager.canvas.draw()
   File "/Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/site-packages/matplotlib/backends/backend_wxagg.py", line 62, in draw
     self.bitmap = _convert_agg_to_wx_bitmap(self.get_renderer(), None)
MemoryError: _wxagg.convert_agg_to_wx_bitmap(): could not create the wx.Bitmap

···

***********************************************

ipython -pylab
loaded rc file /Users/smithsm/.matplotlib/matplotlibrc
matplotlib version 0.87.1
verbose.level helpful
interactive is False
platform is darwin
numerix numpy 0.9.5.2006
font search path ['/Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/site-packages/matplotlib/mpl-data']
$HOME=/Users/smithsm
CONFIGDIR=/Users/smithsm/.matplotlib
loaded ttfcache file /Users/smithsm/.matplotlib/ttffont.cache
matplotlib data path /Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/site-packages/matplotlib/mpl-data
backend WXAgg version 2.6.2.1
Python 2.4.2 (#3, Jan 8 2006, 20:33:07)
Type "copyright", "credits" or "license" for more information.

IPython 0.7.1.fix1 -- An enhanced Interactive Python.
? -> Introduction to IPython's features.
%magic -> Information about IPython's 'magic' % functions.
help -> Python's own help system.
object? -> Details about 'object'. ?object also works, ?? prints more.

   Welcome to pylab, a matplotlib-based Python environment.
   For more information, type 'help(pylab)'.

In [1]: plot([1,2,3])
---------------------------------------------------------------------------
exceptions.MemoryError Traceback (most recent call last)

/Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/site-packages/matplotlib/backends/backend_wx.py in _onPaint(self, evt)
    1046 self.realize()
    1047 # Render to the bitmap
-> 1048 self.draw(repaint=False)
    1049 # Update the display using a PaintDC
    1050 self.gui_repaint(drawDC=wx.PaintDC(self))

/Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/site-packages/matplotlib/backends/backend_wxagg.py in draw(self, repaint)
      60 FigureCanvasAgg.draw(self)
      61
---> 62 self.bitmap = _convert_agg_to_wx_bitmap(self.get_renderer(), None)
      63 if repaint:
      64 self.gui_repaint()

MemoryError: _wxagg.convert_agg_to_wx_bitmap(): could not create the wx.Bitmap
---------------------------------------------------------------------------
exceptions.MemoryError Traceback (most recent call last)

/Users/smithsm/<ipython console>

/Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/site-packages/matplotlib/pylab.py in plot(*args, **kwargs)
    2125 def plot(*args, **kwargs):
    2126 # allow callers to override the hold state by passing hold=True|False
-> 2127 b = ishold()
    2128 h = popd(kwargs, 'hold', None)
    2129 if h is not None:

/Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/site-packages/matplotlib/pylab.py in ishold()
     965 Return the hold status of the current axes
     966 """
--> 967 return gca().ishold()
     968
     969 def isinteractive():

/Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/site-packages/matplotlib/pylab.py in gca(**kwargs)
     916 """
     917
--> 918 ax = gcf().gca(**kwargs)
     919 return ax
     920

/Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/site-packages/matplotlib/pylab.py in gcf()
     926 return figManager.canvas.figure
     927 else:
--> 928 return figure()
     929
     930 def gci():

/Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/site-packages/matplotlib/pylab.py in figure(num, figsize, dpi, facecolor, edgecolor, frameon)
     896 figManager.canvas.figure.number = num
     897
--> 898 draw_if_interactive()
     899 return figManager.canvas.figure
     900

/Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/site-packages/IPython/genutils.py in wrapper(*args, **kw)
     802 def wrapper(*args,**kw):
     803 wrapper.called = False
--> 804 out = func(*args,**kw)
     805 wrapper.called = True
     806 return out

/Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/site-packages/matplotlib/backends/backend_wx.py in draw_if_interactive()
    1170 figManager = Gcf.get_active()
    1171 if figManager is not None:
-> 1172 figManager.canvas.draw()
    1173
    1174

/Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/site-packages/matplotlib/backends/backend_wxagg.py in draw(self, repaint)
      60 FigureCanvasAgg.draw(self)
      61
---> 62 self.bitmap = _convert_agg_to_wx_bitmap(self.get_renderer(), None)
      63 if repaint:
      64 self.gui_repaint()

MemoryError: _wxagg.convert_agg_to_wx_bitmap(): could not create the wx.Bitmap

In [2]: ---------------------------------------------------------------------------
exceptions.MemoryError Traceback (most recent call last)

/Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/site-packages/matplotlib/backends/backend_wx.py in _onPaint(self, evt)
    1046 self.realize()
    1047 # Render to the bitmap
-> 1048 self.draw(repaint=False)
    1049 # Update the display using a PaintDC
    1050 self.gui_repaint(drawDC=wx.PaintDC(self))

/Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/site-packages/matplotlib/backends/backend_wxagg.py in draw(self, repaint)
      60 FigureCanvasAgg.draw(self)
      61
---> 62 self.bitmap = _convert_agg_to_wx_bitmap(self.get_renderer(), None)
      63 if repaint:
      64 self.gui_repaint()

MemoryError: _wxagg.convert_agg_to_wx_bitmap(): could not create the wx.Bitmap

On 11 Mar, 2006, at 10:25, Ken McIvor wrote:

On Mar 10, 2006, at 2:01 PM, Onsi Fakhouri wrote:

Ken, if you want me to try something let me know -- my guess is I'm using a different version of either the AGG library or wx.

I'm afraid that I'm at a loss right now. Knowing exactly what version of MacOS, Python, wxPython, and matplotlib you're using might help.

If you have MacOS 10.4, Python 2.3, and wxPython 2.6.2.1, then you could see if you have the same problem with my build of matplotlib:

  http://agni.phys.iit.edu/~kmcivor/downloads/matplotlib-0.87.1-py2.3-macosx-10.4-ppc.egg

As an aside, does the cpp implementation run much faster than the python implementation?

You probably won't notice a difference unless you're doing animations. If you are, then it provides a big speedup under Linux but much less of a speedup on a Mac. I think the difference is due to the way wxWidgets is implemented on the two platforms.

Ken

-------------------------------------------------------
This SF.Net email is sponsored by xPML, a groundbreaking scripting language
that extends applications into web and mobile media. Attend the live webcast
and join the prime developer group breaking into this new coding territory!
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=110944&bid=241720&dat=121642
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

**********************************************************************
Samuel M. Smith Ph.D.
2966 Fort Hill Road
Eagle Mountain, Utah 84043
801-768-2768 voice
801-768-2769 fax
**********************************************************************
"The greatest source of failure and unhappiness in the world is
giving up what we want most for what we want at the moment"
**********************************************************************