"""rc1 check helper (A1): is the image preparation copied into the A1 package the same as our evaluation code? Run with CODE/.venv_kraken/bin/python -I. Imports CODE/src/linedet/data.py (the evaluation code) and the package's infer.py (by file path) in one process, only to compare them: the tensors of the same crops (pil_tensor), the padded batch (collate), and the source text of the beam search (inspect.getsource) must be equal. The package itself is tested separately (rc1_check.py) without any CODE path. usage: rc1_ref_compare_a1.py REPORT_JSON PKG_DIR MANIFEST_TSV """ import csv import importlib.util import inspect import json import os import sys sys.path.insert(0, "/path/to/TURKIC_OCR_CODE/src") import torch # noqa: E402 from PIL import Image # noqa: E402 from linedet import data as D # noqa: E402 from linedet import lm_decode as LD # noqa: E402 def main(): rep_path, pkg, man = sys.argv[1:4] spec = importlib.util.spec_from_file_location("pkg_a1", os.path.join(pkg, "infer.py")) mod = importlib.util.module_from_spec(spec) sys.modules["pkg_a1"] = mod spec.loader.exec_module(mod) rows = list(csv.DictReader(open(man, encoding="utf-8", newline=""), delimiter="\t", quoting=csv.QUOTE_NONE)) imgs = [Image.open(r["image"]).convert("RGB") for r in rows] spec_a1 = D.SPECS["A1"] ref = [D.pil_tensor(im, spec_a1) for im in imgs] got = [mod.pil_tensor(im, 96) for im in imgs] out = {"n_images": len(imgs), "height_equal": spec_a1.height == 96, "channels": spec_a1.channels, "pil_tensor_equal": all(torch.equal(a, b) for a, b in zip(ref, got))} cb = D.collate([{"image": t, "target": torch.zeros(0, dtype=torch.int32), "text": "", "gid": i} for i, t in enumerate(ref)]) x, sl = mod.collate(got) out["collate_equal"] = bool(torch.equal(cb["image"], x) and torch.equal(cb["seq_lens"], sl)) out["pad_equal"] = (D.PAD == mod.PAD) import ast import textwrap class Norm(ast.NodeTransformer): """drop `import kenlm` statements and rename the alias _k to kenlm (the evaluation file imports kenlm twice)""" def visit_Import(self, node): return None if any(al.name == "kenlm" for al in node.names) else node def visit_Name(self, node): return ast.copy_location(ast.Name(id="kenlm", ctx=node.ctx), node) if node.id == "_k" else node def body_dump(f): fn = ast.parse(textwrap.dedent(inspect.getsource(f))).body[0] body = fn.body if body and isinstance(body[0], ast.Expr) and isinstance(getattr(body[0], "value", None), ast.Constant) and isinstance(body[0].value.value, str): body = body[1:] # the docstring mod_ast = ast.Module(body=body, type_ignores=[]) mod_ast = Norm().visit(mod_ast) ast.fix_missing_locations(mod_ast) return ast.dump(mod_ast) # the beam search and the log-sum-exp are copied: the statements (ignoring docstrings, comments and the kenlm imports) must be equal out["beam_search_body_ast_equal"] = (body_dump(LD.beam_search) == body_dump(mod.beam_search)) out["lse_body_ast_equal"] = (body_dump(LD._lse) == body_dump(mod._lse)) out["beam_constants_equal"] = (LD.NEG == mod.NEG and LD.LN10 == mod.LN10 and LD.SP_TOKEN == mod.SP_TOKEN) out["beam_defaults"] = {"evaluation": [inspect.signature(LD.beam_search).parameters[k].default for k in ("beam", "prune")], "package": [inspect.signature(mod.beam_search).parameters[k].default for k in ("beam", "prune")]} out["beam_defaults_equal"] = (out["beam_defaults"]["evaluation"] == out["beam_defaults"]["package"]) out["all_equal"] = all(v for k, v in out.items() if k.endswith("_equal")) with open(rep_path, "w", encoding="utf-8") as f: json.dump(out, f, indent=1) print(json.dumps(out)) if __name__ == "__main__": main()