import sys
from pathlib import Path
import matplotlib.pyplot as plt
import numpy as np
def main() -> None:
if len(sys.argv) < 2:
csv_path = Path("target/ffn_confidence_routing.csv")
else:
csv_path = Path(sys.argv[1])
if not csv_path.exists():
raise SystemExit(f"CSV not found: {csv_path}")
data = np.genfromtxt(csv_path, delimiter=",", names=True, dtype=None, encoding=None)
rank = int(data["rank"][0])
mode = str(data["mode"][0])
frac = data["fraction_low_rank"]
speedup = data["estimated_speedup"]
l2 = data["logit_l2"]
top1 = data["top1_agreement"]
fig, axes = plt.subplots(1, 3, figsize=(15, 4.5))
ax = axes[0]
ax.plot(frac * 100.0, l2, "o-", color="tab:blue")
ax.set_xlabel("samples routed through low-rank (%)")
ax.set_ylabel("mean logit L2 drift")
ax.set_title(f"rank={rank}, mode={mode}")
ax.invert_xaxis()
ax.grid(True, ls="--", alpha=0.4)
for x, y, t in zip(frac * 100.0, l2, data["threshold"]):
ax.annotate(f"{t:.2f}", (x, y), textcoords="offset points", xytext=(5, -10), fontsize=7)
ax = axes[1]
ax.plot(frac * 100.0, top1 * 100.0, "o-", color="tab:green")
ax.set_xlabel("samples routed through low-rank (%)")
ax.set_ylabel("top-1 agreement (%)")
ax.set_ylim(0, 105)
ax.set_title("accuracy vs routing fraction")
ax.invert_xaxis()
ax.grid(True, ls="--", alpha=0.4)
for x, y, t in zip(frac * 100.0, top1 * 100.0, data["threshold"]):
ax.annotate(f"{t:.2f}", (x, y), textcoords="offset points", xytext=(5, -10), fontsize=7)
ax = axes[2]
ax.plot(speedup, top1 * 100.0, "o-", color="tab:orange")
ax.set_xlabel("estimated speedup (×)")
ax.set_ylabel("top-1 agreement (%)")
ax.set_ylim(0, 105)
ax.set_title("Pareto: accuracy vs speedup")
ax.grid(True, ls="--", alpha=0.4)
for x, y, t in zip(speedup, top1 * 100.0, data["threshold"]):
ax.annotate(f"{t:.2f}", (x, y), textcoords="offset points", xytext=(5, -10), fontsize=7)
out = csv_path.with_suffix(".png")
fig.tight_layout()
fig.savefig(out, dpi=150)
print(f"Saved {out}")
if __name__ == "__main__":
main()