Files
cuotibiye/bench/report.py
T

142 lines
6.2 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# -*- coding: utf-8 -*-
"""把 results.jsonl 汇总成一份可读的评测报告(markdown)。
口径(重要,别把这几件事混为一谈):
切题率 = 至少切出 1 块的图片占比
题干可用率 = 块里有 ≥6 汉字、或算式带汉字 的占比(标题/页眉/纯图形不算)
可判率 = calc + choice 的块占比(其余是 manual,机器不判)
自算率 = calc 块里,答案由我们自己算出来(而不是抄卷面)的占比
一致率 = 有卷面答案且我们自算时,两边相同的占比(**不是准确率**,是交叉验证)
用法:python bench/report.py
"""
from __future__ import annotations
import json
import re
import statistics
from collections import Counter, defaultdict
from pathlib import Path
ROOT = Path(__file__).resolve().parent.parent
BENCH = ROOT / "data" / "bench"
RESULTS = BENCH / "results.jsonl"
META = BENCH / "meta.jsonl"
OUT = BENCH / "report.md"
GRADE = re.compile(r"([一二三四五六123456])年级")
CN_NUM = {"1": "一", "2": "二", "3": "三", "4": "四", "5": "五", "6": "六"}
HEADER = re.compile(r"班级|姓名|成绩|总分|题号|得分|装订|密封|第\s*\d+\s*页")
def load() -> tuple[list[dict], dict[str, dict]]:
rows = [json.loads(l) for l in RESULTS.open(encoding="utf-8") if l.strip()]
meta = {}
if META.exists():
for line in META.open(encoding="utf-8"):
if line.strip():
m = json.loads(line)
meta[m["id"]] = m
return rows, meta
def pct(a: int, b: int) -> str:
return f"{a}/{b}({a / b * 100:.1f}%)" if b else "0/0"
def main() -> int:
rows, meta = load()
if not rows:
print("没有结果,先跑 python bench/run.py")
return 1
imgs = len(rows)
ok_imgs = sum(1 for r in rows if r["ok"] and r["n_blocks"])
blocks = [b for r in rows for b in r["blocks"]]
nb = len(blocks)
if not nb:
print("一条题块都没有,看看是不是 API 报错")
return 1
header = sum(1 for b in blocks if HEADER.search(b["stem"] or ""))
stmt = sum(1 for b in blocks if b["stmt_ok"])
judgeable = sum(1 for b in blocks if b["judgeable"])
calc = [b for b in blocks if b["type"] == "calc"]
choice = [b for b in blocks if b["type"] == "choice"]
derived = sum(1 for b in calc if b["derived"])
agree_pairs = [b for b in calc if b["derived"] and b["page_answer"]]
agree = sum(1 for b in agree_pairs if b["agree"])
multi = sum(1 for b in calc if (b["stem"] or "").count("=") >= 3)
boxy = sum(1 for b in blocks if re.search(r"\bBox\b|□", b["stem"] or ""))
per_ms = [r["ms"] for r in rows if r["ms"]]
p50 = statistics.median(per_ms) if per_ms else 0
p90 = sorted(per_ms)[int(len(per_ms) * 0.9) - 1] if per_ms else 0
# 按年级/科目分组(关键词里带「X年级」「数学/语文」)
group = defaultdict(lambda: {"img": 0, "blocks": 0, "stmt": 0, "judge": 0})
for r in rows:
q = (meta.get(r["id"]) or {}).get("q", "")
g = GRADE.search(q)
gv = CN_NUM.get(g.group(1), g.group(1)) if g else "?"
key = f"{gv} | {'语文' if '语文' in q else ('数学' if '数学' in q else '?')}"
group[key]["img"] += 1
group[key]["blocks"] += r["n_blocks"]
group[key]["stmt"] += sum(1 for b in r["blocks"] if b["stmt_ok"])
group[key]["judge"] += sum(1 for b in r["blocks"] if b["judgeable"])
type_cnt = Counter(b["type"] or "?" for b in blocks)
lines = []
A = lines.append
A("# 拍题链路评测报告\n")
A(f"- 样本:**{imgs} 张**真实小学试卷图(来源见 `data/bench/meta.jsonl`,只用于内部评测)")
A(f"- 切题:{pct(ok_imgs, imgs)};共 **{nb} 个题块**,平均 {nb / imgs:.1f} 块/张")
A(f"- 单张耗时:中位 {p50 / 1000:.1f}s,P90 {p90 / 1000:.1f}s(含一次腾讯切题调用)\n")
A("## 一、四个硬指标\n")
A("| 指标 | 数值 | 说明 |")
A("|---|---|---|")
A(f"| 切题率 | {pct(ok_imgs, imgs)} | 至少切出 1 块 |")
A(f"| 题干可用率 | {pct(stmt, nb)} | 有≥6 汉字或「算式+汉字」,排除页眉 |")
A(f"| 可判率 | {pct(judgeable, nb)} | calc+choice,其余要孩子自评 |")
A(f"| 自算率 | {pct(derived, len(calc))} | calc 块里答案由我们自己算出的 |")
A(f"| 卷面一致率 | {pct(agree, len(agree_pairs))} | 有卷面答案的 calc 块,两边相同(交叉验证,≠准确率) |")
A("")
A(f"- 题型分布:{dict(type_cnt)}")
A(f"- 页眉/标题被当成题:{header}/{nb}({header / nb * 100:.1f}%)")
A(f"- 有 `Box`/`□` 占位残留:{boxy}/{nb}({boxy / nb * 100:.1f}%)")
A(f"- calc 块里含 ≥3 个「=」的(口算卡整块,目前不能自动算):{multi}/{len(calc)}")
A("")
A("## 二、按年级 × 科目\n")
A("| 年级 \\| 科目 | 图片 | 题块 | 题干可用 | 可判 |")
A("|---|---|---|---|---|")
for k in sorted(group):
g = group[k]
A(f"| {k} | {g['img']} | {g['blocks']} | {pct(g['stmt'], g['blocks'])} | {pct(g['judge'], g['blocks'])} |")
A("")
A("## 三、最典型的 8 个坏块(人工看一眼就知道问题在哪)\n")
bad = [b for b in blocks if not b["stmt_ok"]][:8]
for b in bad:
A(f"- `{b['type'] or '?'}` {b['stem'][:80]!r}")
A("")
A("## 四、结论与下一步\n")
A("1. 切题不是瓶颈,**题干干净度**才是:页眉被当题、图形题只剩符号、`Box` 残留都要在切题后处理。")
A("2. 小学低年级卷子以填空/看图为主,`manual` 占大头是正常的——产品上要靠「孩子自评」,不能承诺机器判。")
A("3. 口算卡已经能逐题算:按 `=` 拆成小题、三步连算整串求值,自算率从 0% 到了 95.9%。")
A("4. 剩下的错集中在「极短算式被读错」(`0+0` 读成 `6+0`)和图形题符号化,不是判题逻辑的问题。")
A("5. 卷面一致率只能做交叉验证;真实误判率看第五节的人工金标(当前 0%,样本只有 83 道,还要扩)。")
A("")
OUT.write_text("\n".join(lines), encoding="utf-8")
print(f"已写 {OUT}")
print("\n".join(lines[:24]))
return 0
if __name__ == "__main__":
raise SystemExit(main())