forked from berndporr/py-ecg-detectors
-
Notifications
You must be signed in to change notification settings - Fork 0
/
show_stats_plots.py
executable file
·251 lines (196 loc) · 7.86 KB
/
show_stats_plots.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
#!/usr/bin/python3
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import scipy.stats as stats
import _tester_utils
def str_join(a, b, c):
a = np.array(a)
b = np.array(b)
c = np.array(c)
str_array = np.core.defchararray.add(np.core.defchararray.add(a, b), c)
return str_array
def get_sensivities(results_df, detector_name, experiment=None):
if experiment!=None:
tp_col_names = str_join(detector_name+' ', [experiment], ' TP')
fp_col_names = str_join(detector_name+' ', [experiment], ' FP')
fn_col_names = str_join(detector_name+' ', [experiment], ' FN')
tn_col_names = str_join(detector_name+' ', [experiment], ' TN')
total_tp = (results_df.loc[:, tp_col_names].values)[:,0]
total_fn = (results_df.loc[:, fn_col_names].values)[:,0]
else:
tp_col_names = detector_name+' '+'TP'
fp_col_names = detector_name+' '+'FP'
fn_col_names = detector_name+' '+'FN'
tn_col_names = detector_name+' '+'TN'
total_tp = results_df.loc[:, tp_col_names].values
total_fn = results_df.loc[:, fn_col_names].values
se = []
for tp,fn in zip(total_tp,total_fn):
if (tp + fn) > 0:
s = tp/(tp+fn)*100.0
se.append(s)
return np.array(se)
def get_result(results_df, det_names, experiment=None):
m = []
s = []
for det in det_names:
m.append(np.mean(get_sensivities(results_df, det, experiment)))
s.append(np.std(get_sensivities(results_df, det, experiment)))
return np.array(m),np.array(s)
def compare_det_test(results_df, detector_name1, detector_name2, experiment=None):
se1 = get_sensivities(results_df, detector_name1, experiment)
if len(se1) < 2:
return 0
se2 = get_sensivities(results_df, detector_name2, experiment)
if len(se2) < 2:
return 0
l = min(len(se1),len(se2))
if (l < 20):
return None
#print("1:",se1[:l])
#print("2:",se2[:l])
try:
t,p = stats.wilcoxon(se1[:l],se2[:l])
return p
except:
return None
def compare_cables(results_df1,results_df2,det1,experiment):
se1 = get_sensivities(results_df1, det1, experiment)
if len(se1) < 2:
return 0
se2 = get_sensivities(results_df2, det1, experiment)
if len(se2) < 2:
return 0
l = min(len(se1),len(se2))
if (l < 20):
return None
try:
t,p = stats.wilcoxon(se1[:l],se2[:l])
return p
except:
return None
def compare_experiments(results_df1,det1):
se1 = get_sensivities(results_df1, det1, 'sitting')
if len(se1) < 2:
return 0
se2 = get_sensivities(results_df1, det1, 'jogging')
if len(se2) < 2:
return 0
l = min(len(se1),len(se2))
if (l < 20):
return None
try:
t,p = stats.wilcoxon(se1[:l],se2[:l])
return p
except:
return None
def single_plot(data, std, y_label, title = None):
fig, ax = plt.subplots()
_tester_utils.det_fullnames = ['Elgendi et al', 'Matched Filter', 'Kalidas and Tamil', 'Engzee Mod', 'Christov', 'Hamilton', 'Pan and Tompkins']
x_pos = np.arange(len(_tester_utils.det_fullnames))
fig.set_size_inches(10, 7)
rects1 = ax.bar(x_pos, data, yerr=std, width = 0.65, align='center', alpha=0.5, ecolor='black', capsize=10)
ax.set_ylim([0,100])
ax.set_ylabel(y_label)
ax.set_xlabel('Detector')
ax.set_xticks(x_pos)
ax.set_xticklabels(_tester_utils.det_fullnames)
if title!=None:
ax.set_title(title)
plt.tight_layout()
return rects1
def double_plot(data1, std1, data2, std2, y_label, legend1, legend2, title=None):
fig, ax = plt.subplots()
_tester_utils.det_fullnames = ['Elgendi et al', 'Matched Filter', 'Kalidas and Tamil', 'Engzee Mod', 'Christov', 'Hamilton', 'Pan and Tompkins']
x_pos = np.arange(len(_tester_utils.det_fullnames))
fig.set_size_inches(10, 7)
width = 0.4
rects1 = ax.bar(x_pos, data1, width, yerr=std1, alpha=0.5, ecolor='black', capsize=10)
rects2 = ax.bar(x_pos+width, data2, width, yerr=std2, alpha=0.5, ecolor='black', capsize=10)
ax.set_ylim([0,100])
ax.set_ylabel(y_label)
ax.set_xlabel('Detector')
ax.set_xticks(x_pos + width / 2)
ax.set_xticklabels(_tester_utils.det_fullnames)
ax.legend((rects1[0], rects2[0]), (legend1, legend2))
if title!=None:
ax.set_title(title)
plt.tight_layout()
return rects1, rects2
def print_stat(p):
if p == None:
print('--- & ',end='')
return
s = ""
if p < 0.05:
s = "*"
print('{:03.2f}{} & '.format(p,s),end='')
def print_detector_comparision_table(title,datafile,experiment=None):
print(title)
print(" & ",end='')
for detlong1 in _tester_utils.det_fullnames:
print(detlong1," & ",end='')
print("\\\\")
for det1,detlong1 in zip(_tester_utils.det_names,_tester_utils.det_fullnames):
print(detlong1," & ",end='')
for det2,detlong2 in zip(_tester_utils.det_names,_tester_utils.det_fullnames):
p = compare_det_test(datafile, det1, det2, experiment)
print_stat(p)
print("\\\\")
print()
# GUDB
gudb_cs_results = pd.read_csv('results_GUDB_chest_strap.csv', dtype=int, index_col=0)
gudb_cable_results = pd.read_csv('results_GUDB_loose_cables.csv', dtype=int, index_col=0)
# MITDB
mitdb_results = pd.read_csv('results_MITDB.csv', dtype=int, index_col=0)
experiment_names = ['sitting','maths','walking','hand_bike','jogging']
output_names = ['TP', 'FP', 'FN', 'TN']
print_detector_comparision_table("MIT database",mitdb_results)
print_detector_comparision_table("EINTHOVEN SITTING",gudb_cable_results,'sitting')
print_detector_comparision_table("CHEST STRAP SITTING",gudb_cs_results,'sitting')
print_detector_comparision_table("EINTHOVEN JOGGING",gudb_cable_results,'jogging')
print_detector_comparision_table("CHEST STRAP JOGGING",gudb_cs_results,'jogging')
print("\n\n\n")
print("CHEST STRAP VS EINTH")
for exp1 in ['sitting','jogging']:
print(exp1,":")
for det1 in _tester_utils.det_names:
p = compare_cables(gudb_cable_results,gudb_cs_results,det1,exp1)
print_stat(p)
print("\\\\")
print("\n\n\n")
print("LOOSE CABLES: SITTING VS JOGGING")
for det1 in _tester_utils.det_names:
p = compare_experiments(gudb_cable_results,det1)
print_stat(p)
print("\\\\")
print("\n\n\n")
print("CHEST STRAP: SITTING VS JOGGING")
for det1 in _tester_utils.det_names:
p = compare_experiments(gudb_cs_results,det1)
print_stat(p)
print("\\\\")
print("\n\n\n")
# calculating results
mitdb_avg,mitdb_std = get_result(mitdb_results, _tester_utils.det_names)
print("mitdb:",mitdb_avg)
gudb_cs_sitting_avg,gudb_cs_sitting_std = get_result(gudb_cs_results,
_tester_utils.det_names, 'sitting')
print("chest strap sitting:",gudb_cs_sitting_avg)
gudb_cable_sitting_avg,gudb_cable_sitting_std = get_result(gudb_cable_results,
_tester_utils.det_names, 'sitting')
print("lose cables sitting:",gudb_cable_sitting_avg)
gudb_cs_jogging_avg,gudb_cs_jogging_std = get_result(gudb_cs_results,
_tester_utils.det_names, 'jogging')
gudb_cable_jogging_avg,gudb_cable_jogging_std = get_result(gudb_cable_results,
_tester_utils.det_names, 'jogging')
# plotting
single_plot(mitdb_avg, mitdb_std, 'Sensitivity (%)', 'MITDB')
double_plot(gudb_cs_sitting_avg, gudb_cs_sitting_std,
gudb_cable_sitting_avg, gudb_cable_sitting_std,
'Sensitivity (%)', 'Chest Strap', 'Loose Cables', 'GUDB: cable, sitting')
double_plot(gudb_cs_jogging_avg, gudb_cs_jogging_std,
gudb_cable_jogging_avg, gudb_cable_jogging_std,
'Sensitivity (%)', 'Chest Strap', 'Loose Cables', 'GUDB: cable, jogging')
plt.show()