assertStructuredAlmostEqual
(function from pyomo.common.unittest
)
- pyomo.common.unittest.assertStructuredAlmostEqual(first, second, places=None, msg=None, delta=None, reltol=None, abstol=None, allow_second_superset=False, item_callback=<function _floatOrCall>, exception=<class 'ValueError'>, formatter=<function _defaultFormatter>)[source]
Test that first and second are equal up to a tolerance
This compares first and second using both an absolute (abstol) and relative (reltol) tolerance. It will recursively descend into Sequence and Mapping containers (allowing for the relative comparison of structured data including lists and dicts).
places and delta is supported for compatibility with assertAlmostEqual. If places is supplied, abstol is computed as 10**-places. delta is an alias for abstol.
If none of {abstol, reltol, places, delta} are specified, reltol defaults to 1e-7.
If allow_second_superset is True, then:
only key/value pairs found in mappings in first are compared to second (allowing mappings in second to contain extra keys)
only values found in sequences in first are compared to second (allowing sequences in second to contain extra values)
The relative error is computed for numerical values as
abs(first - second) / max(abs(first), abs(second))
only when first != second (thereby avoiding divide-by-zero errors).
Items (entries other than Sequence / Mapping containers, matching strings, and items that satisfy first is second) are passed to the item_callback before testing equality and relative tolerances.
Raises exception if first and second are not equal within tolerance.
- Parameters:
first – the first value to compare
second – the second value to compare
places (int) – first and second are considered equivalent if their difference is between places decimal places; equivalent to abstol = 10**-places (included for compatibility with assertAlmostEqual)
msg (str) – the message to raise on failure
delta (float) – alias for abstol
abstol (float) – the absolute tolerance. first and second are considered equivalent if their absolute difference is less than abstol
reltol (float) – the relative tolerance. first and second are considered equivalent if their absolute difference divided by the largest of first and second is less than reltol
allow_second_superset (bool) – If True, then extra entries in containers found on second will not trigger a failure.
item_callback (function) – items (other than Sequence / Mapping containers, matching strings, and items satisfying is) are passed to this callback to generate the (nominally floating point) value to use for comparison.
exception (Exception) – exception to raise when first is not ‘almost equal’ to second.
formatter (function) – callback for generating the final failure message (for compatibility with unittest)