-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiff_outputs.py
More file actions
189 lines (144 loc) · 4.73 KB
/
Copy pathdiff_outputs.py
File metadata and controls
189 lines (144 loc) · 4.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#!/usr/bin/env python3
import os
import sys
import json
import numpy as np
import pandas as pd
from pathlib import Path
# ------------------------------
# CONFIGURATION
# ------------------------------
TOL_ABS = 1e-6
TOL_REL = 1e-4
FILES_TO_COMPARE = [
"results/projection_points.csv",
"results/entropy_summary.csv",
"results/geodesics.npy",
"results/delta_g_summary.csv",
"results/hubble_fit_summary.json",
"results/tda_landscape.npy",
]
# ------------------------------
# UTILITIES
# ------------------------------
def safe_load_csv(path):
try:
return pd.read_csv(path)
except Exception:
return None
def safe_load_json(path):
try:
with open(path) as f:
return json.load(f)
except Exception:
return None
def safe_load_npy(path):
try:
return np.load(path, allow_pickle=True)
except Exception:
return None
def write(report_dir, name, text):
with open(report_dir / name, "w") as f:
f.write(text)
def numeric_diff(a, b):
"""Return max absolute and relative diff."""
a = np.asarray(a)
b = np.asarray(b)
if a.shape != b.shape:
return np.inf, np.inf
abs_diff = np.max(np.abs(a - b))
rel_diff = np.max(np.abs(a - b) / (np.abs(b) + 1e-12))
return abs_diff, rel_diff
# ------------------------------
# MAIN DIFF LOGIC
# ------------------------------
def diff_csv(current, previous):
if current is None or previous is None:
return "Missing file in one of the runs", True
if current.shape != previous.shape:
return f"Shape mismatch: {current.shape} vs {previous.shape}", True
diffs = []
for col in current.columns:
if col not in previous.columns:
diffs.append(f"Column {col} missing in previous")
continue
a = current[col].values
b = previous[col].values
absd, reld = numeric_diff(a, b)
diffs.append(f"{col}: abs={absd:.3e}, rel={reld:.3e}")
# Determine if any diff exceeds tolerance
fail = any(
("abs=" in d and float(d.split("abs=")[1].split(",")[0]) > TOL_ABS)
or ("rel=" in d and float(d.split("rel=")[1]) > TOL_REL)
for d in diffs
)
return "\n".join(diffs), fail
def diff_json(current, previous):
if current is None or previous is None:
return "Missing JSON file", True
diffs = []
fail = False
keys = set(current.keys()) | set(previous.keys())
for k in keys:
if k not in current:
diffs.append(f"{k}: missing in current")
fail = True
continue
if k not in previous:
diffs.append(f"{k}: missing in previous")
fail = True
continue
a = current[k]
b = previous[k]
if isinstance(a, (int, float)) and isinstance(b, (int, float)):
absd = abs(a - b)
reld = abs(a - b) / (abs(b) + 1e-12)
diffs.append(f"{k}: abs={absd:.3e}, rel={reld:.3e}")
if absd > TOL_ABS or reld > TOL_REL:
fail = True
else:
if a != b:
diffs.append(f"{k}: {a} != {b}")
fail = True
return "\n".join(diffs), fail
def diff_npy(current, previous):
if current is None or previous is None:
return "Missing NPY file", True
absd, reld = numeric_diff(current, previous)
fail = absd > TOL_ABS or reld > TOL_REL
return f"abs={absd:.3e}, rel={reld:.3e}", fail
# ------------------------------
# ENTRY POINT
# ------------------------------
def main():
if len(sys.argv) != 4:
print("Usage: diff_outputs.py <current_dir> <previous_dir> <report_dir>")
sys.exit(1)
current_dir = Path(sys.argv[1])
previous_dir = Path(sys.argv[2])
report_dir = Path(sys.argv[3])
report_dir.mkdir(parents=True, exist_ok=True)
overall_fail = False
for relpath in FILES_TO_COMPARE:
cur = current_dir / relpath
prev = previous_dir / relpath
report_name = relpath.replace("/", "_") + ".txt"
if relpath.endswith(".csv"):
result, fail = diff_csv(safe_load_csv(cur), safe_load_csv(prev))
elif relpath.endswith(".json"):
result, fail = diff_json(safe_load_json(cur), safe_load_json(prev))
elif relpath.endswith(".npy"):
result, fail = diff_npy(safe_load_npy(cur), safe_load_npy(prev))
else:
result = "Unsupported file type"
fail = False
write(report_dir, report_name, result)
overall_fail = overall_fail or fail
if overall_fail:
print("Regression detected — see diff_report/")
sys.exit(1)
else:
print("No significant regressions detected.")
sys.exit(0)
if __name__ == "__main__":
main()