def h5diff(f1, f2, key=None, precision=1.e-6): """ Modified version of triqs.utility.h5diff.h5diff key is the path of the data set to be compared: e.g., "dmft_out/Sigma_iw" """ if key is None: h5diff_org(os.path.abspath(f1), os.path.abspath(f2), precision) return keys = key.split("/") h1 = HDFArchive(os.path.abspath(f1), 'r') h2 = HDFArchive(os.path.abspath(f2), 'r') for k in keys: h1 = h1[k] h2 = h2[k] compare(key, h1, h2, 0, precision) if failures: print('-' * 50, file=sys.stderr) print('-' * 20 + ' FAILED ' + '-' * 20, file=sys.stderr) print('-' * 50, file=sys.stderr) for x in failures: print(x, file=sys.stderr) print('-' * 50, file=sys.stderr) raise RuntimeError("FAILED")
def compare(key, a, b, level, precision): """Compare two objects named key""" if verbose and key: print(level * ' ' + "Comparing %s ...." % key) try: t = type(a) assert isinstance(b, t), "%s have different types" % key # !!! added AnalyzerResult in the following line if t == dict or isinstance(a, (HDFArchiveGroup, AnalyzerResult)): if list(a.keys()) != list(b.keys()): failures.append( "Two archive groups '%s' with different keys \n %s \n vs\n %s" % (key, list(a.keys()), list(b.keys()))) for k in set(a.keys()).intersection(list(b.keys())): compare(key + '/' + k, a[k], b[k], level + 1, precision) # The TRIQS object which are comparable starts here .... elif t in [GfImFreq, GfImTime, GfReFreq, GfReTime, GfLegendre]: assert_gfs_are_close(a, b, precision) elif t in [BlockGf]: assert_block_gfs_are_close(a, b, precision) elif t in [Operator]: assert (a - b).is_zero(), "Many body operators not equal" # ... until here elif isinstance(a, numpy.ndarray): # !!! changed this so that it allows NaN values np.testing.assert_almost_equal( a, b, -int(np.round(np.log10(precision / 1.5)))) elif t in [int, float, complex]: assert abs(a - b) < 1.e-10, " a-b = %" % (a - b) elif t in [bool, numpy.bool_]: assert a == b elif t in [list, tuple]: assert len(a) == len(b), "List of different size" for x, y in zip(a, b): compare(key, x, y, level + 1, precision) elif t in [str]: assert a == b, "Strings '%s' and '%s' are different" % (a, b) else: raise NotImplementedError( "The type %s for key '%s' is not comparable by h5diff" % (t, key)) except (AssertionError, RuntimeError, ValueError) as e: # eliminate the lines starting with .., which are not the main error # message mess = '\n'.join([ l for l in str(e).split('\n') if l.strip() and not l.startswith('..') ]) failures.append("Comparison of key '%s' has failed:\n " "" % key + mess)
elif t in [str]: assert a == b, "Strings '%s' and '%s' are different" % (a, b) else: raise NotImplementedError( "The type %s for key '%s' is not comparable by h5diff" % (t, key)) except (AssertionError, RuntimeError, ValueError) as e: # eliminate the lines starting with .., which are not the main error # message mess = '\n'.join([ l for l in str(e).split('\n') if l.strip() and not l.startswith('..') ]) failures.append("Comparison of key '%s' has failed:\n " "" % key + mess) compare('', HDFArchive('analyzers.out.h5', 'r'), HDFArchive('analyzers.ref.h5', 'r'), 0, 1.e-6) if failures: print('-' * 50, file=sys.stderr) print('-' * 20 + ' FAILED ' + '-' * 20, file=sys.stderr) print('-' * 50, file=sys.stderr) for x in failures: print(x, file=sys.stderr) print('-' * 50, file=sys.stderr) raise RuntimeError("FAILED")
def cmp(a, b, precision=1.e-15): compare('', a, b, 0, precision) if failures: raise AssertionError('\n'.join(failures))