-
Notifications
You must be signed in to change notification settings - Fork 0
/
analyze-all-reasoning-steps-plot.py
229 lines (202 loc) · 8.32 KB
/
analyze-all-reasoning-steps-plot.py
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
import re
import os
from collections import defaultdict
import typer
from rich.console import Console
import matplotlib.pyplot as plt
from constants import SEPARATOR
console = Console()
PERTURBATIONS = ["irrelevant", "relevant", "pathological", "combo"]
OUTPUT_DIR = "plots"
def analyze_by_reasoning_steps(data: str, model: str, perturbation: str):
"""Analyze the data and group results based on the number of reasoning steps."""
datapoints = data.split(SEPARATOR)
if datapoints[-1] == "\n":
datapoints = datapoints[:-1]
grouped_results = defaultdict(
lambda: {"total": 0, "baseline_correct": 0, "experiment_correct": 0}
)
for datapoint in datapoints:
reasoning_steps_match = re.search(r"Reasoning Steps:\s*(\d+)", datapoint)
if reasoning_steps_match:
correct_answer = re.findall(
r">>>> Extracted Correct Answer:\s*(.*?)\n", datapoint
)[0]
baseline_response = re.findall(
r">>>> Extracted Baseline Response:\s*(.*?)\n", datapoint
)[0]
experiment_response = re.findall(
r">>>> Extracted Experiment Response:\s*(.*?)\n", datapoint
)[0]
reasoning_steps = reasoning_steps_match.group(1)
if int(reasoning_steps) >= 7 and int(reasoning_steps) <= 11:
reasoning_steps = ">7"
grouped_results[reasoning_steps]["total"] += 1
if baseline_response == correct_answer:
grouped_results[reasoning_steps]["baseline_correct"] += 1
if experiment_response == correct_answer:
grouped_results[reasoning_steps]["experiment_correct"] += 1
reasoning_steps_list = sorted(grouped_results.keys())
baseline_accuracies = []
experiment_accuracies = []
for steps in reasoning_steps_list:
results = grouped_results[steps]
total = results["total"]
baseline_accuracies.append((results["baseline_correct"] / total) * 100)
experiment_accuracies.append((results["experiment_correct"] / total) * 100)
return reasoning_steps_list, baseline_accuracies, experiment_accuracies
def create_legend_figure(labels, markers, filename):
"""Create a separate figure for the legend."""
fig_legend = plt.figure(figsize=(4, len(labels) * 0.5))
for label, marker in zip(labels, markers):
plt.plot([], [], marker=marker, label=label)
plt.axis("off")
plt.legend(loc="center", frameon=False)
plt.tight_layout()
plt.savefig(f"{OUTPUT_DIR}/{filename}")
plt.close()
def plot_results(perturbation: str, results):
"""Plot results for the given perturbation."""
# Baseline Accuracy
baseline_labels = []
baseline_markers = []
plt.figure(figsize=(12, 6))
for model, (reasoning_steps, baseline_accuracies, _) in results.items():
marker = "o"
plt.plot(reasoning_steps, baseline_accuracies, marker=marker, label=model)
baseline_labels.append(model)
baseline_markers.append(marker)
plt.title(f"Baseline Accuracy - {perturbation.capitalize()}")
plt.xlabel("Reasoning Steps")
plt.ylabel("Accuracy (%)")
plt.grid(True)
plt.tight_layout()
plt.savefig(f"{OUTPUT_DIR}/{perturbation}_baseline.png")
plt.close()
create_legend_figure(
baseline_labels, baseline_markers, f"{perturbation}_baseline_legend.png"
)
# Experiment Accuracy
experiment_labels = []
experiment_markers = []
plt.figure(figsize=(12, 6))
for model, (reasoning_steps, _, experiment_accuracies) in results.items():
marker = "x"
plt.plot(reasoning_steps, experiment_accuracies, marker=marker, label=model)
experiment_labels.append(model)
experiment_markers.append(marker)
plt.title(f"Experiment Accuracy - {perturbation.capitalize()}")
plt.xlabel("Reasoning Steps")
plt.ylabel("Accuracy (%)")
plt.grid(True)
plt.tight_layout()
plt.savefig(f"{OUTPUT_DIR}/{perturbation}_experiment.png")
plt.close()
create_legend_figure(
experiment_labels, experiment_markers, f"{perturbation}_experiment_legend.png"
)
def plot_aggregate_results(aggregated_results):
"""Plot aggregate results for all perturbations."""
# Baseline Accuracy
baseline_labels = []
baseline_markers = []
plt.figure(figsize=(12, 6))
for perturbation, data in aggregated_results.items():
reasoning_steps = sorted(data.keys())
baseline_accuracies = [data[step]["baseline"] for step in reasoning_steps]
marker = "o"
plt.plot(
reasoning_steps,
baseline_accuracies,
marker=marker,
label=perturbation.capitalize(),
)
baseline_labels.append(perturbation.capitalize())
baseline_markers.append(marker)
plt.title("Aggregate Baseline Accuracy vs. Reasoning Steps")
plt.xlabel("Reasoning Steps")
plt.ylabel("Accuracy (%)")
plt.grid(True)
plt.tight_layout()
plt.savefig(f"{OUTPUT_DIR}/aggregate_baseline.png")
plt.close()
create_legend_figure(
baseline_labels, baseline_markers, "aggregate_baseline_legend.png"
)
# Experiment Accuracy
experiment_labels = []
experiment_markers = []
plt.figure(figsize=(12, 6))
for perturbation, data in aggregated_results.items():
reasoning_steps = sorted(data.keys())
experiment_accuracies = [data[step]["experiment"] for step in reasoning_steps]
marker = "x"
plt.plot(
reasoning_steps,
experiment_accuracies,
marker=marker,
label=perturbation.capitalize(),
)
experiment_labels.append(perturbation.capitalize())
experiment_markers.append(marker)
plt.title("Aggregate Experiment Accuracy vs. Reasoning Steps")
plt.xlabel("Reasoning Steps")
plt.ylabel("Accuracy (%)")
plt.grid(True)
plt.tight_layout()
plt.savefig(f"{OUTPUT_DIR}/aggregate_experiment.png")
plt.close()
create_legend_figure(
experiment_labels, experiment_markers, "aggregate_experiment_legend.png"
)
def main():
base_path = "data/experiments"
os.makedirs(OUTPUT_DIR, exist_ok=True)
aggregated_results = {
perturbation: defaultdict(lambda: {"baseline": 0, "experiment": 0, "count": 0})
for perturbation in PERTURBATIONS
}
for perturbation in PERTURBATIONS:
results = {}
for model in sorted(
[path.lower() for path in os.listdir(base_path)], reverse=True
):
model_path = os.path.join(base_path, model)
if os.path.isdir(model_path):
pattern = f"cleaned-{perturbation}.txt"
for file in os.listdir(model_path):
if file == pattern:
file_path = os.path.join(model_path, file)
console.print(
f"[white bold]Processing file: {file_path}[/white bold]\n"
)
with open(file_path, "r") as f:
data = f.read()
reasoning_steps, baseline_accuracies, experiment_accuracies = (
analyze_by_reasoning_steps(data, model, perturbation)
)
results[model] = (
reasoning_steps,
baseline_accuracies,
experiment_accuracies,
)
# Aggregate data
for i, step in enumerate(reasoning_steps):
aggregated_results[perturbation][step][
"baseline"
] += baseline_accuracies[i]
aggregated_results[perturbation][step][
"experiment"
] += experiment_accuracies[i]
aggregated_results[perturbation][step]["count"] += 1
plot_results(perturbation, results)
# Normalize aggregate data
for perturbation, data in aggregated_results.items():
for step, values in data.items():
count = values["count"]
if count > 0:
values["baseline"] /= count
values["experiment"] /= count
plot_aggregate_results(aggregated_results)
if __name__ == "__main__":
typer.run(main)