File size: 24,511 Bytes
4949db9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
#!/usr/bin/env python3
"""
Ablation study: Effect of prompt enhancement (adding physical law/phenomenon descriptions)
on VLM evaluation scores.

Compares backup (pre-enhancement) eval files with current (post-enhancement) eval files,
analyzing score deltas per video where the prompt actually changed.

Run from the anonymous root dir:
    python -m dataprocessing.analysis.ablation_prompt_enhancement
"""

import json
import os
import re
import sys
from collections import defaultdict
from pathlib import Path
from typing import Any, Dict, List, Optional, Tuple

# ---------------------------------------------------------------------------
# Configuration
# ---------------------------------------------------------------------------

DATA_ROOT = "data"
BACKUP_DIR = os.path.join(DATA_ROOT, "backup_before_laws_update")
VIDEOS_DIR = os.path.join(DATA_ROOT, "videos")

GENERAL_METRICS = ["SA", "PTV", "persistence"]
SCORE_BINS = [(0, 1), (1, 2), (2, 3), (3, 4), (4, 5)]

# Law-to-domain mapping
LAW_TO_DOMAIN = {
    # collision
    "collision": "collision",
    "impenetrability": "collision",
    "momentum_transfer": "collision",
    "momentum": "collision",
    "elastic_deformation": "collision",
    # gravity
    "gravity": "gravity",
    "free_fall": "gravity",
    "projectile_motion": "gravity",
    "buoyancy": "gravity",
    # fluid
    "fluid_continuity": "fluid",
    "flow_dynamics": "fluid",
    "flow": "fluid",
    "viscosity": "fluid",
    "surface_tension": "fluid",
    "pressure": "fluid",
    "continuity": "fluid",
    # temporal / motion
    "inertia": "temporal",
    "acceleration": "temporal",
    "velocity": "temporal",
    "displacement": "temporal",
    # lighting
    "reflection": "lighting",
    "refraction": "lighting",
    "light_absorption": "lighting",
    "shadow": "lighting",
    "illumination": "lighting",
    # deformation
    "deformation": "deformation",
    "plastic_deformation": "deformation",
    # material
    "material": "material",
    "rigidity": "material",
    "elasticity": "material",
    "phase_transition": "material",
    "melting": "material",
    "combustion": "material",
}

# ---------------------------------------------------------------------------
# Helpers
# ---------------------------------------------------------------------------


def parse_judge_key(judge_str: str) -> str:
    """Extract a short judge key like 'gemini', 'qwen', 'gpt' from judge field."""
    if not judge_str:
        return "unknown"
    return judge_str.split(":")[0]


def parse_filename_judge(filename: str) -> str:
    """Extract judge key from filename like eval_gemini_..., eval_qwen_permetric_..."""
    m = re.match(r"eval_(gemini|qwen|gpt)", filename)
    return m.group(1) if m else "unknown"


def is_permetric(filename: str) -> bool:
    return "permetric" in filename


def get_physical_avg(result: dict) -> Optional[float]:
    """Extract physical average from a result entry."""
    phys = result.get("physical")
    if phys is None:
        return None
    if isinstance(phys, (int, float)):
        return float(phys)
    if isinstance(phys, dict):
        avg = phys.get("avg")
        if avg is not None:
            return float(avg)
    return None


def get_per_law_scores(result: dict) -> Dict[str, float]:
    """Extract per-law scores from a result entry."""
    phys = result.get("physical")
    if not isinstance(phys, dict):
        return {}
    laws = phys.get("laws", {})
    out = {}
    for law_name, law_data in laws.items():
        if isinstance(law_data, dict) and "score" in law_data and law_data["score"] is not None:
            out[law_name] = float(law_data["score"])
        elif isinstance(law_data, (int, float)) and law_data is not None:
            out[law_name] = float(law_data)
    return out


