-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathregression_compare.py
More file actions
178 lines (132 loc) · 5.21 KB
/
Copy pathregression_compare.py
File metadata and controls
178 lines (132 loc) · 5.21 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
from __future__ import annotations
import argparse
import json
import sys
from typing import Any, Dict, List
def load_json(path: str) -> Any:
with open(path, "r", encoding="utf-8") as f:
return json.load(f)
def extract_results(payload: Any) -> List[Dict[str, Any]]:
if isinstance(payload, list):
return payload
if isinstance(payload, dict):
if "results" in payload and isinstance(payload["results"], list):
return payload["results"]
raise TypeError("Expected results payload to be a list or a dict containing a 'results' list.")
def get_case_map(results: List[Dict[str, Any]]) -> Dict[str, Dict[str, Any]]:
return {row["id"]: row for row in results}
def get_weighted_score(row: Dict[str, Any]) -> float:
if "weighted_score" in row:
return float(row["weighted_score"])
heuristic = row.get("heuristic_evaluation", {})
if "weighted_score" in heuristic:
return float(heuristic["weighted_score"])
raise KeyError(f"weighted_score not found for row id={row.get('id')}")
def get_verdict(row: Dict[str, Any]) -> str:
if "verdict" in row:
return str(row["verdict"])
heuristic = row.get("heuristic_evaluation", {})
if "verdict" in heuristic:
return str(heuristic["verdict"])
raise KeyError(f"verdict not found for row id={row.get('id')}")
def get_gate_pass(row: Dict[str, Any]) -> bool:
if "gate_pass" in row:
return bool(row["gate_pass"])
heuristic = row.get("heuristic_evaluation", {})
if "gate_pass" in heuristic:
return bool(heuristic["gate_pass"])
raise KeyError(f"gate_pass not found for row id={row.get('id')}")
def compare_results(
baseline: List[Dict[str, Any]],
latest: List[Dict[str, Any]],
max_drop: float,
) -> int:
baseline_map = get_case_map(baseline)
latest_map = get_case_map(latest)
exit_code = 0
missing_cases = sorted(set(baseline_map.keys()) - set(latest_map.keys()))
if missing_cases:
print("ERROR: Missing cases in latest run:")
for case_id in missing_cases:
print(f" - {case_id}")
exit_code = 1
for case_id, base_row in baseline_map.items():
if case_id not in latest_map:
continue
latest_row = latest_map[case_id]
base_gate_pass = get_gate_pass(base_row)
latest_gate_pass = get_gate_pass(latest_row)
if base_gate_pass and not latest_gate_pass:
print(f"ERROR: Gate regression for {case_id}")
exit_code = 1
base_score = get_weighted_score(base_row)
latest_score = get_weighted_score(latest_row)
score_drop = round(base_score - latest_score, 2)
if score_drop > max_drop:
print(
f"ERROR: Score drop too large for {case_id} "
f"(baseline={base_score}, latest={latest_score}, drop={score_drop})"
)
exit_code = 1
base_verdict = get_verdict(base_row)
latest_verdict = get_verdict(latest_row)
verdict_rank = {"FAIL": 0, "WARN": 1, "PASS": 2}
if verdict_rank.get(latest_verdict, -1) < verdict_rank.get(base_verdict, -1):
print(
f"ERROR: Verdict regression for {case_id} "
f"(baseline={base_verdict}, latest={latest_verdict})"
)
exit_code = 1
if exit_code == 0:
print("OK: No regression detected.")
return exit_code
def resolve_paths(
baseline_path: str | None,
latest_path: str | None,
task: str | None,
) -> tuple[str, str]:
if baseline_path and latest_path:
return baseline_path, latest_path
if task:
resolved_baseline = baseline_path or f"baselines/{task}/baseline_results.json"
resolved_latest = latest_path or f"results/{task}/latest_results.json"
return resolved_baseline, resolved_latest
raise ValueError(
"Provide both baseline_path and latest_path, or use --task to resolve example-scoped defaults."
)
def parse_args() -> argparse.Namespace:
parser = argparse.ArgumentParser(description="Compare eval results against baseline.")
parser.add_argument("baseline_path", nargs="?", type=str, help="Path to baseline results JSON")
parser.add_argument("latest_path", nargs="?", type=str, help="Path to latest results JSON")
parser.add_argument(
"--task",
type=str,
default=None,
help="Task/example name used to resolve default paths under baselines/<task>/ and results/<task>/",
)
parser.add_argument(
"--max-drop",
type=float,
default=0.3,
help="Maximum allowed weighted score drop before failing",
)
return parser.parse_args()
def main() -> None:
args = parse_args()
baseline_path, latest_path = resolve_paths(
baseline_path=args.baseline_path,
latest_path=args.latest_path,
task=args.task,
)
baseline_payload = load_json(baseline_path)
latest_payload = load_json(latest_path)
baseline = extract_results(baseline_payload)
latest = extract_results(latest_payload)
exit_code = compare_results(
baseline=baseline,
latest=latest,
max_drop=args.max_drop,
)
sys.exit(exit_code)
if __name__ == "__main__":
main()