-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy patherrors.py
More file actions
214 lines (168 loc) · 6.61 KB
/
errors.py
File metadata and controls
214 lines (168 loc) · 6.61 KB
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
import numpy as np
import plotly.graph_objects as go
def getErrors(bpmES, bpmGT, timesES, timesGT, metrics):
""" Computes various error/quality measures (multiple time windows case)"""
if type(bpmES) == list:
bpmES = np.expand_dims(bpmES, axis=0)
if type(bpmES) == np.ndarray:
if len(bpmES.shape) == 1:
bpmES = np.expand_dims(bpmES, axis=0)
err = []
for m in metrics:
if m == 'RMSE':
e = RMSEerror(bpmES, bpmGT, timesES, timesGT)
elif m == 'MAE':
e = MAEerror(bpmES, bpmGT, timesES, timesGT)
elif m == 'MAPE':
e = MAPEerror(bpmES, bpmGT, timesES, timesGT)
elif m == 'MAX':
e = MAXError(bpmES, bpmGT, timesES, timesGT)
elif m == 'PCC':
e = PearsonCorr(bpmES, bpmGT, timesES, timesGT)
elif m == 'CCC':
e = LinCorr(bpmES, bpmGT, timesES, timesGT)
err.append(e)
err.append([bpmES, bpmGT])
return err
def RMSEerror(bpmES, bpmGT, timesES=None, timesGT=None):
""" Computes RMSE """
diff = bpm_diff(bpmES, bpmGT, timesES, timesGT)
n, m = diff.shape # n = num channels, m = bpm length
df = np.zeros(n)
for j in range(m):
for c in range(n):
df[c] += np.power(diff[c, j], 2)
# -- final RMSE
RMSE = round(float(np.sqrt(df/m)),2)
return RMSE
def MAEerror(bpmES, bpmGT, timesES=None, timesGT=None):
""" Computes MAE """
diff = bpm_diff(bpmES, bpmGT, timesES, timesGT)
n, m = diff.shape # n = num channels, m = bpm length
df = np.sum(np.abs(diff), axis=1)
# -- final MAE
MAE = round(float(df/m),2)
return MAE
def MAPEerror(bpmES, bpmGT, timesES=None, timesGT=None):
""" Computes MAE """
diff = bpm_diff(bpmES, bpmGT, timesES, timesGT, normalize=True)
n, m = diff.shape # n = num channels, m = bpm length
df = np.sum(np.abs(diff), axis=1)
# -- final MAE
MAPE = round(float((df/m) * 100),2)
return MAPE
def MAXError(bpmES, bpmGT, timesES=None, timesGT=None):
""" computes MAX """
diff = bpm_diff(bpmES, bpmGT, timesES, timesGT)
n, m = diff.shape # n = num channels, m = bpm length
df = np.max(np.abs(diff), axis=1)
# -- final MAX
MAX = df
return MAX
def PearsonCorr(bpmES, bpmGT, timesES=None, timesGT=None):
""" Computes PCC """
from scipy import stats
diff = bpm_diff(bpmES, bpmGT, timesES, timesGT)
n, m = diff.shape # n = num channels, m = bpm length
if m < 2:
print('> Warning: Correlation cannot be calculated for signals with len < 2. Returning NaN')
return np.nan
CC = np.zeros(n)
for c in range(n):
# -- corr
r, p = stats.pearsonr(diff[c, :]+bpmES[c, :], bpmES[c, :])
CC[c] = r
return round(float(CC),2)
def LinCorr(bpmES, bpmGT, timesES=None, timesGT=None):
""" Computes CCC """
diff = bpm_diff(bpmES, bpmGT, timesES, timesGT)
n, m = diff.shape # n = num channels, m = bpm length
if m < 2:
print('> Warning: Correlation cannot be calculated for signals with len < 2. Returning NaN')
return np.nan
CCC = np.zeros(n)
for c in range(n):
# -- Lin's Concordance Correlation Coefficient
ccc = concordance_correlation_coefficient(bpmES[c, :], diff[c, :]+bpmES[c, :])
CCC[c] = ccc
return round(float(CCC),2)
def printErrors(RMSE, MAE, MAX, PCC, CCC):
print("\n * Errors: RMSE = %.2f, MAE = %.2f, MAX = %.2f, PCC = %.2f, CCC = %.2f" %
(RMSE, MAE, MAX, PCC, CCC))
def displayErrors(bpmES, bpmGT, timesES=None, timesGT=None):
""""Plots errors"""
if type(bpmES) == list:
bpmES = np.expand_dims(bpmES, axis=0)
if type(bpmES) == np.ndarray:
if len(bpmES.shape) == 1:
bpmES = np.expand_dims(bpmES, axis=0)
if (timesES is None) or (timesGT is None):
timesES = np.arange(m)
timesGT = timesES
diff = bpm_diff(bpmES, bpmGT, timesES, timesGT)
n, m = diff.shape # n = num channels, m = bpm length
df = np.abs(diff)
dfMean = np.around(np.mean(df, axis=1), 1)
# -- plot errors
fig = go.Figure()
name = 'Ch 1 (µ = ' + str(dfMean[0]) + ' )'
fig.add_trace(go.Scatter(
x=timesES, y=df[0, :], name=name, mode='lines+markers'))
if n > 1:
name = 'Ch 2 (µ = ' + str(dfMean[1]) + ' )'
fig.add_trace(go.Scatter(
x=timesES, y=df[1, :], name=name, mode='lines+markers'))
name = 'Ch 3 (µ = ' + str(dfMean[2]) + ' )'
fig.add_trace(go.Scatter(
x=timesES, y=df[2, :], name=name, mode='lines+markers'))
fig.update_layout(xaxis_title='Times (sec)',
yaxis_title='MAE', showlegend=True)
fig.show()
# -- plot bpm Gt and ES
fig = go.Figure()
GTmean = np.around(np.mean(bpmGT), 1)
name = 'GT (µ = ' + str(GTmean) + ' )'
fig.add_trace(go.Scatter(x=timesGT, y=bpmGT,
name=name, mode='lines+markers'))
ESmean = np.around(np.mean(bpmES[0, :]), 1)
name = 'ES1 (µ = ' + str(ESmean) + ' )'
fig.add_trace(go.Scatter(
x=timesES, y=bpmES[0, :], name=name, mode='lines+markers'))
if n > 1:
ESmean = np.around(np.mean(bpmES[1, :]), 1)
name = 'ES2 (µ = ' + str(ESmean) + ' )'
fig.add_trace(go.Scatter(
x=timesES, y=bpmES[1, :], name=name, mode='lines+markers'))
ESmean = np.around(np.mean(bpmES[2, :]), 1)
name = 'E3 (µ = ' + str(ESmean) + ' )'
fig.add_trace(go.Scatter(
x=timesES, y=bpmES[2, :], name=name, mode='lines+markers'))
fig.update_layout(xaxis_title='Times (sec)',
yaxis_title='BPM', showlegend=True)
fig.show()
def bpm_diff(bpmES, bpmGT, timesES=None, timesGT=None, normalize=False):
n, m = bpmES.shape # n = num channels, m = bpm length
if (timesES is None) or (timesGT is None):
timesES = np.arange(m)
timesGT = timesES
diff = np.zeros((n, m))
for j in range(m):
t = timesES[j]
i = np.argmin(np.abs(t-timesGT))
for c in range(n):
if not normalize:
diff[c, j] = bpmGT[i]-bpmES[c, j]
else:
diff[c, j] = (bpmGT[i]-bpmES[c, j]) / bpmGT[i]
return diff
def concordance_correlation_coefficient(bpm_true, bpm_pred):
cor=np.corrcoef(bpm_true, bpm_pred)[0][1]
mean_true = np.mean(bpm_true)
mean_pred = np.mean(bpm_pred)
var_true = np.var(bpm_true)
var_pred = np.var(bpm_pred)
sd_true = np.std(bpm_true)
sd_pred = np.std(bpm_pred)
numerator = 2*cor*sd_true*sd_pred
denominator = var_true + var_pred + (mean_true - mean_pred)**2
return numerator/denominator