-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate_result_comparison.py
189 lines (159 loc) · 7.23 KB
/
generate_result_comparison.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
import re
import numpy as np
import argparse
from pathlib import Path
import pandas as pd
def read_markdown_table(file_path):
with file_path.open("r") as file:
lines = file.readlines()
# Extract the teacher
teacher = lines[0].strip().split("|")[1:-1]
# Extract the rows
rows = [line.strip().split("|")[1:-1] for line in lines[2:]]
return teacher, rows
def parse_value(value):
# Handle values with ± symbol
if "±" in value:
mean, std = value.split("±")
return float(mean), float(std)
else:
return float(value), None
def create_dataframe(teachers, rows):
data = {teacher: [] for teacher in teachers}
for row in rows:
for teacher, value in zip(teachers, row):
data[teacher].append(value)
return pd.DataFrame(data)
def calculate_percentage_change(val1, val2):
if val2 != 0:
return ((val1 - val2) / abs(val2 + 10e-5)) * 100
return float("inf")
def compare_experiments(exp1_path: str, exp2_path: str, id1: str, id2: str):
experiments_paths = [Path(exp1_path), Path(exp2_path)]
tables_path = "tables"
statistics_types = ["iqm", "mean", "lowest"]
functions = ["Ackley", "Rastrigin", "Rosenbrock"]#, "Sphere"]
agent_list = ["td3_bc", "cql", "awac", "edac", "sac_n", "lb_sac","bc", "iql", "td3"]
results = {
stat_type: {func: {} for func in functions}
for stat_type in statistics_types
}
for stat_type in statistics_types:
for func in functions:
file_name = [f"{stat_type}_{func}_{id}.md" for id in [id1, id2]]
file_paths = [
exp / "ToySGD" / tables_path / file_name[i]
for i, exp in enumerate(experiments_paths)
]
if all(file_path.exists() for file_path in file_paths):
teachers_1, rows_1 = read_markdown_table(file_paths[0])
teachers_2, rows_2 = read_markdown_table(file_paths[1])
df1 = create_dataframe(teachers_1, rows_1)
df2 = create_dataframe(teachers_2, rows_2)
# Clean agent names by stripping whitespaces
df1.iloc[:, 0] = df1.iloc[:, 0].str.strip()
df2.iloc[:, 0] = df2.iloc[:, 0].str.strip()
# Find common agents excluding 'teacher'
common_agents = set(df1.iloc[:, 0]).intersection(
set(df2.iloc[:, 0])
) - {"teacher"}
if common_agents:
for agent in common_agents:
row1 = df1[df1.iloc[:, 0] == agent].iloc[0]
row2 = df2[df2.iloc[:, 0] == agent].iloc[0]
for teacher in teachers_1[1:]:
val1, std1 = parse_value(row1[teacher])
val2, std2 = parse_value(row2[teacher])
if std1 is not None and std2 is not None:
val_change = calculate_percentage_change(
val1, val2
)
std_change = calculate_percentage_change(
std1, std2
)
results[stat_type][func].setdefault(
agent, {}
).setdefault(teacher, []).append(val_change)
results[stat_type][func].setdefault(
"overall", {}
).setdefault(teacher, []).append(val_change)
else:
val_change = calculate_percentage_change(
val1, val2
)
results[stat_type][func].setdefault(
agent, {}
).setdefault(teacher, []).append(val_change)
results[stat_type][func].setdefault(
"overall", {}
).setdefault(teacher, []).append(val_change)
else:
print(
f"No common agents found for {stat_type.upper()} - {func} excluding 'teacher'\n"
)
else:
for file_path in file_paths:
if not file_path.exists():
print(f"Warning: File {file_path} does not exist.\n")
# Calculate and print mean relative changes
for stat_type, funcs in results.items():
print(f"\n{stat_type.upper()}:")
# Mean change for each agent over multiple functions
agent_changes = {}
for func, agents in funcs.items():
for agent, teachers in agents.items():
if agent != "overall" and agent in agent_list:
for teacher, changes in teachers.items():
agent_changes.setdefault(agent, []).extend(changes)
print("\nMean change for each agent:")
for agent, changes in agent_changes.items():
mean_change = np.mean(changes)
print(f" Agent: {agent} | Mean Change: {mean_change:.2f}%")
# Mean change for each function over multiple agents
func_changes = {func: [] for func in functions}
for func, agents in funcs.items():
for agent, teachers in agents.items():
if agent != "overall" and agent in agent_list:
for teacher, changes in teachers.items():
func_changes[func].extend(changes)
print("\nMean change for each function:")
for func, changes in func_changes.items():
mean_change = np.mean(changes)
print(f" Function: {func} | Mean Change: {mean_change:.2f}%")
# Mean change for each teacher over multiple functions and agents
teacher_changes = {}
for func, agents in funcs.items():
for agent, teachers in agents.items():
if agent in agent_list:
for teacher, changes in teachers.items():
teacher_changes.setdefault(teacher, []).extend(changes)
print("\nMean change for each Teacher:")
for teacher, changes in teacher_changes.items():
mean_change = np.mean(changes)
print(f" Teacher: {teacher} | Mean Change: {mean_change:.2f}%")
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Compare experiment results between two experiments."
)
parser.add_argument(
"exp1_path", type=str, help="Path to the first experiments folder."
)
parser.add_argument(
"exp2_path", type=str, help="Path to the second experiments folder."
)
parser.add_argument(
"id1",
nargs="?",
type=str,
help="ID which teacher configuration was used in the first experiment.",
default="0",
)
parser.add_argument(
"id2",
nargs="?",
type=str,
help="ID which teacher instantiation was used in the second experiment.",
default="0",
)
args = parser.parse_args()
compare_experiments(args.exp1_path, args.exp2_path, args.id1, args.id2)