"""R5 FREEZE: reads DATA's report-only duplicate check (build_logs/dedupe_all.json + dedupe_all_dropped.tsv) and applies the freeze rule written in records/R5/FREEZE/status.json BEFORE the report existed: - pairs where the train side is kohtd_rand_train or the partner is kohtd_rand_val / kohtd_rand_test are the deliberate replication overlap (amendment 3.3 D5 (ii), clarification H1): left out, counted; - any pair whose partner is kohtd_test: STOP (H1 guarantee and the form split); - any other byte-identical pair: STOP; - near-pHash pairs with the same non-empty label: STOP for manual review; - near-pHash pairs with different labels or empty (clutter) labels: reported, not a stop. Also counts, per manifest, how many row ids have an image hash in hashes.tsv / hashes_synth.tsv (rows without a hash are silently skipped by dedupe_all: they are 'not checked', not 'clean'). Reads ids only from the manifests (no text of test rows, no image). Also checks whether kohtd_rand_train / kohtd_rand_val ids are KOHTD ids (overlap with kohtd_train / kohtd_val ids). usage: python -I dedupe_verdict.py OUT_JSON """ import collections import csv import json import os import sys csv.field_size_limit(10 ** 9) R5 = "/path/to/TURKIC_OCR_DATA/data/processed/r5" LOGS = R5 + "/build_logs" MAN = R5 + "/manifests" out = sys.argv[1] info = json.load(open(LOGS + "/stage_dedupe_all.json", encoding="utf-8")) hs = json.load(open(LOGS + "/stage_hash_synth.json", encoding="utf-8")) RAND_TRAIN = {"kohtd_rand_train"} RAND_EVAL = {"kohtd_rand_val", "kohtd_rand_test"} cnt = collections.Counter() stop = [] near_report = collections.Counter() same_label_near = [] with open(LOGS + "/dedupe_all_dropped.tsv", encoding="utf-8", newline="") as f: rd = csv.DictReader(f, delimiter="\t", quoting=csv.QUOTE_NONE) for r in rd: byte = r["reason"] == "byte_identical" k = (r["manifest"], r["partner_manifest"], "byte" if byte else "near", r["same_label"]) cnt[k] += 1 if r["partner_manifest"] == "kohtd_test": stop.append(("partner is kohtd_test",) + k) continue if r["manifest"] in RAND_TRAIN or r["partner_manifest"] in RAND_EVAL: continue if byte: stop.append(("byte-identical",) + k) elif r["same_label"] == "True" and r["kind"] != "clutter": same_label_near.append([r["train_id"], r["manifest"], r["partner_id"], r["partner_manifest"], r["reason"]]) else: near_report[k] += 1 def ids_of(name): p = os.path.join(MAN, name + ".tsv") with open(p, encoding="utf-8", newline="") as f: rd = csv.reader(f, delimiter="\t", quoting=csv.QUOTE_NONE) h = next(rd) i = h.index("id") return [r[i] for r in rd if r] hashed = set() for fn in ("hashes.tsv", "hashes_synth.tsv"): with open(os.path.join(LOGS, fn), encoding="utf-8") as f: f.readline() for line in f: q = line.split("\t", 3) if len(q) >= 3 and q[2]: hashed.add(q[0]) cover = {} for fn in sorted(os.listdir(MAN)): if not fn.endswith(".tsv") or fn.endswith(".meta.tsv"): continue nm = fn[:-4] ids = ids_of(nm) n_h = sum(1 for i in ids if i in hashed) cover[nm] = {"rows": len(ids), "rows_with_hash": n_h, "duplicate_check": "checked" if n_h == len(ids) else ("NOT CHECKED" if n_h == 0 else "partly checked")} kt, kv = set(ids_of("kohtd_train")), set(ids_of("kohtd_val")) rt, rv = ids_of("kohtd_rand_train"), ids_of("kohtd_rand_val") rand_ids = {"kohtd_rand_train_rows": len(rt), "in_kohtd_train": sum(1 for i in rt if i in kt), "in_kohtd_val": sum(1 for i in rt if i in kv), "kohtd_rand_val_rows": len(rv), "rand_val_in_kohtd_train": sum(1 for i in rv if i in kt), "rand_val_in_kohtd_val": sum(1 for i in rv if i in kv)} res = {"dedupe_all_finished_utc": info.get("finished_utc"), "hash_synth": hs, "would_drop_total": info.get("would_drop_total"), "pairs_by_manifest_partner_reason_samelabel": {" | ".join(k): v for k, v in sorted(cnt.items())}, "STOP_pairs": [" | ".join(x) for x in stop][:200], "n_STOP_pairs": len(stop), "same_label_near_nonclutter": same_label_near[:200], "n_same_label_near_nonclutter": len(same_label_near), "near_reported_not_stop": {" | ".join(k): v for k, v in near_report.items()}, "hash_coverage_by_manifest": cover, "rand_ids_are_kohtd_ids": rand_ids} res["verdict"] = "STOP" if stop else ("NEEDS MANUAL REVIEW (same-label near duplicates)" if same_label_near else "PASS") json.dump(res, open(out, "w", encoding="utf-8"), indent=1, ensure_ascii=False) print(res["verdict"], "stop pairs", len(stop), "same-label near", len(same_label_near), "not checked:", [k for k, v in cover.items() if v["duplicate_check"] != "checked"])