forked from predict-idlab/HeadacheDSS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
evaluate.py
168 lines (155 loc) · 7.68 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
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import os
from terminaltables import AsciiTable
from sklearn.metrics import cohen_kappa_score, accuracy_score, f1_score, confusion_matrix
import click
def bootstrap_test(samples_A, samples_B, repeat=100000, plot=False):
"""
Calculate bootstrap test statistic.
Important: mean_A - mean_B >= 0.
:return: p
"""
# Make sure mean_A > mean_B
if np.mean(samples_B) > np.mean(samples_A):
return bootstrap_test(samples_B, samples_A, repeat)
# Stack together the observations (which will be shuffled)
observations = np.hstack((samples_A, samples_B))
n = len(samples_A)
m = len(samples_B)
# Calculate difference of given population means
# NULL HYPOTHESIS: 'A has larger mean due to sampling'
t_star = np.mean(samples_A) - np.mean(samples_B)
t = np.zeros(repeat)
for i in range(repeat):
# This could be a permutation instead bootstrap resampling
# sample = np.random.permutation(observations)
sample = np.random.choice(
observations,
len(observations),
replace=True
)
x_star = np.mean(sample[0:n])
y_star = np.mean(sample[n:n+m])
t[i] = x_star - y_star
if plot:
plt.hist(t)
plt.axvline(x=t_star)
plt.axvline(x=-t_star)
plt.show()
# Calculate p-value (#resamplings that produced larger difference)
p = float((t > t_star).sum() + (t < -t_star).sum()) / repeat
return p
# Create the ground truth
migbase = pd.read_csv('data/migbase.csv')
CLASS_MAPPING = {'cluster': 0, 'tension': 1, 'migraine': 2}
GROUND_TRUTH = migbase['CLASS'].map(CLASS_MAPPING)
def calculate_metrics(prediction_file, metric):
# Read in prediction file, which contains probabilities for all
# classes. Take argmax as prediction. Calculate accuracy or kappa
pred_df = pd.read_csv(prediction_file)
pred_df = pred_df.drop('Unnamed: 0', axis=1)
predicted_classes = np.argmax(pred_df.values, axis=1)
if metric == 'accuracy':
return accuracy_score(GROUND_TRUTH, predicted_classes)
elif metric == 'kappa':
return cohen_kappa_score(GROUND_TRUTH, predicted_classes)
elif metric == 'f1_cluster':
return f1_score(GROUND_TRUTH, predicted_classes, labels=[0], average='micro')
elif metric == 'f1_tension':
return f1_score(GROUND_TRUTH, predicted_classes, labels=[1], average='micro')
elif metric == 'f1_migraine':
return f1_score(GROUND_TRUTH, predicted_classes, labels=[2], average='micro')
elif metric == 'sensitivity_cluster':
# Sensitivity is equal to TP/(TP+FN) (recall)
GROUND_TRUTH_BINARY = GROUND_TRUTH.map({0: 1, 1: 0, 2: 0})
predicted_classes_binary = pd.Series(predicted_classes).map({0: 1, 1: 0, 2: 0})
tn, fp, fn, tp = confusion_matrix(GROUND_TRUTH_BINARY, predicted_classes_binary).ravel()
return tp / (fn + tp)
elif metric == 'specificity_cluster':
# Specificity is equal to TN/(TN+FP)
GROUND_TRUTH_BINARY = GROUND_TRUTH.map({0: 1, 1: 0, 2: 0})
predicted_classes_binary = pd.Series(predicted_classes).map({0: 1, 1: 0, 2: 0})
tn, fp, fn, tp = confusion_matrix(GROUND_TRUTH_BINARY, predicted_classes_binary).ravel()
return tn / (tn + fp)
elif metric == 'sensitivity_tension':
GROUND_TRUTH_BINARY = GROUND_TRUTH.map({0: 0, 1: 1, 2: 0})
predicted_classes_binary = pd.Series(predicted_classes).map({0: 0, 1: 1, 2: 0})
tn, fp, fn, tp = confusion_matrix(GROUND_TRUTH_BINARY, predicted_classes_binary).ravel()
return tp / (fn + tp)
elif metric == 'specificity_tension':
GROUND_TRUTH_BINARY = GROUND_TRUTH.map({0: 0, 1: 1, 2: 0})
predicted_classes_binary = pd.Series(predicted_classes).map({0: 0, 1: 1, 2: 0})
tn, fp, fn, tp = confusion_matrix(GROUND_TRUTH_BINARY, predicted_classes_binary).ravel()
return tn / (tn + fp)
elif metric == 'sensitivity_migraine':
GROUND_TRUTH_BINARY = GROUND_TRUTH.map({0: 0, 1: 0, 2: 1})
predicted_classes_binary = pd.Series(predicted_classes).map({0: 0, 1: 0, 2: 1})
tn, fp, fn, tp = confusion_matrix(GROUND_TRUTH_BINARY, predicted_classes_binary).ravel()
return tp / (fn + tp)
elif metric == 'specificity_migraine':
GROUND_TRUTH_BINARY = GROUND_TRUTH.map({0: 0, 1: 0, 2: 1})
predicted_classes_binary = pd.Series(predicted_classes).map({0: 0, 1: 0, 2: 1})
tn, fp, fn, tp = confusion_matrix(GROUND_TRUTH_BINARY, predicted_classes_binary).ravel()
return tn / (tn + fp)
def generate_table_data(mode, metric):
root = 'output/'+mode+'/'
metrics = {}
for algorithm in os.listdir(root):
metrics[algorithm] = []
for _file in os.listdir(root+algorithm):
metrics[algorithm].append(calculate_metrics(root+algorithm+os.sep+_file, metric))
metric_table_data = [['', metric]]
all_samplers = list(metrics.keys())
significance_table_data = [[''] + all_samplers]
for sampler1 in all_samplers:
metric_table_row = [sampler1, '{}+/-{}'.format(np.round(np.mean(metrics[sampler1]), 6),
np.round(np.std(metrics[sampler1]), 4))]
metric_table_data.append(metric_table_row)
significance_table_row = [sampler1]
for sampler2 in all_samplers:
if sampler1 != sampler2:
p_value = bootstrap_test(metrics[sampler1], metrics[sampler2])
if p_value >= 0.05:
significance_table_row.append(' ')
else:
if np.mean(metrics[sampler1]) > np.mean(metrics[sampler2]):
symbol = '+'
else:
symbol = '-'
significance_table_row.append(symbol+symbol*(p_value <= 0.01))
else:
significance_table_row.append('\\')
significance_table_data.append(significance_table_row)
return metric_table_data, significance_table_data
@click.command()
@click.option('--mode', default='oversampling', help='The mode to use: either "oversampling", "features" or "both"')
@click.option('--metric', default='all',
help='The metrics to calculate: either "all" or on from ["accuracy", "kappa", "f1_cluster", "f1_tension", "f1_migraine"]')
def generate_tables(mode, metric):
# Depending on the mode, iterate over directories in output/
# Calculate accuracy or kappa metrics, add them to a dict
# and apply bootstrap testing
metrics = ['sensitivity_cluster', 'accuracy', 'kappa', 'f1_cluster', 'f1_tension', 'f1_migraine',
'specificity_cluster', 'sensitivity_tension', 'specificity_tension', 'sensitivity_migraine',
'specificity_migraine']
if mode == 'both':
pass
else:
if metric == 'all':
for metric in metrics:
metric_table_data, significance_table_data = generate_table_data(mode, metric)
metric_table = AsciiTable(metric_table_data, '{}: {}'.format(mode, metric))
print(metric_table.table)
significance_table = AsciiTable(significance_table_data, '{}: {} Significance'.format(mode, metric))
print(significance_table.table)
else:
metric_table_data, significance_table_data = generate_table_data(mode, metric)
print('{} table'.format(metric))
metric_table = AsciiTable(metric_table_data, '{}: {}'.format(mode, metric))
print(metric_table.table)
significance_table = AsciiTable(significance_table_data, '{}: {} Significance'.format(mode, metric))
print(significance_table.table)
if __name__ == '__main__':
generate_tables()