|
a |
|
b/show_stats_plots.py |
|
|
1 |
#!/usr/bin/python3 |
|
|
2 |
import numpy as np |
|
|
3 |
import pandas as pd |
|
|
4 |
import matplotlib.pyplot as plt |
|
|
5 |
import scipy.stats as stats |
|
|
6 |
|
|
|
7 |
def str_join(a, b, c): |
|
|
8 |
|
|
|
9 |
a = np.array(a) |
|
|
10 |
b = np.array(b) |
|
|
11 |
c = np.array(c) |
|
|
12 |
|
|
|
13 |
str_array = np.core.defchararray.add(np.core.defchararray.add(a, b), c) |
|
|
14 |
|
|
|
15 |
return str_array |
|
|
16 |
|
|
|
17 |
|
|
|
18 |
def get_sensivities(results_df, detector_name, experiment=None): |
|
|
19 |
|
|
|
20 |
if experiment!=None: |
|
|
21 |
tp_col_names = str_join(detector_name+' ', [experiment], ' TP') |
|
|
22 |
fp_col_names = str_join(detector_name+' ', [experiment], ' FP') |
|
|
23 |
fn_col_names = str_join(detector_name+' ', [experiment], ' FN') |
|
|
24 |
tn_col_names = str_join(detector_name+' ', [experiment], ' TN') |
|
|
25 |
total_tp = (results_df.loc[:, tp_col_names].values)[:,0] |
|
|
26 |
total_fn = (results_df.loc[:, fn_col_names].values)[:,0] |
|
|
27 |
else: |
|
|
28 |
tp_col_names = detector_name+' '+'TP' |
|
|
29 |
fp_col_names = detector_name+' '+'FP' |
|
|
30 |
fn_col_names = detector_name+' '+'FN' |
|
|
31 |
tn_col_names = detector_name+' '+'TN' |
|
|
32 |
total_tp = results_df.loc[:, tp_col_names].values |
|
|
33 |
total_fn = results_df.loc[:, fn_col_names].values |
|
|
34 |
|
|
|
35 |
se = [] |
|
|
36 |
|
|
|
37 |
for tp,fn in zip(total_tp,total_fn): |
|
|
38 |
if (tp + fn) > 0: |
|
|
39 |
s = tp/(tp+fn)*100.0 |
|
|
40 |
if s > 0: |
|
|
41 |
se.append(s) |
|
|
42 |
|
|
|
43 |
return np.array(se) |
|
|
44 |
|
|
|
45 |
|
|
|
46 |
def get_result(results_df, det_names, experiment=None): |
|
|
47 |
|
|
|
48 |
m = [] |
|
|
49 |
s = [] |
|
|
50 |
for det in det_names: |
|
|
51 |
m.append(np.mean(get_sensivities(results_df, det, experiment))) |
|
|
52 |
s.append(np.std(get_sensivities(results_df, det, experiment))) |
|
|
53 |
|
|
|
54 |
return np.array(m),np.array(s) |
|
|
55 |
|
|
|
56 |
|
|
|
57 |
|
|
|
58 |
def compare_det_test(results_df, detector_name1, detector_name2, experiment=None): |
|
|
59 |
se1 = get_sensivities(results_df, detector_name1, experiment) |
|
|
60 |
if len(se1) < 2: |
|
|
61 |
return 0 |
|
|
62 |
se2 = get_sensivities(results_df, detector_name2, experiment) |
|
|
63 |
if len(se2) < 2: |
|
|
64 |
return 0 |
|
|
65 |
l = min(len(se1),len(se2)) |
|
|
66 |
#print("1:",se1[:l]) |
|
|
67 |
#print("2:",se2[:l]) |
|
|
68 |
try: |
|
|
69 |
t,p = stats.wilcoxon(se1[:l],se2[:l]) |
|
|
70 |
return p |
|
|
71 |
except: |
|
|
72 |
return None |
|
|
73 |
|
|
|
74 |
|
|
|
75 |
def single_plot(data, std, y_label, title = None): |
|
|
76 |
fig, ax = plt.subplots() |
|
|
77 |
plot_names = ['Elgendi et al', 'Matched Filter', 'Kalidas and Tamil', 'Engzee Mod', 'Christov', 'Hamilton', 'Pan and Tompkins'] |
|
|
78 |
x_pos = np.arange(len(plot_names)) |
|
|
79 |
|
|
|
80 |
fig.set_size_inches(10, 7) |
|
|
81 |
rects1 = ax.bar(x_pos, data, yerr=std, width = 0.65, align='center', alpha=0.5, ecolor='black', capsize=10) |
|
|
82 |
ax.set_ylim([0,100]) |
|
|
83 |
ax.set_ylabel(y_label) |
|
|
84 |
ax.set_xlabel('Detector') |
|
|
85 |
ax.set_xticks(x_pos) |
|
|
86 |
ax.set_xticklabels(plot_names) |
|
|
87 |
|
|
|
88 |
if title!=None: |
|
|
89 |
ax.set_title(title) |
|
|
90 |
|
|
|
91 |
plt.tight_layout() |
|
|
92 |
|
|
|
93 |
return rects1 |
|
|
94 |
|
|
|
95 |
|
|
|
96 |
def double_plot(data1, std1, data2, std2, y_label, legend1, legend2, title=None): |
|
|
97 |
fig, ax = plt.subplots() |
|
|
98 |
plot_names = ['Elgendi et al', 'Matched Filter', 'Kalidas and Tamil', 'Engzee Mod', 'Christov', 'Hamilton', 'Pan and Tompkins'] |
|
|
99 |
x_pos = np.arange(len(plot_names)) |
|
|
100 |
|
|
|
101 |
fig.set_size_inches(10, 7) |
|
|
102 |
width = 0.4 |
|
|
103 |
rects1 = ax.bar(x_pos, data1, width, yerr=std1, alpha=0.5, ecolor='black', capsize=10) |
|
|
104 |
rects2 = ax.bar(x_pos+width, data2, width, yerr=std2, alpha=0.5, ecolor='black', capsize=10) |
|
|
105 |
ax.set_ylim([0,100]) |
|
|
106 |
ax.set_ylabel(y_label) |
|
|
107 |
ax.set_xlabel('Detector') |
|
|
108 |
ax.set_xticks(x_pos + width / 2) |
|
|
109 |
ax.set_xticklabels(plot_names) |
|
|
110 |
ax.legend((rects1[0], rects2[0]), (legend1, legend2)) |
|
|
111 |
|
|
|
112 |
if title!=None: |
|
|
113 |
ax.set_title(title) |
|
|
114 |
|
|
|
115 |
plt.tight_layout() |
|
|
116 |
|
|
|
117 |
return rects1, rects2 |
|
|
118 |
|
|
|
119 |
|
|
|
120 |
def print_stat(p): |
|
|
121 |
if p == None: |
|
|
122 |
print('--- & ',end='') |
|
|
123 |
return |
|
|
124 |
s = "" |
|
|
125 |
if p < 0.05: |
|
|
126 |
s = "*" |
|
|
127 |
print('{:03.2f}{} & '.format(p,s),end='') |
|
|
128 |
|
|
|
129 |
|
|
|
130 |
# GUDB |
|
|
131 |
gudb_cs_results = pd.read_csv('results_GUDB_chest_strap.csv', dtype=int, index_col=0) |
|
|
132 |
gudb_cable_results = pd.read_csv('results_GUDB_loose_cables.csv', dtype=int, index_col=0) |
|
|
133 |
|
|
|
134 |
# MITDB |
|
|
135 |
mitdb_results = pd.read_csv('results_MITDB.csv', dtype=int, index_col=0) |
|
|
136 |
|
|
|
137 |
det_names = ['two_average', 'matched_filter', 'swt', 'engzee', 'christov', 'hamilton', 'pan_tompkins'] |
|
|
138 |
plot_names = ['Elgendi et al', 'Matched Filter', 'Kalidas and Tamil', 'Engzee Mod', 'Christov', 'Hamilton', 'Pan and Tompkins'] |
|
|
139 |
experiment_names = ['sitting','maths','walking','hand_bike','jogging'] |
|
|
140 |
output_names = ['TP', 'FP', 'FN', 'TN'] |
|
|
141 |
|
|
|
142 |
print("MIT") |
|
|
143 |
for det1 in det_names: |
|
|
144 |
for det2 in det_names: |
|
|
145 |
p = compare_det_test(mitdb_results, det1, det2) |
|
|
146 |
print_stat(p) |
|
|
147 |
print("\\\\") |
|
|
148 |
|
|
|
149 |
|
|
|
150 |
print("CHEST STRAP SITTING") |
|
|
151 |
for det1 in det_names: |
|
|
152 |
for det2 in det_names: |
|
|
153 |
p = compare_det_test(gudb_cs_results, det1, det2, 'sitting') |
|
|
154 |
print_stat(p) |
|
|
155 |
print("\\\\") |
|
|
156 |
|
|
|
157 |
|
|
|
158 |
print("CHEST STRAP JOGGING") |
|
|
159 |
for det1 in det_names: |
|
|
160 |
for det2 in det_names: |
|
|
161 |
p = compare_det_test(gudb_cs_results, det1, det2, 'jogging') |
|
|
162 |
print_stat(p) |
|
|
163 |
print("\\\\") |
|
|
164 |
|
|
|
165 |
|
|
|
166 |
print("LOOSE CABLE SITTING") |
|
|
167 |
for det1 in det_names: |
|
|
168 |
for det2 in det_names: |
|
|
169 |
p = compare_det_test(gudb_cable_results, det1, det2, 'sitting') |
|
|
170 |
print_stat(p) |
|
|
171 |
print("\\\\") |
|
|
172 |
|
|
|
173 |
|
|
|
174 |
print("LOOSE CABLE JOGGING") |
|
|
175 |
for det1 in det_names: |
|
|
176 |
for det2 in det_names: |
|
|
177 |
p = compare_det_test(gudb_cable_results, det1, det2, 'jogging') |
|
|
178 |
print_stat(p) |
|
|
179 |
print("\\\\") |
|
|
180 |
|
|
|
181 |
|
|
|
182 |
# calculating results |
|
|
183 |
mitdb_avg,mitdb_std = get_result(mitdb_results, det_names) |
|
|
184 |
print("mitdb:",mitdb_avg) |
|
|
185 |
gudb_cs_sitting_avg,gudb_cs_sitting_std = get_result(gudb_cs_results, det_names, 'sitting') |
|
|
186 |
print("chest strap sitting:",gudb_cs_sitting_avg) |
|
|
187 |
gudb_cable_sitting_avg,gudb_cable_sitting_std = get_result(gudb_cable_results, det_names, 'sitting') |
|
|
188 |
print("lose cables sitting:",gudb_cable_sitting_avg) |
|
|
189 |
gudb_cs_jogging_avg,gudb_cs_jogging_std = get_result(gudb_cs_results, det_names, 'jogging') |
|
|
190 |
gudb_cable_jogging_avg,gudb_cable_jogging_std = get_result(gudb_cable_results, det_names, 'jogging') |
|
|
191 |
|
|
|
192 |
# plotting |
|
|
193 |
single_plot(mitdb_avg, mitdb_std, 'Sensitivity (%)', 'MITDB') |
|
|
194 |
|
|
|
195 |
double_plot(gudb_cs_sitting_avg, gudb_cs_sitting_std, |
|
|
196 |
gudb_cable_sitting_avg, gudb_cable_sitting_std, |
|
|
197 |
'Sensitivity (%)', 'Chest Strap', 'Loose Cables', 'GUDB: cable, sitting') |
|
|
198 |
|
|
|
199 |
double_plot(gudb_cs_jogging_avg, gudb_cs_jogging_std, |
|
|
200 |
gudb_cable_jogging_avg, gudb_cable_jogging_std, |
|
|
201 |
'Sensitivity (%)', 'Chest Strap', 'Loose Cables', 'GUDB: cable, jogging') |
|
|
202 |
|
|
|
203 |
|
|
|
204 |
|
|
|
205 |
plt.show() |