Datasets:
Download code/scripts_seqroute/judge_validation.py from divy123/sequential-router-v2: direct link, hf CLI and curl.
- Browser
- Download file 4.53 kB
-
https://huggingface.co/datasets/divy123/sequential-router-v2/resolve/main/code/scripts_seqroute/judge_validation.py
- Command line
-
hf download hf://datasets/divy123/sequential-router-v2/code/scripts_seqroute/judge_validation.py
-
curl -L -o judge_validation.py https://huggingface.co/datasets/divy123/sequential-router-v2/resolve/main/code/scripts_seqroute/judge_validation.py
4.53 kB
| """Judge validation: controls, repeat noise, cross-judge agreement, agreement with reference-based ROUGE-L. Writes manifests/judge_validation.json.""" | |
| import json | |
| import sys | |
| from pathlib import Path | |
| import numpy as np | |
| import pandas as pd | |
| from scipy.stats import pearsonr, spearmanr | |
| sys.path.insert(0, "src") | |
| ROOT = Path("/mnt/localssd/seqroute1m") | |
| S = ROOT / "scores/judge" | |
| def jq(d): | |
| return 0.4 * d.factuality + 0.4 * d.coverage + 0.2 * (d.relevance + d.coherence + d.instruction_adherence) / 3 | |
| def load(name): | |
| d = pd.read_json(S / f"parsed_{name}.jsonl", lines=True) | |
| return d | |
| q = pd.read_parquet(ROOT / "queries/queries_20k.parquet", columns=["query_id", "task_family", "reference", "prompt"]).set_index("query_id") | |
| out = {} | |
| P = load("primary") | |
| raw = pd.read_json(S / "raw_primary.jsonl", lines=True) | |
| out["primary_calls"] = int(raw.id.str.contains("#r0$").sum()) | |
| out["primary_retried_calls"] = int(raw.id.str.contains("~rt").sum()) | |
| out["primary_unparsed"] = int((~P.parsed).sum()) | |
| P = P[P.parsed & P.get("empty_final", pd.Series(False, index=P.index)).fillna(False).eq(False)].copy() | |
| P["Qw"] = jq(P) | |
| ctrl = P[P.id.str.startswith("ctrl_")].copy() | |
| ctrl["kind"] = ctrl.id.str.split("|").str[0] | |
| ctrl["query_id"] = ctrl.id.str.split("|").str[1] | |
| ctrl["family"] = ctrl.query_id.map(q.task_family) | |
| out["controls_mean_Q"] = ctrl.groupby(["family", "kind"]).Qw.mean().round(3).unstack().to_dict() | |
| pos, neg = ctrl[ctrl.kind == "ctrl_ref"].set_index("query_id").Qw, ctrl[ctrl.kind == "ctrl_neg"].set_index("query_id").Qw | |
| both = pd.concat([pos.rename("ref"), neg.rename("neg")], axis=1).dropna() | |
| out["control_pairs"] = int(len(both)) | |
| out["control_ref_gt_neg_rate"] = float((both.ref > both.neg).mean()) | |
| out["control_neg_Q_below_3_rate"] = float((both.neg < 3).mean()) | |
| M = P[~P.id.str.startswith("ctrl_")].copy() | |
| M[["model_id", "query_id"]] = M.id.str.split("|", n=1, expand=True) | |
| M["family"] = M.query_id.map(q.task_family) | |
| out["Q_saturation"] = {f: dict(mean=float(g.Qw.mean()), share_ge_9=float((g.Qw >= 9).mean()), share_eq_10=float((g.Qw >= 9.99).mean())) for f, g in M.groupby("family")} | |
| # agreement with ROUGE-L against the dataset reference summary | |
| try: | |
| from rouge_score import rouge_scorer | |
| sc = rouge_scorer.RougeScorer(["rougeL"]) | |
| resp = {} | |
| for mfile in (ROOT / "responses").glob("*.jsonl"): | |
| pass | |
| sub = M.sample(min(3000, len(M)), random_state=0) | |
| vals = [] | |
| cache = {} | |
| for r in sub.itertuples(): | |
| if r.model_id not in cache: | |
| cache[r.model_id] = {j["id"]: j for j in map(json.loads, open(ROOT / "responses" / f"{r.model_id}.jsonl"))} | |
| t = cache[r.model_id][r.query_id]["text"] | |
| t = t.rsplit("</think>", 1)[-1].strip() | |
| vals.append(sc.score(q.reference[r.query_id], t)["rougeL"].fmeasure) | |
| sub = sub.assign(rougeL=vals) | |
| out["corr_with_rougeL"] = {f: dict(pearson=float(pearsonr(g.Qw, g.rougeL)[0]), spearman=float(spearmanr(g.Qw, g.rougeL)[0]), n=int(len(g))) for f, g in sub.groupby("family")} | |
| except Exception as e: | |
| out["corr_with_rougeL"] = f"unavailable: {e!r}" | |
| # cross-judge | |
| try: | |
| A = load("alt") | |
| A = A[A.parsed & ~A.id.str.startswith("ctrl_")].copy() | |
| A["Qw"] = jq(A) | |
| A[["model_id", "query_id"]] = A.id.str.split("|", n=1, expand=True) | |
| J = M.merge(A[["id", "Qw"]], on="id", suffixes=("", "_alt")) | |
| out["cross_judge"] = {f: dict(n=int(len(g)), pearson=float(pearsonr(g.Qw, g.Qw_alt)[0]), spearman=float(spearmanr(g.Qw, g.Qw_alt)[0]), mean_primary=float(g.Qw.mean()), mean_alt=float(g.Qw_alt.mean()), | |
| mean_abs_diff=float((g.Qw - g.Qw_alt).abs().mean())) for f, g in J.groupby("family")} | |
| mp = J.groupby("model_id").Qw.mean(); ma = J.groupby("model_id").Qw_alt.mean() | |
| out["cross_judge_model_ranking_spearman"] = float(spearmanr(mp, ma.reindex(mp.index))[0]) | |
| except Exception as e: | |
| out["cross_judge"] = f"unavailable: {e!r}" | |
| # repeat noise on the weighted score | |
| try: | |
| R = load("repeat"); R = R[R.parsed & ~R.id.str.startswith("ctrl_")].copy(); R["Qw"] = jq(R) | |
| R["family"] = R.id.str.split("|").str[1].map(q.task_family) | |
| out["repeat"] = {f: dict(n_responses=int(g.id.nunique()), within_sd=float(np.sqrt(g.groupby("id").Qw.var(ddof=1).mean())), total_sd=float(g.Qw.std(ddof=1))) for f, g in R.groupby("family")} | |
| except Exception as e: | |
| out["repeat"] = f"unavailable: {e!r}" | |
| json.dump(out, open(ROOT / "manifests/judge_validation.json", "w"), indent=1) | |
| print(json.dumps({k: out[k] for k in out if k not in ("Q_saturation",)}, indent=1)[:3000]) | |