"""CSV -> LaTeX fragments for the paper (booktabs). Numbers are formatted here only; values come from results/tables CSVs.""" from __future__ import annotations import pandas as pd, numpy as np from pathlib import Path from ..util import ROOT TAB = ROOT / "results/tables"; OUT = ROOT / "paper/tables" def esc(s): return str(s).replace("_", "\\_").replace("%", "\\%").replace("&", "\\&").replace("→", "$\\to$").replace("≥", "$\\ge$").replace("×", "$\\times$").replace("—", "--") def df_to_tex(df: pd.DataFrame, path: Path, colfmt: str | None = None, floatfmt="{:.2f}"): cols = list(df.columns); colfmt = colfmt or "l" * len(cols) lines = ["\\begin{tabular}{@{}" + colfmt + "@{}}", "\\toprule", " & ".join(esc(c) for c in cols) + " \\\\", "\\midrule"] for _, r in df.iterrows(): cells = [] for c in cols: v = r[c] if isinstance(v, float): cells.append("--" if np.isnan(v) else (floatfmt.format(v) if abs(v) < 1e4 else f"{v:,.0f}")) else: cells.append(esc(v)) lines.append(" & ".join(cells) + " \\\\") lines += ["\\bottomrule", "\\end{tabular}"] OUT.mkdir(parents=True, exist_ok=True); path.write_text("\n".join(lines) + "\n") def main(): t1 = pd.read_csv(TAB / "table1_taxonomy.csv"); df_to_tex(t1, OUT / "table1_taxonomy.tex", "lp{4.2cm}p{2cm}p{2.3cm}p{2.3cm}p{1.6cm}p{2.6cm}") t2 = pd.read_csv(TAB / "table2_models.csv")[["alias", "repo", "revision", "layers", "kv_heads", "q_heads", "head_dim", "max_pos", "cache_bytes_per_token_bf16", "prefill_ms_8k", "prefill_ms_16k"]] t2.columns = ["alias", "checkpoint", "rev.", "L", "H_kv", "H_q", "d_h", "max pos", "cache B/token", "prefill 8k", "prefill 16k"]; df_to_tex(t2, OUT / "table2_models.tex", "llllllllrrr") t3 = pd.read_csv(TAB / "table3_datasets.csv"); df_to_tex(t3, OUT / "table3_datasets.tex", "llrrr", "{:.0f}") pr = TAB / "E1_core_primary.csv" if pr.exists(): tp = pd.read_csv(pr) tp["pair_s"] = tp["pair"].map({"Qwen2.5-3B-Instruct-bf16->Qwen2.5-3B-Instruct-bf16": "Qwen", "Llama-3.2-3B-Instruct-bf16->Llama-3.2-3B-Instruct-bf16": "Llama"}).fillna(tp["pair"]) tp["fam_s"] = tp["family"].str.replace("public_", "") tp["cell"] = tp.apply(lambda r: f"{100*r.quality:.0f} [{100*r.q_lo:.0f},{100*r.q_hi:.0f}]", axis=1) tp["cond_s"] = tp["condition"].str.replace("budget_tokens=512", "512", regex=False).str.replace("keep_frac=0.25", "1/4", regex=False).str.replace("|max_fetch_bytes=2000|max_lookups=6", "", regex=False).str.replace("policy=attention", "att", regex=False).str.replace("codec=zstd", "zstd", regex=False).str.replace("|", " ", regex=False) piv = tp.pivot_table(index="cond_s", columns=["fam_s", "pair_s"], values="cell", aggfunc="first") piv = piv.reindex(columns=[(f, p) for f in ["synthetic", "exec", "qasper", "2wikimqa"] for p in ["Qwen", "Llama"]]) piv.columns = [f"{f} {p}" for f, p in piv.columns] order = sorted(piv.index, key=lambda c: (c[0], c)) piv = piv.loc[order].reset_index().rename(columns={"cond_s": "condition"}).fillna("--") df_to_tex(piv, OUT / "table_primary.tex", "l" + "r" * (len(piv.columns) - 1)) hh = TAB / "E4_core_handoffs.csv" if hh.exists(): th = pd.read_csv(hh) th["chain_s"] = th["chain"].str.replace("qwen3b-llama3b", "Qwen/Llama alt.", regex=False).str.replace("qwen3b", "Qwen", regex=False).str.replace("|", " ", regex=False) th["accuracy_ci"] = th.apply(lambda r: f"{100*r.accuracy:.0f} [{100*r.acc_lo:.0f},{100*r.acc_hi:.0f}]", axis=1) th["retention_ci"] = th.apply(lambda r: f"{100*r.retention:.0f} [{100*r.ret_lo:.0f},{100*r.ret_hi:.0f}]", axis=1) out = th[["chain_s", "handoffs", "accuracy_ci", "retention_ci", "unsupported_mean", "msg_bytes_mean", "fetched_bytes_mean"]].rename(columns={"chain_s": "chain", "accuracy_ci": "accuracy (\\%)", "retention_ci": "retained facts (\\%)", "unsupported_mean": "unsupported/msg", "msg_bytes_mean": "message B", "fetched_bytes_mean": "fetched B"}) df_to_tex(out, OUT / "table_handoffs.tex", "llrrrrr", "{:,.2f}") ff = TAB / "E5_core_fanin.csv" if ff.exists(): tf = pd.read_csv(ff) tf["cell"] = tf.apply(lambda r: f"{100*r.quality:.0f} [{100*r.lo:.0f},{100*r.hi:.0f}]", axis=1) tf["method"] = tf["condition"].str.replace("|k=", " k=", regex=False).str.replace("cache_concat_negative_control", "cache concat (neg. control)", regex=False).str.replace("_", " ", regex=False) piv = tf.pivot_table(index="method", columns="qtype", values="cell", aggfunc="first").reset_index().fillna("--") df_to_tex(piv, OUT / "table_fanin.tex", "l" + "r" * (len(piv.columns) - 1)) h = TAB / "ED_hosted.csv" if h.exists(): th = pd.read_csv(h) th["cond"] = th["condition"].str.replace(r"\|(anthropic|openai)\|", "|", regex=True).str.replace("|budget_tokens=512", " (512)", regex=False).str.replace("|max_fetch_bytes=2000|max_lookups=6", "", regex=False).str.replace("|provider_cache=True", " +cache", regex=False).str.rstrip("|") th["model"] = th["provider_model"].str.replace("-20251001", "").str.replace("-2025-08-07", "") th["quality_ci"] = th.apply(lambda r: f"{r.quality:.2f} [{r.lo:.2f}, {r.hi:.2f}]", axis=1) th["cost_musd"] = (th["cost_usd_mean"] * 1e3).round(2) out = th[["model", "cond", "family", "n", "quality_ci", "cost_musd", "ttft_ms_p50", "total_ms_p50"]].rename(columns={"cond": "condition", "cost_musd": "cost (m\\$)", "ttft_ms_p50": "TTFT p50 (ms)", "total_ms_p50": "total p50 (ms)"}) out["family"] = out["family"].str.replace("public_", "") df_to_tex(out.sort_values(["model", "family", "condition"]), OUT / "table_hosted.tex", "llllrrrr", "{:,.0f}") fq = TAB / "E1_core_by_question_type.csv" if fq.exists(): tq = pd.read_csv(fq); tq["cell"] = tq.apply(lambda r: f"{100*r.quality:.0f} [{100*r.lo:.0f},{100*r.hi:.0f}]", axis=1) tq["cond"] = tq["condition"].str.replace("budget_tokens=512", "512", regex=False).str.replace("keep_frac=0.25", "1/4", regex=False).str.replace("|max_fetch_bytes=2000|max_lookups=6", "", regex=False).str.replace("policy=attention", "att", regex=False).str.replace("codec=zstd", "zstd", regex=False).str.replace("|", " ", regex=False) piv = tq.pivot_table(index="cond", columns="qtype", values="cell", aggfunc="first").reset_index().rename(columns={"cond": "condition"}).fillna("--") df_to_tex(piv, OUT / "table_qtype_core.tex", "l" + "r" * (len(piv.columns) - 1)) ff = TAB / "E1_core_failures.csv" if ff.exists(): tf = pd.read_csv(ff); df_to_tex(tf.rename(columns={tf.columns[0]: "method"}), OUT / "table_failures.tex", "l" + "r" * (len(tf.columns) - 1), "{:.0f}") f7 = TAB / "E7_qtypes.csv" if f7.exists(): t7 = pd.read_csv(f7); t7["cell"] = t7.apply(lambda r: f"{100*r.quality:.0f} [{100*r.lo:.0f},{100*r.hi:.0f}]", axis=1) t7["cond"] = t7["condition"].str.replace("budget_tokens=512", "512", regex=False).str.replace("keep_frac=0.25", "1/4", regex=False).str.replace("|", " ", regex=False) + " " + t7["pair"] piv = t7.pivot_table(index="cond", columns="qtype", values="cell", aggfunc="first").reset_index().rename(columns={"cond": "condition / pair"}).fillna("--") df_to_tex(piv, OUT / "table_qtypes_exploratory.tex", "l" + "r" * (len(piv.columns) - 1)) f3 = TAB / "E3_core_transfer.csv" if f3.exists(): t3 = pd.read_csv(f3) t3["quality_ci"] = t3.apply(lambda r: f"{100*r.quality:.0f} [{100*r.lo:.0f},{100*r.hi:.0f}]", axis=1) cols = ["family", "condition", "n", "quality_ci", "unknown_share"] + [c for c in ["q_single_fact", "q_exact_copy", "q_multi_hop", "q_negation", "q_exec"] if c in t3.columns] + ["payload_bytes_median", "warm_ms_p50"] out = t3[cols].rename(columns={"quality_ci": "accuracy (\\%)", "unknown_share": "`unknown' share", "payload_bytes_median": "payload B", "warm_ms_p50": "warm ms"}) out.columns = [c.replace("q_", "") for c in out.columns] df_to_tex(out.sort_values(["family", "condition"]), OUT / "table_transfer.tex", "ll" + "r" * (len(out.columns) - 2), "{:,.2f}") print("latex tables written") if __name__ == "__main__": main()