Jouni K. Seppänen wrote:
> John Hunter <jdh2...@gmail.com> writes:
>
>
>>> pyplot.savefig('foo1')
>>>
>> Take a look at the pyplot "switch_backends" function.
>>
>
> Yes, that function was on the next line after the part you quoted. :-)
> It calls matplotlib.use with warn=False, but that function ends up doing
> nothing.
>
>
>> Alternatively, agg knows how to save pdf if given the extension, so we
>> could wire up the testing to use a module level extension set
>> somewhere which could be updated for each backend. This is probably
>> safer and cleaner than switch_backends
>>
>
> That sounds complicated. How about having the test cases call savefig
> with all the relevant file formats? That doesn't look so nice if the
> test cases end up with a big block of savefig calls, but it has the
> advantage that there is no magic involved and it is very obvious what is
> going on.
>
Sorry, I should have been more clear. I was thinking that the
image_compare() decorator would call the test function multiple times,
having switched the backend between invocations. Thus, the call to
savefig() would continue not to explicitly set the extension. I've
quickly modified the source to reflect my idea, but I haven't had a
chance to flesh it out or test it. It should show the idea, though. See
attached.
-Andrew
diff --git a/lib/matplotlib/testing/decorators.py b/lib/matplotlib/testing/decorators.py
index 6363663..53b276b 100644
--- a/lib/matplotlib/testing/decorators.py
+++ b/lib/matplotlib/testing/decorators.py
@@ -48,42 +48,50 @@ def image_comparison(baseline_images=None):
raise ValueError('baseline_images must be specified')
def compare_images_decorator(func):
def decorated_compare_images(*args,**kwargs):
- result = func(*args,**kwargs)
- extension = '.png' # TODO: test more backends
- for fname in baseline_images:
- # FIXME: place "actual", or current images, images in
- # a more reasonable location than the current
- # directory. Also, perhaps put them in sub-directory
- # according to the name of the test module like the
- # baseline images.
- actual = fname + extension
- # compute filename for baseline image
- module_name = func.__module__
- if module_name=='__main__':
- # FIXME: this won't work for nested packages in matplotlib.tests
- import warnings
- warnings.warn('test module run as script. guessing baseline image locations')
- script_name = sys.argv[0]
- basedir = os.path.abspath(os.path.dirname(script_name))
- subdir = os.path.splitext(os.path.split(script_name)[1])[0]
- else:
- mods = module_name.split('.')
- assert mods.pop(0)=='matplotlib'
- assert mods.pop(0)=='tests'
- subdir = os.path.join(*mods)
- basedir = os.path.dirname(matplotlib.tests.__file__)
- baseline_dir = os.path.join(basedir,'baseline_images',subdir)
- expected = os.path.join(baseline_dir,fname) + extension
+ # compute baseline image directory
+ module_name = func.__module__
+ if module_name=='__main__':
+ # FIXME: this won't work for nested packages in matplotlib.tests
+ import warnings
+ warnings.warn('test module run as script. guessing baseline image locations')
+ script_name = sys.argv[0]
+ basedir = os.path.abspath(os.path.dirname(script_name))
+ subdir = os.path.splitext(os.path.split(script_name)[1])[0]
+ else:
+ mods = module_name.split('.')
+ assert mods.pop(0)=='matplotlib'
+ assert mods.pop(0)=='tests'
+ subdir = os.path.join(*mods)
+ basedir = os.path.dirname(matplotlib.tests.__file__)
+ baseline_dir = os.path.join(basedir,'baseline_images',subdir)
- # compare the images
- tol=1e-3 # default tolerance
- err = compare_images( expected, actual, tol,
- in_decorator=True )
- if err:
- raise ImageComparisonFailure(
- 'images not close: %(actual)s vs. %(expected)s '
- '(RMS %(rms).3f)'%err)
- return result
+ for extension in ['.png', '.pdf']:
+ switch_backends_somehow(extension) # FIXME: implement this
+ last_result = func(*args,**kwargs) # actually call the test function
+ for fname in baseline_images:
+ # FIXME: place "actual", or current images, images in
+ # a more reasonable location than the current
+ # directory. Also, perhaps put them in sub-directory
+ # according to the name of the test module like the
+ # baseline images.
+ actual = fname + extension
+
+ # compute filename for baseline image
+ expected = os.path.join(baseline_dir,fname) + extension
+
+ if extension != '.png':
+ raise NotImplementedError('No support for comparing '
+ 'non .png images yet')
+
+ # compare the images
+ tol=1e-3 # default tolerance
+ err = compare_images( expected, actual, tol,
+ in_decorator=True )
+ if err:
+ raise ImageComparisonFailure(
+ 'images not close: %(actual)s vs. %(expected)s '
+ '(RMS %(rms).3f)'%err)
+ return last_result
return nose.tools.make_decorator(func)(decorated_compare_images)
return compare_images_decorator
------------------------------------------------------------------------------
Come build with us! The BlackBerry® Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay
ahead of the curve. Join us from November 9-12, 2009. Register now!
http://p.sf.net/sfu/devconf
_______________________________________________
Matplotlib-devel mailing list
Matplotlib-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-devel