8000 Update some image_comparison tests. by anntzer · Pull Request #10335 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content

Update some image_comparison tests. #10335

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 29, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions lib/matplotlib/tests/test_backend_pgf.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import matplotlib.pyplot as plt
from matplotlib.compat import subprocess
from matplotlib.testing.compare import compare_images, ImageComparisonFailure
from matplotlib.testing.decorators import _image_directories
from matplotlib.testing.decorators import image_comparison, _image_directories

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

Expand Down Expand Up @@ -81,20 +81,21 @@ def create_figure():

# test compiling a figure to pdf with xelatex
@needs_xelatex
@pytest.mark.style('default')
@pytest.mark.backend('pgf')
@image_comparison(baseline_images=['pgf_xelatex'], extensions=['pdf'],
style='default')
def test_xelatex():
rc_xelatex = {'font.family': 'serif',
'pgf.rcfonts': False}
mpl.rcParams.update(rc_xelatex)
create_figure()
compare_figure('pgf_xelatex.pdf', tol=0)


# test compiling a figure to pdf with pdflatex
@needs_pdflatex
@pytest.mark.style('default')
@pytest.mark.backend('pgf')
@image_comparison(baseline_images=['pgf_pdflatex'], extensions=['pdf'],
style='default')
def test_pdflatex():
import os
if os.environ.get('APPVEYOR', False):
Expand All @@ -108,7 +109,6 @@ def test_pdflatex():
'\\usepackage[T1]{fontenc}']}
mpl.rcParams.update(rc_pdflatex)
create_figure()
compare_figure('pgf_pdflatex.pdf', tol=0)


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

# test mixed mode rendering
@needs_xelatex
@pytest.mark.style('default')
@pytest.mark.backend('pgf')
@image_comparison(baseline_images=['pgf_mixedmode'], extensions=['pdf'],
style='default')
def test_mixedmode():
rc_xelatex = {'font.family': 'serif',
'pgf.rcfonts': False}
Expand All @@ -172,7 +173,6 @@ def test_mixedmode():
Y, X = np.ogrid[-1:1:40j, -1:1:40j]
plt.figure()
plt.pcolor(X**2 + Y**2).set_rasterized(True)
compare_figure('pgf_mixedmode.pdf', tol=0)


# test bbox_inches clipping
Expand Down
99 changes: 37 additions & 62 deletions lib/matplotlib/tests/test_compare_images.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

from numpy.testing import assert_almost_equal
import pytest
from pytest import approx

from matplotlib.testing.compare import compare_images
from matplotlib.testing.decorators import _image_directories, image_comparison
Expand All @@ -19,7 +20,40 @@


# Tests of the image comparison algorithm.
def image_comparison_expect_rms(im1, im2, tol, expect_rms):
@pytest.mark.parametrize(
'im1, im2, tol, expect_rms',
[
# Comparison of an image and the same image with minor differences.
# This expects the images to compare equal under normal tolerance, and
# have a small RMS.
('basn3p02.png', 'basn3p02-minorchange.png', 10, None),
# Now test with no tolerance.
('basn3p02.png', 'basn3p02-minorchange.png', 0, 6.50646),
# Comparison with an image that is shifted by 1px in the X axis.
('basn3p02.png', 'basn3p02-1px-offset.png', 0, 90.15611),
# Comparison with an image with half the pixels shifted by 1px in the X
# axis.
('basn3p02.png', 'basn3p02-half-1px-offset.png', 0, 63.75),
# Comparison of an image and the same image scrambled.
# This expects the images to compare completely different, with a very
# large RMS.
# Note: The image has been scrambled in a specific way, by having
# each color component of each pixel randomly placed somewhere in the
# image. It contains exactly the same number of pixels of each color
# value of R, G and B, but in a totally different position.
# Test with no tolerance to make sure that we pick up even a very small
# RMS error.
('basn3p02.png', 'basn3p02-scrambled.png', 0, 172.63582),
# Comparison of an image and a slightly brighter image.
# The two images are solid color, with the second image being exactly 1
# color value brighter.
# This expects the images to compare equal under normal tolerance, and
# have an RMS of exactly 1.
('all127.png', 'all128.png', 0, 1),
# Now test the reverse comparison.
('all128.png', 'all127.png', 0, 1),
])
def test_image_comparison_expect_rms(im1, im2, tol, expect_rms):
"""Compare two images, expecting a particular RMS error.

im1 and im2 are filenames relative to the baseline_dir directory.
Expand All @@ -43,72 +77,13 @@ def image_comparison_expect_rms(im1, im2, tol, expect_rms):
assert results is None
else:
assert results is not None
assert_almost_equal(expect_rms, results['rms'], decimal=4)


def test_image_compare_basic():
#: Test comparison of an image and the same image with minor differences.

# This expects the images to compare equal under normal tolerance, and have
# a small RMS.
im1 = 'basn3p02.png'
im2 = 'basn3p02-minorchange.png'
image_comparison_expect_rms(im1, im2, tol=10, expect_rms=None)

# Now test with no tolerance.
image_comparison_expect_rms(im1, im2, tol=0, expect_rms=6.50646)


def test_image_compare_1px_offset():
#: Test comparison with an image that is shifted by 1px in the X axis.
im1 = 'basn3p02.png'
im2 = 'basn3p02-1px-offset.png'
image_comparison_expect_rms(im1, im2, tol=0, expect_rms=90.15611)
assert results['rms'] == approx(expect_rms, abs=1e-4)


def test_image_compare_half_1px_offset():
#: Test comparison with an image with half the pixels shifted by 1px in
#: the X axis.
im1 = 'basn3p02.png'
im2 = 'basn3p02-half-1px-offset.png'
image_comparison_expect_rms(im1, im2, tol=0, expect_rms=63.75)


def test_image_compare_scrambled():
#: Test comparison of an image and the same image scrambled.

# This expects the images to compare completely different, with a very
# large RMS.
# Note: The image has been scrambled in a specific way, by having each
# color component of each pixel randomly placed somewhere in the image. It
# contains exactly the same number of pixels of each color value of R, G
# and B, but in a totally different position.
im1 = 'basn3p02.png'
im2 = 'basn3p02-scrambled.png'
# Test with no tolerance to make sure that we pick up even a very small RMS
# error.
image_comparison_expect_rms(im1, im2, tol=0, expect_rms=172.63582)


def test_image_compare_shade_difference():
#: Test comparison of an image and a slightly brighter image.
# The two images are solid color, with the second image being exactly 1
# color value brighter.
# This expects the images to compare equal under normal tolerance, and have
# an RMS of exactly 1.
im1 = 'all127.png'
im2 = 'all128.png'
image_comparison_expect_rms(im1, im2, tol=0, expect_rms=1.0)

# Now test the reverse comparison.
image_comparison_expect_rms(im2, im1, tol=0, expect_rms=1.0)


#
# The following tests are used by test_nose_image_comparison to ensure that the
# image_comparison decorator continues to work with nose. They should not be
# prefixed by test_ so they don't run with pytest.
#


def nosetest_empty():
pass
Expand Down
0