-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanalyze.py
More file actions
1027 lines (818 loc) · 33.3 KB
/
Copy pathanalyze.py
File metadata and controls
1027 lines (818 loc) · 33.3 KB
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
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
"""
Publication-quality statistical analysis using rliable.
Implements the pre-registered analysis protocol from ``paper/analysis_plan.md``:
- **Point estimates:** IQM (not raw mean) per Agarwal et al. 2021
- **Confidence intervals:** 95% stratified bootstrap with 2,000 resamples
- **Significance test:** Probability of Improvement > 0.75
- **Plots:** aggregate metrics, performance profiles, probability-of-improvement
heatmap, sample-efficiency curves
Input data comes from ``scripts/eval.py`` JSON outputs, organized as::
eval_results/
+-- pixel/
| +-- seed_0.json
| +-- seed_1.json
+-- symbolic/
| +-- seed_0.json
| +-- seed_1.json
+-- hybrid/
+-- seed_0.json
+-- seed_1.json
Or from a single CSV file (see ``--csv``).
Usage::
# Analyze from eval JSON directory
python scripts/analyze.py --results-dir eval_results/
# Analyze from CSV
python scripts/analyze.py --csv paper/notebooks/all_results.csv
# Generate only specific plots
python scripts/analyze.py --results-dir eval_results/ --plots iqm poi
Run ``python scripts/analyze.py --help`` for all options.
"""
import argparse
import json
import logging
import os
import sys
from pathlib import Path
from typing import Any, Dict, List, Optional, Tuple
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
# Add project root to path
_PROJECT_ROOT = str(Path(__file__).resolve().parent.parent)
if _PROJECT_ROOT not in sys.path:
sys.path.insert(0, _PROJECT_ROOT)
from pokemon_red_ai.analysis import (
TREATMENT_COLORS,
TREATMENT_DISPLAY,
UNKNOWN_COLOR,
)
logger = logging.getLogger(__name__)
# ──────────────────────────────────────────────────────────────────────
# Constants
# ──────────────────────────────────────────────────────────────────────
# Metrics we extract from eval.py JSON and feed to rliable
METRIC_KEYS = {
"brock_win_rate": "Brock Win Rate",
"mean_return": "Mean Return",
"mean_event_flags_triggered": "Event Flags",
"unique_maps_visited": "Unique Maps",
}
# Pre-registered analysis constants (analysis_plan.md S6)
BOOTSTRAP_REPS = 2_000
CONFIDENCE_LEVEL = 0.95
# Figure defaults
FIGURE_DPI = 300
FIGURE_FORMAT = "pdf"
# ──────────────────────────────────────────────────────────────────────
# Data loading
# ──────────────────────────────────────────────────────────────────────
def load_eval_jsons(results_dir: Path) -> Dict[str, List[Dict[str, Any]]]:
"""
Load eval JSON files organized by treatment subdirectories.
Expected structure::
results_dir/
+-- pixel/
| +-- seed_0.json (output of scripts/eval.py)
| +-- seed_1.json
+-- symbolic/
| +-- seed_0.json
+-- hybrid/
+-- seed_0.json
Returns:
Dict mapping treatment name to list of eval metric dicts.
"""
results: Dict[str, List[Dict[str, Any]]] = {}
if not results_dir.is_dir():
raise FileNotFoundError(f"Results directory not found: {results_dir}")
for treatment_dir in sorted(results_dir.iterdir()):
if not treatment_dir.is_dir():
continue
treatment = treatment_dir.name
evals = []
for json_file in sorted(treatment_dir.glob("*.json")):
with open(json_file) as f:
data = json.load(f)
data["_source_file"] = str(json_file)
evals.append(data)
logger.debug(f"Loaded {json_file} ({len(data)} keys)")
if evals:
results[treatment] = evals
logger.info(
f" {treatment}: {len(evals)} seeds loaded"
)
if not results:
raise ValueError(
f"No eval JSONs found in {results_dir}. "
f"Expected subdirectories like pixel/, symbolic/, hybrid/ "
f"containing *.json files from scripts/eval.py."
)
return results
def load_csv(csv_path: Path) -> Dict[str, List[Dict[str, Any]]]:
"""
Load results from a single CSV file.
Expected columns: ``treatment``, ``seed``, ``brock_win_rate``,
``mean_return``, ``mean_event_flags_triggered``, ``unique_maps_visited``.
Returns:
Dict mapping treatment name to list of metric dicts (one per seed).
"""
import csv
results: Dict[str, List[Dict[str, Any]]] = {}
with open(csv_path) as f:
reader = csv.DictReader(f)
for row in reader:
treatment = row["treatment"]
metrics = {}
for key in METRIC_KEYS:
raw = row.get(key)
if raw is not None:
try:
metrics[key] = float(raw)
except ValueError:
metrics[key] = None
else:
metrics[key] = None
# Carry through extra metadata
metrics["seed"] = row.get("seed")
metrics["_source_file"] = str(csv_path)
results.setdefault(treatment, []).append(metrics)
logger.info(f"Loaded CSV: {csv_path}")
for t, evals in results.items():
logger.info(f" {t}: {len(evals)} seeds")
return results
def load_sample_efficiency_csv(csv_path: Path) -> Dict[str, np.ndarray]:
"""
Load sample efficiency data from a CSV.
Expected columns: ``treatment``, ``seed``, ``timestep``,
``brock_win_rate`` (or whichever metric you want curves for).
Returns:
Dict with keys:
- 'frames': 1-D array of timestep values
- One key per treatment: (n_seeds, n_frames) array of scores
"""
import csv
raw: Dict[str, Dict[int, Dict[int, float]]] = {} # treatment -> seed -> step -> score
all_steps = set()
with open(csv_path) as f:
reader = csv.DictReader(f)
for row in reader:
treatment = row["treatment"]
seed = int(row["seed"])
step = int(row["timestep"])
score = float(row["brock_win_rate"])
raw.setdefault(treatment, {}).setdefault(seed, {})[step] = score
all_steps.add(step)
frames = np.array(sorted(all_steps))
result: Dict[str, np.ndarray] = {"frames": frames}
for treatment, seed_data in raw.items():
seeds = sorted(seed_data.keys())
matrix = np.zeros((len(seeds), len(frames)))
for i, seed in enumerate(seeds):
for j, step in enumerate(frames):
matrix[i, j] = seed_data[seed].get(step, 0.0)
result[treatment] = matrix
return result
# ──────────────────────────────────────────────────────────────────────
# Score matrix construction
# ──────────────────────────────────────────────────────────────────────
def build_score_matrix(
results: Dict[str, List[Dict[str, Any]]],
metric: str = "brock_win_rate",
normalize: bool = False,
max_score: Optional[float] = None,
min_score: float = 0.0,
) -> Dict[str, np.ndarray]:
"""
Convert loaded results into the ``scores_dict`` format rliable expects.
Args:
results: Output of ``load_eval_jsons`` or ``load_csv``.
metric: Which metric key to extract.
normalize: If True, min-max normalize scores to [0, 1].
max_score: Max score for normalization (auto-detected if None).
min_score: Min score for normalization.
Returns:
Dict mapping treatment name to (n_seeds, 1) array.
rliable's second axis is task index; we have a single task.
"""
scores_dict: Dict[str, np.ndarray] = {}
for treatment, evals in results.items():
values = []
for e in evals:
val = e.get(metric)
if val is None:
logger.warning(
f"Missing metric '{metric}' in {e.get('_source_file', '?')}, "
f"using 0.0"
)
val = 0.0
values.append(float(val))
arr = np.array(values).reshape(-1, 1) # (n_seeds, 1)
if normalize:
if max_score is None:
max_score = float(arr.max()) if arr.max() > min_score else 1.0
arr = (arr - min_score) / (max_score - min_score)
arr = np.clip(arr, 0.0, 1.0)
scores_dict[treatment] = arr
logger.debug(
f" {treatment}: {len(values)} seeds, "
f"values={[f'{v:.3f}' for v in values]}"
)
return scores_dict
# ──────────────────────────────────────────────────────────────────────
# rliable analysis functions
# ──────────────────────────────────────────────────────────────────────
def compute_aggregate_metrics(
scores_dict: Dict[str, np.ndarray],
reps: int = BOOTSTRAP_REPS,
) -> Tuple[Dict[str, Dict[str, float]], Dict[str, Dict[str, np.ndarray]]]:
"""
Compute IQM, mean, median, and optimality gap with bootstrap CIs.
Returns:
(point_estimates, interval_estimates) — each a dict mapping
metric name to {treatment: value} or {treatment: [lo, hi]}.
"""
from rliable import library as rly
from rliable import metrics
aggregate_func = lambda x: np.array([
metrics.aggregate_iqm(x),
metrics.aggregate_mean(x),
metrics.aggregate_median(x),
metrics.aggregate_optimality_gap(x),
])
aggregate_scores, aggregate_cis = rly.get_interval_estimates(
scores_dict,
aggregate_func,
reps=reps,
)
metric_names = ["IQM", "Mean", "Median", "Optimality Gap"]
point_estimates: Dict[str, Dict[str, float]] = {}
interval_estimates: Dict[str, Dict[str, np.ndarray]] = {}
for i, name in enumerate(metric_names):
point_estimates[name] = {}
interval_estimates[name] = {}
for treatment in scores_dict:
point_estimates[name][treatment] = float(
aggregate_scores[treatment][i]
)
interval_estimates[name][treatment] = aggregate_cis[treatment][:, i]
return point_estimates, interval_estimates
def compute_probability_of_improvement(
scores_dict: Dict[str, np.ndarray],
reps: int = BOOTSTRAP_REPS,
) -> Tuple[Dict[Tuple[str, str], float], Dict[Tuple[str, str], np.ndarray]]:
"""
Compute pairwise Probability of Improvement between all treatments.
A treatment A is considered superior to B if P(A > B) > 0.75
and the 95% CI excludes 0.5 (analysis_plan.md S6).
Uses stratified bootstrap to compute CIs on the POI estimate,
since ``metrics.probability_of_improvement(scores_x, scores_y)``
takes two separate arrays and cannot go through
``get_interval_estimates``.
Returns:
(poi_point, poi_ci): dicts mapping (treatment_a, treatment_b)
to P(A > B) point estimate and [lo, hi] CI.
"""
from rliable import metrics
treatments = sorted(scores_dict.keys())
poi_point: Dict[Tuple[str, str], float] = {}
poi_ci: Dict[Tuple[str, str], np.ndarray] = {}
alpha = (1 - CONFIDENCE_LEVEL) / 2 # 0.025 for 95% CI
for i, t_a in enumerate(treatments):
for j, t_b in enumerate(treatments):
if i >= j:
continue
scores_a = scores_dict[t_a] # (n_seeds_a, 1)
scores_b = scores_dict[t_b] # (n_seeds_b, 1)
# Point estimate
poi_ab = float(metrics.probability_of_improvement(scores_a, scores_b))
poi_ba = 1.0 - poi_ab
# Bootstrap CI by resampling seed indices
n_a, n_b = scores_a.shape[0], scores_b.shape[0]
boot_pois = np.zeros(reps)
rng = np.random.RandomState(42)
for r in range(reps):
idx_a = rng.choice(n_a, size=n_a, replace=True)
idx_b = rng.choice(n_b, size=n_b, replace=True)
boot_pois[r] = metrics.probability_of_improvement(
scores_a[idx_a], scores_b[idx_b],
)
ci_lo = float(np.percentile(boot_pois, 100 * alpha))
ci_hi = float(np.percentile(boot_pois, 100 * (1 - alpha)))
poi_point[(t_a, t_b)] = poi_ab
poi_ci[(t_a, t_b)] = np.array([ci_lo, ci_hi])
poi_point[(t_b, t_a)] = poi_ba
poi_ci[(t_b, t_a)] = np.array([1.0 - ci_hi, 1.0 - ci_lo])
return poi_point, poi_ci
def compute_performance_profiles(
scores_dict: Dict[str, np.ndarray],
tau_range: Optional[np.ndarray] = None,
reps: int = BOOTSTRAP_REPS,
) -> Tuple[np.ndarray, Dict[str, np.ndarray], Dict[str, np.ndarray]]:
"""
Compute performance profiles (fraction of runs above threshold tau).
Returns:
(taus, profile_point, profile_ci): threshold array, point estimates,
and bootstrap CIs per treatment.
"""
from rliable import library as rly
if tau_range is None:
tau_range = np.linspace(0.0, 1.0, 51)
profiles, profile_cis = rly.create_performance_profile(
scores_dict,
tau_list=tau_range,
reps=reps,
)
return tau_range, profiles, profile_cis
# ──────────────────────────────────────────────────────────────────────
# Plotting
# ──────────────────────────────────────────────────────────────────────
def _setup_matplotlib() -> None:
"""Configure matplotlib for publication-quality figures."""
matplotlib.rcParams.update({
"font.family": "serif",
"font.size": 11,
"axes.labelsize": 12,
"axes.titlesize": 13,
"legend.fontsize": 10,
"xtick.labelsize": 10,
"ytick.labelsize": 10,
"figure.dpi": FIGURE_DPI,
"savefig.dpi": FIGURE_DPI,
"savefig.bbox": "tight",
"savefig.pad_inches": 0.05,
"axes.grid": True,
"grid.alpha": 0.3,
"axes.spines.top": False,
"axes.spines.right": False,
})
def _get_color(treatment: str) -> str:
"""Get the color for a treatment, with a fallback."""
return TREATMENT_COLORS.get(treatment, UNKNOWN_COLOR)
def _get_label(treatment: str) -> str:
"""Get the display label for a treatment."""
return TREATMENT_DISPLAY.get(treatment, treatment.title())
def plot_aggregate_metrics(
point_estimates: Dict[str, Dict[str, float]],
interval_estimates: Dict[str, Dict[str, np.ndarray]],
metric_name: str = "IQM",
output_path: Optional[Path] = None,
title: Optional[str] = None,
) -> plt.Figure:
"""
Bar chart of aggregate metric with bootstrap CIs.
Produces the main result figure showing IQM across treatments.
"""
treatments = sorted(point_estimates[metric_name].keys())
values = [point_estimates[metric_name][t] for t in treatments]
cis = [interval_estimates[metric_name][t] for t in treatments]
colors = [_get_color(t) for t in treatments]
labels = [_get_label(t) for t in treatments]
# Compute error bars (distance from point estimate to CI bounds)
errors = np.array([
[v - ci[0], ci[1] - v] for v, ci in zip(values, cis)
]).T
fig, ax = plt.subplots(figsize=(5, 3.5))
x = np.arange(len(treatments))
bars = ax.bar(
x, values, yerr=errors,
color=colors, edgecolor="white", linewidth=0.5,
capsize=4, error_kw={"linewidth": 1.5},
width=0.6,
)
ax.set_xticks(x)
ax.set_xticklabels(labels)
ax.set_ylabel(metric_name)
if title:
ax.set_title(title)
else:
ax.set_title(
f"{metric_name} (Brock Win Rate)\n"
f"95% Bootstrap CI, {BOOTSTRAP_REPS:,} resamples"
)
# Annotate values
for bar, val, ci in zip(bars, values, cis):
ax.text(
bar.get_x() + bar.get_width() / 2,
ci[1] + 0.02,
f"{val:.3f}",
ha="center", va="bottom", fontsize=9,
)
fig.tight_layout()
if output_path:
fig.savefig(output_path, format=FIGURE_FORMAT)
logger.info(f"Saved: {output_path}")
return fig
def plot_performance_profiles(
taus: np.ndarray,
profiles: Dict[str, np.ndarray],
profile_cis: Dict[str, np.ndarray],
output_path: Optional[Path] = None,
title: Optional[str] = None,
) -> plt.Figure:
"""
Performance profile plot: fraction of runs achieving score >= tau.
"""
fig, ax = plt.subplots(figsize=(5, 3.5))
for treatment in sorted(profiles.keys()):
color = _get_color(treatment)
label = _get_label(treatment)
profile = profiles[treatment]
ci = profile_cis[treatment]
ax.plot(taus, profile, color=color, label=label, linewidth=2)
ax.fill_between(
taus, ci[0], ci[1],
color=color, alpha=0.15,
)
ax.set_xlabel(r"Normalized Score ($\tau$)")
ax.set_ylabel(r"Fraction of runs with score $\geq \tau$")
ax.set_xlim(0, 1)
ax.set_ylim(0, 1.05)
ax.legend(loc="lower left")
if title:
ax.set_title(title)
else:
ax.set_title("Performance Profiles")
fig.tight_layout()
if output_path:
fig.savefig(output_path, format=FIGURE_FORMAT)
logger.info(f"Saved: {output_path}")
return fig
def plot_probability_of_improvement(
poi_point: Dict[Tuple[str, str], float],
poi_ci: Dict[Tuple[str, str], np.ndarray],
output_path: Optional[Path] = None,
title: Optional[str] = None,
) -> plt.Figure:
"""
Heatmap of pairwise Probability of Improvement.
Cells where P(A > B) > 0.75 with CI excluding 0.5 are highlighted
per the pre-registered significance threshold.
"""
treatments = sorted({t for pair in poi_point for t in pair})
n = len(treatments)
matrix = np.full((n, n), 0.5)
significant = np.zeros((n, n), dtype=bool)
for i, t_a in enumerate(treatments):
for j, t_b in enumerate(treatments):
if i == j:
continue
key = (t_a, t_b)
if key in poi_point:
matrix[i, j] = poi_point[key]
ci = poi_ci[key]
# Significant if P(A > B) > 0.75 and CI excludes 0.5
significant[i, j] = (
poi_point[key] > 0.75 and ci[0] > 0.5
)
fig, ax = plt.subplots(figsize=(4.5, 4))
im = ax.imshow(
matrix, cmap="RdYlGn", vmin=0.0, vmax=1.0,
aspect="equal",
)
labels = [_get_label(t) for t in treatments]
ax.set_xticks(range(n))
ax.set_yticks(range(n))
ax.set_xticklabels(labels, rotation=45, ha="right")
ax.set_yticklabels(labels)
# Annotate cells
for i in range(n):
for j in range(n):
if i == j:
ax.text(j, i, "—", ha="center", va="center", fontsize=11)
continue
val = matrix[i, j]
sig_marker = "*" if significant[i, j] else ""
color = "white" if (val > 0.7 or val < 0.3) else "black"
ax.text(
j, i, f"{val:.2f}{sig_marker}",
ha="center", va="center",
fontsize=10, color=color, fontweight="bold",
)
ax.set_xlabel("B (column)")
ax.set_ylabel("A (row)")
cbar = fig.colorbar(im, ax=ax, shrink=0.8)
cbar.set_label("P(A > B)")
if title:
ax.set_title(title)
else:
ax.set_title("Probability of Improvement\n(* = significant, P > 0.75, CI excl. 0.5)")
fig.tight_layout()
if output_path:
fig.savefig(output_path, format=FIGURE_FORMAT)
logger.info(f"Saved: {output_path}")
return fig
def plot_sample_efficiency(
frames: np.ndarray,
scores_dict: Dict[str, np.ndarray],
output_path: Optional[Path] = None,
title: Optional[str] = None,
reps: int = BOOTSTRAP_REPS,
) -> plt.Figure:
"""
Sample efficiency curves with bootstrap CIs.
Shows IQM at each checkpoint across training, revealing which
treatment learns faster.
Args:
frames: 1-D array of timestep values.
scores_dict: Dict mapping treatment to (n_seeds, n_frames) array.
output_path: Where to save the figure.
title: Optional title override.
reps: Bootstrap resamples.
"""
from rliable import library as rly
from rliable import metrics
fig, ax = plt.subplots(figsize=(6, 4))
for treatment in sorted(scores_dict.keys()):
color = _get_color(treatment)
label = _get_label(treatment)
data = scores_dict[treatment] # (n_seeds, n_frames)
# Compute IQM and CI at each frame
iqm_values = np.zeros(len(frames))
ci_lo = np.zeros(len(frames))
ci_hi = np.zeros(len(frames))
for t_idx in range(len(frames)):
col = data[:, t_idx:t_idx + 1] # (n_seeds, 1)
single_scores = {treatment: col}
try:
agg, agg_ci = rly.get_interval_estimates(
single_scores,
metrics.aggregate_iqm,
reps=reps,
)
iqm_values[t_idx] = float(agg[treatment])
ci_lo[t_idx] = float(agg_ci[treatment][0])
ci_hi[t_idx] = float(agg_ci[treatment][1])
except Exception:
iqm_values[t_idx] = np.mean(col)
ci_lo[t_idx] = np.percentile(col, 2.5)
ci_hi[t_idx] = np.percentile(col, 97.5)
ax.plot(frames, iqm_values, color=color, label=label, linewidth=2)
ax.fill_between(frames, ci_lo, ci_hi, color=color, alpha=0.15)
ax.set_xlabel("Environment Steps")
ax.set_ylabel("IQM (Brock Win Rate)")
ax.legend()
if title:
ax.set_title(title)
else:
ax.set_title(
f"Sample Efficiency\n"
f"IQM with 95% Bootstrap CI ({reps:,} resamples)"
)
# Format x-axis with M suffix
ax.xaxis.set_major_formatter(
matplotlib.ticker.FuncFormatter(lambda x, _: f"{x / 1e6:.0f}M")
)
fig.tight_layout()
if output_path:
fig.savefig(output_path, format=FIGURE_FORMAT)
logger.info(f"Saved: {output_path}")
return fig
# ──────────────────────────────────────────────────────────────────────
# Summary report
# ──────────────────────────────────────────────────────────────────────
def print_summary_report(
results: Dict[str, List[Dict[str, Any]]],
point_estimates: Dict[str, Dict[str, float]],
interval_estimates: Dict[str, Dict[str, np.ndarray]],
poi_point: Optional[Dict[Tuple[str, str], float]] = None,
poi_ci: Optional[Dict[Tuple[str, str], np.ndarray]] = None,
) -> str:
"""
Print and return a human-readable summary of all analysis results.
"""
lines: List[str] = []
sep = "=" * 65
lines.append(sep)
lines.append("RLIABLE ANALYSIS REPORT")
lines.append(f"Pre-registered protocol: analysis_plan.md S6")
lines.append(f"Bootstrap resamples: {BOOTSTRAP_REPS:,}")
lines.append(sep)
# Data summary
lines.append("\nDATA SUMMARY")
lines.append("-" * 40)
for treatment, evals in sorted(results.items()):
label = _get_label(treatment)
lines.append(f" {label:12s}: {len(evals)} seeds")
# Aggregate metrics
lines.append(f"\nAGGREGATE METRICS (Brock Win Rate)")
lines.append("-" * 40)
for metric_name in ["IQM", "Mean", "Median", "Optimality Gap"]:
if metric_name not in point_estimates:
continue
lines.append(f"\n {metric_name}:")
for treatment in sorted(point_estimates[metric_name].keys()):
label = _get_label(treatment)
val = point_estimates[metric_name][treatment]
ci = interval_estimates[metric_name][treatment]
lines.append(
f" {label:12s}: {val:.4f} "
f"[{ci[0]:.4f}, {ci[1]:.4f}]"
)
# Probability of improvement
if poi_point:
lines.append(f"\nPROBABILITY OF IMPROVEMENT")
lines.append("-" * 40)
for (t_a, t_b), val in sorted(poi_point.items()):
ci = poi_ci[(t_a, t_b)]
label_a = _get_label(t_a)
label_b = _get_label(t_b)
sig = ""
if val > 0.75 and ci[0] > 0.5:
sig = " *SIGNIFICANT*"
lines.append(
f" P({label_a} > {label_b}): "
f"{val:.3f} [{ci[0]:.3f}, {ci[1]:.3f}]{sig}"
)
lines.append(f"\n{sep}")
lines.append("* Significant = P > 0.75 with 95% CI excluding 0.5")
lines.append(sep)
report = "\n".join(lines)
print(report)
return report
# ──────────────────────────────────────────────────────────────────────
# CLI
# ──────────────────────────────────────────────────────────────────────
def build_parser() -> argparse.ArgumentParser:
p = argparse.ArgumentParser(
description=(
"Publication-quality statistical analysis of Pokemon Red AI "
"experiments using rliable (Agarwal et al. 2021)."
),
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
)
# Input data (mutually exclusive)
input_group = p.add_mutually_exclusive_group(required=True)
input_group.add_argument(
"--results-dir", type=Path,
help="Directory containing treatment subdirectories with eval JSONs.",
)
input_group.add_argument(
"--csv", type=Path,
help="CSV file with columns: treatment, seed, brock_win_rate, ...",
)
# Sample efficiency (optional, separate data source)
p.add_argument(
"--efficiency-csv", type=Path, default=None,
help=(
"CSV file for sample efficiency curves. Columns: "
"treatment, seed, timestep, brock_win_rate."
),
)
# Metric to analyze
p.add_argument(
"--metric", type=str, default="brock_win_rate",
choices=list(METRIC_KEYS.keys()),
help="Primary metric to analyze.",
)
# Which plots to generate
p.add_argument(
"--plots", nargs="+", default=["all"],
choices=["all", "iqm", "profiles", "poi", "efficiency"],
help="Which plots to generate.",
)
# Normalization
p.add_argument(
"--normalize", action="store_true",
help="Min-max normalize scores to [0, 1] for rliable.",
)
p.add_argument(
"--max-score", type=float, default=None,
help="Maximum score for normalization (auto if omitted).",
)
# Output
p.add_argument(
"--output-dir", type=Path,
default=Path("paper/figures"),
help="Directory for output figures.",
)
p.add_argument(
"--report-file", type=Path, default=None,
help="Write the text report to a file.",
)
p.add_argument(
"--format", type=str, default="pdf",
choices=["pdf", "png", "svg"],
help="Figure output format.",
)
# Bootstrap
p.add_argument(
"--reps", type=int, default=BOOTSTRAP_REPS,
help="Number of bootstrap resamples.",
)
# Misc
p.add_argument(
"--no-show", action="store_true",
help="Do not display figures (useful in CI/headless).",
)
p.add_argument(
"-v", "--verbose", action="count", default=0,
help="Increase verbosity (-v INFO, -vv DEBUG).",
)
return p
def main() -> int:
parser = build_parser()
args = parser.parse_args()
# Logging
level = {0: logging.WARNING, 1: logging.INFO}.get(args.verbose, logging.DEBUG)
logging.basicConfig(
level=level,
format="[%(asctime)s] %(name)s %(levelname)s - %(message)s",
datefmt="%H:%M:%S",
)
# Override module-level constants from CLI
global BOOTSTRAP_REPS, FIGURE_FORMAT
BOOTSTRAP_REPS = args.reps
FIGURE_FORMAT = args.format
_setup_matplotlib()
if args.no_show:
matplotlib.use("Agg")
# Create output directory
args.output_dir.mkdir(parents=True, exist_ok=True)
# ── Load data ────────────────────────────────────────────────────
logger.info("Loading evaluation results...")
if args.results_dir:
results = load_eval_jsons(args.results_dir)
else:
results = load_csv(args.csv)
# ── Build score matrix ───────────────────────────────────────────
logger.info(f"Building score matrix for metric: {args.metric}")
scores_dict = build_score_matrix(
results,
metric=args.metric,
normalize=args.normalize,
max_score=args.max_score,
)
# Validate minimum seed count
for treatment, arr in scores_dict.items():
n_seeds = arr.shape[0]
if n_seeds < 3:
logger.warning(
f"{treatment}: only {n_seeds} seed(s). "
f"rliable recommends >= 5 seeds for reliable CIs. "
f"Results may be unstable."
)
# ── Compute aggregate metrics ────────────────────────────────────
plots_to_make = set(args.plots)
if "all" in plots_to_make:
plots_to_make = {"iqm", "profiles", "poi", "efficiency"}
logger.info("Computing aggregate metrics (IQM, Mean, Median)...")
point_estimates, interval_estimates = compute_aggregate_metrics(
scores_dict, reps=args.reps,
)
# ── Probability of improvement ───────────────────────────────────
poi_point, poi_ci = None, None
if len(scores_dict) >= 2:
logger.info("Computing probability of improvement...")
poi_point, poi_ci = compute_probability_of_improvement(
scores_dict, reps=args.reps,
)
# ── Summary report ───────────────────────────────────────────────
report = print_summary_report(
results, point_estimates, interval_estimates,
poi_point, poi_ci,
)
if args.report_file:
args.report_file.parent.mkdir(parents=True, exist_ok=True)
args.report_file.write_text(report)
logger.info(f"Report written to {args.report_file}")
# ── Generate plots ───────────────────────────────────────────────
metric_label = METRIC_KEYS.get(args.metric, args.metric)
suffix = f".{args.format}"
if "iqm" in plots_to_make:
logger.info("Plotting aggregate metrics...")
plot_aggregate_metrics(
point_estimates, interval_estimates,
metric_name="IQM",
output_path=args.output_dir / f"aggregate_iqm{suffix}",
)
if "profiles" in plots_to_make:
logger.info("Computing and plotting performance profiles...")
taus, profiles, profile_cis = compute_performance_profiles(
scores_dict, reps=args.reps,
)
plot_performance_profiles(
taus, profiles, profile_cis,
output_path=args.output_dir / f"performance_profiles{suffix}",
)