File size: 20,124 Bytes
ac94d57
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
# plot_report_figures.py
# ------------------------------------------------------------
# Plot + save figures to answer:
# 1) Training prompt info entropy (HNO1/2/3) effect
# 2) Eval set hardness differences (P/R/A templates + original)
# 3) Label context structure (0-shot vs CoT vs Fake CoT)
# 4) Training duration / steps (scaling curves)
#
# Assumptions from your pipeline:
# - Each config dir /workspace/v121rc_exp1/{A..I} contains many "*_results.json"
# - Each "*_results.json" is a list of entries with keys: system, prompt, gold_label, gold_output,
#   and per-checkpoint keys like "step_1000", "step_2000", ... each containing "accuracy" field (0/1).
# - Eval files are named like:
#   "..._eval_wo_reasoning.json" (original trimmed)
#   "..._eval_wo_reasoning_P1.json" ... P5
#   "..._eval_wo_reasoning_R1.json" ... R3
#   "..._eval_wo_reasoning_A1.json" ... A4
#
# Output:
# - Saves a set of PNG (and PDF) figures under --out_dir (default: /workspace/v121rc_exp1/FIGS)
#
# Usage:
#   python plot_report_figures.py \
#       --base_dir /workspace/v121rc_exp1 \
#       --out_dir  /workspace/v121rc_exp1/FIGS
# ------------------------------------------------------------

import argparse
import glob
import json
import os
import re
from dataclasses import dataclass
from typing import Dict, List, Optional, Tuple

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt


# -------------------------
# Config mapping (per your table)
# -------------------------
CONFIG_META = {
    "A": {"hno": "HNO3", "variant": "0-shot"},
    "B": {"hno": "HNO3", "variant": "CoT"},
    "C": {"hno": "HNO3", "variant": "Fake CoT"},
    "D": {"hno": "HNO2", "variant": "0-shot"},
    "E": {"hno": "HNO2", "variant": "CoT"},
    "F": {"hno": "HNO2", "variant": "Fake CoT"},
    "G": {"hno": "HNO1", "variant": "0-shot"},
    "H": {"hno": "HNO1", "variant": "CoT"},
    "I": {"hno": "HNO1", "variant": "Fake CoT"},
}

EVAL_TYPE_ORDER = [
    "Original",
    "Paraphrase P1",
    "Paraphrase P2",
    "Paraphrase P3",
    "Paraphrase P4",
    "Paraphrase P5",
    "Reverse R1",
    "Reverse R2",
    "Reverse R3",
    "Aggregate A1",
    "Aggregate A2",
    "Aggregate A3",
    "Aggregate A4",
]

# Regex to parse eval type from filename
RE_P = re.compile(r"_P([1-5])(?:\.json|_results\.json)$")
RE_R = re.compile(r"_R([1-3])(?:\.json|_results\.json)$")
RE_A = re.compile(r"_A([1-4])(?:\.json|_results\.json)$")


def infer_eval_type_from_filename(fn: str) -> str:
    base = os.path.basename(fn)
    m = RE_P.search(base)
    if m:
        return f"Paraphrase P{m.group(1)}"
    m = RE_R.search(base)
    if m:
        return f"Reverse R{m.group(1)}"
    m = RE_A.search(base)
    if m:
        return f"Aggregate A{m.group(1)}"
    # If none of P/R/A, treat as original trimmed eval set
    return "Original"


def safe_read_json(path: str):
    try:
        with open(path, "r", encoding="utf-8") as f:
            return json.load(f)
    except Exception:
        return None


def list_result_files(config_dir: str) -> List[str]:
    # Your output naming: <eval_file>_results.json
    return sorted(glob.glob(os.path.join(config_dir, "*_results.json")))


def extract_steps_from_one_entry(entry: dict) -> List[int]:
    steps = []
    for k in entry.keys():
        if k.startswith("step_"):
            try:
                steps.append(int(k.split("_", 1)[1]))
            except Exception:
                pass
    return sorted(set(steps))


def summarize_results_file(path: str) -> Optional[pd.DataFrame]:
    """
    Return a dataframe with columns: step, accuracy_mean, n
    computed from entry["step_<s>"]["accuracy"] for all entries.
    """
    data = safe_read_json(path)
    if not isinstance(data, list) or len(data) == 0:
        return None

    steps = extract_steps_from_one_entry(data[0])
    if not steps:
        # In case first entry is missing some keys, scan a few
        for e in data[:50]:
            steps = extract_steps_from_one_entry(e)
            if steps:
                break
    if not steps:
        return None

    rows = []
    for s in steps:
        k = f"step_{s}"
        accs = []
        for e in data:
            v = e.get(k) or {}
            a = v.get("accuracy", None)
            if isinstance(a, (int, float)):
                accs.append(float(a))
        if len(accs) == 0:
            continue
        rows.append(
            {
                "step": s,
                "accuracy_mean": float(np.mean(accs)),
                "n": int(len(accs)),
            }
        )

    if not rows:
        return None
    return pd.DataFrame(rows).sort_values("step").reset_index(drop=True)


