-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathablation_study_plot.py
More file actions
153 lines (133 loc) · 4.57 KB
/
Copy pathablation_study_plot.py
File metadata and controls
153 lines (133 loc) · 4.57 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
import os
import glob
import json
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import os
import glob
import json
import matplotlib.pyplot as plt
# Path settings
json_dir = 'results'
pattern = os.path.join(json_dir, 'ablation_config_*.json')
json_files = sorted(glob.glob(pattern))
print(f"Found {len(json_files)} JSON files.")
# Load all results
results = []
for path in json_files:
with open(path, 'r') as f:
data = json.load(f)
cfg = data['config']
label = f"P{cfg['position']}_C{cfg['community']}_O{cfg['objects']}_E{cfg['edge_weight']}"
results.append({
'config': label,
'gcn_train_loss': data.get('gcn_train_loss', []),
'gcn_val_acc': data.get('gcn_val_acc', []),
'gcn_test_acc': data.get('gcn_test_acc', None),
'sage_train_loss': data.get('sage_train_loss', []),
'sage_val_acc': data.get('sage_val_acc', []),
'sage_test_acc': data.get('sage_test_acc', None),
'gine_train_loss': data.get('gine_train_loss', []),
'gine_val_acc': data.get('gine_val_acc', []),
'gine_test_acc': data.get('gine_test_acc', None),
})
# Ensure results directory
out_dir = os.path.join(json_dir, 'ab_results')
os.makedirs(out_dir, exist_ok=True)
models = ['gcn', 'sage', 'gine']
# Plot train loss and val acc for each model with horizontal legends
for model in models:
fig, ax1 = plt.subplots(figsize=(12, 6))
ax2 = ax1.twinx()
loss_lines, loss_labels = [], []
acc_lines, acc_labels = [], []
for res in results:
epochs = range(1, len(res[f'{model}_train_loss']) + 1)
# plot loss
line_loss, = ax1.plot(
epochs,
res[f'{model}_train_loss'],
label=f"{res['config']} loss"
)
loss_lines.append(line_loss)
loss_labels.append(f"{res['config']} loss")
# plot validation accuracy
if res[f'{model}_val_acc']:
line_acc, = ax2.plot(
epochs,
res[f'{model}_val_acc'],
'--',
label=f"{res['config']} acc"
)
acc_lines.append(line_acc)
acc_labels.append(f"{res['config']} acc")
# axis labels
ax1.set_xlabel('Epoch')
ax1.set_ylabel('Training Loss')
ax2.set_ylabel('Validation Accuracy')
# # Horizontal legend for loss at top center
# ax1.legend(
# loss_lines,
# loss_labels,
# loc='upper center',
# bbox_to_anchor=(0.5, 1.3),
# ncol=5,
# title='Loss'
# )
# Horizontal legend for validation accuracy at bottom center
ax2.legend(
acc_lines+loss_lines,
acc_labels+loss_labels,
loc='upper center',
bbox_to_anchor=(0.5, -0.2),
ncol=5,
)
fig.suptitle(f'{model.upper()} Training Loss & Validation Accuracy')
fig.tight_layout(rect=[0, 0, 1, 0.95])
fig.savefig(os.path.join(out_dir, f'{model}_train_val_horizontal_legends.png'))
plt.close(fig)
# Plot combined test accuracy for all models
configs = [res['config'] for res in results]
indices = np.arange(len(configs))
bar_width = 0.25
fig, ax = plt.subplots(figsize=(10, 6))
# Bars for each model
bars_gcn = ax.bar(indices - bar_width, [res['gcn_test_acc'] for res in results],
width=bar_width, label='GCN')
bars_sage = ax.bar(indices, [res['sage_test_acc'] for res in results],
width=bar_width, label='GraphSAGE')
bars_gine = ax.bar(indices + bar_width, [res['gine_test_acc'] for res in results],
width=bar_width, label='GINE')
# Annotate each bar with its height
for bar in list(bars_gcn) + list(bars_sage) + list(bars_gine):
height = bar.get_height()
ax.text(
bar.get_x() + bar.get_width() / 2,
height,
f'{height:.3f}',
ha='center',
va='center',
rotation=90
)
ax.set_xlabel('Config')
ax.set_ylabel('Test Accuracy')
ax.set_title('Test Accuracy per Config for All Models')
ax.set_xticks(indices)
ax.set_xticklabels(configs, rotation=45, ha='right')
ax.legend()
fig.tight_layout()
fig.savefig(os.path.join(out_dir, 'all_models_test_acc.png'))
plt.close(fig)
print(f"Plots saved in {out_dir}")
# save acc results to json
acc_results = {
'config': configs,
'gcn_test_acc': [res['gcn_test_acc'] for res in results],
'sage_test_acc': [res['sage_test_acc'] for res in results],
'gine_test_acc': [res['gine_test_acc'] for res in results],
}
acc_json_path = os.path.join(out_dir, 'test_acc_results.json')
with open(acc_json_path, 'w') as f:
json.dump(acc_results, f)
print(f"Test accuracy results saved to {acc_json_path}")