-
Notifications
You must be signed in to change notification settings - Fork 121
Improved error reporting for failed tolerance checks #988
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 9 commits
e31d6a0
93053e1
8f9793f
a84e311
c27c941
c817dfe
9123f8b
35fbc7e
dd38696
6329b04
a7f38f2
34c618a
dda29ac
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -45,23 +45,92 @@ def compare(candidate: Pack, golden: Pack, tol: Tolerance) -> typing.Tuple[Error | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| error = compute_error(cVal, gVal) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| avg_err.push(error) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def raise_err(msg: str): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def raise_err_with_max_diagnostics(msg: str): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # Find maximum errors across ALL files for diagnostics | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| max_errors = find_maximum_errors(candidate, golden) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| diagnostic_msg = format_diagnostic_message(max_errors) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return None, f"""\ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Variable n°{valIndex+1} (1-indexed) in {gFilepath} {msg}: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| - Candidate: {cVal} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| - Golden: {gVal} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| - Error: {error} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| - Tolerance: {tol} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| - Tolerance: {tol}{diagnostic_msg} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if math.isnan(gVal): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return raise_err("is NaN in the golden file") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return raise_err_with_max_diagnostics("is NaN in the golden file") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if math.isnan(cVal): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return raise_err("is NaN in the pack file") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return raise_err_with_max_diagnostics("is NaN in the pack file") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if not is_close(error, tol): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return raise_err("is not within tolerance") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return raise_err_with_max_diagnostics("is not within tolerance") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # Return the average relative error | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return avg_err.get(), None | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def format_diagnostic_message(max_errors: typing.Tuple[typing.Optional[typing.Tuple[str, int, float, float, float, float]], typing.Optional[typing.Tuple[str, int, float, float, float, float]]]) -> str: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| """Format the diagnostic message showing maximum errors.""" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| max_abs_info, max_rel_info = max_errors | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| diagnostic_msg = "" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if max_abs_info: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| filepath, var_idx, golden_val, candidate_val, abs_error, rel_error = max_abs_info | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| rel_error_str = f"{rel_error:.2E}" if not math.isnan(rel_error) else "NaN" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| diagnostic_msg += f"\n\nDiagnostics - Maximum absolute error across ALL files:\n" \ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| f" - File: {filepath}\n" \ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| f" - Variable n°{var_idx+1}\n" \ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| f" - Candidate: {candidate_val}\n" \ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| f" - Golden: {golden_val}\n" \ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| f" - Absolute Error: {abs_error:.2E}\n" \ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| f" - Relative Error: {rel_error_str}" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if max_rel_info: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| filepath, var_idx, golden_val, candidate_val, rel_error, abs_error = max_rel_info | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| diagnostic_msg += f"\n\nDiagnostics - Maximum relative error across ALL files:\n" \ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| f" - File: {filepath}\n" \ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| f" - Variable n°{var_idx+1}\n" \ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| f" - Candidate: {candidate_val}\n" \ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| f" - Golden: {golden_val}\n" \ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| f" - Relative Error: {rel_error:.2E}\n" \ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| f" - Absolute Error: {abs_error:.2E}" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return diagnostic_msg | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def find_maximum_errors(candidate: Pack, golden: Pack) -> typing.Tuple[typing.Optional[typing.Tuple[str, int, float, float, float, float]], typing.Optional[typing.Tuple[str, int, float, float, float, float]]]: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Scan all files to find the maximum absolute and relative errors. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Returns tuple of: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| - max_abs_info: (filepath, var_index, golden_val, candidate_val, absolute_error, relative_error) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| - max_rel_info: (filepath, var_index, golden_val, candidate_val, relative_error, absolute_error) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| - max_rel_info: (filepath, var_index, golden_val, candidate_val, relative_error, absolute_error) | |
| def format_diagnostic_message(max_errors: Tuple[Optional[ErrorInfo], Optional[ErrorInfo]]) -> str: | |
| """Format the diagnostic message showing maximum errors.""" | |
| max_abs_info, max_rel_info = max_errors | |
| diagnostic_msg = "" | |
| if max_abs_info: | |
| rel_error_str = f"{max_abs_info.relative_error:.2E}" if not math.isnan(max_abs_info.relative_error) else "NaN" | |
| diagnostic_msg += f"\n\nDiagnostics - Maximum absolute error across ALL files:\n" \ | |
| f" - File: {max_abs_info.filepath}\n" \ | |
| f" - Variable n°{max_abs_info.var_index+1}\n" \ | |
| f" - Candidate: {max_abs_info.candidate_val}\n" \ | |
| f" - Golden: {max_abs_info.golden_val}\n" \ | |
| f" - Absolute Error: {max_abs_info.absolute_error:.2E}\n" \ | |
| f" - Relative Error: {rel_error_str}" | |
| if max_rel_info: | |
| diagnostic_msg += f"\n\nDiagnostics - Maximum relative error across ALL files:\n" \ | |
| f" - File: {max_rel_info.filepath}\n" \ | |
| f" - Variable n°{max_rel_info.var_index+1}\n" \ | |
| f" - Candidate: {max_rel_info.candidate_val}\n" \ | |
| f" - Golden: {max_rel_info.golden_val}\n" \ | |
| f" - Relative Error: {max_rel_info.relative_error:.2E}\n" \ | |
| f" - Absolute Error: {max_rel_info.absolute_error:.2E}" | |
| return diagnostic_msg | |
| def find_maximum_errors(candidate: Pack, golden: Pack) -> Tuple[Optional[ErrorInfo], Optional[ErrorInfo]]: | |
| """ | |
| Scan all files to find the maximum absolute and relative errors. | |
| Returns tuple of: | |
| - max_abs_info: ErrorInfo or None | |
| - max_rel_info: ErrorInfo or None |
Copilot
AI
Aug 18, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using -1.0 as an initial value for maximum error tracking is unclear and could be problematic if all errors are actually negative (though unlikely in this context). Consider using float('-inf') or None with explicit checks for better clarity.
| max_rel_error = -1.0 | |
| max_abs_error = float('-inf') | |
| max_abs_info = None | |
| max_rel_error = float('-inf') |
Copilot
AI
Aug 18, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same issue as with max_abs_error - using -1.0 as initial value is unclear. Consider using float('-inf') or None with explicit checks for better clarity.
| max_rel_error = -1.0 | |
| max_abs_error = float('-inf') | |
| max_abs_info = None | |
| max_rel_error = float('-inf') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The function signature uses deeply nested tuple types that are difficult to read and maintain. Consider defining named tuple types or dataclasses for the error information to improve code clarity.