File size: 6,535 Bytes
ae1ed83 | 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 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 | #!/usr/bin/env python3
"""R6 four-milestone comparison: 5k / 30k / 100k / 200k.
Aggregates the 100k and 200k closed-loop evals with the registered
arm_stats of df_eval_agg.py (copied in df_r6_30k_compare.py), joins
the official 5k numbers (df_eval_summary.json + df_corr_eval.json)
and the 30k numbers (df_r6_30k_compare.json), checks Gate Object
Grounding at 100k and 200k, and writes
results/df_r6_200k_compare.json (+ dashboard state). A delta is
flagged within-noise when |delta| <= 2 * binomial SE of the later
milestone's rate.
"""
import json
import sys
from pathlib import Path
sys.path.insert(0, str(Path(__file__).parent))
from df_r6_30k_compare import arm_stats, se_binom # noqa: E402
from v2_dash import merge_item # noqa: E402
EXP = Path.home() / "sera_v4_experiments"
RES = EXP / "trace_validation" / "results"
CONDS = ["P0", "P1", "P2", "P3", "P4", "P5", "P6"]
CF = ["P4", "P5", "P6"]
MAIN = ["P0", "P1", "P2", "P3", "P4", "P5"]
def load_stats(tag):
recs = [json.loads(x) for x in
(RES / f"df_eval_r6_{tag}.jsonl").read_text().splitlines()
if x.strip()]
corr = json.loads(
(RES / f"df_corr_eval_r6_{tag}.json").read_text())["r6"]
return arm_stats(recs), corr, recs
def n_sel(recs, conds):
return sum(1 for r in recs
if not r.get("infeasible") and r["cond"] in conds
and r.get("selection_correct") is not None)
def pct(v):
return None if v is None else round(100 * v, 2)
def row(name, vals, ns):
"""vals/ns: {milestone: rate, effective n}."""
out = {"metric": name}
for m in ("5k", "30k", "100k", "200k"):
out[f"r6_{m}_pct"] = pct(vals.get(m))
for a, b in (("30k", "100k"), ("100k", "200k"), ("5k", "200k")):
d = None
if vals.get(a) is not None and vals.get(b) is not None:
d = round(100.0 * (vals[b] - vals[a]), 2)
se = se_binom(vals.get(b), ns.get(b))
out[f"delta_{b}_vs_{a}_pts"] = d
out[f"within_noise_{b}_vs_{a}"] = \
(abs(d) <= 2 * se) if (d is not None and se) else None
return out
def gate_block(stats, corr, s2):
grasp_gain = round(stats["main"]["grasp_correct_rate"]
- s2["main"]["grasp_correct_rate"], 4)
g = {
"recall@1_intervention>=0.80": bool(
corr["recall@1_intervention"] is not None
and corr["recall@1_intervention"] >= 0.80),
"recall@1_intervention": corr["recall@1_intervention"],
"selection_counterfactual>=0.80": bool(
stats["counterfactual"]["selection_rate"] is not None
and stats["counterfactual"]["selection_rate"] >= 0.80),
"selection_counterfactual":
stats["counterfactual"]["selection_rate"],
"wrong_grasp_counterfactual<0.10": bool(
stats["counterfactual"]["grasp_wrong_rate"] is not None
and stats["counterfactual"]["grasp_wrong_rate"] < 0.10),
"wrong_grasp_counterfactual":
stats["counterfactual"]["grasp_wrong_rate"],
"grasp_gain_vs_R2>=+0.15": bool(grasp_gain >= 0.15),
"grasp_gain_vs_R2": grasp_gain,
}
g["pass"] = all(v for k, v in g.items()
if k.endswith("0.80") or k.endswith("0.10")
or k.endswith("+0.15"))
return g
def main():
summ = json.loads((RES / "df_eval_summary.json").read_text())
s5 = summ["arms"]["r6"]
s2 = summ["arms"]["r2"]
corr5 = json.loads((RES / "df_corr_eval.json").read_text())["r6"]
c30 = json.loads((RES / "df_r6_30k_compare.json").read_text())
s30, corr30 = c30["stats_30k"], c30["corr_30k"]
recs30 = [json.loads(x) for x in
(RES / "df_eval_r6_30k.jsonl").read_text().splitlines()
if x.strip()]
s100, corr100, recs100 = load_stats("100k")
s200, corr200, recs200 = load_stats("200k")
S = {"5k": s5, "30k": s30, "100k": s100, "200k": s200}
C = {"5k": corr5, "30k": corr30, "100k": corr100, "200k": corr200}
R = {"30k": recs30, "100k": recs100, "200k": recs200}
def V(get):
return {m: get(S[m]) for m in S}
def N_main(m):
return S[m]["main"]["n"]
rows = [
row("Success — suite chính (P0–P5)",
V(lambda s: s["main"]["success_rate"]),
{m: N_main(m) for m in S}),
row("Grasp đúng — suite chính",
V(lambda s: s["main"]["grasp_correct_rate"]),
{m: N_main(m) for m in S}),
row("Chọn đúng vật — suite chính",
V(lambda s: s["main"]["selection_rate"]),
{m: (n_sel(R[m], MAIN) if m in R else None)
for m in S}),
row("Chọn đúng vật — phản thực (P4+P5+P6)",
V(lambda s: s["counterfactual"]["selection_rate"]),
{m: (n_sel(R[m], CF) if m in R else None) for m in S}),
row("Grasp sai — phản thực",
V(lambda s: s["counterfactual"]["grasp_wrong_rate"]),
{m: S[m]["counterfactual"]["n"] for m in S}),
row("Success — phản thực",
V(lambda s: s["counterfactual"]["success_rate"]),
{m: S[m]["counterfactual"]["n"] for m in S}),
row("Recall@1 correspondence — can thiệp (480 scene)",
{m: C[m]["recall@1_intervention"] for m in C},
{m: 320 for m in C}),
row("Recall@1 correspondence — mọi scene",
{m: C[m]["recall@1_all"] for m in C},
{m: 480 for m in C}),
]
for c in CONDS:
if all(c in S[m]["by_cond"] for m in S):
rows.append(row(
f"Success — {c}",
V(lambda s: s["by_cond"][c]["success_rate"]),
{m: S[m]["by_cond"][c]["n"] for m in S}))
gates = {"100k": gate_block(s100, corr100, s2),
"200k": gate_block(s200, corr200, s2)}
out = {"arm": "r6",
"stats": {m: S[m] for m in S},
"corr": {m: C[m] for m in C},
"rows": rows, "gate": gates,
"gate_object_grounding_pass_100k": gates["100k"]["pass"],
"gate_object_grounding_pass_200k": gates["200k"]["pass"]}
(RES / "df_r6_200k_compare.json").write_text(
json.dumps(out, indent=1, ensure_ascii=False))
merge_item("data_factory.r6_200k_compare", {
"status": "done", "rows": rows, "gate": gates,
"gate_pass": gates["200k"]["pass"]})
print(json.dumps({"rows": rows, "gate": gates}, indent=1,
ensure_ascii=False))
if __name__ == "__main__":
main()
|