forked from dice-group/favel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Analysis.py
321 lines (272 loc) · 10.7 KB
/
Analysis.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
321
"""
Execute as 'python3 Analysis.py'
"""
from os import path
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.colors as mcolors
def loadPaths():
paths = dict()
favelPath = path.realpath(__file__)
pathLst = favelPath.split('/')
favelPath = "/".join(pathLst[:-2])
paths["Overview"] = path.join(favelPath, "Evaluation/Overview.xlsx")
paths["Analysis"] = path.join(favelPath, "Analysis/")
return paths
def readOverview():
return pd.read_excel(PATHS["Overview"])
def getBpdp(df):
return df.loc[df['Dataset'] == "BPDP_Dataset"]
def getFactBench(df):
return df.loc[df['Dataset'] == "factbench-clean"]
def getFavel(df):
return df.loc[df['Dataset'] == "FinalDataset_Hard"]
def plotImprovement(df):
"""
Boxplot of improvement over all experiments
"""
plt.figure()
df = df[["Improvement"]]
plot = df.plot(kind="box", figsize=(3.5, 5.5))
fig = plot.get_figure()
fig.savefig(path.join(PATHS["Analysis"], "improvement.png"))
def plotStdDev(df, outliers=False):
"""
Boxplot of standart deviation over all experiments
"""
plt.figure()
df = df[["Testing AUC-ROC Std. Dev."]]
plot = df.plot(kind="box", figsize=(3.5, 5.5), showfliers=outliers)
fig = plot.get_figure()
fig.savefig(path.join(PATHS["Analysis"], f"stdDev{'Outliers' if outliers else ''}.png"))
def plotStdDevGood(df, outliers=False):
"""
Boxplot of standart deviation over all experiments with an Improvement > 0
"""
plt.figure()
good = df[df["Improvement"] > 0]
good = good[["Testing AUC-ROC Std. Dev."]]
plot = good.plot(kind="box", figsize=(4, 5.5), showfliers=outliers)
fig = plot.get_figure()
fig.savefig(path.join(PATHS["Analysis"], f"stdDevGood{'Outliers' if outliers else ''}.png"))
def plotPerformanceStdDev(df):
"""
Scatter plot of standard deviation depending on performance
"""
plt.figure()
df = df[["Testing AUC-ROC Mean", "Testing AUC-ROC Std. Dev."]]
plot = df.plot(x="Testing AUC-ROC Mean", y="Testing AUC-ROC Std. Dev.", kind="scatter")
fig = plot.get_figure()
fig.savefig(path.join(PATHS["Analysis"], "performance-stdDev.png"))
def plotMlAlgorithms(df):
"""
Bar chart showing the best performance grouped by ML algorithm
"""
plt.figure()
df = df[["Testing AUC-ROC Mean", "ML Algorithm"]]
gb = df.groupby(by="ML Algorithm")
result = dict()
for group in gb.groups.keys():
result[group] = float(df.loc[gb.groups[group]][["Testing AUC-ROC Mean"]].max())
series = pd.Series(result)
plot = series.plot(kind='bar', ylabel="Best AUC-ROC Score", rot=10)
fig = plot.get_figure()
fig.savefig(path.join(PATHS["Analysis"], "performance-mlAlgorithm.png"))
def plotDataset(df):
"""
Bar chart showing the best ensemble performance
compared to the best single performance grouped by dataset
"""
plt.figure()
labels = []
ensemble = []
single = []
# Get best ensemble and best single score for each dataset
df = df[["Testing AUC-ROC Mean", "Dataset", "Best Single Score"]]
gb = df.groupby(by="Dataset")
for group in gb.groups.keys():
e = float(df.loc[gb.groups[group]][["Testing AUC-ROC Mean"]].max())
s = float(df.loc[gb.groups[group]][["Best Single Score"]].max())
labels.append(group)
ensemble.append(round(e, 4))
single.append(round(s, 4))
x = np.arange(len(labels))
width = 0.35
fig, ax = plt.subplots()
rects1 = ax.bar(x - width/2, ensemble, width, label='Ensemble')
rects2 = ax.bar(x + width/2, single, width, label='Single')
ax.set_ylabel("AUC-ROC Score")
ax.set_xticks(x, labels)
ax.bar_label(rects1, padding=3)
ax.bar_label(rects2, padding=3)
fig.tight_layout()
fig.savefig(path.join(PATHS["Analysis"], "performance-dataset.png"))
def analyzeBestN(df, N:int):
"""
Scatter plot.
For every combination of two datasets, take N best configurations for the first dataset,
plot how they improve in the second dataset.
"""
datasets = dict()
datasets['bpdp'] = getBpdp(df)
datasets['factBench'] = getFactBench(df)
datasets['favel'] = getFavel(df)
for key in datasets.keys():
datasets[key].sort_values(by="Testing AUC-ROC Mean", ascending=False, inplace=True)
primaryKey = ["ML Algorithm", "ML Parameters", "Normalizer", "Iterations", "Fact Validation Approaches"]
result = {"Source Dataset": [], "Target Dataset": [], "Testing AUC-ROC Mean": [], "Improvement": []}
for i in datasets.keys():
for j in datasets.keys():
if i != j:
"""
Take N best configurations for dataset i.
Look up these configurations for dataset j.
"""
for index, row in datasets[i].head(n=N).iterrows():
tmp = _findRow(datasets[j], row, primaryKey)
if not tmp is None:
result["Source Dataset"].append(i)
result["Target Dataset"].append(j)
result["Testing AUC-ROC Mean"].append(tmp["Testing AUC-ROC Mean"])
result["Improvement"].append(tmp["Improvement"])
plt.figure()
result = pd.DataFrame(result)
# Define colors
colors = []
for index, row in result.iterrows():
if row["Source Dataset"] == "bpdp":
colors.append('g')
if row["Source Dataset"] == "factBench":
colors.append('b')
if row["Source Dataset"] == "favel":
colors.append('r')
# Plot results
for key in result:
plot = result.plot(kind="scatter", x="Target Dataset", y="Improvement", c=colors)
fig = plot.get_figure()
fig.savefig(path.join(PATHS["Analysis"], "nBest.png"))
def analyzeUniversalConfig(df):
"""
Scatter plot.
Find all configurations that have an improvement > 0 on all datasets.
"""
datasets = dict()
datasets['bpdp'] = getBpdp(df)
datasets['factBench'] = getFactBench(df)
datasets['favel'] = getFavel(df)
# Sort the datasets by performance
for key in datasets.keys():
datasets[key].sort_values(by="Testing AUC-ROC Mean", ascending=False, inplace=True)
primaryKey = ["ML Algorithm", "ML Parameters", "Normalizer", "Iterations", "Fact Validation Approaches"]
result = {"Dataset": [], "Testing AUC-ROC Mean": [], "Improvement": []}
datasetKeys = list(datasets.keys())
i = datasetKeys.pop()
mColors = list(mcolors.BASE_COLORS.keys())
mColors.remove('w')
colors = []
for index, row in datasets[i].iterrows():
if row["Improvement"] <= 0:
break
rows = dict()
for j in datasetKeys:
rows[j] = _findRow(datasets[j], row, primaryKey)
good = True
for j in rows.keys():
if rows[j]["Improvement"] <= 0:
good = False
break
if good:
c = mColors.pop(0)
colors.extend([c for z in datasets.keys()])
result["Dataset"].append(i)
result["Testing AUC-ROC Mean"].append(row["Testing AUC-ROC Mean"])
result["Improvement"].append(row["Improvement"])
for key in datasetKeys:
result["Dataset"].append(key)
result["Testing AUC-ROC Mean"].append(rows[key]["Testing AUC-ROC Mean"])
result["Improvement"].append(rows[key]["Improvement"])
plt.figure()
result = pd.DataFrame(result)
# Plot results
for key in result:
plot = result.plot(kind="scatter", x="Dataset", y="Improvement", c=colors)
fig = plot.get_figure()
fig.savefig(path.join(PATHS["Analysis"], "universalConfig.png"))
def analyzeUniversalGoodConfig(df):
"""
Scatter plot.
Find configurations that improve over the best single score in every dataset.
"""
datasets = dict()
datasets['bpdp'] = getBpdp(df)
datasets['factBench'] = getFactBench(df)
datasets['favel'] = getFavel(df)
bestSingleScore = dict()
for dataset in datasets.keys():
bestSingleScore[dataset] = float(datasets[dataset][["Best Single Score"]].max())
# Sort the datasets by performance
for key in datasets.keys():
datasets[key].sort_values(by="Testing AUC-ROC Mean", ascending=False, inplace=True)
primaryKey = ["ML Algorithm", "ML Parameters", "Normalizer", "Iterations", "Fact Validation Approaches"]
result = {"Dataset": [], "Testing AUC-ROC Mean": [], "Improvement": []}
datasetKeys = list(datasets.keys())
i = datasetKeys.pop()
mColors = list(mcolors.BASE_COLORS.keys())
mColors.remove('w')
colors = []
for index, row in datasets[i].iterrows():
if row["Testing AUC-ROC Mean"] < bestSingleScore[i]:
break
rows = dict()
for j in datasetKeys:
rows[j] = _findRow(datasets[j], row, primaryKey)
good = True
for j in rows.keys():
if rows[j]["Testing AUC-ROC Mean"] <= bestSingleScore[j]:
good = False
break
if good:
c = mColors.pop(0)
colors.extend([c for z in datasets.keys()])
result["Dataset"].append(i)
result["Testing AUC-ROC Mean"].append(row["Testing AUC-ROC Mean"])
result["Improvement"].append(row["Improvement"])
#print(f"Universally good configuration {row['Experiment']}")
for key in datasetKeys:
result["Dataset"].append(key)
result["Testing AUC-ROC Mean"].append(rows[key]["Testing AUC-ROC Mean"])
result["Improvement"].append(rows[key]["Improvement"])
#print(f"Universally good configuration {rows[key]['Experiment']}")
plt.figure()
result = pd.DataFrame(result)
# Plot results
for key in result:
plot = result.plot(kind="scatter", x="Dataset", y="Improvement", c=colors)
fig = plot.get_figure()
fig.savefig(path.join(PATHS["Analysis"], "universalGoodConfig.png"))
def _findRow(df, row, keys):
result = dict()
for key in keys:
result[key] = set(df.index[df[key] == row[key]].tolist())
intersect = None
for key in keys:
if intersect is None:
intersect = result[key]
else:
intersect &= result[key]
for i in intersect:
return df.loc[i]
PATHS = loadPaths()
df = readOverview()
plotImprovement(df)
plotStdDev(df, True)
plotStdDevGood(df, True)
plotStdDev(df, False)
plotStdDevGood(df, False)
plotPerformanceStdDev(df)
plotMlAlgorithms(df)
plotDataset(df)
analyzeBestN(df, 5)
analyzeUniversalConfig(df)
analyzeUniversalGoodConfig(df)