@@ -22,33 +22,33 @@ def is_close(error: Error, tolerance: Tolerance) -> bool:
2222def compare (candidate : Pack , golden : Pack , tol : Tolerance ) -> typing .Tuple [Error , str ]:
2323 # Keep track of the average error
2424 avg_err = AverageError ()
25-
25+
2626 # Compare entry-count
2727 if len (candidate .entries ) != len (golden .entries ):
2828 return None , "Line count does not match."
29-
29+
3030 # For every entry in the golden's pack
3131 for gFilepath , gEntry in golden .entries .items ():
3232 # Find the corresponding entry in the candidate's pack
3333 cEntry = candidate .find (gFilepath )
34-
34+
3535 if cEntry is None :
3636 return None , f"No reference to { gFilepath } in the candidate's pack."
37-
37+
3838 # Compare variable-count
3939 if len (gEntry .doubles ) != len (cEntry .doubles ):
4040 return None , f"Variable count didn't match for { gFilepath } ."
41-
41+
4242 # Check if each variable is within tolerance
4343 for valIndex , (gVal , cVal ) in enumerate (zip (gEntry .doubles , cEntry .doubles )):
4444 # Keep track of the error and average errors
4545 error = compute_error (cVal , gVal )
4646 avg_err .push (error )
47-
47+
4848 def raise_err_with_max_diagnostics (msg : str ):
4949 # Find maximum errors across ALL files for diagnostics
5050 max_abs_info , max_rel_info = find_maximum_errors (candidate , golden )
51-
51+
5252 diagnostic_msg = ""
5353 if max_abs_info :
5454 max_abs_filepath , max_abs_valIndex , max_abs_gVal , max_abs_cVal , max_abs_error , max_abs_rel_error = max_abs_info
@@ -60,7 +60,7 @@ def raise_err_with_max_diagnostics(msg: str):
6060 f" - Golden: { max_abs_gVal } \n " \
6161 f" - Absolute Error: { max_abs_error :.2E} \n " \
6262 f" - Relative Error: { rel_error_str } "
63-
63+
6464 if max_rel_info :
6565 max_rel_filepath , max_rel_valIndex , max_rel_gVal , max_rel_cVal , max_rel_error , max_rel_abs_error = max_rel_info
6666 diagnostic_msg += f"\n \n Diagnostics - Maximum relative error across ALL files:\n " \
@@ -70,22 +70,22 @@ def raise_err_with_max_diagnostics(msg: str):
7070 f" - Golden: { max_rel_gVal } \n " \
7171 f" - Relative Error: { max_rel_error :.2E} \n " \
7272 f" - Absolute Error: { max_rel_abs_error :.2E} "
73-
73+
7474 return None , f"""\
7575 Variable n°{ valIndex + 1 } (1-indexed) in { gFilepath } { msg } :
7676 - Candidate: { cVal }
7777 - Golden: { gVal }
7878 - Error: { error }
7979 - Tolerance: { tol } { diagnostic_msg }
8080"""
81-
81+
8282 if math .isnan (gVal ):
8383 return raise_err_with_max_diagnostics ("is NaN in the golden file" )
8484 if math .isnan (cVal ):
8585 return raise_err_with_max_diagnostics ("is NaN in the pack file" )
8686 if not is_close (error , tol ):
8787 return raise_err_with_max_diagnostics ("is not within tolerance" )
88-
88+
8989 # Return the average relative error
9090 return avg_err .get (), None
9191
@@ -99,30 +99,30 @@ def find_maximum_errors(candidate: Pack, golden: Pack) -> typing.Tuple[typing.Op
9999 """
100100 max_abs_error = - 1.0
101101 max_abs_info = None
102-
102+
103103 max_rel_error = - 1.0
104104 max_rel_info = None
105-
105+
106106 for gFilepath , gEntry in golden .entries .items ():
107107 cEntry = candidate .find (gFilepath )
108108 if cEntry is None :
109109 continue
110-
110+
111111 for valIndex , (gVal , cVal ) in enumerate (zip (gEntry .doubles , cEntry .doubles )):
112112 # Skip NaN values in golden or candidate
113113 if math .isnan (gVal ) or math .isnan (cVal ):
114114 continue
115-
115+
116116 error = compute_error (cVal , gVal )
117-
117+
118118 # Track maximum absolute error
119119 if error .absolute > max_abs_error :
120120 max_abs_error = error .absolute
121121 max_abs_info = (gFilepath , valIndex , gVal , cVal , error .absolute , error .relative )
122-
122+
123123 # Track maximum relative error (only if it's not NaN)
124124 if not math .isnan (error .relative ) and error .relative > max_rel_error :
125125 max_rel_error = error .relative
126126 max_rel_info = (gFilepath , valIndex , gVal , cVal , error .relative , error .absolute )
127-
127+
128128 return max_abs_info , max_rel_info
0 commit comments