def extract_dataset_from_video_dir(video_dir: str) -> str:
    """Extract dataset name from video_dir field, e.g. 'data/videos/cosmos-predict2.5-2b-wmb/' -> 'wmb'."""
    vd = video_dir.rstrip("/")
    basename = os.path.basename(vd)
    # Try to find the dataset suffix: wmb, video_phy_2, physics_iq, openvid, video_phy_2
    for ds in ["wmb", "video_phy_2", "physics_iq", "openvid", "video_phy_2", "wmb"]:
        if basename.endswith(f"-{ds}"):
            return ds
    return basename


def extract_model_from_video_dir(video_dir: str) -> str:
    """Extract model name from video_dir."""
    vd = video_dir.rstrip("/")
    basename = os.path.basename(vd)
    for ds in ["wmb", "video_phy_2", "physics_iq", "openvid", "video_phy_2", "wmb"]:
        suffix = f"-{ds}"
        if basename.endswith(suffix):
            return basename[: -len(suffix)]
    return basename


def load_eval_file(filepath: str) -> Optional[dict]:
    """Load an eval JSON file, returning None on error."""
    try:
        with open(filepath, "r") as f:
            data = json.load(f)
        if not data.get("results"):
            return None
        return data
    except (json.JSONDecodeError, FileNotFoundError, KeyError):
        return None


def get_timestamp_from_filename(filename: str) -> str:
    """Extract timestamp from filename like eval_gemini_20260322_200226.json."""
    m = re.search(r"(\d{8}_\d{6})", filename)
    return m.group(1) if m else ""


# ---------------------------------------------------------------------------
# Core: find old/new pairs
# ---------------------------------------------------------------------------


def find_pairs() -> List[Dict[str, Any]]:
    """
    Find all (old_file, new_file) pairs for comparison.
    Returns list of dicts with keys: old_path, new_path, model, dataset, judge, mode (batched/permetric).
    """
    pairs = []

    if not os.path.isdir(BACKUP_DIR):
        print(f"ERROR: Backup directory not found: {BACKUP_DIR}", file=sys.stderr)
        return pairs

    for backup_model in sorted(os.listdir(BACKUP_DIR)):
        backup_model_dir = os.path.join(BACKUP_DIR, backup_model)
        if not os.path.isdir(backup_model_dir):
            continue

        for fname in sorted(os.listdir(backup_model_dir)):
            if not fname.startswith("eval_") or not fname.endswith(".json"):
                continue

            old_path = os.path.join(backup_model_dir, fname)
            old_data = load_eval_file(old_path)
            if old_data is None:
                continue

            video_dir = old_data.get("video_dir", "")
            if not video_dir:
                continue

            dataset = extract_dataset_from_video_dir(video_dir)
            model = extract_model_from_video_dir(video_dir)
            judge = parse_judge_key(old_data.get("judge", ""))
            mode = "permetric" if is_permetric(fname) else "batched"

            # Find corresponding current directory
            current_dir = os.path.join(VIDEOS_DIR, f"{model}-{dataset}")
            if not os.path.isdir(current_dir):
                continue

            # Find newest matching eval file in current dir
            old_timestamp = get_timestamp_from_filename(fname)
            best_new_path = None
            best_new_ts = ""

            for cur_fname in os.listdir(current_dir):
                if not cur_fname.startswith("eval_") or not cur_fname.endswith(".json"):
                    continue
                if ".old_pre_t26." in cur_fname:
                    continue

                cur_judge = parse_filename_judge(cur_fname)
                cur_mode = "permetric" if is_permetric(cur_fname) else "batched"

                if cur_judge != judge or cur_mode != mode:
                    continue

                cur_ts = get_timestamp_from_filename(cur_fname)

                # Skip files with same timestamp as the old file (these are copies)
                if cur_ts == old_timestamp:
                    continue

                # Must be newer than old
                if cur_ts <= old_timestamp:
                    continue

                # Pick the newest one
                if cur_ts > best_new_ts:
                    best_new_ts = cur_ts
                    best_new_path = os.path.join(current_dir, cur_fname)

            if best_new_path:
                pairs.append(
                    {
                        "old_path": old_path,
                        "new_path": best_new_path,
                        "model": model,
                        "dataset": dataset,
                        "judge": judge,
                        "mode": mode,
                    }
                )

    return pairs


