-
Notifications
You must be signed in to change notification settings - Fork 0
/
evaluate.py
400 lines (357 loc) · 17.2 KB
/
evaluate.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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
from audioop import avg
import os
import pandas as pd
from tqdm import tqdm
from re import sub
import numpy as np
from sklearn.utils import shuffle
import re
import sys
from setup import *
from sklearn.metrics import f1_score, precision_recall_curve, confusion_matrix
import matplotlib.pyplot as plt
from sklearn.metrics import PrecisionRecallDisplay, RocCurveDisplay
import seaborn as sns
from sklearn.metrics import classification_report
def plot_pr_curve(title, y_pred, y_test):
#_, ax = plt.subplots(figsize=(7, 8))
precision, recall, thresholds = precision_recall_curve(np.array(y_test), np.array(y_pred))
display = PrecisionRecallDisplay(recall = recall, precision = precision)
display.plot(name=f"Precision-recall")
plt.ylim([0.5, 1.0])
plt.savefig(os.path.join(OUT_DIR, EVALUATION_FOLDER, title+".pdf"), bbox_inches='tight')
def plot_roc_curve(title, y_pred, y_test):
display = RocCurveDisplay.from_predictions(y_test, y_pred, name="LinearSVC")
_ = display.ax_.set_title(title)
_ = display.ax_.plot()
_ = display.ax_.set_xlabel('FP Rate')
_ = display.ax_.set_ylabel('TP Rate')
plt.xlim(0, 1)
plt.ylim(0, 1)
plt.savefig(os.path.join(OUT_DIR, EVALUATION_FOLDER, title+".pdf"), bbox_inches='tight')
def gini(x):
'''
Calculates the Gini Coefficient.
:param x: The pd series to calculate on
:return: the Gini Coefficient
'''
x = x.reset_index(drop=True)
w = np.ones_like(x)
w = pd.Series(w).reset_index(drop=True)
n = x.size
wxsum = sum(w * x)
wsum = sum(w)
sxw = np.argsort(x)
sx = x[sxw] * w[sxw]
sw = w[sxw]
pxi = np.cumsum(sx) / wxsum
pci = np.cumsum(sw) / wsum
g = 0.0
for i in np.arange(1, n):
g = g + pxi.iloc[i] * pci.iloc[i - 1] - pci.iloc[i] * pxi.iloc[i - 1]
return g
def eval(df, dataset, method, eval_df, eval_string):
actual = pd.Series(df[PARAPHRASE], name='Actual')
predicted = pd.Series(df[method], name='Predicted')
df_cross = pd.crosstab(actual, predicted)
f1 = f1_score(actual, predicted)
# if there were no predicted non-paraphrases insert 0
try:
tp = df_cross[1][1]
except Exception as e:
tp = 0
pass
try:
tn = df_cross[0][0]
except Exception as e:
tn = 0
pass
try:
fp = df_cross[1][0]
except Exception as e:
fp = 0
pass
try:
fn = df_cross[0][1]
except Exception as e:
fn = 0
pass
specificity = None
if (tn+fp) != 0:
specificity = float(tn / (tn+fp))
eval_df.loc[len(eval_df.index)] = [
dataset,
method,
str(len(df)), # amount of sentence pairs in dataset
tp,
tn,
fp,
fn,
float((tn+tp) / len(df)), # accuracy
float(tp / (tp+fp)), # precision
float(tp / (tp+fn)), # recall
specificity, # specificity (true negative rate)
f1
]
eval_string = eval_string + "------> " + dataset + ":\n" + str(classification_report(actual, predicted)) + "\n\n"
return eval_df, eval_string
if __name__ == "__main__":
c_pal = sns.color_palette("colorblind")
plt.rcParams.update({'font.size': 30})
eval_df = pd.DataFrame()
eval_string = ""
eval_string += "-------------------------" + "\n"
eval_string += "OVERALL DATA STATISTICS" + "\n"
eval_string += "-------------------------" + "\n"
eval_string += "Processed datasets: "
for d in DATASETS:
eval_string += d + ", "
eval_string = eval_string[:-2] + "\n\n"
df_test = pd.read_json(os.path.join(OUT_DIR, DETECTION_FOLDER, "detection_test_result.json"), orient="index")
for dataset in DATASETS:
df = df_test[df_test[DATASET] == dataset]
if df.shape[0] == 0:
continue
print(f"---> Evaluating {dataset}...")
print("Data size of the " + str(dataset) + " test split: " + str(len(df)))
for method in DETECTION_METHODS:
print("-> Method: " + method)
#print("Calculating precision-recall curve...")
#plot_pr_curve(method + "_" + dataset + "_pr", y_pred=np.array(df[method].tolist()), y_test=np.array(df[PARAPHRASE].tolist()).astype(int))
#plot_roc_curve(method + "_" + dataset + "_roc", y_pred=np.array(df[method].tolist()), y_test=np.array(df[PARAPHRASE].tolist()).astype(int))
print("Getting statistics...")
confusion_mat = confusion_matrix(y_true=df[PARAPHRASE].tolist(), y_pred=df[method+PRED_SUF].tolist())
fp = confusion_mat[0][1]
fn = confusion_mat[1][0]
tp = confusion_mat[1][1]
tn = confusion_mat[0][0]
eval_df = pd.concat([eval_df, pd.DataFrame({
DATASET: dataset,
METHOD: method,
PAIRS: df.shape[0],
PARA_PAIRS: df[df[PARAPHRASE] == True].shape[0],
ORIG_PAIRS: df[df[PARAPHRASE] == False].shape[0],
FP: fp,
FN: fn,
TP: tp,
TN: tn,
F1: f1_score(y_true=df[PARAPHRASE].tolist(), y_pred=df[method+PRED_SUF].tolist()),
GINI_PRED: gini(df[method+PRED_SUF]),
GINI_PROB: gini(df[method])
}, index=[dataset+"_"+method])])
eval_df = pd.concat([eval_df, pd.DataFrame({
DATASET: dataset,
METHOD: None,
PAIRS: eval_df[eval_df[DATASET] == dataset][PAIRS].mean(),
PARA_PAIRS: eval_df[eval_df[DATASET] == dataset][PARA_PAIRS].mean(),
ORIG_PAIRS: eval_df[eval_df[DATASET] == dataset][ORIG_PAIRS].mean(),
FP: eval_df[eval_df[DATASET] == dataset][FP].mean(),
FN: eval_df[eval_df[DATASET] == dataset][FN].mean(),
TP: eval_df[eval_df[DATASET] == dataset][TP].mean(),
TN: eval_df[eval_df[DATASET] == dataset][TN].mean(),
F1: eval_df[eval_df[DATASET] == dataset][F1].mean(),
GINI_PRED: eval_df[eval_df[DATASET] == dataset][GINI_PRED].mean(),
GINI_PROB: eval_df[eval_df[DATASET] == dataset][GINI_PROB].mean()
}, index=[dataset + "_MEAN"])])
# mean per method across datasets
for method in DETECTION_METHODS:
eval_df = pd.concat([eval_df, pd.DataFrame({
DATASET: None,
METHOD: method,
PAIRS: eval_df[eval_df[METHOD] == method][PAIRS].mean(),
PARA_PAIRS: eval_df[eval_df[METHOD] == method][PARA_PAIRS].mean(),
ORIG_PAIRS: eval_df[eval_df[METHOD] == method][ORIG_PAIRS].mean(),
FP: eval_df[eval_df[METHOD] == method][FP].mean(),
FN: eval_df[eval_df[METHOD] == method][FN].mean(),
TP: eval_df[eval_df[METHOD] == method][TP].mean(),
TN: eval_df[eval_df[METHOD] == method][TN].mean(),
F1: eval_df[eval_df[METHOD] == method][F1].mean(),
GINI_PRED: eval_df[eval_df[METHOD] == method][GINI_PRED].mean(),
GINI_PROB: eval_df[eval_df[METHOD] == method][GINI_PROB].mean()
}, index=["MEAN_"+method])])
eval_df.to_json(os.path.join(OUT_DIR, EVALUATION_FOLDER, EVALUATION_RESULTS_FILENAME), orient = "index", index = True, indent = 4)
eval_df.to_csv(os.path.join(OUT_DIR, EVALUATION_FOLDER, EVALUATION_RESULTS_FILENAME.replace(".json", ".csv")), index = True)
with open(os.path.join(OUT_DIR, EVALUATION_FOLDER, "eval_stats.txt"), 'w') as f:
f.write(eval_string)
# ------ Correlations
print("\nGenerating correlation graphs for datasets (mixed).")
plt.rcParams.update({'font.size': 15})
# machine-paraphrases & human-paraphrases
corr_df = shuffle(df_test)\
.reset_index(drop=True).drop(columns=df_test.columns.difference(DETECTION_METHODS))
# machine-paraphrases
corr_df_m = shuffle(df_test[(df_test[DATASET].isin(MACHINE_PARAPHRASED_DATASETS))])\
.reset_index(drop=True).drop(columns=df_test.columns.difference(DETECTION_METHODS))
# human-paraphrases
corr_df_h = shuffle(df_test[(df_test[DATASET].isin(HUMAN_PARAPHRASED_DATASETS))])\
.reset_index(drop=True).drop(columns=df_test.columns.difference(DETECTION_METHODS))
for method1 in tqdm(DETECTION_METHODS):
for method2 in DETECTION_METHODS:
if method1 != method2:
# get the correct axis names in nicer writing
if method1 == SEM_BERT:
xlabel = "BERT"
elif method1 == SEM_T5:
xlabel = "T5"
elif method1 == FUZZY:
xlabel = "Fuzzy"
elif method1 == TFIDF_COSINE:
xlabel = "Tf-idf Cosine"
elif method1 == NGRAM3:
xlabel = "3-Gram"
elif method1 == FASTTEXT:
xlabel = "Fasttext"
elif method1 == SEM_GLOVE:
xlabel = "GloVe"
else:
xlabel = "Unknown Method"
if method2 == SEM_BERT:
ylabel = "BERT"
elif method2 == SEM_T5:
ylabel = "T5"
elif method2 == FUZZY:
ylabel = "Fuzzy"
elif method2 == TFIDF_COSINE:
ylabel = "Tf-idf Cosine"
elif method2 == NGRAM3:
ylabel = "3-Gram"
elif method2 == SEM_GLOVE:
ylabel = "GloVe"
elif method2 == FASTTEXT:
ylabel = "Fasttext"
else:
ylabel = "Unknown Method"
this_corr_df_m = corr_df_m[corr_df_m[method1].notnull() & corr_df_m[method2].notnull()].truncate(after=CORR_GRAPH_SIZE)
this_corr_df_h = corr_df_h[corr_df_h[method1].notnull() & corr_df_h[method2].notnull()].truncate(after=CORR_GRAPH_SIZE)
plt.clf()
sns.regplot(x=this_corr_df_m[method1], y=this_corr_df_m[method2], scatter_kws={"color": c_pal[0]}, line_kws={"color": c_pal[3]})
plt.xlabel(xlabel)
plt.ylabel(ylabel)
plt.xlim(0, 1)
plt.ylim(0, 1)
plt.savefig(os.path.join(OUT_DIR, EVALUATION_FOLDER, CORRELATIONS_FOLDER, "mixed", method1 + "_" + method2 + "_machine.pdf"), bbox_inches='tight')
plt.clf()
sns.regplot(x=this_corr_df_h[method1], y=this_corr_df_h[method2], scatter_kws={"color": c_pal[0]}, line_kws={"color": c_pal[3]})
plt.xlabel(xlabel)
plt.ylabel(ylabel)
plt.xlim(0, 1)
plt.ylim(0, 1)
plt.savefig(os.path.join(OUT_DIR, EVALUATION_FOLDER, CORRELATIONS_FOLDER, "mixed", method1 + "_" + method2 + "_human.pdf"), bbox_inches='tight')
print("\nGenerating correlation graphs for datasets (paraphrases only).")
# machine-paraphrases & human-paraphrases
corr_df = shuffle(df_test[df_test[PARAPHRASE] == True])\
.reset_index(drop=True).drop(columns=df_test.columns.difference(DETECTION_METHODS))
# machine-paraphrases
corr_df_m = shuffle(df_test[(df_test[PARAPHRASE] == True) & (df_test[DATASET].isin(MACHINE_PARAPHRASED_DATASETS))])\
.reset_index(drop=True).drop(columns=df_test.columns.difference(DETECTION_METHODS))
# human-paraphrases
corr_df_h = shuffle(df_test[(df_test[PARAPHRASE] == True) & (df_test[DATASET].isin(HUMAN_PARAPHRASED_DATASETS))])\
.reset_index(drop=True).drop(columns=df_test.columns.difference(DETECTION_METHODS))
for method1 in tqdm(DETECTION_METHODS):
for method2 in DETECTION_METHODS:
if method1 != method2:
if method1 == SEM_BERT:
xlabel = "BERT"
elif method1 == SEM_T5:
xlabel = "T5"
elif method1 == FUZZY:
xlabel = "Fuzzy"
elif method1 == TFIDF_COSINE:
xlabel = "Tf-idf Cosine"
elif method1 == NGRAM3:
xlabel = "3-Gram"
elif method1 == FASTTEXT:
xlabel = "Fasttext"
elif method1 == SEM_GLOVE:
xlabel = "GloVe"
else:
xlabel = "Unknown Method"
if method2 == SEM_BERT:
ylabel = "BERT"
elif method2 == SEM_T5:
ylabel = "T5"
elif method2 == FUZZY:
ylabel = "Fuzzy"
elif method2 == TFIDF_COSINE:
ylabel = "Tf-idf Cosine"
elif method2 == NGRAM3:
ylabel = "3-Gram"
elif method2 == SEM_GLOVE:
ylabel = "GloVe"
elif method2 == FASTTEXT:
ylabel = "Fasttext"
else:
ylabel = "Unknown Method"
this_corr_df_m = corr_df_m[corr_df_m[method1].notnull() & corr_df_m[method2].notnull()].truncate(after=CORR_GRAPH_SIZE)
this_corr_df_h = corr_df_h[corr_df_h[method1].notnull() & corr_df_h[method2].notnull()].truncate(after=CORR_GRAPH_SIZE)
plt.clf()
sns.regplot(x=this_corr_df_m[method1], y=this_corr_df_m[method2], scatter_kws={"color": c_pal[0]}, line_kws={"color": c_pal[3]})
plt.xlabel(xlabel)
plt.ylabel(ylabel)
plt.xlim(0, 1)
plt.ylim(0, 1)
plt.savefig(os.path.join(OUT_DIR, EVALUATION_FOLDER, CORRELATIONS_FOLDER, "paraphrases_only", method1 + "_" + method2 + "_machine.pdf"), bbox_inches='tight')
plt.clf()
sns.regplot(x=this_corr_df_h[method1], y=this_corr_df_h[method2], scatter_kws={"color": c_pal[0]}, line_kws={"color": c_pal[3]})
plt.xlabel(xlabel)
plt.ylabel(ylabel)
plt.xlim(0, 1)
plt.ylim(0, 1)
plt.savefig(os.path.join(OUT_DIR, EVALUATION_FOLDER, CORRELATIONS_FOLDER, "paraphrases_only", method1 + "_" + method2 + "_human.pdf"), bbox_inches='tight')
print("\nGenerate Correlation Matrix & Output Heatmap for probabilities...")
# machine-paraphrases & human-paraphrases
corr_df = shuffle(df_test.reset_index(drop=True))\
.drop(columns=df_test.columns.difference(DETECTION_METHODS))
# machine-paraphrases
corr_df_m = shuffle(df_test[(df_test[DATASET].isin(MACHINE_PARAPHRASED_DATASETS))]).reset_index(drop=True)\
.drop(columns=df_test.columns.difference(DETECTION_METHODS))
# human-paraphrases
corr_df_h = shuffle(df_test[(df_test[DATASET].isin(HUMAN_PARAPHRASED_DATASETS))]).reset_index(drop=True)\
.drop(columns=df_test.columns.difference(DETECTION_METHODS))
x_labels = ["TF-IDF", "Fasttext", "GloVe", "BERT", "T5", "Fuzzy", "3-Gram"]
y_labels = ["TF-IDF", "Fasttext", "GloVe", "BERT", "T5", "Fuzzy", "3-Gram"]
plt.clf()
corr_h_matrix = corr_df_h.corr()
mask = np.triu(np.ones_like(corr_h_matrix, dtype=bool)) # Generate a mask for the upper triangle
np.fill_diagonal(mask, False)
sns.heatmap(corr_h_matrix, fmt=".2f", mask=mask, annot=True, xticklabels=x_labels,
yticklabels=y_labels, vmin=0, vmax=1)
plt.yticks(rotation=0)
plt.xticks(rotation=45, ha='right')
plt.savefig(os.path.join(OUT_DIR, EVALUATION_FOLDER, CORRELATIONS_FOLDER, "corr_human_prob.pdf"), bbox_inches="tight")
plt.clf()
corr_m_matrix = corr_df_m.corr()
sns.heatmap(corr_m_matrix, fmt=".2f", mask=mask, annot=True, xticklabels=x_labels,
yticklabels=y_labels, vmin=0, vmax=1)
plt.yticks(rotation=0)
plt.xticks(rotation=45, ha='right')
plt.savefig(os.path.join(OUT_DIR, EVALUATION_FOLDER, CORRELATIONS_FOLDER, "corr_machine_prob.pdf"), bbox_inches="tight")
plt.clf()
print("\nGenerate Correlation Matrix & Output Heatmap for predictions...")
# machine-paraphrases & human-paraphrases
corr_df = shuffle(df_test.reset_index(drop=True)) \
.drop(columns=df_test.columns.difference(METHOD_PRED_CLASSES))
# machine-paraphrases
corr_df_m = shuffle(df_test[(df_test[DATASET].isin(MACHINE_PARAPHRASED_DATASETS))]).reset_index(drop=True) \
.drop(columns=df_test.columns.difference(METHOD_PRED_CLASSES))
# human-paraphrases
corr_df_h = shuffle(df_test[(df_test[DATASET].isin(HUMAN_PARAPHRASED_DATASETS))]).reset_index(drop=True) \
.drop(columns=df_test.columns.difference(METHOD_PRED_CLASSES))
plt.clf()
corr_h_matrix = corr_df_h.corr()
mask = np.triu(np.ones_like(corr_h_matrix, dtype=bool)) # Generate a mask for the upper triangle
np.fill_diagonal(mask, False)
sns.heatmap(corr_h_matrix, fmt=".2f", mask=mask, annot=True, xticklabels=x_labels,
yticklabels=y_labels, vmin=0, vmax=1)
plt.yticks(rotation=0)
plt.xticks(rotation=45, ha='right')
plt.savefig(os.path.join(OUT_DIR, EVALUATION_FOLDER, CORRELATIONS_FOLDER, "corr_human_pred.pdf"), bbox_inches="tight")
plt.clf()
corr_m_matrix = corr_df_m.corr()
sns.heatmap(corr_m_matrix, fmt=".2f", mask=mask, annot=True, xticklabels=x_labels,
yticklabels=y_labels, vmin=0, vmax=1)
plt.yticks(rotation=0)
plt.xticks(rotation=45, ha='right')
plt.savefig(os.path.join(OUT_DIR, EVALUATION_FOLDER, CORRELATIONS_FOLDER, "corr_machine_pred.pdf"), bbox_inches="tight")
plt.clf()
print("Done.")