File size: 2,705 Bytes
24aaa76 | 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 | """Score the positive control with the screen's scorelib (same post-processing, same CER) and write positive_control.tsv.
usage: control_report.py CONTROL_LIST SCORELIB_PY OUT_TSV label=OUT_ROOT [label=OUT_ROOT ...]
label is cpu or gpu; OUT_ROOT holds <model>/s1/preds.jsonl.
"""
import importlib.util
import json
import os
import sys
control_list, scorelib_py, out_tsv = sys.argv[1:4]
roots = [a.split("=", 1) for a in sys.argv[4:]]
spec = importlib.util.spec_from_file_location("scorelib", scorelib_py)
sl = importlib.util.module_from_spec(spec)
spec.loader.exec_module(sl)
rows = []
with open(control_list, encoding="utf-8", newline="") as f:
f.readline()
for line in f:
c, i, lab = line.rstrip("\n").split("\t")
rows.append((c, lab))
label = dict(rows)
order = [c for c, _ in rows]
sample_idx = {40: [order[i] for i in (0, 1, 100, 101, 102)], 80: [order[i] for i in (200, 201, 300, 301, 302)]} # 2 kk + 3 ky... see below
# five fixed crops per height: kk 1, kk 2, ky 1, ky 2, ky 3 (list order: h40 kk, h40 ky, h80 kk, h80 ky)
out = ["model\tdevice\theight_px\tcrops_scored\tcer_pooled\texact_match\tcer_pooled_kk\tcer_pooled_ky\thr4_loop_share\tempty_share\tfive_raw_outputs (reference => raw output)"]
for dev, root in roots:
for model in sorted(os.listdir(root)):
pp = os.path.join(root, model, "s1", "preds.jsonl")
if not os.path.exists(pp):
continue
preds = {}
for line in open(pp, encoding="utf-8"):
try:
r = json.loads(line)
except Exception: # noqa: BLE001
continue
preds[r["crop_file"]] = r
for H in (40, 80):
sc, by_lang = [], {"kk": [], "ky": []}
for c in order:
if f"_h{H}_" in c and c in preds:
s = sl.score_crop(label[c], preds[c]["raw"])
sc.append(s)
by_lang[c[:2]].append(s)
if not sc:
continue
a = sl.aggregate(sc)
ak = sl.aggregate(by_lang["kk"]) if by_lang["kk"] else {}
ay = sl.aggregate(by_lang["ky"]) if by_lang["ky"] else {}
five = " | ".join(f"{label[c]} => {preds[c]['raw']!r}" for c in sample_idx[H] if c in preds)
out.append("\t".join([model, dev, str(H), str(len(sc)), f"{a['cer_pooled']:.4f}", f"{a['exact']:.4f}",
f"{ak.get('cer_pooled', float('nan')):.4f}", f"{ay.get('cer_pooled', float('nan')):.4f}",
f"{a['hr4_loop_share']:.4f}", f"{a['empty_share']:.4f}", five]))
open(out_tsv, "w", encoding="utf-8").write("\n".join(out) + "\n")
print("rows", len(out) - 1)
|