File size: 5,268 Bytes
698308b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
"""Build browsecomp-plus-mini: pick the subset with the honest three-way protocol on the main split
(seed 0), then write data/tasks.jsonl, data/leaderboard.csv, data/pairwise_agreement.csv and
data/metrics.json. Test-split numbers are the honest ones; all-run numbers are reported for reference.

usage: python build.py INPUT_DIR [n] [size_curve.json]
"""
import sys, json, csv
import numpy as np
from mini_lib import (load, honest_pick, rho_on, pair_agreement, calibrated_mae, wspearman, rankdata)

D = load(sys.argv[1])
N = int(sys.argv[2]) if len(sys.argv) > 2 else 15
CURVE = json.load(open(sys.argv[3])) if len(sys.argv) > 3 else None
X, full, w, systems, pool, runs, qids, meta = (D[k] for k in ("X", "full", "w", "systems", "pool", "runs", "qids", "meta"))
R = len(runs)
SEED = 0

h = honest_pick(D, N, SEED)
sub_idx = sorted(h["subset"], key=lambda j: int(qids[j]))
si, vi, ti = h["idx"]
mini = X[:, sub_idx].mean(1)

# ---- honest (test-split) numbers
rho_test = rho_on(D, sub_idx, ti)
mae_test, coef = calibrated_mae(full, mini, w, np.concatenate([si, vi]), ti)
raw_mae_test = float((w[ti] * np.abs(mini[ti] - full[ti])).sum() / w[ti].sum())
pa_test = pair_agreement(full, mini, w, ti)
rng = np.random.default_rng(7)
allowed = np.where(pool)[0]
rand_test, rand_all = [], []
for _ in range(1000):
    r = rng.choice(allowed, N, replace=False); s = X[:, r].mean(1)
    rand_test.append(wspearman(s[ti], full[ti], w[ti])); rand_all.append(wspearman(s, full, w))

# ---- reference numbers on every run (the subset was fit on select+validate, so these are partly in-sample)
rho_all = wspearman(mini, full, w)
pa_all = pair_agreement(full, mini, w, range(R))
mae_all, coef_all = calibrated_mae(full, mini, w, np.arange(R), np.arange(R))
rs = []
for _ in range(50):
    perm = rng.permutation(X.shape[1]); hh = X.shape[1] // 2
    rs.append(wspearman(X[:, perm[:hh]].mean(1), X[:, perm[hh:]].mean(1), w))

# ---- files
sysset = sorted(set(systems))
with open("data/tasks.jsonl", "w") as f:
    for j in sub_idx:
        col = X[:, j]
        sys_rate = np.mean([col[[i for i in range(R) if systems[i] == s]].mean() for s in sysset])
        f.write(json.dumps({"query_id": qids[j], "solve_rate_across_systems": round(float(sys_rate), 4),
                            "solve_rate_across_runs": round(float(col.mean()), 4), "n_runs": R, "n_systems": len(sysset)}) + "\n")

rf = np.array([1 + (full > v).sum() for v in full]); rm = np.array([1 + (mini > v).sum() for v in mini])
order = np.argsort(-full, kind="mergesort")
split_of = {s: name for name, ss in zip(("select", "validate", "test"), h["split"]) for s in ss}
with open("data/leaderboard.csv", "w", newline="") as f:
    wr = csv.writer(f)
    wr.writerow(["rank_full", "rank_mini", "move", "run", "system", "model", "retriever", "source", "split", "weight",
                 "score_full_830", f"score_mini_{N}", "mini_score_linear_fit_to_full", "official_leaderboard_llm_judge"])
    for i in order:
        m = meta[runs[i]]
        wr.writerow([int(rf[i]), int(rm[i]), int(rm[i] - rf[i]), runs[i], systems[i], m.get("model", ""), m.get("retriever", ""),
                     m.get("source", ""), split_of[systems[i]], round(float(w[i]), 4), round(float(full[i]), 4),
                     round(float(mini[i]), 4), round(float(np.polyval(coef_all, mini[i])), 4), m.get("official_leaderboard_acc", "")])

with open("data/pairwise_agreement.csv", "w", newline="") as f:
    wr = csv.writer(f)
    wr.writerow(["gap_between_runs_on_full_830", "test_split_pairs", "test_split_agreement_pct", "all_runs_pairs", "all_runs_agreement_pct"]
                + (["size_curve_mean_test_agreement_pct"] if CURVE else []))
    for a, b in zip(pa_test, pa_all):
        row = [a["gap"], a["n_pairs"], a["agreement_pct"], b["n_pairs"], b["agreement_pct"]]
        if CURVE:
            row.append(CURVE[str(N)]["test_pair_agreement"][a["gap"]]["agreement_pct"])
        wr.writerow(row)

metrics = dict(
    n_queries=N, query_ids=[qids[j] for j in sub_idx], selection_method=h["method"],
    n_runs=R, n_systems=len(sysset), split_systems={k: list(v) for k, v in zip(("select", "validate", "test"), h["split"])},
    test_split_weighted_spearman=round(rho_test, 4),
    test_split_mae_after_linear_fit_pts=round(100 * mae_test, 2),
    test_split_raw_mae_pts=round(100 * raw_mae_test, 2),
    random_subset_test_split_weighted_spearman=round(float(np.mean(rand_test)), 4),
    all_runs_weighted_spearman=round(rho_all, 4),
    random_subset_all_runs_weighted_spearman=round(float(np.mean(rand_all)), 4),
    all_runs_mae_after_linear_fit_pts=round(100 * mae_all, 2),
    linear_fit_full_from_mini=dict(slope=round(float(coef_all[0]), 4), intercept=round(float(coef_all[1]), 4)),
    full_benchmark_split_half_weighted_spearman=round(float(np.mean(rs)), 4),
    size_curve_mean_over_splits=({k: {kk: v[kk] for kk in ("test_spearman_mean", "test_spearman_sd", "random_test_spearman_mean", "test_calibrated_mae_pts")}
                                  for k, v in CURVE.items() if k.isdigit()} if CURVE else None),
)
json.dump(metrics, open("data/metrics.json", "w"), indent=1)
print(json.dumps({k: v for k, v in metrics.items() if k != "split_systems"}, indent=1))
print("test pairs:", pa_test); print("all pairs:", pa_all)