File size: 4,028 Bytes
39b4c8c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import numpy as np
import plotly.graph_objects as go
from pyVHR.signals.bvp import BVPsignal

def getErrors(bpmES, bpmGT, timesES, timesGT):
    RMSE = RMSEerror(bpmES, bpmGT, timesES, timesGT)
    MAE = MAEerror(bpmES, bpmGT, timesES, timesGT)
    MAX = MAXError(bpmES, bpmGT, timesES, timesGT)
    PCC = PearsonCorr(bpmES, bpmGT, timesES, timesGT)
    return RMSE, MAE, MAX, PCC

def RMSEerror(bpmES, bpmGT, timesES=None, timesGT=None):
    """ 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 = np.sqrt(df/m)
    return RMSE

def MAEerror(bpmES, bpmGT, timesES=None, timesGT=None):
    """ 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 = df/m
    return MAE

def MAXError(bpmES, bpmGT, timesES=None, timesGT=None):
    """ MAE: """

    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 MAE
    MAX = df
    return MAX

def PearsonCorr(bpmES, bpmGT, timesES=None, timesGT=None):
    from scipy import stats

    diff = bpm_diff(bpmES, bpmGT, timesES, timesGT)
    n,m = diff.shape  # n = num channels, m = bpm length
    CC = np.zeros(n)
    for c in range(n):
        # -- corr
        r,p = stats.pearsonr(diff[c,:]+bpmES[c,:],bpmES[c,:])
        CC[c] = r
    return CC

def printErrors(RMSE, MAE, MAX, PCC):
    print("\n    * Errors: RMSE = %.2f, MAE = %.2f, MAX = %.2f, PCC = %.2f" %(RMSE,MAE,MAX,PCC))

def displayErrors(bpmES, bpmGT, timesES=None, timesGT=None):
    
    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):
    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):
            diff[c,j] = bpmGT[i]-bpmES[c,j]
    return diff