22 """Helper functions for comparing `lsst.pex.config.Config` instancess. 24 Theses function should be use for any comparison in a `lsst.pex.Config.compare` 25 or `lsst.pex.config.Field._compare` implementation, as they take care of 26 writing messages as well as floating-point comparisons and shortcuts. 31 __all__ = (
"getComparisonName",
"compareScalars",
"compareConfigs")
35 """Create a comparison name that is used for printed output of comparisons. 40 Name of the first configuration. 42 Name of the second configuration. 47 When ``name1`` and ``name2`` are equal, the returned name is 48 simply one of the names. When they are different the returned name is 49 formatted as ``"{name1} / {name2}"``. 52 return "%s / %s" % (name1, name2)
57 """Compare two scalar values for equality. 59 This function is a helper for `lsst.pex.config.Config.compare`. 64 Name to use when reporting differences, typically created by 67 Left-hand side value to compare. 69 Right-hand side value to compare. 70 output : callable or `None` 71 A callable that takes a string, used (possibly repeatedly) to report 72 inequalities (for example, `print`). Set to `None` to disable output. 73 rtol : `float`, optional 74 Relative tolerance for floating point comparisons. 75 atol : `float`, optional 76 Absolute tolerance for floating point comparisons. 77 dtype : class, optional 78 Data type of values for comparison. May be `None` if values are not 84 `True` if the values are equal, `False` if they are not. 88 lsst.pex.config.compareConfigs 92 Floating point comparisons are performed by `numpy.allclose`. 94 if v1
is None or v2
is None:
96 elif dtype
in (float, complex):
97 result = numpy.allclose(v1, v2, rtol=rtol, atol=atol)
or (numpy.isnan(v1)
and numpy.isnan(v2))
100 if not result
and output
is not None:
101 output(
"Inequality in %s: %r != %r" % (name, v1, v2))
105 def compareConfigs(name, c1, c2, shortcut=True, rtol=1E-8, atol=1E-8, output=None):
106 """Compare two `lsst.pex.config.Config` instances for equality. 108 This function is a helper for `lsst.pex.config.Config.compare`. 113 Name to use when reporting differences, typically created by 115 v1 : `lsst.pex.config.Config` 116 Left-hand side config to compare. 117 v2 : `lsst.pex.config.Config` 118 Right-hand side config to compare. 119 shortcut : `bool`, optional 120 If `True`, return as soon as an inequality is found. Default is `True`. 121 rtol : `float`, optional 122 Relative tolerance for floating point comparisons. 123 atol : `float`, optional 124 Absolute tolerance for floating point comparisons. 125 output : callable, optional 126 A callable that takes a string, used (possibly repeatedly) to report 127 inequalities. For example: `print`. 132 `True` when the two `lsst.pex.config.Config` instances are equal. 133 `False` if there is an inequality. 137 lsst.pex.config.compareScalars 141 Floating point comparisons are performed by `numpy.allclose`. 143 If ``c1`` or ``c2`` contain `~lsst.pex.config.RegistryField` or 144 `~lsst.pex.config.ConfigChoiceField` instances, *unselected* 145 `~lsst.pex.config.Config` instances will not be compared. 147 assert name
is not None 152 if output
is not None:
153 output(
"LHS is None for %s" % name)
157 if output
is not None:
158 output(
"RHS is None for %s" % name)
161 if output
is not None:
162 output(
"Config types do not match for %s: %s != %s" % (name,
type(c1),
type(c2)))
165 for field
in c1._fields.values():
166 result = field._compare(c1, c2, shortcut=shortcut, rtol=rtol, atol=atol, output=output)
167 if not result
and shortcut:
169 equal = equal
and result
def compareConfigs(name, c1, c2, shortcut=True, rtol=1E-8, atol=1E-8, output=None)
def compareScalars(name, v1, v2, output, rtol=1E-8, atol=1E-8, dtype=None)
def getComparisonName(name1, name2)