| """Figures for the BALLAST reproduction logbook (Plotly HTML + raw CSV).""" |
|
|
| from __future__ import annotations |
|
|
| import json |
| import os |
| import sys |
|
|
| import numpy as np |
| import plotly.graph_objects as go |
|
|
| OUT = "outputs" |
| COL = { |
| "unif": "#7f8c8d", "sobol": "#2980b9", "dist_sep": "#8e44ad", |
| "eig": "#e67e22", "ballast_opt": "#16a085", "ballast_true": "#c0392b", |
| } |
| NAME = { |
| "unif": "UNIF", "sobol": "SOBOL", "dist_sep": "DIST-SEP", "eig": "EIG", |
| "ballast_opt": "BALLAST-opt", "ballast_true": "BALLAST-true", |
| } |
| LAYOUT = dict( |
| template="plotly_white", font=dict(size=13), height=420, |
| margin=dict(l=60, r=20, t=50, b=50), |
| ) |
|
|
|
|
| def _save(fig, name, csv_rows=None, header=None): |
| os.makedirs(OUT, exist_ok=True) |
| fig.write_html(f"{OUT}/{name}.html", include_plotlyjs="cdn") |
| if csv_rows is not None: |
| with open(f"{OUT}/{name}.csv", "w") as f: |
| f.write(",".join(header) + "\n") |
| for r in csv_rows: |
| f.write(",".join(str(x) for x in r) + "\n") |
| print("wrote", name) |
|
|
|
|
| def fig_spde_cost(path="outputs/raw/spde_cost/spde_cost.json"): |
| d = json.load(open(path)) |
| rows = d["rows"] |
| n = [r["n_sampT"] for r in rows] |
| sp = [r["spde_s"] for r in rows] |
| nv = [r["naive_s"] for r in rows] |
| ok = [i for i, v in enumerate(nv) if v is not None] |
|
|
| fig = go.Figure() |
| fig.add_trace(go.Scatter(x=n, y=sp, name="SPDE (Sec. 4.1)", mode="lines+markers", |
| line=dict(color="#16a085", width=3))) |
| fig.add_trace(go.Scatter(x=[n[i] for i in ok], y=[nv[i] for i in ok], |
| name="naive dense", mode="lines+markers", |
| line=dict(color="#c0392b", width=3))) |
| |
| i0 = ok[-1] |
| ext = [x for x in n if x > n[i0]] |
| if ext: |
| fig.add_trace(go.Scatter( |
| x=[n[i0]] + ext, |
| y=[nv[i0]] + [nv[i0] * (x / n[i0]) ** 3 for x in ext], |
| name="naive (cubic extrapolation, out of memory)", mode="lines", |
| line=dict(color="#c0392b", width=2, dash="dot"))) |
| fig.update_layout( |
| title="Posterior sampling cost: SPDE stays linear, naive dense goes cubic then out of memory", |
| xaxis=dict(title="N_sampT (sampled time slices)", type="log"), |
| yaxis=dict(title="seconds for J=20 samples (A100, fp64)", type="log"), |
| legend=dict(x=0.02, y=0.98), **LAYOUT) |
| _save(fig, "claim2_spde_cost", |
| [(r["n_sampT"], r["spde_s"], r["naive_s"], r["naive_cov_gb"]) for r in rows], |
| ["n_sampT", "spde_seconds", "naive_seconds", "naive_cov_gb"]) |
|
|
|
|
| def fig_ablation(path="outputs/claim5.json"): |
| d = json.load(open(path)) |
| fig = go.Figure() |
| colors = {"3.0": "#2980b9", "5.0": "#16a085", "7.0": "#c0392b"} |
| rows = [] |
| for t, r in d.items(): |
| J = np.array(r["J"]) |
| m = np.array(r["gap_mc_mean"]) |
| s = np.array(r["gap_mc_se2"]) |
| c = colors.get(str(t), "#333") |
| fig.add_trace(go.Scatter(x=J, y=m, name=f"BALLAST t={t}", mode="lines", |
| line=dict(color=c, width=2.5))) |
| fig.add_trace(go.Scatter( |
| x=np.concatenate([J, J[::-1]]), y=np.concatenate([m + s, (m - s)[::-1]]), |
| fill="toself", fillcolor=c.replace("#", "rgba(").replace("", "") if False else c, |
| opacity=0.15, line=dict(width=0), showlegend=False, hoverinfo="skip")) |
| fig.add_trace(go.Scatter(x=[J[0], J[-1]], y=[r["eig_gap_mc"]] * 2, |
| name=f"EIG t={t}", mode="lines", |
| line=dict(color=c, width=1.5, dash="dash"))) |
| fig.add_trace(go.Scatter(x=[J[0], J[-1]], y=[r["unif_gap_mc"]] * 2, |
| name=f"UNIF t={t}", mode="lines", |
| line=dict(color=c, width=1.5, dash="dot"))) |
| for j, v in zip(J, m): |
| rows.append((t, j, v)) |
| fig.add_hline(y=1.0, line=dict(color="black", width=1)) |
| fig.add_annotation(x=np.log10(120), y=np.log10(1.0), text="1% cut-off", |
| showarrow=False, yshift=10) |
| fig.add_vline(x=20, line=dict(color="#888", width=1, dash="dash")) |
| fig.update_layout( |
| title="Percentage utility gap vs Monte Carlo sample number J (2 s.e. bands)", |
| xaxis=dict(title="J (posterior field samples)", type="log"), |
| yaxis=dict(title="Monte Carlo % utility gap", type="log"), |
| **LAYOUT) |
| _save(fig, "claim5_ablation", rows, ["decision_time", "J", "gap_mc_pct"]) |
|
|
|
|
| def fig_policy(path, tag, title): |
| d = json.load(open(path)) |
| pols = d["policies"] |
| n_dep = d["n_deploy"] |
| x = np.arange(1, n_dep + 1) |
|
|
| |
| fig = go.Figure() |
| for i, p in enumerate(pols): |
| m = np.array(d["rank_mean"][i]) |
| s = np.array(d["rank_se2"][i]) |
| fig.add_trace(go.Scatter(x=x, y=m, name=NAME[p], mode="lines+markers", |
| line=dict(color=COL[p], width=2.5), |
| error_y=dict(type="data", array=s, visible=True, thickness=1))) |
| fig.update_layout( |
| title=f"{title}: average policy rank (1 = best), {d['n_runs']} runs", |
| xaxis=dict(title="drifters deployed"), |
| yaxis=dict(title="average rank"), **LAYOUT) |
| _save(fig, f"{tag}_rank", |
| [(p, i + 1, d["rank_mean"][j][i]) for j, p in enumerate(pols) for i in range(n_dep)], |
| ["policy", "n_drifters", "mean_rank"]) |
|
|
| |
| fig = go.Figure() |
| for p in pols: |
| if p == "unif": |
| continue |
| m = np.array(d["iso"][p]["mean"]) |
| s = np.array(d["iso"][p]["se2"]) |
| fig.add_trace(go.Scatter(x=x, y=m, name=NAME[p], mode="lines+markers", |
| line=dict(color=COL[p], width=2.5), |
| error_y=dict(type="data", array=s, visible=True, thickness=1))) |
| fig.add_hline(y=0, line=dict(color="#7f8c8d", width=1, dash="dash")) |
| fig.update_layout( |
| title=f"{title}: drifters saved vs UNIF (iso-performance), {d['n_runs']} runs", |
| xaxis=dict(title="drifters deployed"), |
| yaxis=dict(title="drifters saved (positive = better)"), **LAYOUT) |
| _save(fig, f"{tag}_iso", |
| [(p, i + 1, d["iso"][p]["mean"][i], d["iso"][p]["se2"][i]) |
| for p in pols for i in range(n_dep)], |
| ["policy", "n_drifters", "drifters_saved", "se2"]) |
|
|
| |
| fig = go.Figure() |
| for p in pols: |
| m = np.array(d["err_mean"][p]) |
| s = np.array(d["err_se2"][p]) |
| fig.add_trace(go.Scatter(x=x, y=m, name=NAME[p], mode="lines", |
| line=dict(color=COL[p], width=2.5), |
| error_y=dict(type="data", array=s, visible=True, thickness=1))) |
| fig.update_layout( |
| title=f"{title}: field error vs drifters deployed, {d['n_runs']} runs", |
| xaxis=dict(title="drifters deployed"), |
| yaxis=dict(title="mean L2 error of posterior mean field"), **LAYOUT) |
| _save(fig, f"{tag}_error", |
| [(p, i + 1, d["err_mean"][p][i]) for p in pols for i in range(n_dep)], |
| ["policy", "n_drifters", "mean_l2_error"]) |
|
|
|
|
| if __name__ == "__main__": |
| which = sys.argv[1] if len(sys.argv) > 1 else "all" |
| if which in ("all", "spde"): |
| fig_spde_cost() |
| if which in ("all", "ablation") and os.path.exists("outputs/claim5.json"): |
| fig_ablation() |
| if which in ("all", "synth") and os.path.exists("outputs/claim3.json"): |
| fig_policy("outputs/claim3.json", "claim3", "Temporal Helmholtz ground truth") |
| if which in ("all", "suntans") and os.path.exists("outputs/claim4.json"): |
| fig_policy("outputs/claim4.json", "claim4", "SUNTANS ground truth") |
|
|