Skip to content

Commit

Permalink
Merge pull request #2849 from swryan/2763_assert
Browse files Browse the repository at this point in the history
Remove deprecated assert_rel_error() function
  • Loading branch information
swryan committed Mar 15, 2023
2 parents d0aabfb + b5bbed0 commit 5ec5558
Showing 1 changed file with 0 additions and 89 deletions.
89 changes: 0 additions & 89 deletions openmdao/utils/assert_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -391,95 +391,6 @@ def assert_no_dict_jacobians(system, include_self=True, recurse=True):
raise AssertionError('\n'.join(parts))


def assert_rel_error(test_case, actual, desired, tolerance=1e-15):
"""
Check relative error.
Determine that the relative error between `actual` and `desired`
is within `tolerance`. If `desired` is zero, then use absolute error.
Parameters
----------
test_case : class:`unittest.TestCase`
TestCase instance used for assertions.
actual : float, array-like, dict
The value from the test.
desired : float, array-like, dict
The value expected.
tolerance : float
Maximum relative error ``(actual - desired) / desired``.
Returns
-------
float
The error.
"""
warn_deprecation("'assert_rel_error' has been deprecated. Use "
"'assert_near_equal' instead.")

if isinstance(actual, dict) and isinstance(desired, dict):

actual_keys = set(actual.keys())
desired_keys = set(desired.keys())

if actual_keys.symmetric_difference(desired_keys):
msg = 'Actual and desired keys differ. Actual extra keys: {}, Desired extra keys: {}'
actual_extra = actual_keys.difference(desired_keys)
desired_extra = desired_keys.difference(actual_keys)
test_case.fail(msg.format(actual_extra, desired_extra))

error = 0.

for key in actual_keys:
try:
new_error = assert_rel_error(test_case, actual[key], desired[key], tolerance)
error = max(error, new_error)
except test_case.failureException as exception:
msg = '{}: '.format(key) + str(exception)
raise test_case.failureException(msg) from None

elif isinstance(actual, float) and isinstance(desired, float):
if isnan(actual) and not isnan(desired):
test_case.fail('actual nan, desired %s' % desired)
if desired != 0:
error = (actual - desired) / desired
else:
error = actual
if abs(error) > tolerance:
test_case.fail('actual %s, desired %s, rel error %s, tolerance %s'
% (actual, desired, error, tolerance))

# array values
else:
actual = np.atleast_1d(actual)
desired = np.atleast_1d(desired)
if actual.shape != desired.shape:
test_case.fail(
'actual and desired have differing shapes.'
' actual {}, desired {}'.format(actual.shape, desired.shape))
if not np.all(np.isnan(actual) == np.isnan(desired)):
if actual.size == 1 and desired.size == 1:
test_case.fail('actual %s, desired %s' % (actual, desired))
else:
test_case.fail('actual and desired values have non-matching nan'
' values')

if np.linalg.norm(desired) == 0:
error = np.linalg.norm(actual)
else:
error = np.linalg.norm(actual - desired) / np.linalg.norm(desired)

if abs(error) > tolerance:
if actual.size < 10 and desired.size < 10:
test_case.fail('actual %s, desired %s, rel error %s, tolerance %s'
% (actual, desired, error, tolerance))
else:
test_case.fail('arrays do not match, rel error %.3e > tol (%.3e)' %
(error, tolerance))

return error


def assert_near_equal(actual, desired, tolerance=1e-15):
"""
Check relative error.
Expand Down

0 comments on commit 5ec5558

Please sign in to comment.