File size: 14,173 Bytes
879cc1c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
import json
import os
import argparse
from pathlib import Path

_ACTUAL_DATA_CACHE = {}
_TOTAL_STEPS_CACHE = {}

def _to_int(value):
    try:
        return int(value)
    except Exception:
        return None

def _is_raw_test_data_format(directory_path):
    """Check if directory is in raw_test_data_ca format"""
    dir_path = Path(directory_path)
    if not dir_path.exists():
        return False
    has_runs_dirs = any('-runs-' in d.name for d in dir_path.iterdir() if d.is_dir())
    return has_runs_dirs

def _iter_runs_dirs(directory_path):
    dir_path = Path(directory_path)
    for d in dir_path.iterdir():
        if d.is_dir() and '-runs-' in d.name:
            yield d

def read_predictions(eval_file):
    if not os.path.exists(eval_file):
        print(f"Error: Evaluation file not found at {eval_file}")
        return {}

    try:
        with open(eval_file, 'r', encoding='utf-8') as file:
            lines = file.readlines()
    except Exception as e:
        print(f"Error reading evaluation file {eval_file}: {e}")
        return {}

    predictions = {}
    parsed_count = 0

    for line in lines:
        line = line.strip()
        if not line:
            continue
        try:
            record = json.loads(line)
        except Exception as e:
            print(f"Warning: Could not parse JSON line in {eval_file}: {e}")
            continue

        task_id = record.get("task_id") or record.get("task_name")
        predicted_agent = record.get("predicted_agent", "")
        predicted_step = record.get("predicted_step", "")

        if task_id is None:
            print(f"Warning: Missing task_id in {eval_file} line: {line[:80]}...")
            continue

        predictions[task_id] = {
            "predicted_agent": str(predicted_agent),
            "predicted_step": str(predicted_step)
        }
        parsed_count += 1

    print(f"--- Predictions Read from {eval_file} ---")
    print(f"Successfully parsed predictions for {parsed_count} files.")
    print("=======================================")
    return predictions

def read_actual_data(data_path, task_identifier):
    """
    Read actual annotation data from data path
    Compatible with two formats:
    1. Original format: data_path/task_identifier.json
    2. raw_test_data_ca format: data_path/captain-runs-*/task_identifier/trace_metadata.json

    Returns: (mistake_agent, mistake_step, system_name, category)
    category is 'captain', 'magentic', 'swe' or 'other'
    """
    cache_key = (data_path, task_identifier)
    if cache_key in _ACTUAL_DATA_CACHE:
        return _ACTUAL_DATA_CACHE[cache_key]

    # Try original format
    labeled_json = os.path.join(data_path, f"{task_identifier}")
    if os.path.exists(labeled_json):
        try:
            with open(labeled_json, 'r', encoding='utf-8') as file:
                data = json.load(file)
            mistake_agent = data.get('mistake_agent')
            mistake_step = data.get('mistake_step')
            if mistake_agent is not None and mistake_step is not None:
                result = (str(mistake_agent), str(mistake_step), None, 'other')
                _ACTUAL_DATA_CACHE[cache_key] = result
                return result
        except Exception as e:
            print(f"Error reading {labeled_json}: {e}")

    # Try raw_test_data_ca format
    if _is_raw_test_data_format(data_path):
        dir_path = Path(data_path)
        for runs_dir in _iter_runs_dirs(dir_path):
            task_dir = runs_dir / task_identifier
            trace_metadata_file = task_dir / "trace_metadata.json"
            if trace_metadata_file.exists():
                try:
                    with open(trace_metadata_file, 'r', encoding='utf-8') as file:
                        data = json.load(file)
                    mistake_agent = data.get('mistake_agent')
                    mistake_step = data.get('mistake_step')
                    system_name = data.get('system_name')

                    # Determine category based on directory name
                    runs_dir_name = runs_dir.name.lower()
                    if runs_dir_name.startswith('captain'):
                        category = 'captain'
                    elif runs_dir_name.startswith('magentic'):
                        category = 'magentic'
                    elif runs_dir_name.startswith('swe'):
                        category = 'swe'
                    else:
                        category = 'other'

                    if mistake_agent is not None and mistake_step is not None:
                        result = (str(mistake_agent), str(mistake_step), system_name, category)
                        _ACTUAL_DATA_CACHE[cache_key] = result
                        return result
                except Exception as e:
                    print(f"Error reading {trace_metadata_file}: {e}")

    print(f"Warning: Could not find actual data for {task_identifier}")
    result = (None, None, None, None)
    _ACTUAL_DATA_CACHE[cache_key] = result
    return result