def build_long_dataframe(base_dir: str, configs: List[str]) -> pd.DataFrame:
    """
    Build long-form df:
      config, hno, variant, eval_file, eval_type, step, accuracy, n
    """
    all_rows = []

    for cfg in configs:
        config_dir = os.path.join(base_dir, cfg)
        if not os.path.isdir(config_dir):
            continue

        meta = CONFIG_META.get(cfg, {"hno": "UNKNOWN", "variant": "UNKNOWN"})
        files = list_result_files(config_dir)

        for fpath in files:
            eval_type = infer_eval_type_from_filename(fpath)
            summary = summarize_results_file(fpath)
            if summary is None:
                continue

            eval_file = os.path.basename(fpath).replace("_results.json", ".json")

            for _, r in summary.iterrows():
                all_rows.append(
                    {
                        "config": cfg,
                        "hno": meta["hno"],
                        "variant": meta["variant"],
                        "eval_file": eval_file,
                        "eval_type": eval_type,
                        "step": int(r["step"]),
                        "accuracy": float(r["accuracy_mean"]),
                        "n": int(r["n"]),
                    }
                )

    df = pd.DataFrame(all_rows)
    if df.empty:
        return df

    # Categorical ordering for nicer plots
    df["eval_type"] = pd.Categorical(df["eval_type"], categories=EVAL_TYPE_ORDER, ordered=True)

    # Optional: also add a "task" column (HNO1/2/3) is already "hno"
    return df.sort_values(["hno", "variant", "config", "eval_type", "step"]).reset_index(drop=True)


def ensure_dir(path: str) -> None:
    os.makedirs(path, exist_ok=True)


def save_fig(fig: plt.Figure, out_dir: str, name: str) -> None:
    ensure_dir(out_dir)
    png = os.path.join(out_dir, f"{name}.png")
    pdf = os.path.join(out_dir, f"{name}.pdf")
    fig.savefig(png, dpi=200, bbox_inches="tight")
    fig.savefig(pdf, bbox_inches="tight")
    plt.close(fig)


def pick_final_step(df: pd.DataFrame) -> int:
    # prefer 10000 if present; otherwise max
    steps = sorted(df["step"].unique().tolist())
    if not steps:
        return 0
    if 10000 in steps:
        return 10000
    return steps[-1]


# -------------------------
# Figure builders
# -------------------------
def fig_scaling_curves_overall(df: pd.DataFrame, out_dir: str) -> None:
    """
    Q4: Accuracy vs step (scaling) for each config, averaged over all eval files/types.
    """
    if df.empty:
        return

    # average over eval files/types within each (config, step)
    g = (
        df.groupby(["config", "hno", "variant", "step"], as_index=False)["accuracy"]
        .mean()
        .rename(columns={"accuracy": "acc_mean_overall"})
    )

    # One panel per HNO to keep readable
    for hno in ["HNO1", "HNO2", "HNO3"]:
        gh = g[g["hno"] == hno].copy()
        if gh.empty:
            continue

        fig = plt.figure()
        ax = fig.add_subplot(1, 1, 1)
        for cfg, sub in gh.groupby("config"):
            sub = sub.sort_values("step")
            ax.plot(sub["step"], sub["acc_mean_overall"], marker="o", linewidth=1, label=f"{cfg} ({CONFIG_META[cfg]['variant']})")

        ax.set_title(f"Scaling (Accuracy vs Steps) β€” {hno} β€” Mean over all eval sets")
        ax.set_xlabel("Training step (checkpoint)")
        ax.set_ylabel("Accuracy")
        ax.set_ylim(0.0, 1.0)
        ax.grid(True, linewidth=0.5, alpha=0.5)
        ax.legend(loc="lower right", fontsize=8)

        save_fig(fig, out_dir, f"Q4_scaling_curves_overall_{hno}")


