Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit138df72

Browse files
committed
Use test cache for test result images too.
For image comparison tests in svg/pdf/ps formats, the result images areconverted to png for comparison. Previously the conversion results werecached for the baseline images, but not for the test-generated images(because of non-deterministic svg/pdf/etc. results, due tohash-salting, dict ordering, etc.).Now that the test-generated images are generally deterministic, we canenable 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 imagecomparison tests to see an effect). Also add logging regarding thecache which may help troubleshooting determinacy problems.A simple cache eviction mechanism prevents the cache from growingwithout bounds, limiting it to 2x the size of the baseline_imagesdirectory.This is a much simpler version of PR7764, which added more sophisticatedreporting of cache hits and misses and cache eviction.
1 parent34f99a9 commit138df72

File tree

1 file changed

+35
-1
lines changed

1 file changed

+35
-1
lines changed

‎lib/matplotlib/testing/compare.py

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
"""
44

55
importatexit
6+
importfunctools
67
importhashlib
8+
importlogging
79
importos
810
frompathlibimportPath
911
importre
@@ -19,6 +21,8 @@
1921
frommatplotlibimportcbook
2022
frommatplotlib.testing.exceptionsimportImageComparisonFailure
2123

24+
_log=logging.getLogger(__name__)
25+
2226
__all__= ['compare_images','comparable_formats']
2327

2428

@@ -285,20 +289,50 @@ def convert(filename, cache):
285289
cache_dir=Path(get_cache_dir())ifcacheelseNone
286290

287291
ifcache_dirisnotNone:
292+
_register_conversion_cache_cleaner_once()
288293
hash_value=get_file_hash(path)
289294
cached_path=cache_dir/ (hash_value+newpath.suffix)
290295
ifcached_path.exists():
296+
_log.debug("For %s: reusing cached conversion.",filename)
291297
shutil.copyfile(cached_path,newpath)
292298
returnstr(newpath)
293299

300+
_log.debug("For %s: converting to png.",filename)
294301
converter[path.suffix[1:]](path,newpath)
295302

296303
ifcache_dirisnotNone:
304+
_log.debug("For %s: caching conversion result.",filename)
297305
shutil.copyfile(newpath,cached_path)
298306

299307
returnstr(newpath)
300308

301309

310+
def_clean_conversion_cache():
311+
# This will actually ignore mpl_toolkits baseline images, but they're
312+
# relatively small.
313+
baseline_images_size=sum(
314+
path.stat().st_size
315+
forpathinPath(mpl.__file__).parent.glob("**/baseline_images/**/*"))
316+
# 2x: one full copy of baselines, and one full copy of test results
317+
# (actually an overestimate: we don't convert png baselines and results).
318+
max_cache_size=2*baseline_images_size
319+
# Reduce cache until it fits.
320+
cache_stat= {
321+
path:path.stat()forpathinPath(get_cache_dir()).glob("*")}
322+
cache_size=sum(stat.st_sizeforstatincache_stat.values())
323+
paths_by_atime=sorted(# Oldest at the end.
324+
cache_stat,key=lambdapath:cache_stat[path].st_atime,reverse=True)
325+
whilecache_size>max_cache_size:
326+
path=paths_by_atime.pop()
327+
cache_size-=cache_stat[path].st_size
328+
path.unlink()
329+
330+
331+
@functools.lru_cache()# Ensure this is only registered once.
332+
def_register_conversion_cache_cleaner_once():
333+
atexit.register(_clean_conversion_cache)
334+
335+
302336
defcrop_to_same(actual_path,actual_image,expected_path,expected_image):
303337
# clip the images to the same size -- this is useful only when
304338
# comparing eps to pdf
@@ -387,7 +421,7 @@ def compare_images(expected, actual, tol, in_decorator=False):
387421
raiseIOError('Baseline image %r does not exist.'%expected)
388422
extension=expected.split('.')[-1]
389423
ifextension!='png':
390-
actual=convert(actual,cache=False)
424+
actual=convert(actual,cache=True)
391425
expected=convert(expected,cache=True)
392426

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

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp