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