-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplot_results.py
More file actions
227 lines (180 loc) · 8.34 KB
/
plot_results.py
File metadata and controls
227 lines (180 loc) · 8.34 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
import os
import json
import glob
import matplotlib.pyplot as plt
def get_latest_results():
"""Get the most recent results file from the results directory."""
results_dir = "results"
result_files = glob.glob(os.path.join(results_dir, "results_*.json"))
if not result_files:
raise FileNotFoundError("No results files found")
latest_file = max(result_files, key=os.path.getctime)
with open(latest_file, 'r') as f:
return json.load(f)
def create_markdown_tables(results):
"""Create markdown tables for regression and classification results."""
regression_results = []
classification_results = []
for result in results['results']:
if result['status'] != 'completed':
continue
row = {
'Dataset': result['dataset'],
'Synthetic': str(result['is_synthetic']),
'Samples': result['n_samples'],
result['final_metrics']['metric']['name'] + '@KappaML': round(
result['final_metrics']['metric']['value'], 4
),
result['final_metrics']['metric']['name'] + '@Local': round(
result['local_final_metrics'], 4
)
}
# Add the first baseline model's metric if available
if result['baseline_metrics'] and len(result['baseline_metrics']) > 0:
first_baseline_name = next(iter(result['baseline_metrics']))
first_baseline = result['baseline_metrics'][first_baseline_name]
if first_baseline and len(first_baseline) > 0:
final_baseline_metric = first_baseline[-1]['metrics']['metric']['value']
row[result['final_metrics']['metric']['name'] + f'@{first_baseline_name}'] = round(
final_baseline_metric, 4
)
if result['task'] == 'regression':
regression_results.append(row)
else:
classification_results.append(row)
def create_table(results, title):
if not results:
return f"### {title}\nNo results available.\n\n"
headers = list(results[0].keys())
header_row = " | ".join(headers)
separator = "|".join(["-" * len(h) for h in headers])
rows = []
for result in results:
row = " | ".join(str(result[h]) for h in headers)
rows.append(row)
table = f"### {title}\n| {header_row} |\n| {separator} |\n"
table += "\n".join(f"| {row} |" for row in rows) + "\n\n"
return table
reg_table = create_table(regression_results, "Regression Results")
class_table = create_table(classification_results, "Classification Results")
return reg_table, class_table
def plot_task_results(results, task):
"""Plot all models for a specific task type."""
task_results = [
r for r in results['results']
if r['task'] == task and r['status'] == 'completed'
]
if not task_results:
return
plt.figure(figsize=(12, 6))
for result in task_results:
samples = [m['samples'] for m in result['metrics']]
scores = [m['metrics']['metric']['value'] for m in result['metrics']]
label = f"{result['dataset']} "
label += f"({'synthetic' if result['is_synthetic'] else 'real'})"
plt.plot(samples, scores, label=label)
plt.xlabel('Samples')
plt.ylabel(result['final_metrics']['metric']['name'])
plt.title(f'{task.capitalize()} Models Performance')
plt.legend(bbox_to_anchor=(1.05, 1), loc='upper left')
plt.tight_layout()
plt.savefig(f'figures/{task}_results.png')
plt.close()
def plot_individual_models(results):
"""Create composite figures with separate plots for real and synthetic datasets in regression
and classification tasks."""
# Separate results by task and dataset type
regression_real = [
r for r in results['results']
if r['task'] == 'regression' and not r['is_synthetic'] and r['status'] == 'completed'
]
regression_synthetic = [
r for r in results['results']
if r['task'] == 'regression' and r['is_synthetic'] and r['status'] == 'completed'
]
classification_real = [
r for r in results['results']
if r['task'] == 'classification' and not r['is_synthetic'] and r['status'] == 'completed'
]
classification_synthetic = [
r for r in results['results']
if r['task'] == 'classification' and r['is_synthetic'] and r['status'] == 'completed'
]
def create_task_figure(task_results, task_name, dataset_type):
if not task_results:
return
# Calculate grid dimensions
n_plots = len(task_results)
n_cols = min(3, n_plots) # Max 3 plots per row
n_rows = (n_plots + n_cols - 1) // n_cols
# Create figure
fig = plt.figure(figsize=(15, 5 * n_rows))
title = f'{task_name} Models Performance - {dataset_type} Datasets'
fig.suptitle(title, fontsize=16, y=1.02)
for idx, result in enumerate(task_results, 1):
ax = fig.add_subplot(n_rows, n_cols, idx)
# Plot KappaML performance
samples = [m['samples'] for m in result['metrics']]
scores = [m['metrics']['metric']['value'] for m in result['metrics']]
ax.plot(samples, scores, label='KappaML', linewidth=2)
# Plot baseline models
for baseline_name, baseline_metrics in result['baseline_metrics'].items():
baseline_samples = [m['samples'] for m in baseline_metrics]
baseline_scores = [m['metrics']['metric']['value'] for m in baseline_metrics]
ax.plot(baseline_samples, baseline_scores, '--', label=f'Baseline: {baseline_name}')
ax.set_xlabel('Samples')
ax.set_ylabel(result['final_metrics']['metric']['name'])
ax.set_title(f"{result['dataset']}")
ax.grid(True, alpha=0.3)
ax.legend(fontsize='small')
plt.tight_layout()
# Save plot
filename = f"figures/{task_name.lower()}_{dataset_type.lower()}_composite.png"
plt.savefig(filename, bbox_inches='tight', dpi=300)
plt.close()
# Create composite figures for each task and dataset type
create_task_figure(regression_real, 'Regression', 'Real')
create_task_figure(regression_synthetic, 'Regression', 'Synthetic')
create_task_figure(classification_real, 'Classification', 'Real')
create_task_figure(classification_synthetic, 'Classification', 'Synthetic')
def plot_individual_model(result):
"""Create individual plot for a single model."""
if result['status'] != 'completed':
return
plt.figure(figsize=(10, 6))
# Plot KappaML performance
samples = [m['samples'] for m in result['metrics']]
scores = [m['metrics']['metric']['value'] for m in result['metrics']]
plt.plot(samples, scores, label='KappaML', linewidth=2)
# Plot baseline models
for baseline_name, baseline_metrics in result['baseline_metrics'].items():
baseline_samples = [m['samples'] for m in baseline_metrics]
baseline_scores = [m['metrics']['metric']['value'] for m in baseline_metrics]
plt.plot(baseline_samples, baseline_scores, '--', label=f'Baseline: {baseline_name}')
plt.xlabel('Samples')
plt.ylabel(result['final_metrics']['metric']['name'])
plt.title(f"{result['dataset']} ({'synthetic' if result['is_synthetic'] else 'real'})")
plt.grid(True, alpha=0.3)
plt.legend()
plt.tight_layout()
# Save individual plot
filename = f"figures/model_{result['dataset'].lower()}.png"
plt.savefig(filename, bbox_inches='tight', dpi=300)
plt.close()
def main():
# Load results
results = get_latest_results()
# Create markdown tables
reg_table, class_table = create_markdown_tables(results)
# Update results Markdown file
with open('results/results.md', 'w') as f:
f.write(reg_table + class_table)
# Create plots
plot_task_results(results, 'regression')
plot_task_results(results, 'classification')
plot_individual_models(results)
# Create individual model plots
for result in results['results']:
plot_individual_model(result)
if __name__ == "__main__":
main()