def fig_scaling_curves_by_eval_type(df: pd.DataFrame, out_dir: str) -> None:
    """
    Q2/Q4: Accuracy vs step, separated by eval_type (hardness differences show up as gaps).
    Produces one figure per config (may be many, but comprehensive).
    """
    if df.empty:
        return

    for cfg in sorted(df["config"].unique().tolist()):
        sub = df[df["config"] == cfg].copy()
        if sub.empty:
            continue

        fig = plt.figure(figsize=(10, 6))
        ax = fig.add_subplot(1, 1, 1)

        for et, etdf in sub.groupby("eval_type"):
            etdf = etdf.groupby("step", as_index=False)["accuracy"].mean().sort_values("step")
            ax.plot(etdf["step"], etdf["accuracy"], marker="o", linewidth=1, label=str(et))

        meta = CONFIG_META.get(cfg, {})
        ax.set_title(f"Scaling by Eval Set β€” Config {cfg} ({meta.get('hno','?')}, {meta.get('variant','?')})")
        ax.set_xlabel("Training step (checkpoint)")
        ax.set_ylabel("Accuracy")
        ax.set_ylim(0.0, 1.0)
        ax.grid(True, linewidth=0.5, alpha=0.5)
        ax.legend(loc="lower right", fontsize=8, ncol=2)

        save_fig(fig, out_dir, f"Q2Q4_scaling_by_evaltype_config_{cfg}")


def fig_entropy_effect_final(df: pd.DataFrame, out_dir: str) -> None:
    """
    Q1: Compare HNO1 vs HNO2 vs HNO3 at final step, controlling for variant (0-shot/CoT/Fake CoT).
    We plot:
      - Final accuracy on Original eval
      - Final accuracy averaged over all eval types
    """
    if df.empty:
        return

    final_step = pick_final_step(df)

    # Final, ORIGINAL only
    d1 = df[(df["step"] == final_step) & (df["eval_type"] == "Original")].copy()
    if not d1.empty:
        g1 = d1.groupby(["hno", "variant"], as_index=False)["accuracy"].mean()

        fig = plt.figure()
        ax = fig.add_subplot(1, 1, 1)

        # grouped bars: x=hno, multiple variants
        hnos = ["HNO1", "HNO2", "HNO3"]
        variants = ["0-shot", "CoT", "Fake CoT"]
        x = np.arange(len(hnos))
        width = 0.25

        for j, v in enumerate(variants):
            vals = []
            for h in hnos:
                m = g1[(g1["hno"] == h) & (g1["variant"] == v)]
                vals.append(float(m["accuracy"].iloc[0]) if len(m) else np.nan)
            ax.bar(x + (j - 1) * width, vals, width=width, label=v)

        ax.set_title(f"Q1 Entropy Effect β€” Final step={final_step} β€” Original eval only")
        ax.set_xlabel("Training entropy level (HNO)")
        ax.set_ylabel("Accuracy")
        ax.set_xticks(x)
        ax.set_xticklabels(hnos)
        ax.set_ylim(0.0, 1.0)
        ax.grid(True, axis="y", linewidth=0.5, alpha=0.5)
        ax.legend(loc="lower right", fontsize=9)

        save_fig(fig, out_dir, f"Q1_entropy_effect_finalstep_{final_step}_original")

    # Final, mean over ALL eval types
    d2 = df[df["step"] == final_step].copy()
    if not d2.empty:
        g2 = d2.groupby(["config", "hno", "variant"], as_index=False)["accuracy"].mean()
        g2 = g2.groupby(["hno", "variant"], as_index=False)["accuracy"].mean()

        fig = plt.figure()
        ax = fig.add_subplot(1, 1, 1)

        hnos = ["HNO1", "HNO2", "HNO3"]
        variants = ["0-shot", "CoT", "Fake CoT"]
        x = np.arange(len(hnos))
        width = 0.25

        for j, v in enumerate(variants):
            vals = []
            for h in hnos:
                m = g2[(g2["hno"] == h) & (g2["variant"] == v)]
                vals.append(float(m["accuracy"].iloc[0]) if len(m) else np.nan)
            ax.bar(x + (j - 1) * width, vals, width=width, label=v)

        ax.set_title(f"Q1 Entropy Effect β€” Final step={final_step} β€” Mean over all eval sets")
        ax.set_xlabel("Training entropy level (HNO)")
        ax.set_ylabel("Accuracy")
        ax.set_xticks(x)
        ax.set_xticklabels(hnos)
        ax.set_ylim(0.0, 1.0)
        ax.grid(True, axis="y", linewidth=0.5, alpha=0.5)
        ax.legend(loc="lower right", fontsize=9)

        save_fig(fig, out_dir, f"Q1_entropy_effect_finalstep_{final_step}_overall")


