-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinterpret_results.py
320 lines (223 loc) · 11.4 KB
/
interpret_results.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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
import os
import numpy as np
import pandas as pd
import networkx as nx
import matplotlib.pyplot as plt
from functools import reduce
from networkx.drawing.nx_agraph import write_dot, graphviz_layout
from sklearn.metrics import confusion_matrix, ConfusionMatrixDisplay, classification_report
from taxonomy import source_node_label
from vizualizations import plot_confusion_matrix, plot_roc_curves
def get_indices_where(arr, target):
to_return = []
for i, obs in enumerate(arr):
if obs == target:
to_return.append(i)
return to_return
def get_conditional_probabilites(y_pred, tree):
# Create a new arrays to store pseudo (conditional) probabilities.
pseudo_conditional_probabilities = np.copy(y_pred)
pseudo_probabilities = np.copy(y_pred)
level_order_nodes = list(nx.bfs_tree(tree, source=source_node_label).nodes())
parents = [list(tree.predecessors(node)) for node in level_order_nodes]
for idx in range(len(parents)):
# Make sure the graph is a tree.
assert len(parents[idx]) == 0 or len(parents[idx]) == 1, 'Number of parents for each node should be 0 (for root) or 1.'
if len(parents[idx]) == 0:
parents[idx] = ''
else:
parents[idx] = parents[idx][0]
# Finding unique parents for masking
unique_parents = list(set(parents))
unique_parents.sort()
# Create masks for applying soft max and calculating loss values.
masks = []
for parent in unique_parents:
masks.append(get_indices_where(parents, parent))
# Get the masked softmaxes
for mask in masks:
pseudo_probabilities[:, mask] = np.exp(y_pred[:, mask]) / np.sum(np.exp(y_pred[:, mask]), axis=1, keepdims=True)
for node in level_order_nodes:
# Find path from node to
path = list(nx.shortest_path(tree, source_node_label, node))
# Get the index of the node for which we are calculating the pseudo probability
node_index = level_order_nodes.index(node)
# Indices of all the classes in the path from source to the node for which we are calculating the pseudo probability
path_indices = [level_order_nodes.index(u) for u in path]
#print(node, path, node_index, path_indices, pseudo_probabilities[:, path_indices])
# Multiply the pseudo probabilites of all the classes in the path so that we get the conditional pseudo probabilites
pseudo_conditional_probabilities[:, node_index] = np.prod(pseudo_probabilities[:, path_indices], axis = 1)
return pseudo_probabilities, pseudo_conditional_probabilities
def save_leaf_cf_and_rocs(y_true, y_pred, tree, model_dir, plot_title):
# Find the indexes of the leaf nodes i.e. nodes with out degree = 0.
level_order_nodes = list(nx.bfs_tree(tree, source=source_node_label).nodes())
n_children = [tree.out_degree(node) for node in level_order_nodes]
idx = get_indices_where(n_children, 0)
leaf_labels = np.array(level_order_nodes)[idx]
y_pred_label = [leaf_labels[i] for i in np.argmax(y_pred[:, idx], axis=1)]
y_true_label = [leaf_labels[i] for i in np.argmax(y_true[:, idx], axis=1)]
# Make the dirs to store results
os.makedirs(f"{model_dir}/gif/leaf_cf", exist_ok=True)
os.makedirs(f"{model_dir}/gif/leaf_roc", exist_ok=True)
os.makedirs(f"{model_dir}/gif/leaf_csv", exist_ok=True)
csv_plot_file = f"{model_dir}/gif/leaf_csv/{plot_title}.csv"
plot_confusion_matrix(y_true_label, y_pred_label, leaf_labels, plot_title, f"{model_dir}/gif/leaf_cf/{plot_title}.png")
plt.close()
plot_confusion_matrix(y_true_label, y_pred_label, leaf_labels, plot_title, f"{model_dir}/gif/leaf_cf/{plot_title}.pdf")
plt.close()
plot_roc_curves(y_true[:, idx], y_pred[:, idx], leaf_labels, plot_title, f"{model_dir}/gif/leaf_roc/{plot_title}.png")
plt.close()
plot_roc_curves(y_true[:, idx], y_pred[:, idx], leaf_labels, plot_title, f"{model_dir}/gif/leaf_roc/{plot_title}.pdf")
plt.close()
report = classification_report(y_true_label, y_pred_label)
print(report)
report = classification_report(y_true_label, y_pred_label, output_dict=True)
pd.DataFrame(report).transpose().to_csv(csv_plot_file)
print('===========')
def save_all_cf_and_rocs(y_true, y_pred, tree, model_dir, plot_title):
def get_path_length(tree, source, target):
return len(nx.shortest_path(tree, source=source, target=target)) - 1
# Find the parents
level_order_nodes = list(nx.bfs_tree(tree, source=source_node_label).nodes())
depths = [get_path_length(tree, source_node_label, node) for node in level_order_nodes]
# Finding unique depths for masking
unique_depths = list(set(depths))
unique_depths.sort()
# Create masks for classification
masks = []
for depth in unique_depths:
masks.append(get_indices_where(depths, depth))
# Get the masked softmaxes
for mask, depth in zip(masks, unique_depths):
# Every alert
if depth != 0 and depth != 3:
true_labels = []
pred_labels = []
mask_classes = [level_order_nodes[m] for m in mask]
for i in range(y_true.shape[0]):
# Find the true label
true_class_idx = np.argmax(y_true[i, mask])
true_labels.append(mask_classes[true_class_idx])
# Find the predicted label
predicted_class_idx = np.argmax(y_pred[i, mask])
pred_labels.append(mask_classes[predicted_class_idx])
# Create the dirs to save plots
os.makedirs(f"{model_dir}/gif/level_{depth}_cf", exist_ok=True)
os.makedirs(f"{model_dir}/gif/level_{depth}_roc", exist_ok=True)
os.makedirs(f"{model_dir}/gif/level_{depth}_csv", exist_ok=True)
csv_plot_file = f"{model_dir}/gif/level_{depth}_csv/{plot_title}.csv"
plot_confusion_matrix(true_labels, pred_labels, mask_classes, plot_title, f"{model_dir}/gif/level_{depth}_cf/{plot_title}.png")
plt.close()
plot_confusion_matrix(true_labels, pred_labels, mask_classes, plot_title, f"{model_dir}/gif/level_{depth}_cf/{plot_title}.pdf")
plt.close()
plot_roc_curves(y_true[:, mask], y_pred[:, mask], mask_classes, plot_title, f"{model_dir}/gif/level_{depth}_roc/{plot_title}.png")
plt.close()
plot_roc_curves(y_true[:, mask], y_pred[:, mask], mask_classes, plot_title, f"{model_dir}/gif/level_{depth}_roc/{plot_title}.pdf")
plt.close()
report = classification_report(true_labels, pred_labels)
print(report)
report = classification_report(true_labels, pred_labels, output_dict=True)
pd.DataFrame(report).transpose().to_csv(csv_plot_file)
print('===========')
def save_all_phase_vs_accuracy_plot(model_dir, days = 2 ** np.array(range(11)), levels = ["level_1", "level_2", "leaf"]):
plt.style.use(['default'])
# Making the f1 plot
for level in levels:
f1 = []
for d in days:
df_alpha1 = pd.read_csv(f'{model_dir}/gif/{level}_csv/Trigger + {d} days.csv')
f1.append(df_alpha1['f1-score'].to_numpy()[-2])
plt.plot(days, f1, label=level, marker = 'o')
plt.xlabel("Days from first detection", fontsize='xx-large')
plt.ylabel("Macro avg F1 score", fontsize='xx-large')
plt.grid()
plt.tight_layout()
plt.legend(loc='lower right')
plt.xscale('log')
plt.ylim(0.5, 1.01)
plt.xticks(days, days)
plt.savefig(f"{model_dir}/f1-performance.pdf")
plt.savefig(f"{model_dir}/f1-performance.jpg")
plt.close()
for level in levels:
precision = []
for d in days:
df_alpha1 = pd.read_csv(f'{model_dir}/gif/{level}_csv/Trigger + {d} days.csv')
precision.append(df_alpha1['precision'].to_numpy()[-2])
plt.plot(days, precision, label=level, marker = 'o')
plt.xlabel("Days from first detection", fontsize='xx-large')
plt.ylabel("Macro avg precision", fontsize='xx-large')
plt.grid()
plt.tight_layout()
plt.legend(loc='lower right')
plt.xscale('log')
plt.ylim(0.5, 1.01)
plt.xticks(days, days)
plt.savefig(f"{model_dir}/precision-performance.pdf")
plt.savefig(f"{model_dir}/precision-performance.jpg")
plt.close()
for level in levels:
recall = []
for d in days:
df_alpha1 = pd.read_csv(f'{model_dir}/gif/{level}_csv/Trigger + {d} days.csv')
recall.append(df_alpha1['recall'].to_numpy()[-2])
plt.plot(days, recall, label=level, marker = 'o')
plt.xlabel("Days from first detection", fontsize='xx-large')
plt.ylabel("Macro avg recall", fontsize='xx-large')
plt.grid()
plt.tight_layout()
plt.legend(loc='lower right')
plt.xscale('log')
plt.ylim(0.5, 1.01)
plt.xticks(days, days)
plt.savefig(f"{model_dir}/recall-performance.pdf")
plt.savefig(f"{model_dir}/recall-performance.jpg")
plt.close()
def save_class_wise_phase_vs_accuracy_plot(model_dir, days = 2 ** np.array(range(11)), levels = ["level_1", "level_2", "leaf"]):
plt.style.use(['default'])
cm = plt.get_cmap('gist_rainbow')
# Making the f1 plot
for level in levels:
class_wise_f1s = {}
for d in days:
df_alpha1 = pd.read_csv(f'{model_dir}/gif/{level}_csv/Trigger + {d} days.csv')
classes = df_alpha1.iloc[:-3,0].to_numpy()
f1 = df_alpha1['f1-score'].to_numpy()[:-3]
for i, c in enumerate(classes):
if c not in class_wise_f1s:
class_wise_f1s[c] = []
class_wise_f1s[c].append(f1[i])
for i, c in enumerate(classes):
if i >= 10:
linestyle='dotted'
plt.plot(days, class_wise_f1s[c], label=c, marker = '*', alpha=0.5, linestyle=linestyle)
else:
linestyle='dashed'
plt.plot(days, class_wise_f1s[c], label=c, marker = '.', alpha=0.5, linestyle=linestyle)
plt.xscale('log')
if len(classes) > 10:
plt.ylim(-0.1, 1.01)
plt.legend(ncol=5, fontsize=7, loc='lower right')
else:
plt.legend()
plt.xticks(days, days)
#plt.ylim(0, 1.1)
plt.xlabel("Days from first detection", fontsize='xx-large')
plt.ylabel("F1 score", fontsize='xx-large')
plt.tight_layout()
plt.savefig(f"{model_dir}/per_class_{level}_F1.pdf")
plt.close()
def merge_performance_tables(model_dir, days=[2,8,64,1024]):
levels = ['level_1','level_2','leaf']
for level in levels:
data_frames = []
for d in days:
df = pd.read_csv(f"{model_dir}/gif/{level}_csv/Trigger + {d} days.csv", index_col=0)
df.drop(columns=["support"], inplace=True)
df.index.name = 'Class'
df.rename(columns={'precision': '$p_{' + f"{d}d" + '}$'}, inplace=True)
df.rename(columns={'recall': '$r_{' + f"{d}d" + '}$'}, inplace=True)
df.rename(columns={'f1-score': '$f1_{' + f"{d}d" + '}$'}, inplace=True)
data_frames.append(df)
df_merged = reduce(lambda left,right: pd.merge(left,right, how='left',on='Class', sort=False), data_frames)
print(df_merged.to_latex(float_format="%.2f"))