"""Round-10 val table: per variant, expected CD over the N samples, single-sample CDs, MBR-selected CD and the GT-picked best-of-N (selection ceiling); paired bootstrap of expected CD against a reference variant.""" import os import json import argparse import numpy as np O = "/home/hubin/trellis_work/outputs/opt" def rows(path): return {r["id"]: r["cd"] * 1e3 for r in json.load(open(path))["rows"]} def main(): p = argparse.ArgumentParser() p.add_argument("--ref", default="single_c") p.add_argument("--n", type=int, default=4) args = p.parse_args() table = {} for name in sorted(os.listdir(f"{O}/val")): paths = [f"{O}/val/{name}/metrics_s{j}.json" for j in range(args.n)] if not all(os.path.exists(q) for q in paths): continue s = [rows(q) for q in paths] ids = sorted(set.intersection(*[set(x) for x in s])) cd = np.array([[x[i] for x in s] for i in ids]) # [obj, sample] ent = {"n": len(ids), "ids": ids, "cd": cd} m = f"{O}/val_mbr4/{name}/metrics.json" if os.path.exists(m): mr = rows(m) if all(i in mr for i in ids): ent["mbr"] = np.array([mr[i] for i in ids]) table[name] = ent ref = table.get(args.ref) print(f"{'variant':18s} {'n':>3s} {'E[CD]':>6s} {'samples':>27s} {'MBR4':>6s} {'best4':>6s} E[CD]-ref 95% CI") out = {} for name, e in table.items(): exp = e["cd"].mean(1) line = f"{name:18s} {e['n']:3d} {exp.mean():6.2f} {' '.join(f'{v:6.2f}' for v in e['cd'].mean(0)):>27s} " line += f"{e['mbr'].mean():6.2f} " if "mbr" in e else " - " line += f"{e['cd'].min(1).mean():6.2f}" out[name] = {"n": e["n"], "expected_cd": exp.mean(), "per_sample": e["cd"].mean(0).tolist(), "mbr4": e["mbr"].mean() if "mbr" in e else None, "best_of_n": e["cd"].min(1).mean()} if ref is not None and name != args.ref: common = [i for i in e["ids"] if i in ref["ids"]] a = np.array([exp[e["ids"].index(i)] for i in common]) b = np.array([ref["cd"].mean(1)[ref["ids"].index(i)] for i in common]) d = a - b bs = [d[np.random.default_rng(k).integers(0, len(d), len(d))].mean() for k in range(4000)] lo, hi = np.percentile(bs, [2.5, 97.5]) line += f" {d.mean():+.2f} [{lo:+.2f}, {hi:+.2f}]" out[name]["vs_ref"] = [d.mean(), lo, hi] print(line) json.dump(out, open(f"{O}/val_table.json", "w"), indent=1) if __name__ == "__main__": main()