File size: 3,423 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
58
59
60
61
"""Synthetic test of controls2_report.py. usage: test_controls2_report.py SCRIPTS_DIR WORK_DIR"""
import json
import os
import subprocess
import sys

scripts, work = sys.argv[1:3]
c2 = os.path.join(work, "c2")
pr = os.path.join(work, "printed")


def wlist(path, rows):
    with open(path, "w", encoding="utf-8", newline="") as f:
        f.write("crop_file\tform_id\tlabel\n")
        for i, (c, lab) in enumerate(rows):
            f.write(f"{c}\t{i}\t{lab}\n")


def wpreds(root, model, pairs):
    d = os.path.join(root, model, "s1")
    os.makedirs(d, exist_ok=True)
    with open(os.path.join(d, "preds.jsonl"), "w", encoding="utf-8") as f:
        for c, raw in pairs:
            f.write(json.dumps({"crop_file": c, "raw": raw}) + "\n")


os.makedirs(c2, exist_ok=True)
stored = [("0_001_001.jpg", "салық"), ("0_001_002.jpg", "1")]
stretch = [("0_001_001.png", "салық"), ("0_001_002.png", "1")]
fonts = [("caveat_kk_h40_001.png", "ашаршылық"), ("caveat_ky_h40_001.png", "кийим"), ("badscript_kk_h40_001.png", "ашаршылық")]
wlist(os.path.join(c2, "contrast_stored_screen_set_v1.tsv"), stored)
wlist(os.path.join(c2, "contrast_stretch_screen_set_v1.tsv"), stretch)
wlist(os.path.join(c2, "fonts_screen_set_v1.tsv"), fonts)
printed = [("kk_h40_001.png", "ашаршылық"), ("ky_h40_001.png", "кийим"), ("kk_h80_001.png", "ашаршылық")]
wlist(os.path.join(work, "printed_list_screen_set_v1.tsv"), printed)
m = "toy"
wpreds(os.path.join(c2, "out_stored"), m, [("0_001_001.jpg", "## салык"), ("0_001_002.jpg", "1")])
wpreds(os.path.join(c2, "out_stretch"), m, [("0_001_001.png", "салық"), ("0_001_002.png", "I")])
wpreds(os.path.join(c2, "out_fonts"), m, [(c, "ашаршылық" if "kk" in c else "кийим") for c, _ in fonts])
wpreds(pr, m, [("kk_h40_001.png", "ашаршылык"), ("ky_h40_001.png", "кийим"), ("kk_h80_001.png", "x")])
# the real script loops over two fixed model names; run it with those names by copying the toy folders
for root in (os.path.join(c2, "out_stored"), os.path.join(c2, "out_stretch"), os.path.join(c2, "out_fonts"), pr):
    for name in ("paddleocr_vl_1_6", "qwen3_vl_2b"):
        src = os.path.join(root, m, "s1", "preds.jsonl")
        d = os.path.join(root, name, "s1")
        os.makedirs(d, exist_ok=True)
        open(os.path.join(d, "preds.jsonl"), "w", encoding="utf-8").write(open(src, encoding="utf-8").read())
res = os.path.join(work, "controls_2.tsv")
subprocess.run([sys.executable, "-I", os.path.join(scripts, "controls2_report.py"), c2, os.path.join(work, "printed_list_screen_set_v1.tsv"),
                pr, os.path.join(scripts, "scorelib.py"), res], check=True)
rows = [l.rstrip("\n").split("\t") for l in open(res, encoding="utf-8")]
hdr = rows[0]
d = {(r[0][:2], r[1]): dict(zip(hdr, r)) for r in rows[1:]}
assert d[("A1", "qwen3_vl_2b")]["cer_pooled"] == "0.1429" or d[("A1", "qwen3_vl_2b")]["cer_pooled"] == "0.1667", d[("A1", "qwen3_vl_2b")]
assert d[("A2", "qwen3_vl_2b")]["exact_match"] == "0.5000"
assert d[("B1", "qwen3_vl_2b")]["crops_scored"] == "2" and d[("B2", "qwen3_vl_2b")]["crops_scored"] == "1"
assert d[("B1", "qwen3_vl_2b")]["cer_pooled_kk"] == "0.0000" and d[("B1", "qwen3_vl_2b")]["cer_pooled_ky"] == "0.0000"
assert d[("C ", "qwen3_vl_2b")]["crops_scored"] == "2"            # only the h40 rows
assert len(rows) - 1 == 10, len(rows)
print("controls2_report test passed")