Ishan3141 commited on
Commit
64ae81d
·
verified ·
1 Parent(s): 15bb86a

Delete compile_results.py

Browse files
Files changed (1) hide show
  1. compile_results.py +0 -154
compile_results.py DELETED
@@ -1,154 +0,0 @@
1
- #!/usr/bin/env python3
2
- """Merge row-level metrics with judge annotations into compiled benchmark files."""
3
-
4
- from __future__ import annotations
5
-
6
- import argparse
7
- from pathlib import Path
8
- from typing import Dict, Iterable, Iterator, Tuple
9
-
10
- from apm_metrics import flatten_judge_fields, load_json_or_jsonl, write_json
11
-
12
-
13
- TEXT_FIELDS = (
14
- "clean_text",
15
- "noisy_prompt",
16
- "model_response",
17
- "response",
18
- "assisted_prompt",
19
- "mediated_prompt",
20
- )
21
-
22
-
23
- def parse_args() -> argparse.Namespace:
24
- parser = argparse.ArgumentParser(description=__doc__)
25
- parser.add_argument(
26
- "--metrics-root",
27
- type=Path,
28
- required=True,
29
- help="Root containing <model>/<noise>/metrics.json files.",
30
- )
31
- parser.add_argument(
32
- "--judged-root",
33
- type=Path,
34
- required=True,
35
- help="Root containing <model>/<noise>/results.jsonl files with judge annotations.",
36
- )
37
- parser.add_argument(
38
- "--output-root",
39
- type=Path,
40
- default=Path("compiled"),
41
- help="Directory where compiled JSON files will be written.",
42
- )
43
- parser.add_argument(
44
- "--include-text-fields",
45
- action="store_true",
46
- help="Include source prompts and model responses when present.",
47
- )
48
- return parser.parse_args()
49
-
50
-
51
- def judged_file(noise_dir: Path) -> Path | None:
52
- for preferred in ("results.jsonl", "results.json"):
53
- path = noise_dir / preferred
54
- if path.exists():
55
- return path
56
-
57
- candidates = sorted(
58
- path for path in noise_dir.iterdir() if path.is_file() and path.suffix in {".json", ".jsonl"}
59
- )
60
- return candidates[0] if candidates else None
61
-
62
-
63
- def discover_judged(judged_root: Path) -> Iterator[Tuple[str, str, Path]]:
64
- for model_dir in sorted(path for path in judged_root.iterdir() if path.is_dir()):
65
- for noise_dir in sorted(path for path in model_dir.iterdir() if path.is_dir()):
66
- path = judged_file(noise_dir)
67
- if path is not None:
68
- yield model_dir.name, noise_dir.name, path
69
-
70
-
71
- def compile_file(
72
- *,
73
- model: str,
74
- noise: str,
75
- metrics_path: Path,
76
- judged_path: Path,
77
- output_path: Path,
78
- include_text_fields: bool,
79
- ) -> Tuple[int, int]:
80
- metrics = load_json_or_jsonl(metrics_path)
81
- judged = load_json_or_jsonl(judged_path)
82
-
83
- metrics_by_id: Dict[str, Dict] = {
84
- row["example_id"]: row for row in metrics if row.get("example_id") is not None
85
- }
86
-
87
- compiled = []
88
- missing = 0
89
-
90
- for judged_row in judged:
91
- ex_id = judged_row.get("example_id")
92
- metric_row = metrics_by_id.get(ex_id)
93
- if metric_row is None:
94
- missing += 1
95
- continue
96
-
97
- row = {
98
- "example_id": ex_id,
99
- "model": metric_row.get("model") or judged_row.get("model") or model,
100
- "noise": metric_row.get("noise") or judged_row.get("noise") or noise,
101
- }
102
-
103
- for key, value in metric_row.items():
104
- if key not in {"example_id", "model", "noise"}:
105
- row[key] = value
106
-
107
- row.update(flatten_judge_fields(judged_row))
108
-
109
- if include_text_fields:
110
- for key in TEXT_FIELDS:
111
- if key in judged_row:
112
- row[key] = judged_row[key]
113
-
114
- compiled.append(row)
115
-
116
- write_json(compiled, output_path)
117
- return len(compiled), missing
118
-
119
-
120
- def main() -> None:
121
- args = parse_args()
122
-
123
- total_rows = 0
124
- total_missing = 0
125
- total_files = 0
126
-
127
- for model, noise, judged_path in discover_judged(args.judged_root):
128
- metrics_path = args.metrics_root / model / noise / "metrics.json"
129
- if not metrics_path.exists():
130
- print(f"Skipping {model}/{noise}: missing {metrics_path}")
131
- continue
132
-
133
- output_path = args.output_root / model / noise / "compiled.json"
134
- rows, missing = compile_file(
135
- model=model,
136
- noise=noise,
137
- metrics_path=metrics_path,
138
- judged_path=judged_path,
139
- output_path=output_path,
140
- include_text_fields=args.include_text_fields,
141
- )
142
- total_rows += rows
143
- total_missing += missing
144
- total_files += 1
145
- print(f"{model}/{noise}: {rows} compiled rows, {missing} missing metrics -> {output_path}")
146
-
147
- print(
148
- f"Compiled {total_rows} rows from {total_files} files; "
149
- f"{total_missing} judged rows had no matching metric row"
150
- )
151
-
152
-
153
- if __name__ == "__main__":
154
- main()