.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "_examples\Pre-Processing\plot_SQ.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note Click :ref:`here ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr__examples_Pre-Processing_plot_SQ.py: PPG Pre-processing and SQI Calculations ======================================= Trimming, Filtering, Segmentation and SQI Calculations .. GENERATED FROM PYTHON SOURCE LINES 8-14 .. code-block:: default :lineno-start: 9 # Code Adapted from: # https://github.com/meta00/vital_sqi/blob/main/examples/SQI_pipeline_PPG.ipynb # AND # https://meta00.github.io/vital_sqi/_examples/others/plot_read_signal.html#sphx-glr-examples-others-plot-read-signal-py .. GENERATED FROM PYTHON SOURCE LINES 15-16 Libraries .. GENERATED FROM PYTHON SOURCE LINES 16-33 .. code-block:: default :lineno-start: 17 # Generic import os import numpy as np import pandas as pd import matplotlib.pyplot as plt # Scipy from scipy.stats import skew from scipy.stats import kurtosis # vitalSQI from vital_sqi.data.signal_io import PPG_reader import vital_sqi.highlevel_functions.highlevel as sqi_hl import vital_sqi.data.segment_split as sqi_sg from vital_sqi.common.rpeak_detection import PeakDetector .. rst-class:: sphx-glr-script-out Out: .. code-block:: none Importing the dtw module. When using in academic works please cite: T. Giorgino. Computing and Visualizing Dynamic Time Warping Alignments in R: The dtw Package. J. Stat. Soft., doi:10.18637/jss.v031.i07. .. GENERATED FROM PYTHON SOURCE LINES 34-35 Load data .. GENERATED FROM PYTHON SOURCE LINES 35-72 .. code-block:: default :lineno-start: 37 # Filepath filepath = r'..\..\..\..\OUCRU\01NVa_Dengue\Adults\01NVa-003-2001\PPG' filename = r'01NVa-003-2001 Smartcare.csv' #defining constants trim_amount = 120 hp_filt_params = (1, 1) #(Hz, order) lp_filt_params = (20, 4) #(Hz, order) filter_type = 'butter' segment_length = 30 sampling_rate = 100 #readind PPG data data = PPG_reader(os.path.join(filepath, filename), signal_idx=["PLETH", "IR_ADC"], timestamp_idx=["TIMESTAMP_MS"], info_idx=["SPO2_PCT","PULSE_BPM","PERFUSION_INDEX"], sampling_rate=sampling_rate) print(data) print(data.signals) print(data.sampling_rate) print(data.start_datetime) print(data.wave_type) print(data.signals.shape) plot_range = np.arange(0,1000,1) fig, ax = plt.subplots() ax.plot(plot_range, data.signals[0][0:1000]) .. image:: /_examples/Pre-Processing/images/sphx_glr_plot_SQ_001.png :alt: plot SQ :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out Out: .. code-block:: none [[ 36033 40919 45715 ... 31117 30761 30261] [216524 216470 216415 ... 184296 184554 184828]] 100 None ppg (2, 5626377) [] .. GENERATED FROM PYTHON SOURCE LINES 73-74 Filtering, Trimming and Segmentation .. GENERATED FROM PYTHON SOURCE LINES 74-99 .. code-block:: default :lineno-start: 75 #Saving a copy of the original Sigal original_signal = np.copy(data.signals.T[trim_amount*sampling_rate:-trim_amount*sampling_rate].T) original = pd.DataFrame(original_signal) print("Raw Signals Shape: ", original.shape) #Signal Filtering data.update_signal(sqi_hl.signal_preprocess(signal_channel=data.signals, hp_cutoff_order=hp_filt_params, lp_cutoff_order=lp_filt_params, trim_amount=trim_amount, filter_type=filter_type, sampling_rate=sampling_rate)) signals = pd.DataFrame(data.signals) print("Filtered Signals Shape: ", signals.shape) fig, ax = plt.subplots() ax.plot(plot_range, data.signals[0][0:1000]) # Signal Segmenation data.update_segment_indices(sqi_sg.generate_segment_idx(segment_length=segment_length, sampling_rate=sampling_rate, signal_array=data.signals)) print("Data Segments Shape: ", data.segments.shape) segments= pd.DataFrame(data.segments) .. image:: /_examples/Pre-Processing/images/sphx_glr_plot_SQ_002.png :alt: plot SQ :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out Out: .. code-block:: none Raw Signals Shape: (2, 5602377) Filtered Signals Shape: (2, 5602377) Data Segments Shape: (2, 1868) .. GENERATED FROM PYTHON SOURCE LINES 100-101 Peak Detection .. GENERATED FROM PYTHON SOURCE LINES 101-129 .. code-block:: default :lineno-start: 102 #Peak Detector peak_list, trough_list = PeakDetector().ppg_detector(signals.iloc[0,segments.iloc[0,0]:segments.iloc[0,1]], 7) plot_range = np.arange(0,3000,1) fig, ax = plt.subplots() ax.plot(plot_range, signals.iloc[0, segments.iloc[0,0]:segments.iloc[0,1]]) if len(peak_list)!=0: ax.scatter(peak_list,signals.iloc[0,peak_list],color="r",marker="v") if len(trough_list)!=0: ax.scatter(trough_list,signals.iloc[0,trough_list],color="b",marker="v") #Single period plot fig, ax = plt.subplots() ax.plot(signals.iloc[0,trough_list[0]:trough_list[1]]) print(data.signals[0]) print(data.signals.shape) print(segments.shape) print(data.segments.shape) print(original_signal.shape) print(peak_list.shape) print(trough_list.shape) .. rst-class:: sphx-glr-horizontal * .. image:: /_examples/Pre-Processing/images/sphx_glr_plot_SQ_003.png :alt: plot SQ :class: sphx-glr-multi-img * .. image:: /_examples/Pre-Processing/images/sphx_glr_plot_SQ_004.png :alt: plot SQ :class: sphx-glr-multi-img .. rst-class:: sphx-glr-script-out Out: .. code-block:: none [ 142 821 2090 ... 1555 1462 1302] (2, 5602377) (2, 1868) (2, 1868) (2, 5602377) (71,) (71,) .. GENERATED FROM PYTHON SOURCE LINES 130-131 SQI Calculation .. GENERATED FROM PYTHON SOURCE LINES 131-160 .. code-block:: default :lineno-start: 133 #THE FUNCTION BELOW OUTPUTS AN ERROR, NEEDS TO BE EDITED SO THAT if signals are none, they are replaced with NaN #computed_sqi = sqi_hl.compute_all_SQI(signal=data.signals[0], segments=data.segments[0], raw_signal=original_signal[0], primary_peakdet=7, secondary_peakdet=6, template_type=0) ''' ERROR THAT COMES UP: raise Exception("Not enough peaks in the signal to generate per beat SQI") Exception: Not enough peaks in the signal to generate per beat THIS COMES UP FOR PLETH (index 0) BUT NOT FOR IR_ADC (index 1) ''' ''' computed_sqi = pd.DataFrame(computed_sqi) print(computed_sqi[2].keys()) print(data.segments.shape) print(peak_list.shape) print(trough_list.shape) print(computed_sqi[1873]['snr']) ''' .. rst-class:: sphx-glr-script-out Out: .. code-block:: none "\ncomputed_sqi = pd.DataFrame(computed_sqi)\n\n\nprint(computed_sqi[2].keys())\nprint(data.segments.shape)\nprint(peak_list.shape)\nprint(trough_list.shape)\nprint(computed_sqi[1873]['snr'])\n" .. GENERATED FROM PYTHON SOURCE LINES 161-162 Segment Rejection .. GENERATED FROM PYTHON SOURCE LINES 162-176 .. code-block:: default :lineno-start: 163 #AFTER COMPUTING SQIs REJECT USING CRITERIA IS THE NEXT STEP. plt.show() .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 12.882 seconds) .. _sphx_glr_download__examples_Pre-Processing_plot_SQ.py: .. only :: html .. container:: sphx-glr-footer :class: sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_SQ.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_SQ.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_