Skip to content

Commit e903c34

Browse files
committed
Parametrize image_comparison test.
1 parent f77243b commit e903c34

File tree

1 file changed

+37
-62
lines changed

1 file changed

+37
-62
lines changed

lib/matplotlib/tests/test_compare_images.py

Lines changed: 37 additions & 62 deletions
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)