def read_total_steps(data_path, task_identifier):
    cache_key = (data_path, task_identifier)
    if cache_key in _TOTAL_STEPS_CACHE:
        return _TOTAL_STEPS_CACHE[cache_key]

    if _is_raw_test_data_format(data_path):
        dir_path = Path(data_path)
        for runs_dir in _iter_runs_dirs(dir_path):
            task_dir = runs_dir / task_identifier
            step_records_file = task_dir / "step_records.json"
            if step_records_file.exists():
                try:
                    with open(step_records_file, 'r', encoding='utf-8') as file:
                        data = json.load(file)
                    total_steps = len(data) if isinstance(data, list) else None
                    _TOTAL_STEPS_CACHE[cache_key] = total_steps
                    return total_steps
                except Exception as e:
                    print(f"Error reading {step_records_file}: {e}")

    _TOTAL_STEPS_CACHE[cache_key] = None
    return None

def evaluate_accuracy(predictions, data_path, total_files, system_total_files=None):
    correct_agent = 0
    correct_step = 0
    files_evaluated = 0
    per_system = {}

    if total_files == 0:
        print("Error: No JSON files found in the data path to evaluate against.")
        return 0.0, 0.0, {}

    print(f"\n--- Starting Evaluation ---")
    print(f"Total reference JSON files found in {data_path}: {total_files}")
    print(f"Predictions available for {len(predictions)} files.")
    print("=======================================")

    for idx, pred in predictions.items():
        # Extract task identifier from prediction key (remove .json suffix if exists)
        task_identifier = idx.replace('.json', '')

        # Use updated read_actual_data function
        actual_agent, actual_step, system_name, category = read_actual_data(data_path, task_identifier)

        if actual_agent is not None and actual_step is not None:
            files_evaluated += 1
            agent_correct = actual_agent in pred['predicted_agent']
            step_correct = actual_step in pred['predicted_step']

            if agent_correct:
                correct_agent += 1
            if step_correct:
                correct_step += 1

            # Statistics by system_name
            if system_name is None:
                system_name = "unknown"
            if system_name not in per_system:
                per_system[system_name] = {
                    "correct_agent": 0,
                    "correct_step": 0,
                    "files_evaluated": 0,
                }
            per_system[system_name]["files_evaluated"] += 1
            if agent_correct:
                per_system[system_name]["correct_agent"] += 1
            if step_correct:
                per_system[system_name]["correct_step"] += 1

        else:
            print(f"Skipping evaluation for {idx} due to issues reading actual data.")

    print("\n--- Evaluation Summary ---")
    print(f"Total reference files in data_path: {total_files}")
    print(f"Predictions parsed from eval file:  {len(predictions)}")
    print(f"Files evaluated (prediction found & actual data read): {files_evaluated}")
    print(f"Correct Agent Predictions: {correct_agent}")
    print(f"Correct Step Predictions:  {correct_step}")

    if per_system:
        print("\n--- Per-System Summary ---")
        for system_name, stats in sorted(per_system.items()):
            # Use actual evaluated file count as denominator
            denom = stats["files_evaluated"]
            agent_acc = (stats["correct_agent"] / denom) * 100 if denom > 0 else 0.0
            step_acc = (stats["correct_step"] / denom) * 100 if denom > 0 else 0.0
            if system_total_files and system_name in system_total_files:
                total = system_total_files[system_name]
                print(
                    f"{system_name}: Agent {agent_acc:.2f}%, Step {step_acc:.2f}% "
                    f"(evaluated {denom}/{total})"
                )
            else:
                print(
                    f"{system_name}: Agent {agent_acc:.2f}%, Step {step_acc:.2f}% "
                    f"(evaluated {denom})"
                )

    # Use actual evaluated file count as denominator, not total file count
    agent_accuracy = (correct_agent / files_evaluated) * 100 if files_evaluated > 0 else 0.0
    step_accuracy = (correct_step / files_evaluated) * 100 if files_evaluated > 0 else 0.0

    return (
        agent_accuracy,
        step_accuracy,
    )

