|
| 1 | +from __future__ import print_function |
| 2 | +from matplotlib.testing.compare import compare_images |
| 3 | +from matplotlib.testing.decorators import _image_directories |
| 4 | +import os |
| 5 | +import shutil |
| 6 | + |
| 7 | +baseline_dir, result_dir = _image_directories(lambda: 'dummy func') |
| 8 | + |
| 9 | +# Tests of the image comparison algorithm. |
| 10 | +def image_comparison_expect_rms(im1, im2, tol, expect_rms): |
| 11 | + """Compare two images, expecting a particular RMS error. |
| 12 | +
|
| 13 | + im1 and im2 are filenames relative to the baseline_dir directory. |
| 14 | +
|
| 15 | + tol is the tolerance to pass to compare_images. |
| 16 | +
|
| 17 | + expect_rms is the expected RMS value, or None. If None, the test will |
| 18 | + succeed if compare_images succeeds. Otherwise, the test will succeed if |
| 19 | + compare_images fails and returns an RMS error almost equal to this value. |
| 20 | + """ |
| 21 | + from nose.tools import assert_almost_equal |
| 22 | + from nose.tools import assert_is_none, assert_is_not_none |
| 23 | + im1 = os.path.join(baseline_dir, im1) |
| 24 | + im2_src = os.path.join(baseline_dir, im2) |
| 25 | + im2 = os.path.join(result_dir, im2) |
| 26 | + # Move im2 from baseline_dir to result_dir. This will ensure that |
| 27 | + # compare_images writes the diff file to result_dir, instead of trying to |
| 28 | + # write to the (possibly read-only) baseline_dir. |
| 29 | + shutil.copyfile(im2_src, im2) |
| 30 | + results = compare_images(im1, im2, tol=tol, in_decorator=True) |
| 31 | + |
| 32 | + if expect_rms is None: |
| 33 | + assert_is_none(results) |
| 34 | + else: |
| 35 | + assert_is_not_none(results) |
| 36 | + assert_almost_equal(expect_rms, results['rms'], places=4) |
| 37 | + |
| 38 | +def test_image_compare_basic(): |
| 39 | + """Test comparison of an image and the same image with minor differences.""" |
| 40 | + # This expects the images to compare equal under normal tolerance, and have |
| 41 | + # a small RMS. |
| 42 | + im1 = 'cosine_peak-nn-img.png' |
| 43 | + im2 = 'cosine_peak-nn-img-minorchange.png' |
| 44 | + image_comparison_expect_rms(im1, im2, tol=1e-3, expect_rms=None) |
| 45 | + |
| 46 | + # Now test with no tolerance. |
| 47 | + image_comparison_expect_rms(im1, im2, tol=0, expect_rms=2.99949) |
| 48 | + |
| 49 | +def test_image_compare_scrambled(): |
| 50 | + """Test comparison of an image and the same image scrambled.""" |
| 51 | + # This expects the images to compare completely different, with a very large |
| 52 | + # RMS. |
| 53 | + # Note: The image has been scrambled in a specific way, by having each |
| 54 | + # colour component of each pixel randomly placed somewhere in the image. It |
| 55 | + # contains exactly the same number of pixels of each colour value of R, G |
| 56 | + # and B, but in a totally different position. |
| 57 | + im1 = 'cosine_peak-nn-img.png' |
| 58 | + im2 = 'cosine_peak-nn-img-scrambled.png' |
| 59 | + # Test with no tolerance to make sure that we pick up even a very small RMS |
| 60 | + # error. |
| 61 | + image_comparison_expect_rms(im1, im2, tol=0, expect_rms=153.19994) |
| 62 | + |
| 63 | +def test_image_compare_shade_difference(): |
| 64 | + """Test comparison of an image and a slightly brighter image.""" |
| 65 | + # The two images are solid colour, with the second image being exactly 1 |
| 66 | + # colour value brighter. |
| 67 | + # This expects the images to compare equal under normal tolerance, and have |
| 68 | + # an RMS of exactly 1. |
| 69 | + im1 = 'all127.png' |
| 70 | + im2 = 'all128.png' |
| 71 | + image_comparison_expect_rms(im1, im2, tol=0, expect_rms=1.0) |
| 72 | + |
| 73 | + # Now test the reverse comparison. |
| 74 | + image_comparison_expect_rms(im2, im1, tol=0, expect_rms=1.0) |
0 commit comments