Skip to content

Commit c09f726

Browse files
authored
Merge pull request #10335 from anntzer/parametrize-image-comparison
Update some image_comparison tests.
2 parents 090e9f1 + bbbfef6 commit c09f726

File tree

2 files changed

+44
-69
lines changed

2 files changed

+44
-69
lines changed

lib/matplotlib/tests/test_backend_pgf.py

+7-7
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
import matplotlib.pyplot as plt
1212
from matplotlib.compat import subprocess
1313
from matplotlib.testing.compare import compare_images, ImageComparisonFailure
14-
from matplotlib.testing.decorators import _image_directories
14+
from matplotlib.testing.decorators import image_comparison, _image_directories
1515

1616
baseline_dir, result_dir = _image_directories(lambda: 'dummy func')
1717

@@ -81,20 +81,21 @@ def create_figure():
8181

8282
# test compiling a figure to pdf with xelatex
8383
@needs_xelatex
84-
@pytest.mark.style('default')
8584
@pytest.mark.backend('pgf')
85+
@image_comparison(baseline_images=['pgf_xelatex'], extensions=['pdf'],
86+
style='default')
8687
def test_xelatex():
8788
rc_xelatex = {'font.family': 'serif',
8889
'pgf.rcfonts': False}
8990
mpl.rcParams.update(rc_xelatex)
9091
create_figure()
91-
compare_figure('pgf_xelatex.pdf', tol=0)
9292

9393

9494
# test compiling a figure to pdf with pdflatex
9595
@needs_pdflatex
96-
@pytest.mark.style('default')
9796
@pytest.mark.backend('pgf')
97+
@image_comparison(baseline_images=['pgf_pdflatex'], extensions=['pdf'],
98+
style='default')
9899
def test_pdflatex():
99100
import os
100101
if os.environ.get('APPVEYOR', False):
@@ -108,7 +109,6 @@ def test_pdflatex():
108109
'\\usepackage[T1]{fontenc}']}
109110
mpl.rcParams.update(rc_pdflatex)
110111
create_figure()
111-
compare_figure('pgf_pdflatex.pdf', tol=0)
112112

113113

114114
# test updating the rc parameters for each figure
@@ -162,8 +162,9 @@ def test_pathclip():
162162

163163
# test mixed mode rendering
164164
@needs_xelatex
165-
@pytest.mark.style('default')
166165
@pytest.mark.backend('pgf')
166+
@image_comparison(baseline_images=['pgf_mixedmode'], extensions=['pdf'],
167+
style='default')
167168
def test_mixedmode():
168169
rc_xelatex = {'font.family': 'serif',
169170
'pgf.rcfonts': False}
@@ -172,7 +173,6 @@ def test_mixedmode():
172173
Y, X = np.ogrid[-1:1:40j, -1:1:40j]
173174
plt.figure()
174175
plt.pcolor(X**2 + Y**2).set_rasterized(True)
175-
compare_figure('pgf_mixedmode.pdf', tol=0)
176176

177177

178178
# test bbox_inches clipping

lib/matplotlib/tests/test_compare_images.py

+37-62
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
from numpy.testing import assert_almost_equal
1111
import pytest
12+
from pytest import approx
1213

1314
from matplotlib.testing.compare import compare_images
1415
from matplotlib.testing.decorators import _image_directories, image_comparison
@@ -19,7 +20,40 @@
1920

2021

