Download chaincheck_eval.py from ThakiCloud/ChainCheck: direct link, hf CLI and curl.
- Browser
- Download file 3.29 kB
-
https://huggingface.co/datasets/ThakiCloud/ChainCheck/resolve/main/chaincheck_eval.py
- Command line
-
hf download hf://datasets/ThakiCloud/ChainCheck/chaincheck_eval.py
-
curl -L -o chaincheck_eval.py https://huggingface.co/datasets/ThakiCloud/ChainCheck/resolve/main/chaincheck_eval.py
3.29 kB
| #!/usr/bin/env python3 | |
| """ChainCheck evaluator (standalone; needs only numpy). | |
| Score every item of a ChainCheck file with your evidence-sufficiency system (higher = "the passages are | |
| sufficient"), then: | |
| python chaincheck_eval.py --data twowiki_replication.jsonl.gz --scores my_scores.jsonl [--exclude-cb27b] | |
| my_scores.jsonl: one JSON object per line, {"item_id": ..., "score": float}. | |
| Reports, separately for real and synthetic replacement entities: | |
| Nominal = AUC(A > D) what an ordinary benchmark reports | |
| CE = AUC(B > D) - 0.5 chain effect: response to breaking the chain, edit held fixed | |
| EE = AUC(A > B) - 0.5 edit effect: response to an edit that leaves the chain intact | |
| Sigma = CE - |EE| chain selectivity; chain-selective if the 95% lower bound > 0 | |
| Pair-bootstrap 95% intervals (2,000 resamples, seed 0). These are matched contrasts, not an additive | |
| decomposition: Nominal is not CE + EE. | |
| --exclude-cb27b drops pairs whose question Qwen3.8-27B answered closed-book (the paper's closed-book-hard filter). | |
| """ | |
| import argparse, gzip, json | |
| from collections import defaultdict | |
| import numpy as np | |
| def load(path): | |
| op = gzip.open if path.endswith(".gz") else open | |
| with op(path, "rt", encoding="utf-8") as f: | |
| return [json.loads(x) for x in f if x.strip()] | |
| def wins(s, hi, lo): | |
| return np.array([(s[h] > s[l]) + 0.5 * (s[h] == s[l]) for h, l in zip(hi, lo)], dtype=float) | |
| def metrics(s, quads, n_boot=2000, seed=0): | |
| A = [q["A"] for q in quads]; B = [q["B"] for q in quads]; D = [q["D"] for q in quads] | |
| W = {"nominal": wins(s, A, D), "chain": wins(s, B, D), "edit": wins(s, A, B)} | |
| idx = np.random.default_rng(seed).integers(0, len(quads), size=(n_boot, len(quads))) | |
| ce_b = W["chain"][idx].mean(1) - 0.5; ee_b = W["edit"][idx].mean(1) - 0.5; sig_b = ce_b - np.abs(ee_b) | |
| ci = lambda x: [round(float(np.percentile(x, 2.5)), 4), round(float(np.percentile(x, 97.5)), 4)] | |
| ce, ee = W["chain"].mean() - 0.5, W["edit"].mean() - 0.5 | |
| return {"n_pairs": len(quads), "nominal": round(float(W["nominal"].mean()), 4), "CE": round(float(ce), 4), | |
| "CE_ci": ci(ce_b), "EE": round(float(ee), 4), "EE_ci": ci(ee_b), "sigma": round(float(ce - abs(ee)), 4), | |
| "sigma_ci": ci(sig_b), "chain_selective": bool(np.percentile(sig_b, 2.5) > 0)} | |
| def main(): | |
| ap = argparse.ArgumentParser() | |
| ap.add_argument("--data", required=True); ap.add_argument("--scores", required=True) | |
| ap.add_argument("--exclude-cb27b", action="store_true") | |
| a = ap.parse_args() | |
| rows = load(a.data) | |
| sc = {x["item_id"]: float(x["score"]) for x in load(a.scores)} | |
| missing = [r["item_id"] for r in rows if r["item_id"] not in sc] | |
| if missing: | |
| raise SystemExit(f"{len(missing)} items have no score, e.g. {missing[:3]}") | |
| quads = defaultdict(dict) | |
| for r in rows: | |
| if a.exclude_cb27b and r["cb_27b_correct"]: | |
| continue | |
| quads[(r["pair_id"], r["variant"])][r["cell"]] = r["item_id"] | |
| out = {} | |
| for variant in ("real", "fict"): | |
| qs = [q for (p, v), q in sorted(quads.items()) if v == variant and len(q) == 3] | |
| if qs: | |
| out[variant] = metrics(sc, qs) | |
| print(json.dumps(out, indent=1)) | |
| if __name__ == "__main__": | |
| main() | |