File size: 2,591 Bytes
7c61ee6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""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()