def test_oximeter(self): serial = serialSim() oxi = Oximeter(serial=serial, add_channels=1) oxi.setup() serial.ppg = serial.ppg[-2:] # To the end of recording oxi.read(10) oxi.find_peaks() # Simulate events in recording for idx in np.random.choice(len(oxi.recording), 5): oxi.channels['Channel_0'][idx] = 1 ax = oxi.plot_events() assert isinstance(ax, matplotlib.axes.Axes) ax = oxi.plot_hr() assert isinstance(ax, matplotlib.axes.Axes) ax = oxi.plot_recording() assert isinstance(ax, matplotlib.axes.Axes) oxi.serial.ppg = [1000] # Insert error in recording with self.assertRaises(ValueError): oxi.readInWaiting(stop=True) serial = serialSim() oxi.serial.ppg = [1000, -1, 1000, -1, -1] # Insert error in recording oxi.readInWaiting(stop=False) oxi.serial.ppg = [1000, -1, 1000, -1, -1] # Insert error in recording oxi.waitBeat() oxi.find_peaks() oxi.peaks = [] oxi.instant_rr = [] oxi.times = [] oxi.threshold = [] oxi.save('test') assert os.path.exists("test.npy") os.remove("test.npy") oxi = Oximeter(serial=serial)
#%% # If you want to allow online data acquisition, you should uncomment the # following lines and provide the reference of the COM port where the pulse # oximeter is plugged in. ############################################################################### # .. code-block:: python # # import serial # ser = serial.Serial('COM4') # Change this value according to your setup #%% # Create an Oximeter instance, initialize recording and record for 10 seconds oxi = Oximeter(serial=ser, sfreq=75, add_channels=4).setup() #%% # Create an Oxymeter instance, initialize recording and record for 10 seconds beat = Sound("C", secs=0.1) diastole1 = Sound("E", secs=0.1) diastole2 = Sound("G", secs=0.1) diastole3 = Sound("Bfl", secs=0.1) systoleTime1, systoleTime2, systoleTime3 = None, None, None tstart = time.time() while time.time() - tstart < 30: # Check if there are new data to read while oxi.serial.inWaiting() >= 5:
#%% # Reading # ------- # Recording and plotting your first time-series will only require 5 lines # of code: import time import serial from systole.recording import Oximeter ser = serial.Serial("COM4") # Add your USB port here # Open serial port, initialize and plot recording for Oximeter oxi = Oximeter(serial=ser).setup().read(duration=10) # The signal can be directly plotted using built-in functions. oxi.plot_oximeter() ############################################################################## # .. figure:: https://github.com/embodied-computation-group/systole/raw/master/Images/recording.png # :align: center ############################################################################## #%% # Interfacing with PsychoPy # ------------------------- # * The ``read()`` method will record for a predefined amount of time # (specified by the ``duration`` parameter, in seconds). This 'serial mode'
# Author: Nicolas Legrand <*****@*****.**> import numpy as np import pandas as pd import unittest import matplotlib from unittest import TestCase from systole.plotting import plot_hr, plot_events, plot_oximeter,\ plot_subspaces, circular, plot_circular, plot_psd from systole import import_ppg, import_rr, serialSim from systole.recording import Oximeter from systole.detection import hrv_subspaces serial = serialSim() oxi = Oximeter(serial=serial, add_channels=1).setup().read(10) oxi.channels['Channel_0'][100] = 1 # Simulate oximeter instance from recorded signal ppg = import_ppg() oxi = Oximeter(serial=None, add_channels=1) oxi.threshold = [0] * 75 oxi.peaks = [0] * 75 oxi.instant_rr = [0] * 75 oxi.recording = list(ppg[0, :75]) for i in range(len(ppg[0, 75:750])): oxi.add_paquet(ppg[0, 75 + i]) oxi.channels['Channel_0'] = np.zeros(750, dtype=int) oxi.channels['Channel_0'][np.random.choice(np.arange(0, 750), 5)] = 1 oxi.times = list(np.arange(0, 10, 1 / 75))
def test_oximeter(self): serial = serialSim() oxi = Oximeter(serial=serial, add_channels=1) oxi.setup() oxi.read(10) oxi.find_peaks() # Simulate events in recording for idx in np.random.choice(len(oxi.recording), 5): oxi.channels['Channel_0'][idx] = 1 ax = oxi.plot_events() assert isinstance(ax, matplotlib.axes.Axes) ax = oxi.plot_hr() assert isinstance(ax, matplotlib.axes.Axes) ax = oxi.plot_recording() assert isinstance(ax, matplotlib.axes.Axes) oxi.readInWaiting() oxi.waitBeat() oxi.find_peaks() oxi.peaks = [] oxi.instant_rr = [] oxi.times = [] oxi.threshold = [] oxi.save('test') assert os.path.exists("test.npy") os.remove("test.npy")
ser = serialSim() #%% # If you want to enable online data acquisition, you should uncomment the # following lines and provide the reference of the COM port where the pulse # oximeter is plugged in. ############################################################################### # .. code-block:: python # # import serial # ser = serial.Serial('COM4') # Change this value according to your setup # Create an Oxymeter instance, initialize recording and record for 10 seconds oxi = Oximeter(serial=ser, sfreq=75).setup() oxi.read(30) #%% # Plotting # -------- fig, ax = plt.subplots(3, 1, figsize=(13, 8), sharex=True) oxi.plot_recording(ax=ax[0]) ax[1].plot(oxi.times, oxi.peaks, 'k') ax[1].set_title('Peaks vector', fontweight='bold') ax[1].set_xlabel('Time (s)') ax[1].set_ylabel('Peak\n detection') hr, time = heart_rate(oxi.peaks, sfreq=75, unit='rr', kind='cubic')
# It can easily interface with `PsychoPy <https://www.psychopy.org/>`_ to # record PPG signal during psychological experiments, and to synchronize # stimulus deliver to e.g., systole or diastole. # For example, you can record and plot data in less than 6 lines of code: #%% # Event related cardiac deceleration # ---------------------------------- import serial from systole.recording import Oximeter ser = serial.Serial('COM4') # Add your USB port here # Open serial port, initialize and plot recording for Oximeter oxi = Oximeter(serial=ser).setup().read(duration=10) Interfacing with PsychoPy ------------------------- The ``Oximeter`` class can be used together with a stimulus presentation software to record cardiac activity during psychological experiments. * The ``read()`` method will record for a predefined amount of time (specified by the ``duration`` parameter, in seconds). This 'serial mode' is the easiest and most robust method, but it does not allow the execution of other instructions in the meantime. .. code-block:: python # Code 1 {} oximeter.read(duration=10)