2122
# Tests of the image comparison algorithm.
22-
def image_comparison_expect_rms(im1, im2, tol, expect_rms):
23+
@pytest.mark.parametrize(
24+
'im1, im2, tol, expect_rms',
25+
[
26+
# Comparison of an image and the same image with minor differences.
27+
# This expects the images to compare equal under normal tolerance, and
28+
# have a small RMS.
29+
('basn3p02.png', 'basn3p02-minorchange.png', 10, None),
30+
# Now test with no tolerance.
31+
('basn3p02.png', 'basn3p02-minorchange.png', 0, 6.50646),
32+
# Comparison with an image that is shifted by 1px in the X axis.
33+
('basn3p02.png', 'basn3p02-1px-offset.png', 0, 90.15611),
34+
# Comparison with an image with half the pixels shifted by 1px in the X
35+
# axis.
36+
('basn3p02.png', 'basn3p02-half-1px-offset.png', 0, 63.75),
37+
# Comparison of an image and the same image scrambled.
38+
# This expects the images to compare completely different, with a very
39+
# large RMS.
40+
# Note: The image has been scrambled in a specific way, by having
41+
# each color component of each pixel randomly placed somewhere in the
42+
# image. It contains exactly the same number of pixels of each color
43+
# value of R, G and B, but in a totally different position.
44+
# Test with no tolerance to make sure that we pick up even a very small
45+
# RMS error.
46+
('basn3p02.png', 'basn3p02-scrambled.png', 0, 172.63582),
47+
# Comparison of an image and a slightly brighter image.
48+
# The two images are solid color, with the second image being exactly 1
49+
# color value brighter.
50+
# This expects the images to compare equal under normal tolerance, and
51+
# have an RMS of exactly 1.
52+
('all127.png', 'all128.png', 0, 1),
53+
# Now test the reverse comparison.
54+
('all128.png', 'all127.png', 0, 1),
55+
])
56+
def test_image_comparison_expect_rms(im1, im2, tol, expect_rms):
2357
"""Compare two images, expecting a particular RMS error.
2458
2559
im1 and im2 are filenames relative to the baseline_dir directory.
@@ -43,72 +77,13 @@ def image_comparison_expect_rms(im1, im2, tol, expect_rms):
4377
assert results is None
4478
else:
4579
assert results is not None
46-
assert_almost_equal(expect_rms, results['rms'], decimal=4)
47-
48-
49-
def test_image_compare_basic():
50-
#: Test comparison of an image and the same image with minor differences.
51-
52-
# This expects the images to compare equal under normal tolerance, and have
53-
# a small RMS.
54-
im1 = 'basn3p02.png'
55-
im2 = 'basn3p02-minorchange.png'
56-
image_comparison_expect_rms(im1, im2, tol=10, expect_rms=None)
57-
58-
# Now test with no tolerance.
59-
image_comparison_expect_rms(im1, im2, tol=0, expect_rms=6.50646)
60-
61-
62-
def test_image_compare_1px_offset():
63-
#: Test comparison with an image that is shifted by 1px in the X axis.
64-
im1 = 'basn3p02.png'
65-
im2 = 'basn3p02-1px-offset.png'
66-
image_comparison_expect_rms(im1, im2, tol=0, expect_rms=90.15611)
80+
assert results['rms'] == approx(expect_rms, abs=1e-4)
6781

6882

69-
def test_image_compare_half_1px_offset():
70-
#: Test comparison with an image with half the pixels shifted by 1px in
71-
#: the X axis.
72-
im1 = 'basn3p02.png'
73-
im2 = 'basn3p02-half-1px-offset.png'
74-
image_comparison_expect_rms(im1, im2, tol=0, expect_rms=63.75)
75-
76-
77-
def test_image_compare_scrambled():
78-
#: Test comparison of an image and the same image scrambled.
79-
80-
# This expects the images to compare completely different, with a very
81-
# large RMS.
82-
# Note: The image has been scrambled in a specific way, by having each
83-
# color component of each pixel randomly placed somewhere in the image. It
84-
# contains exactly the same number of pixels of each color value of R, G
85-
# and B, but in a totally different position.
86-
im1 = 'basn3p02.png'
87-
im2 = 'basn3p02-scrambled.png'
88-
# Test with no tolerance to make sure that we pick up even a very small RMS
89-
# error.
90-
image_comparison_expect_rms(im1, im2, tol=0, expect_rms=172.63582)
91-
92-
93-
def test_image_compare_shade_difference():
94-
#: Test comparison of an image and a slightly brighter image.
95-
# The two images are solid color, with the second image being exactly 1
96-
# color value brighter.
97-
# This expects the images to compare equal under normal tolerance, and have
98-
# an RMS of exactly 1.
99-
im1 = 'all127.png'
100-
im2 = 'all128.png'
101-
image_comparison_expect_rms(im1, im2, tol=0, expect_rms=1.0)
102-
103-
# Now test the reverse comparison.
104-
image_comparison_expect_rms(im2, im1, tol=0, expect_rms=1.0)
105-
106-
107-
#
10883
# The following tests are used by test_nose_image_comparison to ensure that the
10984
# image_comparison decorator continues to work with nose. They should not be
11085
# prefixed by test_ so they don't run with pytest.
111-
#
86+
11287

11388
def nosetest_empty():
11489
pass

0 commit comments

Comments
 (0)