Skip to content

Commit

Permalink
verif: Add check_result function to check for errors and compute error
Browse files Browse the repository at this point in the history
  • Loading branch information
fischeti committed Feb 13, 2024
1 parent ae717f1 commit 8dea6b8
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions util/sim/data_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,3 +211,29 @@ def from_buffer(byte_array, ctype='uint32_t'):
value *= -1
array.append(value)
return array


def check_result(expected, actual, atol=None, rtol=None):
"""Check if the actual result is within the expected range.
Args:
expected: The expected result.
actual: The actual result.
atol: Absolute tolerance. The maximum absolute difference between
the expected and actual result. Mutually exclusive with `rtol`.
rtol: Relative tolerance. The maximum relative difference between
the expected and actual result. Mutually exclusive with `atol`.
"""
if atol is not None and rtol is not None:
raise ValueError('atol and rtol are mutually exclusive.')
if atol is not None:
fail = np.allclose(expected, actual, atol=atol, equal_nan=False)
error = np.abs(expected - actual)
elif rtol is not None:
fail = np.allclose(expected, actual, rtol=rtol, equal_nan=False)
scale = np.maximum(np.abs(expected), np.abs(actual))
scale[scale == 0] = 1 # Avoid division by zero, use absolute tolerance instead
error = np.abs(expected - actual) / scale
else:
raise ValueError('Either atol or rtol must be specified.')
return fail, error

0 comments on commit 8dea6b8

Please sign in to comment.