# ---------------------------------------------------------------------------
# Core: compute deltas
# ---------------------------------------------------------------------------


def compute_deltas(
    old_data: dict, new_data: dict
) -> List[Dict[str, Any]]:
    """
    For each video present in both old and new where the prompt changed,
    compute score deltas.
    Returns list of per-video delta records.
    """
    old_by_video = {r["video"]: r for r in old_data["results"]}
    new_by_video = {r["video"]: r for r in new_data["results"]}

    deltas = []
    common_videos = set(old_by_video.keys()) & set(new_by_video.keys())

    for vid in sorted(common_videos):
        old_r = old_by_video[vid]
        new_r = new_by_video[vid]

        # Only analyze videos where the prompt actually changed
        if old_r.get("prompt", "") == new_r.get("prompt", ""):
            continue

        rec: Dict[str, Any] = {
            "video": vid,
            "old_prompt": old_r.get("prompt", ""),
            "new_prompt": new_r.get("prompt", ""),
            "physical_laws": new_r.get("physical_laws", old_r.get("physical_laws", [])),
        }

        # General metrics deltas
        for m in GENERAL_METRICS:
            old_val = old_r.get(m)
            new_val = new_r.get(m)
            if old_val is not None and new_val is not None:
                rec[f"{m}_old"] = float(old_val)
                rec[f"{m}_new"] = float(new_val)
                rec[f"{m}_delta"] = float(new_val) - float(old_val)

        # general_avg delta
        old_ga = old_r.get("general_avg")
        new_ga = new_r.get("general_avg")
        if old_ga is not None and new_ga is not None:
            rec["general_avg_old"] = float(old_ga)
            rec["general_avg_new"] = float(new_ga)
            rec["general_avg_delta"] = float(new_ga) - float(old_ga)

        # physical_avg delta
        old_pa = get_physical_avg(old_r)
        new_pa = get_physical_avg(new_r)
        if old_pa is not None and new_pa is not None:
            rec["physical_avg_old"] = float(old_pa)
            rec["physical_avg_new"] = float(new_pa)
            rec["physical_avg_delta"] = float(new_pa) - float(old_pa)

        # Per-law score deltas
        old_laws = get_per_law_scores(old_r)
        new_laws = get_per_law_scores(new_r)
        law_deltas = {}
        for law in set(old_laws.keys()) & set(new_laws.keys()):
            law_deltas[law] = new_laws[law] - old_laws[law]
        rec["per_law_deltas"] = law_deltas

        deltas.append(rec)

    return deltas


# ---------------------------------------------------------------------------
# Aggregation
# ---------------------------------------------------------------------------


def safe_mean(values: list) -> Optional[float]:
    if not values:
        return None
    return sum(values) / len(values)


def format_delta(val: Optional[float], decimals: int = 4) -> str:
    if val is None:
        return "N/A"
    sign = "+" if val >= 0 else ""
    return f"{sign}{val:.{decimals}f}"


def format_float(val: Optional[float], decimals: int = 4) -> str:
    if val is None:
        return "N/A"
    return f"{val:.{decimals}f}"


