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

Skip to content

Commit afdd890

Browse files
committed
Use test cache for test result images too.
For image comparison tests in svg/pdf/ps formats, the result images are converted to png for comparison. Previously the conversion results were cached for the baseline images, but not for the test-generated images (because of non-deterministic svg/pdf/etc. results, due to hash-salting, dict ordering, etc.). Now that the test-generated images are generally deterministic, we can enable the cache for baseline images as well. This speeds up `pytest -k '[svg]'` by ~30% (81s initially -> 55s on a seeded cache) and `pytest -k '[pdf]'` by ~10% (62s -> 55s) (there are too few (e)ps image comparison tests to see an effect). Also add logging regarding the cache which may help troubleshooting determinacy problems. This is a much simpler version of PR7764, which added more sophisticated reporting of cache hits and misses, as well as cache invalidation (I think the cache can reasonably be cleaned manually, at least for now).
1 parent db7cd7e commit afdd890

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

lib/matplotlib/testing/compare.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
"""
22
Provides a collection of utilities for comparing (image) results.
3-
43
"""
54

65
import atexit
76
import hashlib
7+
import logging
88
import os
99
from pathlib import Path
1010
import re
@@ -19,6 +19,8 @@
1919
import matplotlib as mpl
2020
from matplotlib.testing.exceptions import ImageComparisonFailure
2121

22+
_log = logging.getLogger(__name__)
23+
2224
__all__ = ['compare_images', 'comparable_formats']
2325

2426

@@ -285,12 +287,15 @@ def convert(filename, cache):
285287
new_ext = os.path.splitext(newname)[1]
286288
cached_file = os.path.join(cache_dir, hash_value + new_ext)
287289
if os.path.exists(cached_file):
290+
_log.debug("For %s: reusing cached conversion.", filename)
288291
shutil.copyfile(cached_file, newname)
289292
return newname
290293

294+
_log.debug("For %s: converting to png.", filename)
291295
converter[extension](filename, newname)
292296

293297
if cache_dir is not None:
298+
_log.debug("For %s: caching conversion result.", filename)
294299
shutil.copyfile(newname, cached_file)
295300

296301
return newname
@@ -382,7 +387,7 @@ def compare_images(expected, actual, tol, in_decorator=False):
382387
raise IOError('Baseline image %r does not exist.' % expected)
383388
extension = expected.split('.')[-1]
384389
if extension != 'png':
385-
actual = convert(actual, cache=False)
390+
actual = convert(actual, cache=True)
386391
expected = convert(expected, cache=True)
387392

388393
# open the image files and remove the alpha channel (if it exists)

0 commit comments

Comments
 (0)