Skip to content

Commit 8c60d5c

Browse files
committed
Beginnings of a capability to test the pdf backend.
svn path=/trunk/matplotlib/; revision=7830
1 parent ea1b2d7 commit 8c60d5c

36 files changed

+87
-36
lines changed

CHANGELOG

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
2009-09-27 Beginnings of a capability to test the pdf backend. - JKS
2+
13
2009-09-27 Add a savefig.extension rcparam to control the default
24
filename extension used by savefig. - JKS
35

lib/matplotlib/testing/compare.py

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import os
99
import numpy as np
1010
import shutil
11+
import subprocess
1112

1213
#=======================================================================
1314

@@ -73,6 +74,33 @@ def compare_float( expected, actual, relTol = None, absTol = None ):
7374
return None
7475

7576
#-----------------------------------------------------------------------
77+
# A dictionary that maps filename extensions to functions that map
78+
# parameters old and new to a list that can be passed to Popen to
79+
# convert files with that extension to png format.
80+
converter = { 'pdf': lambda old, new: \
81+
['gs', '-q', '-sDEVICE=png16m', '-dNOPAUSE', '-dBATCH',
82+
'-sOutputFile=' + new, old],
83+
}
84+
def convert(filename):
85+
'''Convert the named file into a png file.
86+
Returns the name of the created file.
87+
'''
88+
base, extension = filename.rsplit('.', 1)
89+
if extension not in converter:
90+
raise KeyError, "Don't know how to convert %s files to png" % extension
91+
newname = base + '_' + extension + '.png'
92+
cmd = converter[extension](filename, newname)
93+
pipe = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
94+
stdout, stderr = pipe.communicate()
95+
if not os.path.exists(newname):
96+
msg = "Conversion command failed:\n%s\n" % ' '.join(cmd)
97+
if stdout:
98+
msg += "Standard output:\n%s\n" % stdout
99+
if stderr:
100+
msg += "Standard error:\n%s\n" % stderr
101+
raise IOError, msg
102+
return newname
103+
76104
def compare_images( expected, actual, tol, in_decorator=False ):
77105
'''Compare two image files - not the greatest, but fast and good enough.
78106
@@ -99,7 +127,15 @@ def compare_images( expected, actual, tol, in_decorator=False ):
99127
"be installed. To run tests without using PIL, then use " \
100128
"the '--without-tag=PIL' command-line option.\n" \
101129
"Importing PIL failed with the following error:\n%s" % e
102-
return msg
130+
if in_decorator:
131+
raise NotImplementedError, e
132+
else:
133+
return msg
134+
135+
# Convert the image to png
136+
extension = expected.split('.')[-1]
137+
if extension != 'png':
138+
expected, actual = convert(expected), convert(actual)
103139

104140
# open the image files and remove the alpha channel (if it exists)
105141
expectedImage = Image.open( expected ).convert("RGB")

lib/matplotlib/testing/decorators.py

Lines changed: 48 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
KnownFailureDidNotFailTest, ImageComparisonFailure
33
import os, sys
44
import nose
5+
import matplotlib
56
import matplotlib.tests
67
from matplotlib.testing.compare import compare_images
78

@@ -48,42 +49,54 @@ def image_comparison(baseline_images=None):
4849
raise ValueError('baseline_images must be specified')
4950
def compare_images_decorator(func):
5051
def decorated_compare_images(*args,**kwargs):
51-
result = func(*args,**kwargs)
52-
extension = '.png' # TODO: test more backends
53-
for fname in baseline_images:
54-
# FIXME: place "actual", or current images, images in
55-
# a more reasonable location than the current
56-
# directory. Also, perhaps put them in sub-directory
57-
# according to the name of the test module like the
58-
# baseline images.
59-
actual = fname + extension
6052

61-
# compute filename for baseline image
62-
module_name = func.__module__
63-
if module_name=='__main__':
64-
# FIXME: this won't work for nested packages in matplotlib.tests
65-
import warnings
66-
warnings.warn('test module run as script. guessing baseline image locations')
67-
script_name = sys.argv[0]
68-
basedir = os.path.abspath(os.path.dirname(script_name))
69-
subdir = os.path.splitext(os.path.split(script_name)[1])[0]
70-
else:
71-
mods = module_name.split('.')
72-
assert mods.pop(0)=='matplotlib'
73-
assert mods.pop(0)=='tests'
74-
subdir = os.path.join(*mods)
75-
basedir = os.path.dirname(matplotlib.tests.__file__)
76-
baseline_dir = os.path.join(basedir,'baseline_images',subdir)
77-
expected = os.path.join(baseline_dir,fname) + extension
53+
# compute baseline image directory
54+
module_name = func.__module__
55+
if module_name=='__main__':
56+
# FIXME: this won't work for nested packages in matplotlib.tests
57+
import warnings
58+
warnings.warn('test module run as script. guessing baseline image locations')
59+
script_name = sys.argv[0]
60+
basedir = os.path.abspath(os.path.dirname(script_name))
61+
subdir = os.path.splitext(os.path.split(script_name)[1])[0]
62+
else:
63+
mods = module_name.split('.')
64+
assert mods.pop(0)=='matplotlib'
65+
assert mods.pop(0)=='tests'
66+
subdir = os.path.join(*mods)
67+
basedir = os.path.dirname(matplotlib.tests.__file__)
68+
baseline_dir = os.path.join(basedir,'baseline_images',subdir)
69+
result_dir = os.path.join(basedir,'current_images',subdir)
70+
if not os.path.exists(result_dir):
71+
try:
72+
# make the current_images directory first
73+
os.mkdir(os.path.join(basedir,'current_images'))
74+
except OSError:
75+
pass # probably exists already
76+
os.mkdir(result_dir)
7877

79-
# compare the images
80-
tol=1e-3 # default tolerance
81-
err = compare_images( expected, actual, tol,
82-
in_decorator=True )
83-
if err:
84-
raise ImageComparisonFailure(
85-
'images not close: %(actual)s vs. %(expected)s '
86-
'(RMS %(rms).3f)'%err)
87-
return result
78+
for extension in ['png', 'pdf']:
79+
# set the default format of savefig
80+
matplotlib.rc('savefig', extension=extension)
81+
# change to the result directory for the duration of the test
82+
old_dir = os.getcwd()
83+
os.chdir(result_dir)
84+
try:
85+
last_result = func(*args,**kwargs) # actually call the test function
86+
finally:
87+
os.chdir(old_dir)
88+
for fname in baseline_images:
89+
actual = os.path.join(result_dir, fname) + '.' + extension
90+
expected = os.path.join(baseline_dir,fname) + '.' + extension
91+
92+
# compare the images
93+
tol=1e-3 # default tolerance
94+
err = compare_images( expected, actual, tol,
95+
in_decorator=True )
96+
if err:
97+
raise ImageComparisonFailure(
98+
'images not close: %(actual)s vs. %(expected)s '
99+
'(RMS %(rms).3f)'%err)
100+
return last_result
88101
return nose.tools.make_decorator(func)(decorated_compare_images)
89102
return compare_images_decorator
8.18 KB
Binary file not shown.
8.19 KB
Binary file not shown.
8.17 KB
Binary file not shown.
12.4 KB
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

0 commit comments

Comments
 (0)