class AblationAnalysis:
    def __init__(self):
        # Each entry: (model, dataset, judge, mode, delta_record)
        self.all_records: List[Tuple[str, str, str, str, Dict[str, Any]]] = []

    def add(self, model: str, dataset: str, judge: str, mode: str, deltas: List[Dict[str, Any]]):
        for d in deltas:
            self.all_records.append((model, dataset, judge, mode, d))

    def _filter(
        self,
        model: Optional[str] = None,
        dataset: Optional[str] = None,
        judge: Optional[str] = None,
    ) -> List[Dict[str, Any]]:
        out = []
        for m, ds, j, mode, rec in self.all_records:
            if model and m != model:
                continue
            if dataset and ds != dataset:
                continue
            if judge and j != judge:
                continue
            out.append(rec)
        return out

    def _metric_deltas(self, records: List[Dict], metric_key: str) -> List[float]:
        key = f"{metric_key}_delta"
        return [r[key] for r in records if key in r]

    def overall_summary(self) -> str:
        lines = []
        records = self._filter()
        n = len(records)
        lines.append(f"**Total video comparisons (prompt changed):** {n}")
        lines.append("")

        if n == 0:
            lines.append("No data to analyze.")
            return "\n".join(lines)

        # Table header
        metrics = GENERAL_METRICS + ["general_avg", "physical_avg"]
        lines.append("| Metric | Mean Delta | Median Delta | Std Dev | N |")
        lines.append("|--------|-----------|-------------|---------|---|")
        for metric in metrics:
            vals = self._metric_deltas(records, metric)
            if not vals:
                lines.append(f"| {metric} | N/A | N/A | N/A | 0 |")
                continue
            import statistics

            mean = statistics.mean(vals)
            median = statistics.median(vals)
            stdev = statistics.stdev(vals) if len(vals) > 1 else 0.0
            lines.append(
                f"| {metric} | {format_delta(mean)} | {format_delta(median)} | {format_float(stdev)} | {len(vals)} |"
            )
        return "\n".join(lines)

    def per_group_table(self, group_key: str) -> str:
        """Group by model, dataset, or judge."""
        groups: Dict[str, List[Dict]] = defaultdict(list)
        for m, ds, j, mode, rec in self.all_records:
            if group_key == "model":
                key = m
            elif group_key == "dataset":
                key = ds
            elif group_key == "judge":
                key = j
            else:
                key = "all"
            groups[key].append(rec)

        metrics = GENERAL_METRICS + ["general_avg", "physical_avg"]
        lines = []
        header = f"| {group_key.capitalize()} | N |"
        sep = "|---|---|"
        for metric in metrics:
            header += f" {metric} |"
            sep += "---|"
        lines.append(header)
        lines.append(sep)

        for gname in sorted(groups.keys()):
            recs = groups[gname]
            row = f"| {gname} | {len(recs)} |"
            for metric in metrics:
                vals = self._metric_deltas(recs, metric)
                mean = safe_mean(vals)
                row += f" {format_delta(mean)} |"
            lines.append(row)

        return "\n".join(lines)

    def per_domain_table(self) -> str:
        """Aggregate per-law deltas into domains."""
        domain_deltas: Dict[str, List[float]] = defaultdict(list)
        for _, _, _, _, rec in self.all_records:
            for law, delta in rec.get("per_law_deltas", {}).items():
                domain = LAW_TO_DOMAIN.get(law, "other")
                domain_deltas[domain].append(delta)

        lines = []
        lines.append("| Domain | Mean Delta | N | Improved% | Degraded% | Unchanged% |")
        lines.append("|--------|-----------|---|----------|----------|-----------|")

        for domain in sorted(domain_deltas.keys()):
            vals = domain_deltas[domain]
            n = len(vals)
            mean = safe_mean(vals)
            improved = sum(1 for v in vals if v > 0) / n * 100 if n else 0
            degraded = sum(1 for v in vals if v < 0) / n * 100 if n else 0
            unchanged = sum(1 for v in vals if v == 0) / n * 100 if n else 0
            lines.append(
                f"| {domain} | {format_delta(mean)} | {n} | {improved:.1f}% | {degraded:.1f}% | {unchanged:.1f}% |"
            )

        return "\n".join(lines)

    def per_law_table(self) -> str:
        """Per-law breakdown."""
        law_deltas: Dict[str, List[float]] = defaultdict(list)
        for _, _, _, _, rec in self.all_records:
            for law, delta in rec.get("per_law_deltas", {}).items():
                law_deltas[law].append(delta)

        lines = []
        lines.append("| Law | Domain | Mean Delta | N | Improved% | Degraded% |")
        lines.append("|-----|--------|-----------|---|----------|----------|")

        # Sort by mean delta descending
        sorted_laws = sorted(
            law_deltas.keys(), key=lambda l: safe_mean(law_deltas[l]) or 0, reverse=True
        )

        for law in sorted_laws:
            vals = law_deltas[law]
            n = len(vals)
            mean = safe_mean(vals)
            domain = LAW_TO_DOMAIN.get(law, "other")
            improved = sum(1 for v in vals if v > 0) / n * 100 if n else 0
            degraded = sum(1 for v in vals if v < 0) / n * 100 if n else 0
            lines.append(
                f"| {law} | {domain} | {format_delta(mean)} | {n} | {improved:.1f}% | {degraded:.1f}% |"
            )

        return "\n".join(lines)

    def score_bin_table(self) -> str:
        """Effect on low-score vs high-score videos, binned by old general_avg."""
        bin_deltas: Dict[str, Dict[str, List[float]]] = {}
        for lo, hi in SCORE_BINS:
            label = f"{lo}-{hi}"
            bin_deltas[label] = defaultdict(list)

        metrics = GENERAL_METRICS + ["general_avg", "physical_avg"]
        for _, _, _, _, rec in self.all_records:
            old_ga = rec.get("general_avg_old")
            if old_ga is None:
                continue
            for lo, hi in SCORE_BINS:
                if lo <= old_ga < hi or (hi == 5 and old_ga == 5):
                    label = f"{lo}-{hi}"
                    for metric in metrics:
                        key = f"{metric}_delta"
                        if key in rec:
                            bin_deltas[label][metric].append(rec[key])
                    break

        lines = []
        header = "| Old general_avg Bin | N |"
        sep = "|---|---|"
        for metric in metrics:
            header += f" {metric} |"
            sep += "---|"
        lines.append(header)
        lines.append(sep)

        for lo, hi in SCORE_BINS:
            label = f"{lo}-{hi}"
            bd = bin_deltas[label]
            # N = number of records in this bin
            n_vals = bd.get("general_avg", [])
            n = len(n_vals)
            row = f"| {label} | {n} |"
            for metric in metrics:
                vals = bd.get(metric, [])
                mean = safe_mean(vals)
                row += f" {format_delta(mean)} |"
            lines.append(row)

        return "\n".join(lines)

    def prompt_length_analysis(self) -> str:
        """Analyze whether longer prompt additions correlate with bigger deltas."""
        records_with_len = []
        for _, _, _, _, rec in self.all_records:
            old_len = len(rec.get("old_prompt", ""))
            new_len = len(rec.get("new_prompt", ""))
            delta_len = new_len - old_len
            ga_delta = rec.get("general_avg_delta")
            pa_delta = rec.get("physical_avg_delta")
            if ga_delta is not None:
                records_with_len.append((delta_len, ga_delta, pa_delta))

        if not records_with_len:
            return "No data for prompt length analysis."

        lines = []
        # Bin by prompt length increase
        bins = [(0, 30), (30, 60), (60, 90), (90, 200)]
        lines.append("| Prompt Length Increase | N | Mean general_avg Delta | Mean physical_avg Delta |")
        lines.append("|----------------------|---|----------------------|----------------------|")

        for lo, hi in bins:
            subset = [(d, g, p) for d, g, p in records_with_len if lo <= d < hi]
            n = len(subset)
            ga_mean = safe_mean([g for _, g, _ in subset])
            pa_mean = safe_mean([p for _, _, p in subset if p is not None])
            lines.append(
                f"| {lo}-{hi} chars | {n} | {format_delta(ga_mean)} | {format_delta(pa_mean)} |"
            )

        return "\n".join(lines)