def fig_label_structure_effect(df: pd.DataFrame, out_dir: str) -> None:
    """
    Q3: Compare (0-shot vs CoT vs Fake CoT) within each HNO level across steps.
    Use mean over eval types to avoid 12-line clutter.
    """
    if df.empty:
        return

    g = df.groupby(["hno", "variant", "step"], as_index=False)["accuracy"].mean()

    for hno in ["HNO1", "HNO2", "HNO3"]:
        sub = g[g["hno"] == hno].copy()
        if sub.empty:
            continue

        fig = plt.figure()
        ax = fig.add_subplot(1, 1, 1)

        for v, vdf in sub.groupby("variant"):
            vdf = vdf.sort_values("step")
            ax.plot(vdf["step"], vdf["accuracy"], marker="o", linewidth=1, label=v)

        ax.set_title(f"Q3 Label Context Structure β€” {hno} β€” Mean over all eval sets")
        ax.set_xlabel("Training step (checkpoint)")
        ax.set_ylabel("Accuracy")
        ax.set_ylim(0.0, 1.0)
        ax.grid(True, linewidth=0.5, alpha=0.5)
        ax.legend(loc="lower right", fontsize=9)

        save_fig(fig, out_dir, f"Q3_label_structure_over_steps_{hno}")


def fig_eval_hardness_final(df: pd.DataFrame, out_dir: str) -> None:
    """
    Q2: "Hardness" by eval set type at final step:
      - Average across all configs (global hardness)
      - Also per HNO level (since training data differs)
    """
    if df.empty:
        return

    final_step = pick_final_step(df)
    d = df[df["step"] == final_step].copy()
    if d.empty:
        return

    # Global mean over configs
    g_all = d.groupby(["eval_type"], as_index=False)["accuracy"].mean()
    g_all = g_all.sort_values("eval_type")

    fig = plt.figure(figsize=(11, 5))
    ax = fig.add_subplot(1, 1, 1)
    x = np.arange(len(g_all))
    ax.bar(x, g_all["accuracy"].to_numpy())
    ax.set_title(f"Q2 Eval Hardness β€” Final step={final_step} β€” Mean across ALL configs")
    ax.set_xlabel("Eval set type")
    ax.set_ylabel("Accuracy")
    ax.set_ylim(0.0, 1.0)
    ax.set_xticks(x)
    ax.set_xticklabels([str(v) for v in g_all["eval_type"].tolist()], rotation=35, ha="right")
    ax.grid(True, axis="y", linewidth=0.5, alpha=0.5)

    save_fig(fig, out_dir, f"Q2_eval_hardness_finalstep_{final_step}_allconfigs")

    # Per HNO
    for hno in ["HNO1", "HNO2", "HNO3"]:
        dh = d[d["hno"] == hno].copy()
        if dh.empty:
            continue
        gh = dh.groupby(["eval_type"], as_index=False)["accuracy"].mean().sort_values("eval_type")

        fig = plt.figure(figsize=(11, 5))
        ax = fig.add_subplot(1, 1, 1)
        x = np.arange(len(gh))
        ax.bar(x, gh["accuracy"].to_numpy())
        ax.set_title(f"Q2 Eval Hardness β€” {hno} β€” Final step={final_step} β€” Mean across configs")
        ax.set_xlabel("Eval set type")
        ax.set_ylabel("Accuracy")
        ax.set_ylim(0.0, 1.0)
        ax.set_xticks(x)
        ax.set_xticklabels([str(v) for v in gh["eval_type"].tolist()], rotation=35, ha="right")
        ax.grid(True, axis="y", linewidth=0.5, alpha=0.5)

        save_fig(fig, out_dir, f"Q2_eval_hardness_finalstep_{final_step}_{hno}")


