-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathplot_results.py
More file actions
238 lines (202 loc) · 6.66 KB
/
Copy pathplot_results.py
File metadata and controls
238 lines (202 loc) · 6.66 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
#!/usr/bin/env python3
"""
plot_results.py — Plot autoresearch experiment progress from results.tsv
Usage:
python3 plot_results.py # default: experiments/results.tsv → experiments/progress.png
python3 plot_results.py -i path/to/results.tsv # custom input
python3 plot_results.py -o plot.pdf # custom output
python3 plot_results.py --metric "lower_is_better" # flip y-axis for loss-style metrics
"""
import argparse
import csv
import textwrap
from pathlib import Path
import matplotlib.pyplot as plt
import matplotlib.ticker as mticker
import numpy as np
def load_results(tsv_path: str) -> list[dict]:
"""Load experiments from a TSV file."""
rows = []
with open(tsv_path, encoding="utf-8") as f:
reader = csv.DictReader(f, delimiter="\t")
for row in reader:
rows.append(
{
"experiment": row["experiment"],
"score": float(row["overall_score"]),
"status": row["status"].strip(),
"description": row["description"].strip(),
}
)
return rows
def plot_progress(
rows: list[dict],
output_path: str,
metric_label: str = "Overall Score (higher is better)",
lower_is_better: bool = False,
):
"""Create the autoresearch progress plot."""
n = len(rows)
xs = list(range(n))
scores = [r["score"] for r in rows]
statuses = [r["status"] for r in rows]
descriptions = [r["description"] for r in rows]
# --- Compute running best ---
running_best = []
best_so_far = scores[0]
for s in scores:
if lower_is_better:
best_so_far = min(best_so_far, s)
else:
best_so_far = max(best_so_far, s)
running_best.append(best_so_far)
# --- Determine kept vs discarded ---
kept_x, kept_y, kept_desc = [], [], []
disc_x, disc_y = [], []
for i, (s, st, d) in enumerate(zip(scores, statuses, descriptions)):
if st == "keep":
kept_x.append(i)
kept_y.append(s)
kept_desc.append(d)
else:
disc_x.append(i)
disc_y.append(s)
# --- Figure setup ---
fig, ax = plt.subplots(figsize=(max(14, n * 0.6), 7))
fig.patch.set_facecolor("white")
ax.set_facecolor("#fafafa")
# Grid
ax.grid(True, axis="y", color="#e0e0e0", linewidth=0.5, zorder=0)
ax.grid(True, axis="x", color="#f0f0f0", linewidth=0.3, zorder=0)
# --- Running best step line ---
ax.step(
xs,
running_best,
where="post",
color="#2ecc71",
linewidth=2.0,
alpha=0.7,
label="Running best",
zorder=2,
)
# --- Discarded points ---
if disc_x:
ax.scatter(
disc_x,
disc_y,
color="#cccccc",
s=50,
alpha=0.6,
edgecolors="none",
label="Discarded",
zorder=3,
)
# --- Kept points ---
if kept_x:
ax.scatter(
kept_x,
kept_y,
color="#2ecc71",
s=90,
edgecolors="white",
linewidths=1.2,
label="Kept",
zorder=4,
)
# --- Labels for kept experiments ---
y_range = max(scores) - min(scores) if max(scores) != min(scores) else 0.1
label_offset = y_range * 0.03
# Track label positions to avoid overlap
placed_labels: list[tuple[float, float]] = []
for i, (x, y, desc) in enumerate(zip(kept_x, kept_y, kept_desc)):
# Truncate long descriptions
label = textwrap.shorten(desc, width=48, placeholder="...")
# Alternate label placement above/below to reduce overlap
# Default: below-left
va = "top"
y_pos = y - label_offset
ha = "left"
x_pos = x + 0.15
# Check for overlaps with previously placed labels
for px, py in placed_labels:
if abs(x_pos - px) < 3 and abs(y_pos - py) < y_range * 0.06:
# Flip to above
va = "bottom"
y_pos = y + label_offset
break
placed_labels.append((x_pos, y_pos))
ax.annotate(
label,
xy=(x, y),
xytext=(x_pos, y_pos),
fontsize=7.5,
color="#2e7d32",
fontstyle="italic",
ha=ha,
va=va,
zorder=5,
)
# --- Axis labels and title ---
n_kept = len(kept_x)
ax.set_title(
f"Autoresearch Progress: {n} Experiments, {n_kept} Kept Improvements",
fontsize=14,
fontweight="bold",
pad=12,
)
ax.set_xlabel("Experiment #", fontsize=11)
ax.set_ylabel(metric_label, fontsize=11)
# X ticks
ax.set_xticks(xs)
ax.set_xlim(-0.5, n - 0.5)
# Y axis formatting
ax.yaxis.set_major_formatter(mticker.FormatStrFormatter("%.3f"))
y_min, y_max = min(scores), max(scores)
y_pad = y_range * 0.15 if y_range > 0 else 0.05
ax.set_ylim(y_min - y_pad, y_max + y_pad)
if lower_is_better:
ax.invert_yaxis()
# Legend
ax.legend(loc="upper left" if not lower_is_better else "lower left", fontsize=9, framealpha=0.9)
# --- Save ---
fig.tight_layout()
fig.savefig(output_path, dpi=180, bbox_inches="tight")
print(f"Plot saved to {output_path}")
plt.close(fig)
def main():
parser = argparse.ArgumentParser(description="Plot autoresearch experiment progress.")
parser.add_argument(
"-i", "--input",
default="experiments/results.tsv",
help="Path to results.tsv (default: experiments/results.tsv)",
)
parser.add_argument(
"-o", "--output",
default=None,
help="Output image path (default: <input_dir>/progress.png)",
)
parser.add_argument(
"--metric",
choices=["higher_is_better", "lower_is_better"],
default="higher_is_better",
help="Whether higher or lower scores are better (default: higher_is_better)",
)
parser.add_argument(
"--ylabel",
default=None,
help="Custom Y-axis label (default: auto from --metric)",
)
args = parser.parse_args()
if args.output is None:
args.output = str(Path(args.input).parent / "progress.png")
lower_is_better = args.metric == "lower_is_better"
ylabel = args.ylabel or (
"Overall Score (higher is better)" if not lower_is_better else "Score (lower is better)"
)
rows = load_results(args.input)
if not rows:
print("No experiments found in TSV.")
return
plot_progress(rows, args.output, metric_label=ylabel, lower_is_better=lower_is_better)
if __name__ == "__main__":
main()