File size: 1,953 Bytes
203a7fb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Prune broken rows (empty raw_output / judge_fail) from eval_results.jsonl
files in-place so the next eval run will re-process them.

A backup .bak is written next to each modified file.

Usage:
  python3 /home/ubuntu/prune_broken_rows.py <jsonl_or_dir> [more...]
  python3 /home/ubuntu/prune_broken_rows.py /home/ubuntu/eval_results/sync/sync_xiaomi_mimo-v2.5_audioMuxed
  python3 /home/ubuntu/prune_broken_rows.py /home/ubuntu/eval_results/{sync,swap,mute,swap_original,mute_original}/*xiaomi*
"""
import json
import shutil
import sys
from pathlib import Path


def is_broken(row: dict) -> str:
    raw = (row.get("raw_output") or "").strip()
    if not raw:
        return "empty_raw"
    if row.get("parse_method") == "judge_fail":
        return "judge_fail"
    return ""


def prune(jsonl: Path) -> None:
    if jsonl.is_dir():
        jsonl = jsonl / "eval_results.jsonl"
    if not jsonl.exists():
        print(f"[skip] {jsonl} does not exist")
        return
    rows = [json.loads(l) for l in open(jsonl) if l.strip()]
    keep, drop = [], []
    for r in rows:
        why = is_broken(r)
        (drop if why else keep).append((r, why))
    if not drop:
        print(f"[ok]   {jsonl.parent.name}: {len(rows)} rows, none broken")
        return

    bak = jsonl.with_suffix(".jsonl.bak")
    if not bak.exists():
        shutil.copy2(jsonl, bak)
    with open(jsonl, "w") as f:
        for r, _ in keep:
            f.write(json.dumps(r, ensure_ascii=False) + "\n")

    by_reason = {}
    for r, why in drop:
        by_reason[why] = by_reason.get(why, 0) + 1
    reasons = ", ".join(f"{k}={v}" for k, v in by_reason.items())
    print(f"[pruned] {jsonl.parent.name}: kept {len(keep)} / dropped {len(drop)} ({reasons})  -> backup: {bak.name}")


def main():
    args = sys.argv[1:]
    if not args:
        print(__doc__)
        sys.exit(1)
    for a in args:
        prune(Path(a).expanduser())


if __name__ == "__main__":
    main()