test_pickle.py 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. from io import BytesIO
  2. import ast
  3. import pickle
  4. import pickletools
  5. import numpy as np
  6. import pytest
  7. import matplotlib as mpl
  8. from matplotlib import cm
  9. from matplotlib.testing import subprocess_run_helper
  10. from matplotlib.testing.decorators import check_figures_equal
  11. from matplotlib.dates import rrulewrapper
  12. from matplotlib.lines import VertexSelector
  13. import matplotlib.pyplot as plt
  14. import matplotlib.transforms as mtransforms
  15. import matplotlib.figure as mfigure
  16. from mpl_toolkits.axes_grid1 import parasite_axes # type: ignore
  17. def test_simple():
  18. fig = plt.figure()
  19. pickle.dump(fig, BytesIO(), pickle.HIGHEST_PROTOCOL)
  20. ax = plt.subplot(121)
  21. pickle.dump(ax, BytesIO(), pickle.HIGHEST_PROTOCOL)
  22. ax = plt.axes(projection='polar')
  23. plt.plot(np.arange(10), label='foobar')
  24. plt.legend()
  25. pickle.dump(ax, BytesIO(), pickle.HIGHEST_PROTOCOL)
  26. # ax = plt.subplot(121, projection='hammer')
  27. # pickle.dump(ax, BytesIO(), pickle.HIGHEST_PROTOCOL)
  28. plt.figure()
  29. plt.bar(x=np.arange(10), height=np.arange(10))
  30. pickle.dump(plt.gca(), BytesIO(), pickle.HIGHEST_PROTOCOL)
  31. fig = plt.figure()
  32. ax = plt.axes()
  33. plt.plot(np.arange(10))
  34. ax.set_yscale('log')
  35. pickle.dump(fig, BytesIO(), pickle.HIGHEST_PROTOCOL)
  36. def _generate_complete_test_figure(fig_ref):
  37. fig_ref.set_size_inches((10, 6))
  38. plt.figure(fig_ref)
  39. plt.suptitle('Can you fit any more in a figure?')
  40. # make some arbitrary data
  41. x, y = np.arange(8), np.arange(10)
  42. data = u = v = np.linspace(0, 10, 80).reshape(10, 8)
  43. v = np.sin(v * -0.6)
  44. # Ensure lists also pickle correctly.
  45. plt.subplot(3, 3, 1)
  46. plt.plot(list(range(10)))
  47. plt.ylabel("hello")
  48. plt.subplot(3, 3, 2)
  49. plt.contourf(data, hatches=['//', 'ooo'])
  50. plt.colorbar()
  51. plt.subplot(3, 3, 3)
  52. plt.pcolormesh(data)
  53. plt.subplot(3, 3, 4)
  54. plt.imshow(data)
  55. plt.ylabel("hello\nworld!")
  56. plt.subplot(3, 3, 5)
  57. plt.pcolor(data)
  58. ax = plt.subplot(3, 3, 6)
  59. ax.set_xlim(0, 7)
  60. ax.set_ylim(0, 9)
  61. plt.streamplot(x, y, u, v)
  62. ax = plt.subplot(3, 3, 7)
  63. ax.set_xlim(0, 7)
  64. ax.set_ylim(0, 9)
  65. plt.quiver(x, y, u, v)
  66. plt.subplot(3, 3, 8)
  67. plt.scatter(x, x ** 2, label='$x^2$')
  68. plt.legend(loc='upper left')
  69. plt.subplot(3, 3, 9)
  70. plt.errorbar(x, x * -0.5, xerr=0.2, yerr=0.4)
  71. plt.legend(draggable=True)
  72. fig_ref.align_ylabels() # Test handling of _align_label_groups Groupers.
  73. @mpl.style.context("default")
  74. @check_figures_equal(extensions=["png"])
  75. def test_complete(fig_test, fig_ref):
  76. _generate_complete_test_figure(fig_ref)
  77. # plotting is done, now test its pickle-ability
  78. pkl = pickle.dumps(fig_ref, pickle.HIGHEST_PROTOCOL)
  79. # FigureCanvasAgg is picklable and GUI canvases are generally not, but there should
  80. # be no reference to the canvas in the pickle stream in either case. In order to
  81. # keep the test independent of GUI toolkits, run it with Agg and check that there's
  82. # no reference to FigureCanvasAgg in the pickle stream.
  83. assert "FigureCanvasAgg" not in [arg for op, arg, pos in pickletools.genops(pkl)]
  84. loaded = pickle.loads(pkl)
  85. loaded.canvas.draw()
  86. fig_test.set_size_inches(loaded.get_size_inches())
  87. fig_test.figimage(loaded.canvas.renderer.buffer_rgba())
  88. plt.close(loaded)
  89. def _pickle_load_subprocess():
  90. import os
  91. import pickle
  92. path = os.environ['PICKLE_FILE_PATH']
  93. with open(path, 'rb') as blob:
  94. fig = pickle.load(blob)
  95. print(str(pickle.dumps(fig)))
  96. @mpl.style.context("default")
  97. @check_figures_equal(extensions=['png'])
  98. def test_pickle_load_from_subprocess(fig_test, fig_ref, tmp_path):
  99. _generate_complete_test_figure(fig_ref)
  100. fp = tmp_path / 'sinus.pickle'
  101. assert not fp.exists()
  102. with fp.open('wb') as file:
  103. pickle.dump(fig_ref, file, pickle.HIGHEST_PROTOCOL)
  104. assert fp.exists()
  105. proc = subprocess_run_helper(
  106. _pickle_load_subprocess,
  107. timeout=60,
  108. extra_env={'PICKLE_FILE_PATH': str(fp), 'MPLBACKEND': 'Agg'}
  109. )
  110. loaded_fig = pickle.loads(ast.literal_eval(proc.stdout))
  111. loaded_fig.canvas.draw()
  112. fig_test.set_size_inches(loaded_fig.get_size_inches())
  113. fig_test.figimage(loaded_fig.canvas.renderer.buffer_rgba())
  114. plt.close(loaded_fig)
  115. def test_gcf():
  116. fig = plt.figure("a label")
  117. buf = BytesIO()
  118. pickle.dump(fig, buf, pickle.HIGHEST_PROTOCOL)
  119. plt.close("all")
  120. assert plt._pylab_helpers.Gcf.figs == {} # No figures must be left.
  121. fig = pickle.loads(buf.getbuffer())
  122. assert plt._pylab_helpers.Gcf.figs != {} # A manager is there again.
  123. assert fig.get_label() == "a label"
  124. def test_no_pyplot():
  125. # tests pickle-ability of a figure not created with pyplot
  126. from matplotlib.backends.backend_pdf import FigureCanvasPdf
  127. fig = mfigure.Figure()
  128. _ = FigureCanvasPdf(fig)
  129. ax = fig.add_subplot(1, 1, 1)
  130. ax.plot([1, 2, 3], [1, 2, 3])
  131. pickle.dump(fig, BytesIO(), pickle.HIGHEST_PROTOCOL)
  132. def test_renderer():
  133. from matplotlib.backends.backend_agg import RendererAgg
  134. renderer = RendererAgg(10, 20, 30)
  135. pickle.dump(renderer, BytesIO())
  136. def test_image():
  137. # Prior to v1.4.0 the Image would cache data which was not picklable
  138. # once it had been drawn.
  139. from matplotlib.backends.backend_agg import new_figure_manager
  140. manager = new_figure_manager(1000)
  141. fig = manager.canvas.figure
  142. ax = fig.add_subplot(1, 1, 1)
  143. ax.imshow(np.arange(12).reshape(3, 4))
  144. manager.canvas.draw()
  145. pickle.dump(fig, BytesIO())
  146. def test_polar():
  147. plt.subplot(polar=True)
  148. fig = plt.gcf()
  149. pf = pickle.dumps(fig)
  150. pickle.loads(pf)
  151. plt.draw()
  152. class TransformBlob:
  153. def __init__(self):
  154. self.identity = mtransforms.IdentityTransform()
  155. self.identity2 = mtransforms.IdentityTransform()
  156. # Force use of the more complex composition.
  157. self.composite = mtransforms.CompositeGenericTransform(
  158. self.identity,
  159. self.identity2)
  160. # Check parent -> child links of TransformWrapper.
  161. self.wrapper = mtransforms.TransformWrapper(self.composite)
  162. # Check child -> parent links of TransformWrapper.
  163. self.composite2 = mtransforms.CompositeGenericTransform(
  164. self.wrapper,
  165. self.identity)
  166. def test_transform():
  167. obj = TransformBlob()
  168. pf = pickle.dumps(obj)
  169. del obj
  170. obj = pickle.loads(pf)
  171. # Check parent -> child links of TransformWrapper.
  172. assert obj.wrapper._child == obj.composite
  173. # Check child -> parent links of TransformWrapper.
  174. assert [v() for v in obj.wrapper._parents.values()] == [obj.composite2]
  175. # Check input and output dimensions are set as expected.
  176. assert obj.wrapper.input_dims == obj.composite.input_dims
  177. assert obj.wrapper.output_dims == obj.composite.output_dims
  178. def test_rrulewrapper():
  179. r = rrulewrapper(2)
  180. try:
  181. pickle.loads(pickle.dumps(r))
  182. except RecursionError:
  183. print('rrulewrapper pickling test failed')
  184. raise
  185. def test_shared():
  186. fig, axs = plt.subplots(2, sharex=True)
  187. fig = pickle.loads(pickle.dumps(fig))
  188. fig.axes[0].set_xlim(10, 20)
  189. assert fig.axes[1].get_xlim() == (10, 20)
  190. def test_inset_and_secondary():
  191. fig, ax = plt.subplots()
  192. ax.inset_axes([.1, .1, .3, .3])
  193. ax.secondary_xaxis("top", functions=(np.square, np.sqrt))
  194. pickle.loads(pickle.dumps(fig))
  195. @pytest.mark.parametrize("cmap", cm._colormaps.values())
  196. def test_cmap(cmap):
  197. pickle.dumps(cmap)
  198. def test_unpickle_canvas():
  199. fig = mfigure.Figure()
  200. assert fig.canvas is not None
  201. out = BytesIO()
  202. pickle.dump(fig, out)
  203. out.seek(0)
  204. fig2 = pickle.load(out)
  205. assert fig2.canvas is not None
  206. def test_mpl_toolkits():
  207. ax = parasite_axes.host_axes([0, 0, 1, 1])
  208. assert type(pickle.loads(pickle.dumps(ax))) == parasite_axes.HostAxes
  209. def test_standard_norm():
  210. assert type(pickle.loads(pickle.dumps(mpl.colors.LogNorm()))) \
  211. == mpl.colors.LogNorm
  212. def test_dynamic_norm():
  213. logit_norm_instance = mpl.colors.make_norm_from_scale(
  214. mpl.scale.LogitScale, mpl.colors.Normalize)()
  215. assert type(pickle.loads(pickle.dumps(logit_norm_instance))) \
  216. == type(logit_norm_instance)
  217. def test_vertexselector():
  218. line, = plt.plot([0, 1], picker=True)
  219. pickle.loads(pickle.dumps(VertexSelector(line)))
  220. def test_cycler():
  221. ax = plt.figure().add_subplot()
  222. ax.set_prop_cycle(c=["c", "m", "y", "k"])
  223. ax.plot([1, 2])
  224. ax = pickle.loads(pickle.dumps(ax))
  225. l, = ax.plot([3, 4])
  226. assert l.get_color() == "m"