Skip to content

Fixed Image and Renderer pickling #3627

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 10, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions lib/matplotlib/backends/backend_agg.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,14 @@ def __init__(self, width, height, dpi):
if __debug__: verbose.report('RendererAgg.__init__ done',
'debug-annoying')

def __getstate__(self):
# We only want to preserve the init keywords of the Renderer.
# Anything else can be re-created.
return {'width': self.width, 'height': self.height, 'dpi': self.dpi}

def __setstate__(self, state):
self.__init__(state['width'], state['height'], state['dpi'])

def _get_hinting_flag(self):
if rcParams['text.hinting']:
return LOAD_FORCE_AUTOHINT
Expand Down
8 changes: 6 additions & 2 deletions lib/matplotlib/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,8 @@

import os
import warnings
import math

import numpy as np
from numpy import ma

from matplotlib import rcParams
import matplotlib.artist as martist
Expand Down Expand Up @@ -113,6 +111,12 @@ def __init__(self, ax,

self.update(kwargs)

def __getstate__(self):
state = super(_AxesImageBase, self).__getstate__()
# We can't pickle the C Image cached object.
state.pop('_imcache', None)
return state

def get_size(self):
"""Get the numrows, numcols of the input image"""
if self._A is None:
Expand Down
6 changes: 6 additions & 0 deletions lib/matplotlib/lines.py
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,12 @@ def __init__(self, xdata, ydata,
self._invalidy = True
self.set_data(xdata, ydata)

def __getstate__(self):
state = super(Line2D, self).__getstate__()
# _linefunc will be restored on draw time.
state.pop('_lineFunc', None)
return state

def contains(self, mouseevent):
"""
Test whether the mouse event occurred on the line. The pick
Expand Down
46 changes: 39 additions & 7 deletions lib/matplotlib/tests/test_pickle.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ def test_simple():
plt.plot(list(xrange(10)), label='foobar')
plt.legend()

# Uncomment to debug any unpicklable objects. This is slow so is not
# uncommented by default.
# recursive_pickle(fig)
pickle.dump(ax, BytesIO(), pickle.HIGHEST_PROTOCOL)

Expand Down Expand Up @@ -195,17 +197,47 @@ def test_complete():

def test_no_pyplot():
# tests pickle-ability of a figure not created with pyplot

import pickle as p
from matplotlib.backends.backend_pdf import FigureCanvasPdf as fc
from matplotlib.figure import Figure

fig = Figure()
can = fc(fig)
_ = fc(fig)
ax = fig.add_subplot(1, 1, 1)
ax.plot([1, 2, 3], [1, 2, 3])

# Uncomment to debug any unpicklable objects. This is slow so is not
# uncommented by default.
# recursive_pickle(fig)
pickle.dump(fig, BytesIO(), pickle.HIGHEST_PROTOCOL)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

keeping this comment around might be helpful...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Woops. I put it back in the wrong place. Still, it applies unilaterally.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I see it now.


def test_renderer():
from matplotlib.backends.backend_agg import RendererAgg
renderer = RendererAgg(10, 20, 30)
pickle.dump(renderer, BytesIO())


def test_image():
# Prior to v1.4.0 the Image would cache data which was not picklable
# once it had been drawn.
from matplotlib.backends.backend_agg import new_figure_manager
manager = new_figure_manager(1000)
fig = manager.canvas.figure
ax = fig.add_subplot(1, 1, 1)
ax.imshow(np.arange(12).reshape(3, 4))
manager.canvas.draw()
pickle.dump(fig, BytesIO())


def test_grid():
from matplotlib.backends.backend_agg import new_figure_manager
manager = new_figure_manager(1000)
fig = manager.canvas.figure
ax = fig.add_subplot(1, 1, 1)
ax.grid()
# Drawing the grid triggers instance methods to be attached
# to the Line2D object (_lineFunc).
manager.canvas.draw()

pickle.dump(ax, BytesIO())


if __name__ == '__main__':
import nose
nose.runmodule(argv=['-s'])