# ---------------------------------------------------------------------------
# Main
# ---------------------------------------------------------------------------


def main():
    # Ensure we're in anonymous root or adjust paths
    if not os.path.isdir(DATA_ROOT):
        # Try from the script's location
        script_dir = os.path.dirname(os.path.abspath(__file__))
        anonymous_root = os.path.abspath(os.path.join(script_dir, "..", ".."))
        os.chdir(anonymous_root)
        if not os.path.isdir(DATA_ROOT):
            print(f"ERROR: Cannot find {DATA_ROOT}. Run from the anonymous root directory.", file=sys.stderr)
            sys.exit(1)

    print("# Ablation Study: Effect of Prompt Enhancement on VLM Evaluation Scores")
    print()
    print("Comparing backup (pre-enhancement) vs current (post-enhancement) evaluation files.")
    print("Only videos where the prompt actually changed between versions are included.")
    print()

    # Step 1: Find all old/new pairs
    pairs = find_pairs()
    print(f"Found **{len(pairs)}** old/new eval file pairs.")
    print()

    if not pairs:
        print("No pairs found. Exiting.")
        return

    # Print pairs summary
    print("## File Pairs Found")
    print()
    print("| # | Model | Dataset | Judge | Mode | Old File | New File |")
    print("|---|-------|---------|-------|------|----------|----------|")
    for i, p in enumerate(pairs, 1):
        old_base = os.path.basename(p["old_path"])
        new_base = os.path.basename(p["new_path"])
        print(
            f"| {i} | {p['model']} | {p['dataset']} | {p['judge']} | {p['mode']} | {old_base} | {new_base} |"
        )
    print()

    # Step 2: Compute deltas for each pair
    analysis = AblationAnalysis()
    total_videos = 0
    total_changed = 0
    skipped_pairs = 0

    for p in pairs:
        old_data = load_eval_file(p["old_path"])
        new_data = load_eval_file(p["new_path"])
        if old_data is None or new_data is None:
            skipped_pairs += 1
            continue

        old_by_video = {r["video"]: r for r in old_data["results"]}
        new_by_video = {r["video"]: r for r in new_data["results"]}
        common = set(old_by_video.keys()) & set(new_by_video.keys())
        total_videos += len(common)

        deltas = compute_deltas(old_data, new_data)
        total_changed += len(deltas)

        analysis.add(p["model"], p["dataset"], p["judge"], p["mode"], deltas)

    print(f"**Total matched videos across all pairs:** {total_videos}")
    print(f"**Videos with prompt changes:** {total_changed}")
    if skipped_pairs:
        print(f"**Skipped pairs (load errors):** {skipped_pairs}")
    print()

    if total_changed == 0:
        print("No videos with prompt changes found. Exiting.")
        return

    # Step 3: Reports
    print("---")
    print()
    print("## 1. Overall Score Deltas (All Models, Datasets, Judges)")
    print()
    print(analysis.overall_summary())
    print()

    print("---")
    print()
    print("## 2. Per-Model Breakdown")
    print()
    print(analysis.per_group_table("model"))
    print()

    print("---")
    print()
    print("## 3. Per-Dataset Breakdown")
    print()
    print(analysis.per_group_table("dataset"))
    print()

    print("---")
    print()
    print("## 4. Per-Judge Breakdown")
    print()
    print(analysis.per_group_table("judge"))
    print()

    print("---")
    print()
    print("## 5. Per-Domain Breakdown (Physical Laws)")
    print()
    print(analysis.per_domain_table())
    print()

    print("---")
    print()
    print("## 6. Per-Law Breakdown (sorted by mean delta, descending)")
    print()
    print(analysis.per_law_table())
    print()

    print("---")
    print()
    print("## 7. Effect by Old Score Bin")
    print()
    print("Videos binned by their old general_avg score:")
    print()
    print(analysis.score_bin_table())
    print()

    print("---")
    print()
    print("## 8. Prompt Length Increase Analysis")
    print()
    print(analysis.prompt_length_analysis())
    print()


if __name__ == "__main__":
    main()