def fig_training_accuracy_proxy(df: pd.DataFrame, out_dir: str) -> None:
    """
    If you treat "Original" trimmed eval (from train distribution) as a proxy for "learning/training accuracy",
    plot it vs steps for each config, and also aggregate per HNO/variant.
    (If you have true train-set eval elsewhere, point the script at those results similarly.)
    """
    if df.empty:
        return

    d = df[df["eval_type"] == "Original"].copy()
    if d.empty:
        return

    # Per config curves
    fig = plt.figure(figsize=(10, 6))
    ax = fig.add_subplot(1, 1, 1)
    for cfg, sub in d.groupby("config"):
        sub = sub.groupby("step", as_index=False)["accuracy"].mean().sort_values("step")
        ax.plot(sub["step"], sub["accuracy"], marker="o", linewidth=1, label=cfg)
    ax.set_title("Training-Accuracy Proxy β€” Original eval only β€” per config")
    ax.set_xlabel("Training step (checkpoint)")
    ax.set_ylabel("Accuracy")
    ax.set_ylim(0.0, 1.0)
    ax.grid(True, linewidth=0.5, alpha=0.5)
    ax.legend(loc="lower right", fontsize=8, ncol=3)
    save_fig(fig, out_dir, "Training_accuracy_proxy_original_per_config")

    # By HNO & variant curves
    g = d.groupby(["hno", "variant", "step"], as_index=False)["accuracy"].mean()
    for hno in ["HNO1", "HNO2", "HNO3"]:
        sub = g[g["hno"] == hno].copy()
        if sub.empty:
            continue
        fig = plt.figure()
        ax = fig.add_subplot(1, 1, 1)
        for v, vdf in sub.groupby("variant"):
            vdf = vdf.sort_values("step")
            ax.plot(vdf["step"], vdf["accuracy"], marker="o", linewidth=1, label=v)
        ax.set_title(f"Training-Accuracy Proxy β€” {hno} β€” Original eval only")
        ax.set_xlabel("Training step (checkpoint)")
        ax.set_ylabel("Accuracy")
        ax.set_ylim(0.0, 1.0)
        ax.grid(True, linewidth=0.5, alpha=0.5)
        ax.legend(loc="lower right", fontsize=9)
        save_fig(fig, out_dir, f"Training_accuracy_proxy_original_{hno}")


def export_summary_tables(df: pd.DataFrame, out_dir: str) -> None:
    """
    Save a couple CSVs that are useful for the report:
      - long dataframe
      - final-step pivot tables
    """
    if df.empty:
        return
    ensure_dir(out_dir)

    long_csv = os.path.join(out_dir, "summary_long.csv")
    df.to_csv(long_csv, index=False)

    final_step = pick_final_step(df)
    d = df[df["step"] == final_step].copy()

    # Pivot: accuracy by (config x eval_type)
    pivot1 = (
        d.groupby(["config", "hno", "variant", "eval_type"], as_index=False)["accuracy"]
        .mean()
        .pivot_table(index=["config", "hno", "variant"], columns="eval_type", values="accuracy", aggfunc="mean")
    )
    pivot1.to_csv(os.path.join(out_dir, f"finalstep_{final_step}_pivot_config_by_evaltype.csv"))

    # Pivot: accuracy by (hno, variant) x eval_type
    pivot2 = (
        d.groupby(["hno", "variant", "eval_type"], as_index=False)["accuracy"]
        .mean()
        .pivot_table(index=["hno", "variant"], columns="eval_type", values="accuracy", aggfunc="mean")
    )
    pivot2.to_csv(os.path.join(out_dir, f"finalstep_{final_step}_pivot_hno_variant_by_evaltype.csv"))


def main():
    ap = argparse.ArgumentParser()
    ap.add_argument("--base_dir", type=str, default="/workspace/v121rc_exp1", help="Base exp dir containing A..I")
    ap.add_argument("--out_dir", type=str, default="/workspace/v121rc_exp1/FIGS", help="Where to save figures")
    ap.add_argument(
        "--configs",
        type=str,
        default="ABCDEFGHI",
        help="Which configs to include, e.g. ABC or ABCDEFGHI",
    )
    args = ap.parse_args()

    configs = [c for c in args.configs if c in CONFIG_META]
    if not configs:
        raise SystemExit("No valid configs selected. Use --configs like ABCDEFGHI.")

    df = build_long_dataframe(args.base_dir, configs)
    if df.empty:
        raise SystemExit(
            "No results found. Check that /workspace/v121rc_exp1/{A..I} contain '*_results.json' "
            "with 'step_<n>' fields."
        )

    ensure_dir(args.out_dir)

    # Save tables first (helps debugging/report)
    export_summary_tables(df, args.out_dir)

    # Core figures
    fig_training_accuracy_proxy(df, args.out_dir)      # Learning proxy
    fig_scaling_curves_overall(df, args.out_dir)       # Q4
    fig_label_structure_effect(df, args.out_dir)       # Q3
    fig_entropy_effect_final(df, args.out_dir)         # Q1
    fig_eval_hardness_final(df, args.out_dir)          # Q2

    # More comprehensive (lots of figs, but complete)
    fig_scaling_curves_by_eval_type(df, args.out_dir)  # Q2/Q4 per config

    print(f"Done. Figures + CSV summaries saved to: {args.out_dir}")


if __name__ == "__main__":
    main()