def main():
    parser = argparse.ArgumentParser(description="Evaluate agent and step prediction accuracy from a JSONL prediction file.")
    parser.add_argument(
        "--data_path",
        type=str,
        default='../Who&When/Algorithm-Generated',
        help="Path to the directory containing the ground truth files."
    )
    parser.add_argument(
        "--eval_file",
        type=str,
        required=True,
        help="Path to the evaluation log file containing the predictions."
    )
    parser.add_argument(
        "--output_file",
        type=str,
        default="",
        help="Optional path to write evaluation summary."
    )
    args = parser.parse_args()
    
    data_path = args.data_path
    eval_file = args.eval_file

    if not os.path.isdir(data_path):
        print(f"Error: Data directory not found at {data_path}")
        actual_total_files = 0
        system_total_files = None
    else:
        try:
            # Check data format and count files
            if _is_raw_test_data_format(data_path):
                # raw_test_data_ca format: count task directories
                dir_path = Path(data_path)
                actual_total_files = 0
                system_total_files = {}
                for runs_dir in _iter_runs_dirs(dir_path):
                    if runs_dir.is_dir():
                        task_dirs = [d for d in runs_dir.iterdir() if d.is_dir() and (d / "trace_metadata.json").exists()]
                        for task_dir in task_dirs:
                            actual_total_files += 1
                            trace_metadata_file = task_dir / "trace_metadata.json"
                            try:
                                with open(trace_metadata_file, 'r', encoding='utf-8') as file:
                                    data = json.load(file)
                                system_name = data.get("system_name") or "unknown"
                            except Exception:
                                system_name = "unknown"
                            system_total_files[system_name] = system_total_files.get(system_name, 0) + 1
                print(f"Detected raw_test_data_ca format: {actual_total_files} tasks found")
            else:
                # Original format: count JSON files
                json_files_in_data_path = [
                    f for f in os.listdir(data_path)
                    if f.endswith('.json') and os.path.isfile(os.path.join(data_path, f))
                ]
                actual_total_files = len(json_files_in_data_path)
                system_total_files = None
                print(f"Detected original JSON format: {actual_total_files} files found")
        except Exception as e:
            print(f"Error reading data directory {data_path}: {e}")
            actual_total_files = 0
            system_total_files = None

    predictions = read_predictions(eval_file)

    # Calculate actual number of evaluated files
    files_evaluated = 0
    for idx, pred in predictions.items():
        task_identifier = idx.replace('.json', '')
        actual_agent, actual_step, system_name, category = read_actual_data(data_path, task_identifier)
        if actual_agent is not None and actual_step is not None:
            files_evaluated += 1

    (
        agent_accuracy,
        step_accuracy,
    ) = evaluate_accuracy(
        predictions,
        data_path,
        actual_total_files,
        system_total_files=system_total_files,
    )

    print("\n--- Final Accuracy Results ---")
    print(f"Evaluation File: {eval_file}")
    print(f"Data Path:       {data_path}")
    print(f"Agent Accuracy: {agent_accuracy:.2f}%")
    print(f"Step Accuracy:  {step_accuracy:.2f}%")
    print(f"(Accuracy calculated based on {files_evaluated} evaluated files out of {actual_total_files} total files in data path)")

    if args.output_file:
        try:
            with open(args.output_file, 'w', encoding='utf-8') as out_f:
                out_f.write("--- Final Accuracy Results ---\n")
                out_f.write(f"Evaluation File: {eval_file}\n")
                out_f.write(f"Data Path:       {data_path}\n")
                out_f.write(f"Agent Accuracy: {agent_accuracy:.2f}%\n")
                out_f.write(f"Step Accuracy:  {step_accuracy:.2f}%\n")
                out_f.write(f"(Accuracy calculated based on {files_evaluated} evaluated files out of {actual_total_files} total files in data path)\n")

            print(f"Saved evaluation summary to {args.output_file}")
        except Exception as e:
            print(f"Error writing output_file {args.output_file}: {e}")

if __name__ == "__main__":
    main()