test_pickle.py 9.8 KB

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