def decorated_compare_images(): # set the default format of savefig matplotlib.rc('savefig', extension=extension) # change to the result directory for the duration of the test old_dir = os.getcwd() os.chdir(result_dir) try: result = func() # actually call the test function finally: os.chdir(old_dir) for actual, expected in zip(actual_fnames, expected_fnames): if not os.path.exists(expected): raise ImageComparisonFailure( 'image does not exist: %s' % expected) # compare the images 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
def do_test(): figure = plt.figure(fignum) if self._remove_text: self.remove_text(figure) figure.savefig(actual_fname, **self._savefig_kwarg) err = compare_images(expected_fname, actual_fname, self._tol, in_decorator=True) try: if not os.path.exists(expected_fname): raise ImageComparisonFailure( 'image does not exist: %s' % expected_fname) if err: raise ImageComparisonFailure( 'images not close: %(actual)s vs. %(expected)s ' '(RMS %(rms).3f)'%err) except ImageComparisonFailure: if not check_freetype_version(self._freetype_version): raise KnownFailureTest( "Mismatched version of freetype. Test requires '%s', you have '%s'" % (self._freetype_version, ft2font.__freetype_version__)) raise
def do_test(): figure.savefig(actual_fname) if not os.path.exists(expected_fname): raise ImageComparisonFailure( 'image does not exist: %s' % expected_fname) err = compare_images(expected_fname, actual_fname, self._tol, in_decorator=True) if err: raise ImageComparisonFailure( 'images not close: %(actual)s vs. %(expected)s ' '(RMS %(rms).3f)' % err)
def decorated_compare_images(*args,**kwargs): result = func(*args,**kwargs) extension = '.png' # TODO: test more backends for fname in 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 # 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
def assert_fig(expected, extensions=['png']): j = os.path.join for extension in extensions: if not extension in comparable_formats(): raise ImageComparisonFailure('Cannot compare %s files ' 'on this system' % extension) for fignum in plt.get_fignums(): figure = plt.figure(fignum) path = os.path.dirname(__file__) actual_fname = j(path, 'plots', expected) + '_actual_.' + extension expected_fname = j(path, 'plots', expected) + '.' + extension figure.savefig(actual_fname) err = compare_images(expected_fname, actual_fname, 13) if err: raise ImageComparisonFailure( 'images not close: %(actual)s vs. %(expected)s ' '(RMS %(rms).3f)' % err)
def _compare_images(self, ax, filename, tol=10): """ Helper method to do the comparisons """ assert isinstance(ax, Artist) if GENERATE_BASELINE: savefig(os.path.join(BASELINE_DIR, filename)) savefig(os.path.join(self.tempdir, filename)) err = compare_images(os.path.join(BASELINE_DIR, filename), os.path.join(self.tempdir, filename), tol, in_decorator=True) if err: raise ImageComparisonFailure('images not close: %(actual)s ' 'vs. %(expected)s ' '(RMS %(rms).3f)' % err)
def convert(filename): ''' Convert the named file into a png file. Returns the name of the created file. ''' base, extension = filename.rsplit('.', 1) if extension not in converter: raise ImageComparisonFailure("Don't know how to convert %s files to png" % extension) newname = base + '_' + extension + '.png' if not os.path.exists(filename): raise IOError("'%s' does not exist" % filename) # Only convert the file if the destination doesn't already exist or # is out of date. if (not os.path.exists(newname) or os.stat(newname).st_mtime < os.stat(filename).st_mtime): converter[extension](filename, newname) return newname
def convert(filename, cache): """ Convert the named file into a png file. Returns the name of the created file. If *cache* is True, the result of the conversion is cached in `matplotlib._get_cachedir() + '/test_cache/'`. The caching is based on a hash of the exact contents of the input file. The is no limit on the size of the cache, so it may need to be manually cleared periodically. """ base, extension = filename.rsplit('.', 1) if extension not in converter: raise ImageComparisonFailure( "Don't know how to convert %s files to png" % extension) newname = base + '_' + extension + '.png' if not os.path.exists(filename): raise IOError("'%s' does not exist" % filename) # Only convert the file if the destination doesn't already exist or # is out of date. if (not os.path.exists(newname) or os.stat(newname).st_mtime < os.stat(filename).st_mtime): if cache: cache_dir = get_cache_dir() else: cache_dir = None if cache_dir is not None: hash_value = get_file_hash(filename) new_ext = os.path.splitext(newname)[1] cached_file = os.path.join(cache_dir, hash_value + new_ext) if os.path.exists(cached_file): shutil.copyfile(cached_file, newname) return newname converter[extension](filename, newname) if cache_dir is not None: shutil.copyfile(newname, cached_file) return newname
def check_figure(filename, tol=1E-3): """ Compare the given filename to the baseline """ file_fmt = os.path.splitext(filename)[0] + "_{0}.png" dirname, fname = os.path.split(filename) testdir = os.path.dirname(__file__) baseline = os.path.abspath(os.path.join(testdir, 'baseline', file_fmt)) result = os.path.abspath(os.path.join(testdir, 'results', file_fmt)) resultdir = os.path.dirname(result) if not os.path.exists(resultdir): os.makedirs(resultdir) plt.close('all') mpl_setup = open(os.path.join(testdir, 'settings.py')).read() with set_cwd(dirname): with open(fname) as f: code = compile(mpl_setup + f.read(), "somefile.py", 'exec') exec(code, {'pl': plt, 'plt': plt, 'pylab': plt}) for fignum in plt.get_fignums(): fig = plt.figure(fignum) if colors.colorConverter.to_rgb(fig.get_facecolor()) == (0, 0, 0): fig.savefig(result.format(fignum), facecolor='k', edgecolor='none') else: fig.savefig(result.format(fignum)) err = compare_images(baseline.format(fignum), result.format(fignum), tol) if err: if filename in KNOWN_FAILURES: raise SkipTest("known errors in {0}".format(filename)) else: raise ImageComparisonFailure(err)