"""Test collect.py on synthetic outputs with a half-written last line and no meta.json. usage: test_collect.py SCRIPTS_DIR WORK_DIR""" import json import os import subprocess import sys scripts, work = sys.argv[1:3] root = os.path.join(work, "out") os.makedirs(os.path.join(root, "toymodel", "s1"), exist_ok=True) os.makedirs(os.path.join(root, "loadtest"), exist_ok=True) with open(os.path.join(root, "screen_set_v1.tsv"), "w", encoding="utf-8") as f: f.write("crop_file\tform_id\tlabel\n0_001_001.jpg\t0\tсалық\n0_001_002.jpg\t0\t1\n0_001_003.jpg\t0\tәке\n") with open(os.path.join(root, "toymodel", "s1", "preds.jsonl"), "w", encoding="utf-8") as f: f.write(json.dumps({"crop_file": "0_001_001.jpg", "raw": "салық", "n_new": 3, "hit_cap": False, "n_img_tokens": 10}) + "\n") f.write(json.dumps({"crop_file": "0_001_002.jpg", "raw": "## 1", "n_new": 2, "hit_cap": False, "n_img_tokens": 9}) + "\n") f.write('{"crop_file": "0_001_003.jpg", "raw": "ә') # killed in the middle of a line with open(os.path.join(root, "toymodel", "s1", "timing.jsonl"), "w", encoding="utf-8") as f: f.write(json.dumps({"n": 2, "t_img": 0.01, "t_pre": 0.1, "t_gen": 0.4, "new_tokens": 5, "job_id": "1", "batch_first_crop": "a"}) + "\n") f.write('{"n": 16, "t_im') json.dump({"revision": "abc", "model_params_unique": 123}, open(os.path.join(root, "loadtest", "toymodel.json"), "w")) res = os.path.join(work, "results_test.tsv") subprocess.run([sys.executable, "-I", os.path.join(scripts, "collect.py"), root, os.path.join(root, "screen_set_v1.tsv"), os.path.join(scripts, "scorelib.py"), res], check=True) rows = [l.rstrip("\n").split("\t") for l in open(res, encoding="utf-8")] hdr, row = rows[0], dict(zip(rows[0], rows[1])) assert row["crops_done"] == "2" and row["crops_in_list"] == "3" and row["complete"].startswith("no"), row assert row["bad_lines"] == "2" and row["version_revision"] == "abc" and row["params_unique"] == "123", row assert row["cer_pooled"] == "0.0000" and row["exact_match"] == "1.0000", row # '## 1' -> '1' after the wrapper step assert row["batch_size"] == "2" and row["job_ids"] == "1", row print("collect.py robustness test passed")