Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit be7e3fa

Browse files
committed
TST: update testing decorators to newer Matplotlib
1 parent ec52af7 commit be7e3fa

File tree

2 files changed

+37
-136
lines changed

2 files changed

+37
-136
lines changed

trendvis/testing.py

Lines changed: 9 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -1,88 +1,9 @@
1-
from nose.tools import make_decorator
2-
from matplotlib.testing.decorators import cleanup
3-
import matplotlib.pyplot as plt
4-
5-
6-
def _assert_same_figure_images(fig, name, test_file, tol=17):
7-
"""Asserts that the figure object produces the right image"""
8-
import os
9-
import shutil
10-
from matplotlib import cbook
11-
from matplotlib.testing.compare import compare_images
12-
13-
if not ".png" in name:
14-
name = name+".png"
15-
16-
basedir = os.path.abspath(os.path.dirname(test_file))
17-
basename = os.path.basename(test_file)
18-
subdir = os.path.splitext(basename)[0]
19-
20-
baseline_dir = os.path.join(basedir, 'baseline_images', subdir)
21-
result_dir = os.path.abspath(os.path.join('result_images', subdir))
22-
23-
if not os.path.exists(result_dir):
24-
cbook.mkdirs(result_dir)
25-
26-
orig_expected_fname = os.path.join(baseline_dir, name)
27-
actual_fname = os.path.join(result_dir, name)
28-
29-
def make_test_fn(fname, purpose):
30-
base, ext = os.path.splitext(fname)
31-
return '%s-%s%s' % (base, purpose, ext)
32-
expected_fname = make_test_fn(actual_fname, 'expected')
33-
# Save the figure before testing whether the original image
34-
# actually exists. This make creating new tests much easier,
35-
# as the result image can afterwards just be copied.
36-
fig.savefig(actual_fname)
37-
if os.path.exists(orig_expected_fname):
38-
shutil.copyfile(orig_expected_fname, expected_fname)
39-
else:
40-
raise Exception("Baseline image %s is missing" % orig_expected_fname)
41-
err = compare_images(expected_fname, actual_fname,
42-
tol, in_decorator=True)
43-
if err:
44-
msg = 'images not close: {actual:s} vs. {expected:s} (RMS {rms:.2f})'.format(**err)
45-
raise ImagesComparisonFailure(msg)
46-
return err
47-
48-
49-
def image_comparison(baseline_images=None, tol=17, extensions=None):
50-
"""
51-
call signature::
52-
image_comparison(baseline_images=['my_figure'], tol=17)
53-
Compare images generated by the test with those specified in
54-
*baseline_images*, which must correspond else an
55-
ImagesComparisonFailure exception will be raised.
56-
Keyword arguments:
57-
*baseline_images*: list
58-
A list of strings specifying the names of the images generated
59-
by calls to :meth:`matplotlib.figure.savefig`.
60-
*tol*: (default 13)
61-
The RMS threshold above which the test is considered failed.
62-
"""
63-
64-
if baseline_images is None:
65-
raise ValueError('baseline_images must be specified')
66-
67-
if extensions:
68-
# ignored, only for compatibility with matplotlibs decorator!
69-
pass
70-
71-
def compare_images_decorator(func):
72-
import inspect
73-
_file = inspect.getfile(func)
74-
def decorated():
75-
# make sure we don't carry over bad images from former tests.
76-
assert len(plt.get_fignums()) == 0, "no of open figs: %s -> find the last test with ' " \
77-
"python tests.py -v' and add a '@cleanup' decorator." % \
78-
str(plt.get_fignums())
79-
func()
80-
assert len(plt.get_fignums()) == len(baseline_images), "different number of " \
81-
"baseline_images and actuall " \
82-
"plots."
83-
for fignum, baseline in zip(plt.get_fignums(), baseline_images):
84-
figure = plt.figure(fignum)
85-
_assert_same_figure_images(figure, baseline, _file, tol=tol)
86-
# also use the cleanup decorator to close any open figures!
87-
return make_decorator(cleanup(func))(decorated)
88-
return compare_images_decorator
1+
from matplotlib.testing.decorators import image_comparison as _ic
2+
from functools import partial
3+
4+
image_comparison = partial(
5+
_ic,
6+
extensions=["png"],
7+
tol=22,
8+
style=["classic", "_classic_test_patch", {"figure.figsize": (10, 10)}],
9+
)

