-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathplugin.py
385 lines (304 loc) · 15 KB
/
plugin.py
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
# Copyright (c) 2015, Thomas P. Robitaille
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
# The code below includes code adapted from WCSAxes, which is released
# under a 3-clause BSD license and can be found here:
#
# https://github.com/astrofrog/wcsaxes
from functools import wraps
import contextlib
import os
import sys
import shutil
import inspect
import tempfile
import warnings
from distutils.version import LooseVersion
import pytest
if sys.version_info[0] == 2:
from urllib import urlopen
string_types = basestring # noqa
else:
from urllib.request import urlopen
string_types = str
SHAPE_MISMATCH_ERROR = """Error: Image dimensions did not match.
Expected shape: {expected_shape}
{expected_path}
Actual shape: {actual_shape}
{actual_path}"""
def _download_file(baseline, filename):
# Note that baseline can be a comma-separated list of URLs that we can
# then treat as mirrors
for base_url in baseline.split(','):
try:
u = urlopen(base_url + filename)
content = u.read()
except Exception as e:
warnings.warn('Downloading {0} failed: {1}'.format(base_url + filename, e))
else:
break
else:
raise Exception("Could not download baseline image from any of the "
"available URLs")
result_dir = tempfile.mkdtemp()
filename = os.path.join(result_dir, 'downloaded')
with open(filename, 'wb') as tmpfile:
tmpfile.write(content)
return filename
def pytest_report_header(config, startdir):
import matplotlib
import matplotlib.ft2font
return ["Matplotlib: {0}".format(matplotlib.__version__),
"Freetype: {0}".format(matplotlib.ft2font.__freetype_version__)]
def pytest_addoption(parser):
group = parser.getgroup("matplotlib image comparison")
group.addoption('--mpl', action='store_true',
help="Enable comparison of matplotlib figures to reference files")
group.addoption('--mpl-generate-path',
help="directory to generate reference images in, relative "
"to location where py.test is run", action='store')
group.addoption('--mpl-baseline-path',
help="directory containing baseline images, relative to "
"location where py.test is run. This can also be a URL or a "
"set of comma-separated URLs (in case mirrors are "
"specified)", action='store')
results_path_help = "directory for test results, relative to location where py.test is run"
group.addoption('--mpl-results-path', help=results_path_help, action='store')
parser.addini('mpl-results-path', help=results_path_help)
def pytest_configure(config):
config.addinivalue_line('markers',
"mpl_image_compare: Compares matplotlib figures "
"against a baseline image")
if config.getoption("--mpl") or config.getoption("--mpl-generate-path") is not None:
baseline_dir = config.getoption("--mpl-baseline-path")
generate_dir = config.getoption("--mpl-generate-path")
results_dir = config.getoption("--mpl-results-path") or config.getini("mpl-results-path")
# Note that results_dir is an empty string if not specified
if not results_dir:
results_dir = None
if generate_dir is not None:
if baseline_dir is not None:
warnings.warn("Ignoring --mpl-baseline-path since --mpl-generate-path is set")
if results_dir is not None and generate_dir is not None:
warnings.warn("Ignoring --mpl-result-path since --mpl-generate-path is set")
if baseline_dir is not None and not baseline_dir.startswith(("https", "http")):
baseline_dir = os.path.abspath(baseline_dir)
if generate_dir is not None:
baseline_dir = os.path.abspath(generate_dir)
if results_dir is not None:
results_dir = os.path.abspath(results_dir)
config.pluginmanager.register(ImageComparison(config,
baseline_dir=baseline_dir,
generate_dir=generate_dir,
results_dir=results_dir))
else:
config.pluginmanager.register(FigureCloser(config))
@contextlib.contextmanager
def switch_backend(backend):
import matplotlib
import matplotlib.pyplot as plt
prev_backend = matplotlib.get_backend().lower()
if prev_backend != backend.lower():
plt.switch_backend(backend)
yield
plt.switch_backend(prev_backend)
else:
yield
def close_mpl_figure(fig):
"Close a given matplotlib Figure. Any other type of figure is ignored"
import matplotlib.pyplot as plt
from matplotlib.figure import Figure
# We only need to close actual Matplotlib figure objects. If
# we are dealing with a figure-like object that provides
# savefig but is not a real Matplotlib object, we shouldn't
# try closing it here.
if isinstance(fig, Figure):
plt.close(fig)
def get_marker(item, marker_name):
if hasattr(item, 'get_closest_marker'):
return item.get_closest_marker(marker_name)
else:
# "item.keywords.get" was deprecated in pytest 3.6
# See https://docs.pytest.org/en/latest/mark.html#updating-code
return item.keywords.get(marker_name)
def _raise_on_image_difference(expected, actual, tol):
"""
Based on matplotlib.testing.decorators._raise_on_image_difference
Compare image size ourselves since the Matplotlib
exception is a bit cryptic in this case and doesn't show
the filenames
"""
from matplotlib.image import imread
from matplotlib.testing.compare import compare_images
expected_shape = imread(expected).shape[:2]
actual_shape = imread(actual).shape[:2]
if expected_shape != actual_shape:
error = SHAPE_MISMATCH_ERROR.format(expected_path=expected,
expected_shape=expected_shape,
actual_path=actual,
actual_shape=actual_shape)
pytest.fail(error, pytrace=False)
msg = compare_images(expected, actual, tol=tol)
if msg is None:
shutil.rmtree(os.path.dirname(expected))
else:
pytest.fail(msg, pytrace=False)
class ImageComparison(object):
def __init__(self, config, baseline_dir=None, generate_dir=None, results_dir=None):
self.config = config
self.baseline_dir = baseline_dir
self.generate_dir = generate_dir
self.results_dir = results_dir
if self.results_dir and not os.path.exists(self.results_dir):
os.mkdir(self.results_dir)
def pytest_runtest_setup(self, item):
compare = get_marker(item, 'mpl_image_compare')
if compare is None:
return
import matplotlib
import matplotlib.pyplot as plt
try:
from matplotlib.testing.decorators import remove_ticks_and_titles
except ImportError:
from matplotlib.testing.decorators import ImageComparisonTest as MplImageComparisonTest
remove_ticks_and_titles = MplImageComparisonTest.remove_text
MPL_LT_15 = LooseVersion(matplotlib.__version__) < LooseVersion('1.5')
tolerance = compare.kwargs.get('tolerance', 2)
savefig_kwargs = compare.kwargs.get('savefig_kwargs', {})
style = compare.kwargs.get('style', 'classic')
remove_text = compare.kwargs.get('remove_text', False)
backend = compare.kwargs.get('backend', 'agg')
if MPL_LT_15 and style == 'classic':
style = os.path.join(os.path.dirname(__file__), 'classic.mplstyle')
original = item.function
@wraps(item.function)
def item_function_wrapper(*args, **kwargs):
baseline_dir = compare.kwargs.get('baseline_dir', None)
if baseline_dir is None:
if self.baseline_dir is None:
baseline_dir = os.path.join(os.path.dirname(item.fspath.strpath), 'baseline')
else:
baseline_dir = self.baseline_dir
baseline_remote = False
baseline_remote = baseline_dir.startswith(('http://', 'https://'))
if not baseline_remote:
baseline_dir = os.path.join(os.path.dirname(item.fspath.strpath), baseline_dir)
with plt.style.context(style, after_reset=True), switch_backend(backend):
# Run test and get figure object
if inspect.ismethod(original): # method
# In some cases, for example if setup_method is used,
# original appears to belong to an instance of the test
# class that is not the same as args[0], and args[0] is the
# one that has the correct attributes set up from setup_method
# so we ignore original.__self__ and use args[0] instead.
fig = original.__func__(*args, **kwargs)
else: # function
fig = original(*args, **kwargs)
if remove_text:
if not isinstance(fig, tuple):
remove_ticks_and_titles(fig)
else:
[remove_ticks_and_titles(f) for f in fig]
# Find test name to use as plot name
filename = compare.kwargs.get('filename', None)
if filename is None:
filename = item.name + '.png'
filename = filename.replace('[', '_').replace(']', '_')
filename = filename.replace('/', '_')
filename = filename.replace('_.png', '.png')
# What we do now depends on whether we are generating the
# reference images or simply running the test.
if self.generate_dir is None:
# Save the figure(s)
result_dir = tempfile.mkdtemp(dir=self.results_dir)
test_image = os.path.abspath(os.path.join(result_dir, filename))
baseline_image = os.path.abspath(os.path.join(result_dir,
'baseline-' + filename))
if not isinstance(fig, tuple):
fig.savefig(test_image, **savefig_kwargs)
close_mpl_figure(fig)
# Find path to baseline image
if baseline_remote:
baseline_image_ref = _download_file(baseline_dir, filename)
else:
baseline_image_ref = os.path.abspath(os.path.join(
os.path.dirname(item.fspath.strpath), baseline_dir, filename))
if not os.path.exists(baseline_image_ref):
pytest.fail("Image file not found for comparison test in: "
"\n\t{baseline_dir}"
"\n(This is expected for new tests.)\nGenerated Image: "
"\n\t{test}".format(baseline_dir=baseline_dir,
test=test_image),
pytrace=False)
# distutils may put the baseline images in non-accessible places,
# copy to our tmpdir to be sure to keep them in case of failure
shutil.copyfile(baseline_image_ref, baseline_image)
else:
fig[0].savefig(test_image, **savefig_kwargs)
close_mpl_figure(fig[0])
fig[1].savefig(baseline_image, **savefig_kwargs)
close_mpl_figure(fig[1])
_raise_on_image_difference(
expected=baseline_image,
actual=test_image,
tol=tolerance
)
elif self.generate_dir and isinstance(fig, tuple):
close_mpl_figure(fig[0])
close_mpl_figure(fig[1])
pytest.skip("Skipping image comparison test")
else:
if not os.path.exists(self.generate_dir):
os.makedirs(self.generate_dir)
fig.savefig(os.path.abspath(os.path.join(self.generate_dir, filename)),
**savefig_kwargs)
close_mpl_figure(fig)
pytest.skip("Skipping test, since generating data")
if item.cls is not None:
setattr(item.cls, item.function.__name__, item_function_wrapper)
else:
item.obj = item_function_wrapper
class FigureCloser(object):
"""
This is used in place of ImageComparison when the --mpl option is not used,
to make sure that we still close figures returned by tests.
"""
def __init__(self, config):
self.config = config
def pytest_runtest_setup(self, item):
compare = get_marker(item, 'mpl_image_compare')
if compare is None:
return
original = item.function
@wraps(item.function)
def item_function_wrapper(*args, **kwargs):
if inspect.ismethod(original): # method
fig = original.__func__(*args, **kwargs)
else: # function
fig = original(*args, **kwargs)
close_mpl_figure(fig)
if item.cls is not None:
setattr(item.cls, item.function.__name__, item_function_wrapper)
else:
item.obj = item_function_wrapper