File size: 3,514 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
"""Summarises the RUKOPYS crop-to-label review (judgements.json + review.json) with Wilson 95% intervals and writes it into
checks.json under 'rukopys_alignment'.
usage: tp2_rk_align_summary.py TESTPASS2_DIR
"""
import collections
import hashlib
import json
import math
import os
import sys

TP = sys.argv[1]
J = json.load(open(os.path.join(TP, "rk_align", "judgements.json"), encoding="utf-8"))
R = {r["k"]: r for r in json.load(open(os.path.join(TP, "rk_align", "rk_align", "review.json"), encoding="utf-8"))}


def wilson(k, n, z=1.959964):
    p = k / n
    c = (p + z * z / (2 * n)) / (1 + z * z / n)
    h = z * math.sqrt(p * (1 - p) / n + z * z / (4 * n * n)) / (1 + z * z / n)
    return [round(c - h, 4), round(c + h, 4)]


rows = [dict(r, **{kk: R[r["k"]][kk] for kk in ("kind", "id", "m1_cer", "crop_w", "crop_h")}) for r in J["rows"]]
rand = [r for r in rows if r["kind"] == "random"]
worst = [r for r in rows if r["kind"] == "worst_m1_cer"]
n = len(rand)
k_any = sum(1 for r in rand if r["cat"] != "A")
k_strict = sum(1 for r in rand if r["cat"] in ("C", "D"))
out = {
    "method": ("20 rows drawn with numpy default_rng(20261125) over the 6,172 manifest rows plus the 10 rows with the highest M1 CER among the "
               "rest (tp2_rk_align_prep.py, job 55355); each crop viewed beside its reference, the page view with all boxes viewed where the "
               "crop and label disagreed; no prediction, no tuning"),
    "categories": J["categories"], "reviewer": J["reviewer"],
    "random_sample": {"n": n, "counts": dict(collections.Counter(r["cat"] for r in rand)),
                      "mismatch_rate_any_B_C_D": {"k": k_any, "rate": k_any / n, "wilson95": wilson(k_any, n)},
                      "mismatch_rate_strict_C_D": {"k": k_strict, "rate": k_strict / n, "wilson95": wilson(k_strict, n),
                                                   "definition": "labelled text not fully in the crop, or the crop shows other text"}},
    "worst_10_by_m1_cer": {"counts": dict(collections.Counter(r["cat"] for r in worst)),
                           "problem": dict(collections.Counter(r.get("problem", "") for r in worst)),
                           "reading": ("label or crop problems (B, C, D) in %d of 10; reading errors with a correct crop (A) in %d of 10"
                                       % (sum(1 for r in worst if r["cat"] != "A"), sum(1 for r in worst if r["cat"] == "A")))},
    "rows": [{k: r[k] for k in ("k", "kind", "id", "cat", "note", "m1_cer", "crop_w", "crop_h")} for r in rows],
    "examples_max3": J["examples_max3"],
    "consequence": ("RUKOPYS CER mixes reading errors with annotation-geometry noise: boxes that hold an extra line inflate insertions for "
                    "models that read everything in the crop (M1) and labels with words outside the box inflate deletions; one page had "
                    "boxes offset from its lines. Treat RUKOPYS figures as upper bounds of the reading error; the comparison of models "
                    "is less affected because all models read the same crops."),
}
p = os.path.join(TP, "checks.json")
C = json.load(open(p, encoding="utf-8"))
C["rukopys_alignment"] = out
C.setdefault("generated_from", {})["rk_align/judgements.json"] = hashlib.sha256(open(os.path.join(TP, "rk_align", "judgements.json"), "rb").read()).hexdigest()
json.dump(C, open(p, "w", encoding="utf-8"), indent=1, ensure_ascii=False)
print(json.dumps({k: out[k] for k in ("random_sample", "worst_10_by_m1_cer")}, ensure_ascii=False))