trendvis/tests/test_trendvis.py

Lines changed: 28 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -11,96 +11,82 @@
1111

1212
from trendvis.testing import image_comparison
1313

14-
from matplotlib.testing.decorators import cleanup
1514
import matplotlib.pyplot as plt
16-
from matplotlib.testing.noseclasses import KnownFailure
17-
from nose.tools import assert_equal, assert_raises, assert_false, assert_true
1815

1916
# baseline images use the old default figsize of 10x10 inches.
20-
plt.rcParams['figure.figsize'] = '10, 10'
17+
plt.rcParams["figure.figsize"] = "10, 10"
2118

2219

23-
@cleanup
2420
def test_xgrid_init():
2521
trendvis.XGrid([2, 3, 1, 2])
2622

2723

28-
@cleanup
2924
def test_ygrid_init():
3025
trendvis.YGrid([2, 3, 1, 2])
3126

3227

33-
@cleanup
34-
@image_comparison(baseline_images=['xgrid_clean'])
28+
@image_comparison(baseline_images=["xgrid_clean"])
3529
def test_xgrid_clean():
3630
testgrid = trendvis.XGrid([2, 3, 1, 2])
3731
testgrid.cleanup_grid()
3832

3933

40-
@cleanup
41-
@image_comparison(baseline_images=['ygrid_clean'])
34+
@image_comparison(baseline_images=["ygrid_clean"])
4235
def test_ygrid_clean():
4336
testgrid = trendvis.YGrid([4, 6])
4437
testgrid.cleanup_grid()
4538

4639

47-
@cleanup
48-
@image_comparison(baseline_images=['xgrid_twins'])
40+
@image_comparison(baseline_images=["xgrid_twins"])
4941
def test_xgrid_twins():
5042
testgrid = trendvis.XGrid([2, 5, 1], alternate_sides=False, xratios=[2, 1])
5143
testgrid.make_twins([0, 2])
5244
testgrid.cleanup_grid()
5345

5446

55-
@cleanup
56-
@image_comparison(baseline_images=['ygrid_twins'])
47+
@image_comparison(baseline_images=["ygrid_twins"])
5748
def test_ygrid_twins():
5849
testgrid = trendvis.YGrid([2, 2, 1], alternate_sides=False, yratios=[2, 1])
5950
testgrid.make_twins([0, 2])
6051
testgrid.cleanup_grid()
6152

6253

63-
@cleanup
64-
@image_comparison(baseline_images=['xgrid_multitwin'])
54+
@image_comparison(baseline_images=["xgrid_multitwin"])
6555
def test_xgrid_multitwin():
6656
testgrid = trendvis.XGrid([2, 1])
6757
testgrid.make_twins([0, 1])
6858
testgrid.make_twins(3)
6959

7060

71-
@cleanup
72-
@image_comparison(baseline_images=['ygrid_multitwin'])
61+
@image_comparison(baseline_images=["ygrid_multitwin"])
7362
def test_ygrid_multitwin():
7463
testgrid = trendvis.YGrid([2, 1])
7564
testgrid.make_twins([0, 1])
7665
testgrid.make_twins(3)
7766

7867

79-
@cleanup
8068
def test_get_axes_xgrid():
8169
testgrid = trendvis.XGrid([2, 1])
8270
testgrid.make_twins([0])
8371

84-
assert_equal(testgrid.get_axis(0, is_twin=True), testgrid.axes[2][0])
72+
assert testgrid.get_axis(0, is_twin=True) == testgrid.axes[2][0]
8573

8674

87-
@cleanup
8875
def test_get_axes_ygrid():
8976
testgrid = trendvis.YGrid([2, 1])
9077
testgrid.make_twins([0])
9178

92-
assert_equal(testgrid.get_axis(0, is_twin=True), testgrid.axes[2][0])
79+
assert testgrid.get_axis(0, is_twin=True) == testgrid.axes[2][0]
9380

9481

95-
# @cleanup
82+
#
9683
# def test_get_twin_rownum():
9784

9885

99-
# @cleanup
86+
#
10087
# def test_get_twin_colnum():
10188

10289

103-
@cleanup
10490
def test_reverse_axes_xgrid():
10591
testgrid = trendvis.XGrid([2, 1])
10692
lim0 = testgrid.fig.axes[0].get_ylim()
@@ -110,12 +96,11 @@ def test_reverse_axes_xgrid():
11096
testgrid.reverse_yaxis()
11197
testgrid.reverse_xaxis()
11298

