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
|
"""
Extract a stratified subset of runs from an experiment directory.
Usage:
# Extract 20% subset from Study 1 (54 runs from 270)
python scripts/extract_subset.py \
--logs-dir logs/v2_study1 \
--output-dir logs/v2_study1_subset \
--subset-fraction 0.2
# Extract 10% subset
python scripts/extract_subset.py \
--logs-dir logs/v2_study1 \
--output-dir logs/v2_study1_subset \
--subset-fraction 0.1
"""
import argparse
import os
import random
import shutil
import sys
from collections import defaultdict
def discover_runs(logs_dir: str) -> dict[str, list[str]]:
"""Discover all runs, grouped by (model, scenario, goal_type, oversight)."""
runs = defaultdict(list)
for root, dirs, files in os.walk(logs_dir):
basename = os.path.basename(root)
if basename == "baseline":
continue
for f in files:
if f.endswith(".json") and not f.startswith("_") and not f.startswith("."):
rel_path = os.path.relpath(root, logs_dir)
parts = rel_path.split(os.sep)
if len(parts) == 4:
model, scenario, goal_type, oversight = parts
key = (model, scenario, goal_type, oversight)
runs[key].append(os.path.join(root, f))
return runs
def extract_subset(
logs_dir: str,
output_dir: str,
subset_fraction: float,
seed: int = 42,
) -> None:
"""Extract a stratified random subset of runs."""
random.seed(seed)
runs_by_cell = discover_runs(logs_dir)
print(f"Found {len(runs_by_cell)} cells:")
total_runs = 0
for cell, paths in sorted(runs_by_cell.items()):
print(f" {'/'.join(cell)}: {len(paths)} runs")
total_runs += len(paths)
print(f"Total: {total_runs} runs\n")
subset_runs = []
for cell, paths in runs_by_cell.items():
n_subset = max(1, int(len(paths) * subset_fraction))
selected = random.sample(paths, min(n_subset, len(paths)))
subset_runs.extend(selected)
print(f" {'/'.join(cell)}: selected {len(selected)}/{len(paths)} runs")
print(f"\nTotal subset: {len(subset_runs)} runs\n")
copied_runs = 0
scenario_dirs = set()
for src_path in subset_runs:
rel_path = os.path.relpath(src_path, logs_dir)
dst_path = os.path.join(output_dir, rel_path)
os.makedirs(os.path.dirname(dst_path), exist_ok=True)
shutil.copy2(src_path, dst_path)
copied_runs += 1
cell_parts = rel_path.split(os.sep)
if len(cell_parts) >= 4:
src_scenario_dir = os.path.join(logs_dir, cell_parts[0], cell_parts[1])
dst_scenario_dir = os.path.join(output_dir, cell_parts[0], cell_parts[1])
scenario_dirs.add((src_scenario_dir, dst_scenario_dir))
if len(cell_parts) >= 4:
cell_str = "/".join(cell_parts[:4])
else:
cell_str = rel_path
print(f" Copied: {cell_str}/{os.path.basename(src_path)}")
print(f"\nCopying baselines...")
for src_dir, dst_dir in sorted(scenario_dirs):
baseline_src = os.path.join(src_dir, "baseline.md")
baseline_dst = os.path.join(dst_dir, "baseline.md")
if os.path.exists(baseline_src):
os.makedirs(dst_dir, exist_ok=True)
shutil.copy2(baseline_src, baseline_dst)
print(f" Copied baseline: {os.path.relpath(dst_dir, output_dir)}")
else:
print(f" Warning: no baseline.md found at {src_dir}")
print(
f"\nDone. Copied {copied_runs} runs + {len(scenario_dirs)} baselines to {output_dir}"
)
def main():
parser = argparse.ArgumentParser(
description="Extract stratified subset from experiment logs"
)
parser.add_argument(
"--logs-dir",
required=True,
help="Source directory containing experiment runs",
)
parser.add_argument(
"--output-dir",
required=True,
help="Output directory for subset",
)
parser.add_argument(
"--subset-fraction",
type=float,
default=0.2,
help="Fraction of runs to extract per cell (default: 0.2)",
)
parser.add_argument(
"--seed",
type=int,
default=42,
help="Random seed for reproducibility (default: 42)",
)
args = parser.parse_args()
if not os.path.isdir(args.logs_dir):
print(f"Error: {args.logs_dir} is not a directory")
sys.exit(1)
if args.subset_fraction <= 0 or args.subset_fraction > 1:
print(
f"Error: subset-fraction must be between 0 and 1, got {args.subset_fraction}"
)
sys.exit(1)
extract_subset(
logs_dir=args.logs_dir,
output_dir=args.output_dir,
subset_fraction=args.subset_fraction,
seed=args.seed,
)
if __name__ == "__main__":
main()
|