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

Skip to content

Commit 1283feb

Browse files
committed
Attempt to fix image comparison on 32-bit platforms
1 parent 0c3310e commit 1283feb

File tree

1 file changed

+52
-27
lines changed

1 file changed

+52
-27
lines changed

lib/matplotlib/testing/compare.py

Lines changed: 52 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,42 @@ def crop_to_same(actual_path, actual_image, expected_path, expected_image):
250250
actual_image = actual_image[int(aw/2-ew/2):int(aw/2+ew/2),int(ah/2-eh/2):int(ah/2+eh/2)]
251251
return actual_image, expected_image
252252

253+
def calculate_rms(expectedImage, actualImage):
254+
# compare the resulting image histogram functions
255+
expected_version = version.LooseVersion("1.6")
256+
found_version = version.LooseVersion(np.__version__)
257+
258+
# On Numpy 1.6, we can use bincount with minlength, which is much faster than
259+
# using histogram
260+
if found_version >= expected_version:
261+
rms = 0
262+
263+
for i in xrange(0, 3):
264+
h1p = expectedImage[:,:,i]
265+
h2p = actualImage[:,:,i]
266+
267+
h1h = np.bincount(h1p.ravel(), minlength=256)
268+
h2h = np.bincount(h2p.ravel(), minlength=256)
269+
270+
rms += np.sum(np.power((h1h-h2h), 2))
271+
else:
272+
rms = 0
273+
bins = np.arange(257)
274+
275+
for i in xrange(0, 3):
276+
h1p = expectedImage[:,:,i]
277+
h2p = actualImage[:,:,i]
278+
279+
h1h = np.histogram(h1p, bins=bins)[0]
280+
h2h = np.histogram(h2p, bins=bins)[0]
281+
282+
rms += np.sum(np.power((h1h-h2h), 2))
283+
284+
rms = np.sqrt(rms / (256 * 3))
285+
286+
return rms
287+
288+
253289
def compare_images( expected, actual, tol, in_decorator=False ):
254290
'''Compare two image files - not the greatest, but fast and good enough.
255291
@@ -287,33 +323,7 @@ def compare_images( expected, actual, tol, in_decorator=False ):
287323
expected_version = version.LooseVersion("1.6")
288324
found_version = version.LooseVersion(np.__version__)
289325

290-
# On Numpy 1.6, we can use bincount with minlength, which is much faster than
291-
# using histogram
292-
if found_version >= expected_version:
293-
rms = 0
294-
295-
for i in xrange(0, 3):
296-
h1p = expectedImage[:,:,i]
297-
h2p = actualImage[:,:,i]
298-
299-
h1h = np.bincount(h1p.ravel(), minlength=256)
300-
h2h = np.bincount(h2p.ravel(), minlength=256)
301-
302-
rms += np.sum(np.power((h1h-h2h), 2))
303-
else:
304-
rms = 0
305-
bins = np.arange(257)
306-
307-
for i in xrange(0, 3):
308-
h1p = expectedImage[:,:,i]
309-
h2p = actualImage[:,:,i]
310-
311-
h1h = np.histogram(h1p, bins=bins)[0]
312-
h2h = np.histogram(h2p, bins=bins)[0]
313-
314-
rms += np.sum(np.power((h1h-h2h), 2))
315-
316-
rms = np.sqrt(rms / (256 * 3))
326+
rms = calculate_rms(expectedImage, actualImage)
317327

318328
diff_image = make_test_filename(actual, 'failed-diff')
319329

@@ -322,6 +332,21 @@ def compare_images( expected, actual, tol, in_decorator=False ):
322332
os.unlink(diff_image)
323333
return None
324334

335+
# For Agg-rendered images, we can retry by ignoring pixels with
336+
# differences of only 1
337+
if extension == 'png':
338+
# Remove differences of only 1
339+
diffImage = np.abs(np.asarray(actualImage, dtype=np.int) -
340+
np.asarray(expectedImage, dtype=np.int))
341+
actualImage = np.where(diffImage <= 1, expectedImage, actualImage)
342+
343+
rms = calculate_rms(expectedImage, actualImage)
344+
345+
if ( (rms / 10000.0) <= tol ):
346+
if os.path.exists(diff_image):
347+
os.unlink(diff_image)
348+
return None
349+
325350
save_diff_image( expected, actual, diff_image )
326351

327352
if in_decorator:

0 commit comments

Comments
 (0)