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/generate_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)
pos = data["position"]
dense = data["dense_token"]
target = data["target_token"]
matched = data["matched"]
routed = data["routed_low_rank"]
conf = data["confidence"]
fig, axes = plt.subplots(2, 1, figsize=(12, 7), sharex=True)
ax = axes[0]
ax.plot(pos, dense, "-", label="dense", color="tab:blue", alpha=0.7)
ax.plot(pos, target, "-", label="target", color="tab:orange", alpha=0.7)
mismatch = pos[matched == 0]
ax.scatter(
mismatch,
target[matched == 0],
color="tab:red",
s=30,
zorder=5,
label="mismatch",
)
ax.set_ylabel("token id")
ax.set_title(f"{csv_path.name} — agreement {matched.mean()*100:.1f}%")
ax.legend()
ax.grid(True, ls="--", alpha=0.4)
ax = axes[1]
routed_mask = routed == 1
if np.any(routed_mask):
ax.bar(pos[routed_mask], conf[routed_mask], color="tab:green", width=0.8, label="low-rank")
fallback_mask = (~routed_mask) & np.isfinite(conf)
if np.any(fallback_mask):
ax.bar(
pos[fallback_mask],
conf[fallback_mask],
color="tab:red",
width=0.8,
label="dense fallback",
)
ax.axhline(np.nanmean(conf), color="black", ls="--", lw=1, label="mean confidence")
ax.set_xlabel("generated position")
ax.set_ylabel("confidence")
ax.set_title("per-position confidence (gated mode only)")
ax.legend()
ax.grid(True, ls="--", alpha=0.4)
out = csv_path.with_suffix(".png")
fig.tight_layout()
fig.savefig(out, dpi=150)
print(f"Saved {out}")
if __name__ == "__main__":
main()