Menu

[r7472]: / branches / mathtex / test / mplTest / compare.py  Maximize  Restore  History

Download this file

139 lines (113 with data), 4.9 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#=======================================================================
""" A set of utilities for comparing results.
"""
#=======================================================================
import math
import operator
import os
import numpy as np
#=======================================================================
__all__ = [
'compareFloat',
'compareImages',
]
#-----------------------------------------------------------------------
def compareFloat( expected, actual, relTol = None, absTol = None ):
"""Fail if the floating point values are not close enough, with
the givem message.
You can specify a relative tolerance, absolute tolerance, or both.
"""
if relTol is None and absTol is None:
exMsg = "You haven't specified a 'relTol' relative tolerance "
exMsg += "or a 'absTol' absolute tolerance function argument. "
exMsg += "You must specify one."
raise ValueError, exMsg
msg = ""
if absTol is not None:
absDiff = abs( expected - actual )
if absTol < absDiff:
expectedStr = str( expected )
actualStr = str( actual )
absDiffStr = str( absDiff )
absTolStr = str( absTol )
msg += "\n"
msg += " Expected: " + expectedStr + "\n"
msg += " Actual: " + actualStr + "\n"
msg += " Abs Diff: " + absDiffStr + "\n"
msg += " Abs Tol: " + absTolStr + "\n"
if relTol is not None:
# The relative difference of the two values. If the expected value is
# zero, then return the absolute value of the difference.
relDiff = abs( expected - actual )
if expected:
relDiff = relDiff / abs( expected )
if relTol < relDiff:
# The relative difference is a ratio, so it's always unitless.
relDiffStr = str( relDiff )
relTolStr = str( relTol )
expectedStr = str( expected )
actualStr = str( actual )
msg += "\n"
msg += " Expected: " + expectedStr + "\n"
msg += " Actual: " + actualStr + "\n"
msg += " Rel Diff: " + relDiffStr + "\n"
msg += " Rel Tol: " + relTolStr + "\n"
if msg:
return msg
else:
return None
#-----------------------------------------------------------------------
def compareImages( expected, actual, tol ):
'''Compare two image files - not the greatest, but fast and good enough.
= EXAMPLE
# img1 = "./baseline/plot.png"
# img2 = "./output/plot.png"
#
# compareImage( img1, img2, 0.001 ):
= INPUT VARIABLES
- expected The filename of the expected image.
- actual The filename of the actual image.
- tol The tolerance (a unitless float). This is used to
determinte the 'fuzziness' to use when comparing images.
'''
try:
from PIL import Image, ImageOps, ImageFilter
except ImportError, e:
msg = "Image Comparison requires the Python Imaging Library to " \
"be installed. To run tests without using PIL, then use " \
"the '--without-tag=PIL' command-line option.\n" \
"Importing PIL failed with the following error:\n%s" % e
return msg
# open the image files and remove the alpha channel (if it exists)
expectedImage = Image.open( expected ).convert("RGB")
actualImage = Image.open( actual ).convert("RGB")
# normalize the images
expectedImage = ImageOps.autocontrast( expectedImage, 2 )
actualImage = ImageOps.autocontrast( actualImage, 2 )
# compare the resulting image histogram functions
h1 = expectedImage.histogram()
h2 = actualImage.histogram()
rms = math.sqrt( reduce(operator.add, map(lambda a,b: (a-b)**2, h1, h2)) / len(h1) )
if ( (rms / 10000.0) <= tol ):
return None
else:
diff_image = os.path.join(os.path.dirname(actual),
'failed-diff-'+os.path.basename(actual))
saveDiffImage( expected, actual, diff_image )
msg = " Error: Image files did not match.\n" \
" RMS Value: " + str( rms / 10000.0 ) + "\n" \
" Expected:\n " + str( expected ) + "\n" \
" Actual:\n " + str( actual ) + "\n" \
" Difference:\n " + str( diff_image ) + "\n" \
" Tolerance: " + str( tol ) + "\n"
return msg
def saveDiffImage( expected, actual, output ):
from PIL import Image
expectedImage = np.array(Image.open( expected ).convert("RGB")).astype(np.float)
actualImage = np.array(Image.open( actual ).convert("RGB")).astype(np.float)
absDiffImage = abs(expectedImage-actualImage)
# expand differences in luminance domain
absDiffImage *= 10
save_image_np = absDiffImage.astype(np.uint8)
save_image = Image.fromarray(save_image_np)
save_image.save(output)
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.