113-
assert_equal(lim0, testgrid.fig.axes[0].get_ylim()[::-1])
114-
assert_equal(lim1, testgrid.fig.axes[1].get_ylim()[::-1])
115-
assert_equal(mainlim, testgrid.fig.axes[1].get_xlim()[::-1])
99+
assert lim0 == testgrid.fig.axes[0].get_ylim()[::-1]
100+
assert lim1 == testgrid.fig.axes[1].get_ylim()[::-1]
101+
assert mainlim == testgrid.fig.axes[1].get_xlim()[::-1]
116102

117103

118-
@cleanup
119104
def test_reverse_axes_ygrid():
120105
testgrid = trendvis.YGrid([2, 1])
121106
lim0 = testgrid.fig.axes[0].get_xlim()
@@ -125,63 +110,58 @@ def test_reverse_axes_ygrid():
125110
testgrid.reverse_xaxis()
126111
testgrid.reverse_yaxis()
127112

128-
assert_equal(lim0, testgrid.fig.axes[0].get_xlim()[::-1])
129-
assert_equal(lim1, testgrid.fig.axes[1].get_xlim()[::-1])
130-
assert_equal(mainlim, testgrid.fig.axes[1].get_ylim()[::-1])
113+
assert lim0 == testgrid.fig.axes[0].get_xlim()[::-1]
114+
assert lim1 == testgrid.fig.axes[1].get_xlim()[::-1]
115+
assert mainlim == testgrid.fig.axes[1].get_ylim()[::-1]
131116

132-
@cleanup
133-
@image_comparison(baseline_images=['xgrid_cutout'])
117+
118+
@image_comparison(baseline_images=["xgrid_cutout"])
134119
def test_xgrid_cutout():
135120
testgrid = trendvis.XGrid([1], xratios=[1, 2])
136121
testgrid.cleanup_grid()
137122
testgrid.draw_cutout()
138123

139124

140-
@cleanup
141-
@image_comparison(baseline_images=['ygrid_cutout'])
125+
@image_comparison(baseline_images=["ygrid_cutout"])
142126
def test_ygrid_cutout():
143127
testgrid = trendvis.YGrid([1], yratios=[1, 2])
144128
testgrid.cleanup_grid()
145129
testgrid.draw_cutout()
146130

147131

148-
@cleanup
149-
@image_comparison(baseline_images=['xgrid_ylabels'])
132+
@image_comparison(baseline_images=["xgrid_ylabels"])
150133
def test_xgrid_ylabels():
151134
testgrid = trendvis.XGrid([1, 2])
152-
testgrid.set_ylabels(['0', '1'])
135+
testgrid.set_ylabels(["0", "1"])
153136

154137

155-
@cleanup
156-
@image_comparison(baseline_images=['ygrid_xlabels'])
138+
@image_comparison(baseline_images=["ygrid_xlabels"])
157139
def test_ygrid_xlabels():
158140
testgrid = trendvis.YGrid([1, 2, 1])
159-
testgrid.set_xlabels(['0', '1', None])
141+
testgrid.set_xlabels(["0", "1", None])
160142

161143

162-
@cleanup
163-
@image_comparison(baseline_images=['xgrid_frame'])
144+
@image_comparison(baseline_images=["xgrid_frame"])
164145
def test_xgrid_frame():
165146
testgrid = trendvis.XGrid([1, 2, 1], xratios=[1, 1])
166147
testgrid.cleanup_grid()
167148
testgrid.draw_frame()
168149

169150

170-
@cleanup
171-
@image_comparison(baseline_images=['ygrid_frame'])
151+
@image_comparison(baseline_images=["ygrid_frame"])
172152
def test_ygrid_frame():
173153
testgrid = trendvis.YGrid([1, 2, 1], yratios=[1, 1])
174154
testgrid.cleanup_grid()
175155
testgrid.draw_frame()
176156

177157

178-
if __name__ == '__main__':
158+
if __name__ == "__main__":
179159
import nose
180160
import sys
181161

182162
nose.main(addplugins=[KnownFailure()])
183163

184-
args = ['-s', '--with-doctest']
164+
args = ["-s", "--with-doctest"]
185165
argv = sys.argv
186166
argv = argv[:1] + args + argv[1:]
187167
nose.runmodule(argv=argv, exit=False)

0 commit comments

Comments
 (0)