"""Summarize results.jsonl: perplexity by sparsity and method, plus paired-bootstrap 95% CIs of the
change in mean NLL relative to the dense model (all conditions are evaluated on the same windows)."""
import json
import sys

import numpy as np

rows = [json.loads(line) for line in open(sys.argv[1]) if line.strip()]
dense = [r for r in rows if r["method"] == "dense"][-1]
methods = [m for m in ("layerwise", "global", "random") if any(r["method"] == m for r in rows)]
table = {(r["method"], r["sparsity"]): r for r in rows if r["method"] in methods}
levels = sorted({s for _, s in table})
has_windows = "window_nll" in dense
if has_windows:
    base = np.array(dense["window_nll"])
    idx = np.random.default_rng(1).integers(0, len(base), size=(10000, len(base)))


def fmt(ppl):
    if ppl == float("inf") or ppl > 1e6:
        return "> 1e6"
    return f"{ppl:,.0f}" if ppl >= 1000 else (f"{ppl:,.1f}" if ppl >= 100 else f"{ppl:.2f}")


def delta_ci(r, ref=None):
    """Mean NLL change vs dense (or vs another result), with a paired bootstrap 95% CI over windows.
    Windows all have the same length, so the window mean equals the token-weighted mean."""
    d = np.array(r["window_nll"]) - (base if ref is None else np.array(ref["window_nll"]))
    boots = d[idx].mean(axis=1)
    return d.mean(), np.percentile(boots, 2.5), np.percentile(boots, 97.5)


print(f"{dense['model']} / WikiText-2 test, first {dense['windows']} windows x {dense['seq_len']} tokens "
      f"({dense['tokens']:,} predicted tokens) / {dense['targets']} matrices, {dense['prunable_weights']:,} prunable weights\n")
print("Perplexity (ratio to dense)\n")
print("| sparsity | " + " | ".join(methods) + " |")
print("|---|" + "---|" * len(methods))
print("| 0% (dense) | " + " | ".join(fmt(dense["ppl"]) for _ in methods) + " |")
for s in levels:
    cells = []
    for m in methods:
        r = table.get((m, s))
        if not r:
            cells.append("-")
            continue
        ratio = f" ({r['ppl'] / dense['ppl']:.2f}x)" if r["ppl"] < 1e6 and r["ppl"] / dense["ppl"] < 100 else ""
        cells.append(fmt(r["ppl"]) + ratio)
    print(f"| {round(s * 100)}% | " + " | ".join(cells) + " |")

if has_windows:
    print("\nChange in mean NLL vs dense, with paired-bootstrap 95% CI (negative = better than dense)\n")
    print("| sparsity | " + " | ".join(methods) + " |")
    print("|---|" + "---|" * len(methods))
for s in levels if has_windows else []:
    cells = []
    for m in methods:
        r = table.get((m, s))
        if not r:
            cells.append("-")
            continue
        mean, lo, hi = delta_ci(r)
        cells.append(f"{mean:+.4f} [{lo:+.4f}, {hi:+.4f}]")
    print(f"| {round(s * 100)}% | " + " | ".join(cells) + " |")

pairs = [s for s in levels if ("global", s) in table and ("layerwise", s) in table]
if has_windows and pairs:
    print("\nglobal minus layerwise, mean NLL with paired-bootstrap 95% CI (positive = global worse)\n")
    print("| sparsity | global - layerwise |")
    print("|---|---|")
    for s in pairs:
        mean, lo, hi = delta_ci(table[("global", s)], table[("layerwise", s)])
        print(f"| {round(s * 100)}% | {mean:+.4f} [{lo:+.4f}, {hi:+.4f}] |")

print("\nActual sparsity of the pruned matrices\n")
print(", ".join(f"{m}: " + " ".join(f"{table[(m, s)]['actual_sparsity']:.4f}" for s in levels if (m, s) in table) for m in methods))
