Skip to content

plot_bvp

plot_bvp

Plots the Blood Volume Pulse (BVP) signal, heart rate (HR), and peaks on a given figure.

Parameters:

Name Type Description Default
fig matplotlib.figure.Figure or None

Figure object to plot on. If None, a new figure will be created.

required
window_size int

Size of the window for the plot.

required
bvp dict

Dictionary containing BVP signals for different methods.

required
hr dict

Dictionary containing HR values for different methods.

required
peaks dict

Dictionary containing peak locations for different methods.

required
bvp_method str

Method name for BVP.

required
lm str

Landmark name for BVP.

required

Returns:

Name Type Description
tuple

A tuple containing the updated figure and the plot image in BGR format.

Source code in redesign_pipeline/utils/plot_bvp.py
 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
def plot_bvp(fig,window_size,bvp,hr,peaks,bvp_method,lm):
    """Plots the Blood Volume Pulse (BVP) signal, heart rate (HR), and peaks on a given figure.

    Args:
        fig (matplotlib.figure.Figure or None): Figure object to plot on. If None, a new figure will be created.
        window_size (int): Size of the window for the plot.
        bvp (dict): Dictionary containing BVP signals for different methods.
        hr (dict): Dictionary containing HR values for different methods.
        peaks (dict): Dictionary containing peak locations for different methods.
        bvp_method (str): Method name for BVP.
        lm (str): Landmark name for BVP.

    Returns:
        tuple: A tuple containing the updated figure and the plot image in BGR format.
    """

    if fig is None:
        fig = plt.figure()

        plt.cla()
        plt.clf()
        plt.plot(np.zeros(window_size))
        plt.xlim([0, window_size])
        plt.xticks(np.linspace(0, window_size, 9).astype(np.int16)) 

        fig.canvas.draw()
        plot = np.fromstring(fig.canvas.tostring_rgb(), dtype=np.uint8, sep='')
        plot = plot.reshape(fig.canvas.get_width_height()[::-1] + (3,))
        plot = cv2.cvtColor(plot,cv2.COLOR_RGB2BGR)
        cv2.putText(img=plot, text='0', org=(100, 100), fontFace=cv2.FONT_HERSHEY_TRIPLEX, fontScale=0.6, color=(0, 0, 255),thickness=2)

    else:

        plt.cla()
        plt.clf()
        for i,k in enumerate(bvp):
            plt.plot(bvp[k][bvp_method][lm],color = 'b' if i==0 else 'g')
        plt.xlim([0, window_size])
        plt.xticks(np.linspace(0, window_size, 9).astype(np.int16)) 
        if peaks is not None:
            for i,k in enumerate(peaks):
                if i==0:
                    plt.vlines(x =peaks[k][bvp_method][lm],ymin=-3,ymax=-3.5,colors = 'blue' )
                else:
                    plt.vlines(x =peaks[k][bvp_method][lm],ymin=-2.5,ymax=-3,colors = 'green')

        fig.canvas.draw()
        plot = np.fromstring(fig.canvas.tostring_rgb(), dtype=np.uint8, sep='')
        plot = plot.reshape(fig.canvas.get_width_height()[::-1] + (3,))
        plot = cv2.cvtColor(plot,cv2.COLOR_RGB2BGR)

        for i,k in enumerate(hr):
            hr_value = hr[k][bvp_method][lm] if isinstance(hr[k][bvp_method],list) else hr[k][bvp_method]
            cv2.putText(img=plot, text='HR from '+k+" : "+str(hr_value), org=(100, 100 if i==0 else 130), fontFace=cv2.FONT_HERSHEY_TRIPLEX, fontScale=0.6, color=(255, 0, 0)if i==0 else (0, 255, 0),thickness=2)

    return fig, plot