23 Helper functions for comparing Configs.
25 The function here should be use for any comparison in a Config.compare
26 or Field._compare implementation, as they take care of writing messages
27 as well as floating-point comparisons and shortcuts.
32 __all__ = (
"getComparisonName",
"compareScalars",
"compareConfigs")
36 return "%s / %s" % (name1, name2)
40 """Helper function for Config.compare; used to compare two scalar values for equality.
42 @param[in] name Name to use when reporting differences
43 @param[in] dtype Data type for comparison; may be None if it's definitely not floating-point.
44 @param[in] v1 LHS value to compare
45 @param[in] v2 RHS value to compare
46 @param[in] output If not None, a callable that takes a string, used (possibly repeatedly)
47 to report inequalities.
48 @param[in] rtol Relative tolerance for floating point comparisons.
49 @param[in] atol Absolute tolerance for floating point comparisons.
50 @param[in] dtype Data type for comparison; may be None if it's definitely not floating-point.
52 Floating point comparisons are performed by numpy.allclose; refer to that for details.
54 if v1
is None or v2
is None:
56 elif dtype
in (float, complex):
57 result = numpy.allclose(v1, v2, rtol=rtol, atol=atol)
or (numpy.isnan(v1)
and numpy.isnan(v2))
60 if not result
and output
is not None:
61 output(
"Inequality in %s: %r != %r" % (name, v1, v2))
64 def compareConfigs(name, c1, c2, shortcut=True, rtol=1E-8, atol=1E-8, output=None):
65 """Helper function for Config.compare; used to compare two Configs for equality.
67 If the Configs contain RegistryFields or ConfigChoiceFields, unselected Configs
70 @param[in] name Name to use when reporting differences
71 @param[in] c1 LHS config to compare
72 @param[in] c2 RHS config to compare
73 @param[in] shortcut If True, return as soon as an inequality is found.
74 @param[in] rtol Relative tolerance for floating point comparisons.
75 @param[in] atol Absolute tolerance for floating point comparisons.
76 @param[in] output If not None, a callable that takes a string, used (possibly repeatedly)
77 to report inequalities.
79 Floating point comparisons are performed by numpy.allclose; refer to that for details.
81 assert name
is not None
86 if output
is not None:
87 output(
"LHS is None for %s" % name)
91 if output
is not None:
92 output(
"RHS is None for %s" % name)
94 if type(c1) != type(c1):
95 if output
is not None:
96 output(
"Config types do not match for %s: %s != %s" % (name, type(c1), type(c2)))
99 for field
in c1._fields.itervalues():
100 result = field._compare(c1, c2, shortcut=shortcut, rtol=rtol, atol=atol, output=output)
101 if not result
and shortcut:
103 equal = equal
and result