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

Skip to content

Commit e2f8829

Browse files
committed
testing: implement image_comparison decorator
svn path=/trunk/matplotlib/; revision=7652
1 parent 26bad18 commit e2f8829

3 files changed

Lines changed: 46 additions & 6 deletions

File tree

lib/matplotlib/testing/compare.py

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ def compare_float( expected, actual, relTol = None, absTol = None ):
7272
return None
7373

7474
#-----------------------------------------------------------------------
75-
def compare_images( expected, actual, tol ):
75+
def compare_images( expected, actual, tol, in_decorator=False ):
7676
'''Compare two image files - not the greatest, but fast and good enough.
7777
7878
= EXAMPLE
@@ -87,6 +87,8 @@ def compare_images( expected, actual, tol ):
8787
- actual The filename of the actual image.
8888
- tol The tolerance (a unitless float). This is used to
8989
determinte the 'fuzziness' to use when comparing images.
90+
- in_decorator If called from image_comparison decorator, this should be
91+
True. (default=False)
9092
'''
9193

9294
try:
@@ -113,11 +115,21 @@ def compare_images( expected, actual, tol ):
113115

114116
if ( (rms / 10000.0) <= tol ):
115117
return None
116-
else:
117-
diff_image = os.path.join(os.path.dirname(actual),
118-
'failed-diff-'+os.path.basename(actual))
119-
save_diff_image( expected, actual, diff_image )
120118

119+
diff_image = os.path.join(os.path.dirname(actual),
120+
'failed-diff-'+os.path.basename(actual))
121+
save_diff_image( expected, actual, diff_image )
122+
123+
if in_decorator:
124+
results = dict(
125+
rms = rms,
126+
expected = str(expected),
127+
actual = str(actual),
128+
diff = str(diff_image),
129+
)
130+
return results
131+
else:
132+
# old-style call from mplTest directory
121133
msg = " Error: Image files did not match.\n" \
122134
" RMS Value: " + str( rms / 10000.0 ) + "\n" \
123135
" Expected:\n " + str( expected ) + "\n" \
@@ -130,6 +142,8 @@ def save_diff_image( expected, actual, output ):
130142
from PIL import Image
131143
expectedImage = np.array(Image.open( expected ).convert("RGB")).astype(np.float)
132144
actualImage = np.array(Image.open( actual ).convert("RGB")).astype(np.float)
145+
assert expectedImage.ndim==expectedImage.ndim
146+
assert expectedImage.shape==expectedImage.shape
133147
absDiffImage = abs(expectedImage-actualImage)
134148
# expand differences in luminance domain
135149
absDiffImage *= 10

lib/matplotlib/testing/decorators.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
from matplotlib.testing.noseclasses import KnownFailureTest, \
2-
KnownFailureDidNotFailTest
2+
KnownFailureDidNotFailTest, ImageComparisonFailure
33
import sys
4+
import nose
5+
from matplotlib.cbook import get_sample_data
6+
from matplotlib.testing.compare import compare_images
47

58
def knownfailureif(fail_condition, msg=None):
69
# based on numpy.testing.dec.knownfailureif
@@ -24,3 +27,23 @@ def failer(*args, **kwargs):
2427
return result
2528
return nose.tools.make_decorator(f)(failer)
2629
return known_fail_decorator
30+
31+
def image_comparison(baseline_images=None, tol=1e-3):
32+
if baseline_images is None:
33+
raise ValueError('baseline_images must be specified')
34+
def compare_images_decorator(func):
35+
def decorated_compare_images(*args,**kwargs):
36+
result = func(*args,**kwargs)
37+
for fname in baseline_images:
38+
actual = fname
39+
expected = get_sample_data('test_baseline_%s'%fname,
40+
asfileobj=False)
41+
err = compare_images( expected, actual, tol,
42+
in_decorator=True )
43+
if err:
44+
raise ImageComparisonFailure(
45+
'images not close: %(actual)s vs. %(expected)s '
46+
'(RMS %(rms).3f)'%err)
47+
return result
48+
return nose.tools.make_decorator(func)(decorated_compare_images)
49+
return compare_images_decorator

lib/matplotlib/testing/noseclasses.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ class KnownFailureDidNotFailTest(Exception):
99
'''Raise this exception to mark a test should have failed but did not.'''
1010
pass
1111

12+
class ImageComparisonFailure(Exception):
13+
'''Raise this exception to mark a test as a comparison between two images.'''
14+
1215
class KnownFailure(ErrorClassPlugin):
1316
'''Plugin that installs a KNOWNFAIL error class for the
1417
KnownFailureClass exception. When KnownFailureTest is raised,

0 commit comments

Comments
 (0)