| |
| """Score one predictions.jsonl against multiple difficulty subsets. |
| |
| The bench script can be run once on the merged 10K test.jsonl (DIFFS=all), |
| saving 2× model-load time vs running per-difficulty. This script then |
| re-uses that single predictions.jsonl and scores it against {easy, medium, |
| hard} qa_roots separately by inner-joining on pair_id, so you still get |
| per-difficulty metrics with a single bench run. |
| |
| Usage: |
| python scripts/score_by_difficulty.py \\ |
| --predictions-jsonl <path>/predictions.jsonl \\ |
| --qa-parent /apdcephfs_cq10/.../all_qa_llm_by_difficulty_v2_filtered_balanced_v4_prompted \\ |
| --output-dir <path>/scores_by_diff/ |
| |
| Output: |
| <output-dir>/easy_score.json |
| <output-dir>/medium_score.json |
| <output-dir>/hard_score.json |
| <output-dir>/all_score.json (full 10K combined) |
| """ |
| from __future__ import annotations |
|
|
| import argparse |
| import json |
| import os |
| import subprocess |
| import sys |
| from pathlib import Path |
|
|
|
|
| def collect_pair_ids(jsonl_path: Path) -> set[str]: |
| ids: set[str] = set() |
| with jsonl_path.open() as fh: |
| for line in fh: |
| line = line.strip() |
| if not line: |
| continue |
| r = json.loads(line) |
| pid = r.get("pair_id") |
| if pid is not None: |
| ids.add(str(pid)) |
| return ids |
|
|
|
|
| def filter_predictions(predictions_jsonl: Path, keep_ids: set[str], out_path: Path) -> int: |
| n = 0 |
| out_path.parent.mkdir(parents=True, exist_ok=True) |
| with predictions_jsonl.open() as fin, out_path.open("w") as fout: |
| for line in fin: |
| line = line.strip() |
| if not line: |
| continue |
| r = json.loads(line) |
| pid = r.get("pair_id") |
| if pid is None or str(pid) not in keep_ids: |
| continue |
| fout.write(line + "\n") |
| n += 1 |
| return n |
|
|
|
|
| def main() -> int: |
| ap = argparse.ArgumentParser() |
| ap.add_argument("--predictions-jsonl", required=True) |
| ap.add_argument("--qa-parent", required=True, |
| help="Parent dir containing easy/, medium/, hard/, and merged test.jsonl") |
| ap.add_argument("--output-dir", required=True) |
| ap.add_argument("--diffs", nargs="+", default=["easy", "medium", "hard", "all"]) |
| ap.add_argument("--score-script", default=None, |
| help="Path to score_test_predictions.py (default: alongside this script)") |
| args = ap.parse_args() |
|
|
| pred_path = Path(args.predictions_jsonl) |
| if not pred_path.exists(): |
| print(f"missing {pred_path}", file=sys.stderr) |
| return 1 |
| out_dir = Path(args.output_dir) |
| out_dir.mkdir(parents=True, exist_ok=True) |
|
|
| score_script = Path(args.score_script) if args.score_script \ |
| else Path(__file__).parent / "score_test_predictions.py" |
| if not score_script.exists(): |
| print(f"missing scorer: {score_script}", file=sys.stderr) |
| return 1 |
|
|
| qa_parent = Path(args.qa_parent) |
| for diff in args.diffs: |
| qa_dir = qa_parent if diff == "all" else qa_parent / diff |
| qa_jsonl = qa_dir / "test.jsonl" |
| if not qa_jsonl.exists(): |
| print(f"[skip] {diff}: missing {qa_jsonl}") |
| continue |
|
|
| |
| |
| |
| if diff == "all": |
| sub_pred = pred_path |
| n_sub = sum(1 for _ in pred_path.open() if _.strip()) |
| else: |
| ids = collect_pair_ids(qa_jsonl) |
| sub_pred = out_dir / f"{diff}_predictions.jsonl" |
| n_sub = filter_predictions(pred_path, ids, sub_pred) |
| print(f"[{diff}] {n_sub} predictions filtered into {sub_pred.name}") |
|
|
| score_json = out_dir / f"{diff}_score.json" |
| scored_jsonl = out_dir / f"{diff}_scored.jsonl" |
| cmd = [ |
| sys.executable, str(score_script), |
| "--predictions-jsonl", str(sub_pred), |
| "--qa-root", str(qa_dir), |
| "--split", "test", |
| "--output-json", str(score_json), |
| "--per-record-jsonl", str(scored_jsonl), |
| ] |
| print(f"[{diff}] scoring n={n_sub} -> {score_json.name}") |
| subprocess.run(cmd, check=True) |
|
|
| |
| print() |
| print(f"{'DIFF':<8} {'n':>6} {'overall':>8} {'detect_src':>12} {'detect_tm':>10} " |
| f"{'azimuth':>8} {'elev':>8} {'identify':>10} {'count':>8}") |
| print("-" * 80) |
| for diff in args.diffs: |
| score_json = out_dir / f"{diff}_score.json" |
| if not score_json.exists(): |
| continue |
| d = json.loads(score_json.read_text()) |
| ov = d.get("overall", {}) |
| per = d.get("per_task", {}) |
| def get(t): |
| return per.get(t, {}).get("correct_all") |
| cells = [ |
| f"{diff:<8}", |
| f"{int(ov.get('n_total', 0)):>6d}", |
| f"{ov.get('correct_all', 0):>7.3f} ", |
| f"{(get('detect_source') or 0):>11.3f} ", |
| f"{(get('detect_time') or 0):>9.3f} ", |
| f"{(get('estimate_azimuth') or 0):>7.3f} ", |
| f"{(get('estimate_elevation') or 0):>7.3f} ", |
| f"{(get('identify_source_by_location') or get('identify_source_by_doa') or 0):>9.3f} ", |
| f"{(get('count_sources') or 0):>7.3f} ", |
| ] |
| print("".join(cells)) |
| return 0 |
|
|
|
|
| if __name__ == "__main__": |
| sys.exit(main()) |
|
|