File size: 13,572 Bytes
9a67fbe |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 |
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Parse *_NB_single_dataset.txt reports and emit LaTeX tables.
Now supports:
• Full "everything" longtable (as before)
• Option B: TWO COMPACT portrait tables (RMSE table + MAE table)
Examples
--------
# Make compact + full tables for all reports in a dir
python3 make_nb_tables_from_reports.py --in ./plotting --out ./plotting/tex
# Only compact tables
python3 make_nb_tables_from_reports.py --in ./plotting --out ./plotting/tex --compact_only
# Specific files
python3 make_nb_tables_from_reports.py \
--files ./plotting/qm9_NB_single_dataset.txt ./plotting/boilingpoint_NB_single_dataset.txt \
--out ./plotting/tex --compact_only
"""
import argparse
import re
from pathlib import Path
from typing import Dict, List, Tuple
import pandas as pd
NUM = r"[-+]?(?:\d+(?:\.\d*)?|\.\d+)(?:[eE][-+]?\d+)?"
def esc_tex(s: str) -> str:
return s.replace("_", r"\_")
def parse_header(lines: List[str]) -> Dict[str, str]:
out = {}
for ln in lines[:60]:
m = re.search(r"^Dataset:\s*(.+?)\s+—", ln)
if m:
out["dataset"] = m.group(1).strip()
m = re.search(r"^Control exp_id:\s*(.+)$", ln)
if m:
out["control"] = m.group(1).strip()
m = re.search(r"^k folds:\s*(\d+),\s*alpha:\s*(" + NUM + r")", ln)
if m:
out["k"] = int(m.group(1))
out["alpha"] = float(m.group(2))
return out
def find_block(lines: List[str], start_pat: str) -> Tuple[int, int]:
start = -1
pat = re.compile(start_pat)
for i, ln in enumerate(lines):
if pat.search(ln):
start = i
break
if start < 0:
return (-1, -1)
end = len(lines)
for j in range(start + 1, len(lines)):
if not lines[j].strip():
end = j
break
return (start, end)
def parse_nb_block(lines: List[str]) -> pd.DataFrame:
hdr_idx = None
for i, ln in enumerate(lines):
if re.search(r"^\s*comparison\s+", ln) and "mean_diff_RMSE" in ln:
hdr_idx = i
break
if hdr_idx is None:
return pd.DataFrame()
data_lines = []
for ln in lines[hdr_idx + 1 :]:
s = ln.rstrip()
if not s:
break
if set(s) <= set("- "):
continue
data_lines.append(s)
rows = []
for ln in data_lines:
# Flexible parse: "comparison num num ... (10 nums total)"
m = re.match(r"^\s*(.+?)\s+(" + NUM + r"(?:\s+" + NUM + r"){9})\s*$", ln)
if not m:
parts = re.split(r"\s{2,}", ln.strip())
if len(parts) < 11:
continue
comp, *nums = parts
else:
comp = m.group(1).strip()
nums = re.split(r"\s+", m.group(2).strip())
if len(nums) != 10:
continue
vals = list(map(float, nums))
rows.append(
{
"comparison": comp,
"mean_diff_RMSE": vals[0],
"t_NB_RMSE": vals[1],
"p_raw_RMSE": vals[2],
"mean_diff_MAE": vals[3],
"t_NB_MAE": vals[4],
"p_raw_MAE": vals[5],
"CI_RMSE_low": vals[6],
"CI_RMSE_high": vals[7],
"CI_MAE_low": vals[8],
"CI_MAE_high": vals[9],
}
)
return pd.DataFrame(rows)
def parse_holm_block(lines: List[str], metric_label: str) -> pd.DataFrame:
hdr = None
for i, ln in enumerate(lines):
if re.search(r"^\s*comparison\s+", ln) and "p_raw" in ln and "p_holm" in ln:
hdr = i
break
if hdr is None:
return pd.DataFrame()
data_lines = []
for ln in lines[hdr + 1 :]:
s = ln.rstrip()
if not s:
break
if set(s) <= set("- "):
continue
data_lines.append(s)
rows = []
for ln in data_lines:
m = re.match(r"^\s*(.+?)\s+(" + NUM + r")\s+(" + NUM + r")\s+(\w+)\s*$", ln)
if m:
comp = m.group(1).strip()
p_raw = float(m.group(2)) # not used here, but parsed ok
p_holm = float(m.group(3))
sig = m.group(4).strip().lower().startswith("t")
else:
parts = re.split(r"\s{2,}", ln.strip())
if len(parts) < 4:
continue
comp = parts[0].strip()
p_holm = float(parts[2])
sig = parts[3].strip().lower().startswith("t")
rows.append(
{
"comparison": comp,
f"p_holm_{metric_label}": p_holm,
f"sig_{metric_label}": sig,
}
)
return pd.DataFrame(rows)
# ---------- FULL longtable (unchanged behavior) ----------
def write_full_longtable(meta: Dict[str, str], df: pd.DataFrame, outpath: Path):
dataset = meta.get("dataset", "dataset")
control = meta.get("control", "control")
k = int(meta.get("k", 5))
alpha = float(meta.get("alpha", 0.05))
header = rf"""
% Auto-generated from NB_single_dataset report
% Requires: \usepackage{{booktabs,longtable,siunitx}}
\begin{{longtable}}{{@{{}}l
S S S S S S S
S
S S S S S S S
@{{}}}}
\caption{{Full NB-corrected one-sided tests (outer folds, $K={k}$, $\alpha={alpha}$) comparing control \texttt{{{esc_tex(control)}}} against each competitor on dataset \texttt{{{esc_tex(dataset)}}}.
Positive $\Delta$ (competitor $-$ control) favors the control (lower loss).
One-sided alternative $\mathbb{{E}}[\bar d^{{(L)}}] > 0$.
Holm step-down controls FWER per metric family (RMSE and MAE reported separately).
CIs are NB-style two-sided.}}
\label{{tab:{esc_tex(dataset)}_nb_full}}\\
\toprule
& \multicolumn{{7}}{{c}}{{\textbf{{RMSE family}}}} & &
\multicolumn{{7}}{{c}}{{\textbf{{MAE family}}}}\\
\cmidrule(lr){{2-8}}\cmidrule(lr){{10-16}}
Comparison
& {{\(\Delta\)RMSE}}
& {{$t_{{\text{{NB}}}}$}}
& {{p (raw)}}
& {{p\(_{{\text{{Holm}}}}\)}}
& {{\(\text{{Sig}}\)}}
& {{CI\(_{{\text{{low}}}}\)}}
& {{CI\(_{{\text{{high}}}}\)}}
& {{}} % spacer
& {{\(\Delta\)MAE}}
& {{$t_{{\text{{NB}}}}$}}
& {{p (raw)}}
& {{p\(_{{\text{{Holm}}}}\)}}
& {{\(\text{{Sig}}\)}}
& {{CI\(_{{\text{{low}}}}\)}}
& {{CI\(_{{\text{{high}}}}\)}}\\
\midrule
\endfirsthead
\toprule
& \multicolumn{{7}}{{c}}{{\textbf{{RMSE family}}}} & &
\multicolumn{{7}}{{c}}{{\textbf{{MAE family}}}}\\
\cmidrule(lr){{2-8}}\cmidrule(lr){{10-16}}
Comparison
& {{\(\Delta\)RMSE}}
& {{$t_{{\text{{NB}}}}$}}
& {{p (raw)}}
& {{p\(_{{\text{{Holm}}}}\)}}
& {{\(\text{{Sig}}\)}}
& {{CI\(_{{\text{{low}}}}\)}}
& {{CI\(_{{\text{{high}}}}\)}}
& {{}} % spacer
& {{\(\Delta\)MAE}}
& {{$t_{{\text{{NB}}}}$}}
& {{p (raw)}}
& {{p\(_{{\text{{Holm}}}}\)}}
& {{\(\text{{Sig}}\)}}
& {{CI\(_{{\text{{low}}}}\)}}
& {{CI\(_{{\text{{high}}}}\)}}\\
\midrule
\endhead
\midrule
\multicolumn{{16}}{{r}}{{\emph{{Continued on next page}}}}\\
\midrule
\endfoot
\bottomrule
\endlastfoot
"""
lines = []
for _, r in df.iterrows():
lines.append(
f"{esc_tex(r['comparison'])} & "
f"{r['mean_diff_RMSE']:.6f} & {r['t_NB_RMSE']:.6f} & {r['p_raw_RMSE']:.6f} & {r['p_holm_RMSE']:.6f} & {1 if r['sig_RMSE'] else 0} & {r['CI_RMSE_low']:.6f} & {r['CI_RMSE_high']:.6f} & & "
f"{r['mean_diff_MAE']:.6f} & {r['t_NB_MAE']:.6f} & {r['p_raw_MAE']:.6f} & {r['p_holm_MAE']:.6f} & {1 if r['sig_MAE'] else 0} & {r['CI_MAE_low']:.6f} & {r['CI_MAE_high']:.6f} \\\\"
)
outpath.write_text(
header + "\n".join(lines) + "\n\\end{longtable}\n", encoding="utf-8"
)
# ---------- COMPACT portrait tables (Option B) ----------
def write_compact_tables(meta: Dict[str, str], df: pd.DataFrame, outdir: Path):
"""
Produce two small portrait tables:
• <dataset>_nb_compact_rmse.tex
• <dataset>_nb_compact_mae.tex
Columns (per metric): Comparison, Δ, t_NB, CI_low, CI_high, p_Holm
"""
dataset = meta.get("dataset", "dataset")
control = meta.get("control", "control")
k = int(meta.get("k", 5))
common_preamble = r"""
% Auto-generated compact NB tables
% Requires in preamble: \usepackage{booktabs,tabularx,siunitx}
% \sisetup{round-mode=places,round-precision=3,scientific-notation=true}
"""
# RMSE table
rmse_header = rf"""{common_preamble}
\begin{{table}}[t]
\centering
\small
\setlength{{\tabcolsep}}{{4pt}}
\caption{{RMSE: NB-corrected one-sided tests (outer folds, $K={k}$) on dataset \texttt{{{esc_tex(dataset)}}}; control \texttt{{{esc_tex(control)}}}.
Positive $\Delta$ (competitor $-$ control) favors control. Holm controls FWER.}}
\label{{tab:{esc_tex(dataset)}_nb_compact_rmse}}
\begin{{tabularx}}{{\linewidth}}{{@{{}}l S S S S S@{{}}}}
\toprule
Comparison & {{\(\Delta\)RMSE}} & {{$t_{{\text{{NB}}}}$}} & {{CI\(_{{\text{{low}}}}\)}} & {{CI\(_{{\text{{high}}}}\)}} & {{p\(_{{\text{{Holm}}}}\)}}\\
\midrule
"""
rmse_rows = []
for _, r in df.sort_values("p_holm_RMSE").iterrows():
rmse_rows.append(
f"{esc_tex(r['comparison'])} & "
f"{r['mean_diff_RMSE']:.3f} & {r['t_NB_RMSE']:.3f} & "
f"{r['CI_RMSE_low']:.3f} & {r['CI_RMSE_high']:.3f} & {r['p_holm_RMSE']:.3g} \\\\"
)
rmse_tex = (
rmse_header
+ "\n".join(rmse_rows)
+ "\n\\bottomrule\n\\end{tabularx}\n\\end{table}\n"
)
(outdir / f"{dataset}_nb_compact_rmse.tex").write_text(rmse_tex, encoding="utf-8")
# MAE table
mae_header = rf"""{common_preamble}
\begin{{table}}[t]
\centering
\small
\setlength{{\tabcolsep}}{{4pt}}
\caption{{MAE: NB-corrected one-sided tests (outer folds, $K={k}$) on dataset \texttt{{{esc_tex(dataset)}}}; control \texttt{{{esc_tex(control)}}}.
Positive $\Delta$ (competitor $-$ control) favors control. Holm controls FWER.}}
\label{{tab:{esc_tex(dataset)}_nb_compact_mae}}
\begin{{tabularx}}{{\linewidth}}{{@{{}}l S S S S S@{{}}}}
\toprule
Comparison & {{\(\Delta\)MAE}} & {{$t_{{\text{{NB}}}}$}} & {{CI\(_{{\text{{low}}}}\)}} & {{CI\(_{{\text{{high}}}}\)}} & {{p\(_{{\text{{Holm}}}}\)}}\\
\midrule
"""
mae_rows = []
for _, r in df.sort_values("p_holm_MAE").iterrows():
mae_rows.append(
f"{esc_tex(r['comparison'])} & "
f"{r['mean_diff_MAE']:.3f} & {r['t_NB_MAE']:.3f} & "
f"{r['CI_MAE_low']:.3f} & {r['CI_MAE_high']:.3f} & {r['p_holm_MAE']:.3g} \\\\"
)
mae_tex = (
mae_header
+ "\n".join(mae_rows)
+ "\n\\bottomrule\n\\end{tabularx}\n\\end{table}\n"
)
(outdir / f"{dataset}_nb_compact_mae.tex").write_text(mae_tex, encoding="utf-8")
def process_file(path: Path, outdir: Path, make_full: bool, make_compact: bool):
txt = path.read_text(encoding="utf-8", errors="ignore").splitlines()
meta = parse_header(txt)
# NB block
nb_start, nb_end = find_block(
txt, r"^\s*--- NB-corrected t \(outer folds\) per competitor ---"
)
if nb_start < 0:
raise RuntimeError(f"NB block not found in {path}")
df_nb = parse_nb_block(txt[nb_start:nb_end])
if df_nb.empty:
raise RuntimeError(f"NB table parsing failed in {path}")
# Holm blocks
hr_start, hr_end = find_block(
txt, r"^\s*--- Holm-adjusted p-values \(RMSE family\)\s*---"
)
hm_start, hm_end = find_block(
txt, r"^\s*--- Holm-adjusted p-values \(MAE family\)\s*---"
)
if hr_start < 0 or hm_start < 0:
raise RuntimeError(f"Holm blocks not found in {path}")
df_hr = parse_holm_block(txt[hr_start:hr_end], "RMSE")
df_hm = parse_holm_block(txt[hm_start:hm_end], "MAE")
# Merge Holm into NB
df = df_nb.merge(df_hr, on="comparison", how="left").merge(
df_hm, on="comparison", how="left"
)
# Write outputs
dataset = meta.get("dataset", path.stem.replace("_NB_single_dataset", ""))
outdir.mkdir(parents=True, exist_ok=True)
if make_full:
full_path = outdir / f"{dataset}_nb_full_summary_from_report.tex"
# order for full table: by raw p (RMSE then MAE)
df_full = df.sort_values(["p_raw_RMSE", "p_raw_MAE"]).reset_index(drop=True)
write_full_longtable(meta, df_full, full_path)
if make_compact:
# For compact tables we sort within each family by Holm p
write_compact_tables(meta, df, outdir)
print(
f"Processed: {path.name} -> {dataset} (full={make_full}, compact={make_compact})"
)
def main():
ap = argparse.ArgumentParser()
ap.add_argument(
"--in",
dest="indir",
type=str,
default=None,
help="Directory containing *_NB_single_dataset.txt files.",
)
ap.add_argument(
"--files", nargs="*", default=None, help="Explicit list of report files."
)
ap.add_argument(
"--out",
dest="outdir",
type=str,
required=True,
help="Output directory for .tex tables.",
)
ap.add_argument(
"--compact_only",
action="store_true",
help="Only write the compact RMSE/MAE tables (skip the full longtable).",
)
args = ap.parse_args()
outdir = Path(args.outdir)
if args.files:
files = [Path(f) for f in args.files]
elif args.indir:
files = sorted(Path(args.indir).glob("*_NB_single_dataset.txt"))
else:
raise SystemExit("Provide either --in DIR or --files file1 file2 ...")
if not files:
raise SystemExit("No *_NB_single_dataset.txt files found.")
for f in files:
process_file(f, outdir, make_full=not args.compact_only, make_compact=True)
if __name__ == "__main__":
main()
|