-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathget_result.py
More file actions
324 lines (274 loc) · 12.9 KB
/
get_result.py
File metadata and controls
324 lines (274 loc) · 12.9 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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
#!/usr/bin/env python3
import json
import argparse
import os
import sys
from collections import defaultdict
def compare_results(ground_truth_path, new_results_path, output_path):
"""
Compare new test results against ground truth results
Args:
ground_truth_path: Path to the ground truth results.json
new_results_path: Path to the new results to compare
output_path: Path to write the comparison results
Returns:
Dictionary with comparison statistics
"""
# Load the ground truth results
with open("data/answer.json", 'r') as f:
ground_truth = json.load(f)
# Load the new results
with open(new_results_path, 'r') as f:
new_results = json.load(f)
# Map ground truth by task_id for easier lookup
ground_truth_map = {result["task_id"]: result for result in ground_truth}
# Initialize comparison results
comparison_results = []
statistics = {
"total_tasks": len(new_results),
"matching_tasks": 0,
"missing_tasks": 0,
"total_tests": 0,
"matching_tests": 0,
"mismatched_tests": 0,
"by_task": {}
}
# Compare each task result
print(len(new_results))
for new_result in new_results:
task_id = new_result["task_id"]
task_comparison = {
"task_id": task_id,
"task_status": "missing_in_ground_truth",
"matches": 0,
"mismatches": 0,
"total_tests": 0,
"comparison_details": []
}
# Check if task exists in ground truth
if task_id not in ground_truth_map:
task_comparison["task_status"] = "missing_in_ground_truth"
statistics["missing_tasks"] += 1
comparison_results.append(task_comparison)
continue
gt_result = ground_truth_map[task_id]
# Compare task status
if new_result["status"] != gt_result["status"]:
task_comparison["task_status"] = "status_mismatch"
task_comparison["ground_truth_status"] = gt_result["status"]
task_comparison["new_status"] = new_result["status"]
if new_result["statistics"]["total_tests"] == 0 and gt_result["statistics"]["total_tests"] != 0:
statistics["total_tests"] += gt_result["statistics"]["total_tests"]
statistics["mismatched_tests"] += gt_result["statistics"]["total_tests"]
statistics["by_task"][task_id] = {
"total_tests": gt_result["statistics"]["total_tests"],
"matches": 0,
"mismatches": gt_result["statistics"]["total_tests"],
"match_percentage": 0
}
comparison_results.append(task_comparison)
continue
# If status is error, compare error message
if new_result["status"] == "error":
if new_result.get("error", "") == gt_result.get("error", ""):
task_comparison["task_status"] = "success"
task_comparison["matches"] = 1
task_comparison["total_tests"] = 1
statistics["matching_tasks"] += 1
statistics["total_tests"] += 1
statistics["matching_tests"] += 1
else:
task_comparison["task_status"] = "error_mismatch"
task_comparison["mismatches"] = 1
task_comparison["total_tests"] = 1
task_comparison["ground_truth_error"] = gt_result.get("error", "")
task_comparison["new_error"] = new_result.get("error", "")
#print(gt_result["statistics"]["total_tests"])
statistics["total_tests"] += 1
statistics["mismatched_tests"] += 1
comparison_results.append(task_comparison)
continue
# Format 1 - list of function/params objects with function and results
if isinstance(new_result.get("results", []), list) and all(isinstance(r, dict) and "function" in r for r in new_result.get("results", [])):
# Map ground truth results by function for easier lookup
gt_function_results = {r["function"]: r for r in gt_result.get("results", [])}
for function_result in new_result.get("results", []):
function_name = function_result["function"]
# Check if function exists in ground truth
if function_name not in gt_function_results:
task_comparison["comparison_details"].append({
"function": function_name,
"status": "missing_in_ground_truth"
})
continue
gt_function_result = gt_function_results[function_name]
# Compare function results
compare_function_results(
function_name,
gt_function_result.get("results", []),
function_result.get("results", []),
task_comparison,
statistics
)
# Format 2 - direct list of results
elif isinstance(new_result.get("results", []), list) and all(isinstance(r, dict) and "params" in r for r in new_result.get("results", [])):
# Compare results directly
compare_function_results(
"default",
gt_result.get("results", []),
new_result.get("results", []),
task_comparison,
statistics
)
task_comparison["task_status"] = "success" if task_comparison["mismatches"] == 0 else "partial_match"
if task_comparison["mismatches"] == 0:
statistics["matching_tasks"] += 1
comparison_results.append(task_comparison)
# Add per-task statistics
statistics["by_task"][task_id] = {
"total_tests": task_comparison["total_tests"],
"matches": task_comparison["matches"],
"mismatches": task_comparison["mismatches"],
"match_percentage": (task_comparison["matches"] / task_comparison["total_tests"] * 100) if task_comparison["total_tests"] > 0 else 0
}
# Calculate overall statistics
if statistics["total_tests"] > 0:
statistics["match_percentage"] = (statistics["matching_tests"] / statistics["total_tests"]) * 100
else:
statistics["match_percentage"] = 0
# Prepare final results
final_results = {
"statistics": statistics,
"comparisons": comparison_results
}
# Write comparison results to file
with open(output_path, 'w') as f:
json.dump(final_results, f, indent=2)
return statistics
def compare_function_results(function_name, gt_results, new_results, task_comparison, statistics):
"""
Compare function results between ground truth and new results
"""
# Create mappings by param hash for better comparison
gt_results_map = {}
for i, result in enumerate(gt_results):
param_hash = json.dumps(result.get("params", {}), sort_keys=True)
gt_results_map[param_hash] = (i, result)
new_results_map = {}
for i, result in enumerate(new_results):
param_hash = json.dumps(result.get("params", {}), sort_keys=True)
new_results_map[param_hash] = (i, result)
# Find matching params in both results
all_params = set(gt_results_map.keys()) | set(new_results_map.keys())
for param_hash in all_params:
# Update total tests
task_comparison["total_tests"] += 1
statistics["total_tests"] += 1
# If params only in one set
if param_hash not in gt_results_map:
task_comparison["comparison_details"].append({
"function": function_name,
"params": json.loads(param_hash),
"status": "missing_in_ground_truth"
})
statistics["mismatched_tests"] += 1
task_comparison["mismatches"] += 1
continue
if param_hash not in new_results_map:
task_comparison["comparison_details"].append({
"function": function_name,
"params": json.loads(param_hash),
"status": "missing_in_new_results"
})
statistics["mismatched_tests"] += 1
task_comparison["mismatches"] += 1
continue
# Get corresponding results
gt_idx, gt_result = gt_results_map[param_hash]
new_idx, new_result = new_results_map[param_hash]
# Compare status
if gt_result["status"] != new_result["status"]:
task_comparison["comparison_details"].append({
"function": function_name,
"params": json.loads(param_hash),
"status": "status_mismatch",
"ground_truth_status": gt_result["status"],
"new_status": new_result["status"]
})
statistics["mismatched_tests"] += 1
task_comparison["mismatches"] += 1
continue
# Compare success result or error message
if gt_result["status"] == "success":
# Compare result value
if gt_result.get("result", "") != new_result.get("result", ""):
task_comparison["comparison_details"].append({
"function": function_name,
"params": json.loads(param_hash),
"status": "result_mismatch",
"ground_truth_result": gt_result.get("result", ""),
"new_result": new_result.get("result", "")
})
statistics["mismatched_tests"] += 1
task_comparison["mismatches"] += 1
else:
task_comparison["comparison_details"].append({
"function": function_name,
"params": json.loads(param_hash),
"status": "success"
})
statistics["matching_tests"] += 1
task_comparison["matches"] += 1
else: # status is error
# Compare error message
if (new_result.get("error_type") != gt_result.get("error_type") and new_result.get("error", "") != gt_result.get("error", "")):
task_comparison["comparison_details"].append({
"function": function_name,
"params": json.loads(param_hash),
"status": "error_mismatch",
"ground_truth_error": gt_result.get("error", ""),
"new_error": new_result.get("error", "")
})
statistics["mismatched_tests"] += 1
task_comparison["mismatches"] += 1
else:
task_comparison["comparison_details"].append({
"function": function_name,
"params": json.loads(param_hash),
"status": "success"
})
statistics["matching_tests"] += 1
task_comparison["matches"] += 1
def print_comparison_summary(statistics):
"""Print summary of comparison results to console"""
print("\nComparison Summary:")
print(f"Total tasks: {statistics['total_tasks']}")
print(f"Matching tasks: {statistics['matching_tasks']}")
print(f"Missing tasks: {statistics['missing_tasks']}")
print(f"Total tests: {statistics['total_tests']}")
print(f"Matching tests: {statistics['matching_tests']}")
print(f"Mismatched tests: {statistics['mismatched_tests']}")
print(f"Overall match percentage: {statistics['match_percentage']:.2f}%")
def main():
parser = argparse.ArgumentParser(description='Compare test results against ground truth')
parser.add_argument('ground_truth', help='Path to ground truth results.json')
parser.add_argument('new_results', help='Path to new results.json to compare')
parser.add_argument('--output', help='File to write comparison results to (default: comparison.json)')
args = parser.parse_args()
# Validate arguments
if not os.path.exists(args.ground_truth):
print(f"Error: Ground truth file {args.ground_truth} not found")
sys.exit(1)
if not os.path.exists(args.new_results):
print(f"Error: New results file {args.new_results} not found")
sys.exit(1)
output_file = args.output or "comparison.json"
# Compare results
print(f"Comparing results...")
print(f"Ground truth: {args.ground_truth}")
print(f"New results: {args.new_results}")
statistics = compare_results(args.ground_truth, args.new_results, output_file)
print(f"\nComparison completed. Results written to {output_file}")
print_comparison_summary(statistics)
if __name__ == "__main__":
main()