Example analysis of a PPG/ECG waveform using the vital_sqi package

The following notebook shows an example of PPG/ECG waveform processing using the vital_sqi package. The aim of the package is to automate signal quality classification for PPG/ECG waveforms. It is achieved by computing various signal quality indices for each signal segment and using them to form a decision.

The pipeline can be briefly summarized as follows:

  1. Load dataset under analysis

  2. Preprocess and segment the dataset

  3. Compute SQI for each dataset segment

  4. Make decision for each segment

Global Imports

[1]:
import numpy as np
import warnings
import os
import pandas as pd
import matplotlib.pyplot as plt
import vital_sqi
from vital_sqi.data.signal_io import ECG_reader,PPG_reader

Start by importing the signal via the PPG_reader function

The function expects a .csv or similar data format with named columns. The column names are used to separate between data column, timestamp columns and any additional information columns. This returns a SignalSQI class that is compatible with other vital_sqi package functions, the main class members of interest are: * signals: an ndarray of shape (m, n) where m is the number of rows and n is the number of channels of the signal * sqis: an ndarray of shape (m, n) where m is the number of signal segments, n is the number of SQIs. * sampling_rate: sampling rate in hertz (Hz)

[2]:
from vital_sqi.pipeline.pipeline_highlevel import get_ppg_sqis

file_in = os.path.abspath('test_data/ppg_smartcare.csv') #FIle input location
sqi_dict = os.path.abspath('test_data/sqi_dict.json') #input dictionary -> which sqi features
segments, signal_obj = get_ppg_sqis(file_in,
                                    signal_idx=['PLETH'],
                                    timestamp_idx=['TIMESTAMP_MS'],
                                    sqi_dict_filename = sqi_dict)
2024-11-29 08:56:35,849 - ERROR - No valid timestamps column found. Generated 'timestamps' column based on index.
 29%|██▊       | 6/21 [00:00<00:02,  6.86it/s]2024-11-29 08:56:36,777 - ERROR - Error in get_nn function: No peaks detected in the signal. RR interval computation failed.
2024-11-29 08:56:36,785 - ERROR - Error in get_nn function: No peaks detected in the signal. RR interval computation failed.
2024-11-29 08:56:36,792 - ERROR - Error in get_nn function: No peaks detected in the signal. RR interval computation failed.
2024-11-29 08:56:36,799 - ERROR - Error in get_nn function: No peaks detected in the signal. RR interval computation failed.
2024-11-29 08:56:36,806 - ERROR - Error in get_nn function: No peaks detected in the signal. RR interval computation failed.
2024-11-29 08:56:36,814 - ERROR - Error in get_nn function: No peaks detected in the signal. RR interval computation failed.
2024-11-29 08:56:36,821 - ERROR - Error in get_nn function: No peaks detected in the signal. RR interval computation failed.
2024-11-29 08:56:36,829 - ERROR - Error in get_nn function: No peaks detected in the signal. RR interval computation failed.
2024-11-29 08:56:36,837 - ERROR - Error in get_nn function: No peaks detected in the signal. RR interval computation failed.
2024-11-29 08:56:36,844 - ERROR - Error in get_nn function: No peaks detected in the signal. RR interval computation failed.
2024-11-29 08:56:36,853 - ERROR - Error in get_nn function: No peaks detected in the signal. RR interval computation failed.
2024-11-29 08:56:36,861 - ERROR - Error in get_nn function: No peaks detected in the signal. RR interval computation failed.
2024-11-29 08:56:36,869 - ERROR - Error in get_nn function: No peaks detected in the signal. RR interval computation failed.
2024-11-29 08:56:36,877 - ERROR - Error in get_nn function: No peaks detected in the signal. RR interval computation failed.
2024-11-29 08:56:36,886 - ERROR - Error in get_nn function: No peaks detected in the signal. RR interval computation failed.
2024-11-29 08:56:36,894 - ERROR - Error in get_nn function: No peaks detected in the signal. RR interval computation failed.
 33%|███▎      | 7/21 [00:01<00:02,  6.75it/s]2024-11-29 08:56:36,930 - ERROR - Error in get_nn function: No peaks detected in the signal. RR interval computation failed.
2024-11-29 08:56:36,937 - ERROR - Error in get_nn function: No peaks detected in the signal. RR interval computation failed.
2024-11-29 08:56:36,944 - ERROR - Error in get_nn function: No peaks detected in the signal. RR interval computation failed.
2024-11-29 08:56:36,952 - ERROR - Error in get_nn function: No peaks detected in the signal. RR interval computation failed.
2024-11-29 08:56:36,959 - ERROR - Error in get_nn function: No peaks detected in the signal. RR interval computation failed.
2024-11-29 08:56:36,966 - ERROR - Error in get_nn function: No peaks detected in the signal. RR interval computation failed.
2024-11-29 08:56:36,974 - ERROR - Error in get_nn function: No peaks detected in the signal. RR interval computation failed.
2024-11-29 08:56:36,981 - ERROR - Error in get_nn function: No peaks detected in the signal. RR interval computation failed.
2024-11-29 08:56:36,989 - ERROR - Error in get_nn function: No peaks detected in the signal. RR interval computation failed.
2024-11-29 08:56:36,996 - ERROR - Error in get_nn function: No peaks detected in the signal. RR interval computation failed.
2024-11-29 08:56:37,004 - ERROR - Error in get_nn function: No peaks detected in the signal. RR interval computation failed.
2024-11-29 08:56:37,011 - ERROR - Error in get_nn function: No peaks detected in the signal. RR interval computation failed.
2024-11-29 08:56:37,019 - ERROR - Error in get_nn function: No peaks detected in the signal. RR interval computation failed.
2024-11-29 08:56:37,027 - ERROR - Error in get_nn function: No peaks detected in the signal. RR interval computation failed.
2024-11-29 08:56:37,035 - ERROR - Error in get_nn function: No peaks detected in the signal. RR interval computation failed.
2024-11-29 08:56:37,043 - ERROR - Error in get_nn function: No peaks detected in the signal. RR interval computation failed.
 81%|████████  | 17/21 [00:02<00:00,  7.45it/s]2024-11-29 08:56:38,322 - ERROR - Error in get_nn function: No peaks detected in the signal. RR interval computation failed.
2024-11-29 08:56:38,330 - ERROR - Error in get_nn function: No peaks detected in the signal. RR interval computation failed.
2024-11-29 08:56:38,337 - ERROR - Error in get_nn function: No peaks detected in the signal. RR interval computation failed.
2024-11-29 08:56:38,345 - ERROR - Error in get_nn function: No peaks detected in the signal. RR interval computation failed.
2024-11-29 08:56:38,352 - ERROR - Error in get_nn function: No peaks detected in the signal. RR interval computation failed.
2024-11-29 08:56:38,360 - ERROR - Error in get_nn function: No peaks detected in the signal. RR interval computation failed.
2024-11-29 08:56:38,367 - ERROR - Error in get_nn function: No peaks detected in the signal. RR interval computation failed.
2024-11-29 08:56:38,374 - ERROR - Error in get_nn function: No peaks detected in the signal. RR interval computation failed.
2024-11-29 08:56:38,382 - ERROR - Error in get_nn function: No peaks detected in the signal. RR interval computation failed.
2024-11-29 08:56:38,389 - ERROR - Error in get_nn function: No peaks detected in the signal. RR interval computation failed.
2024-11-29 08:56:38,397 - ERROR - Error in get_nn function: No peaks detected in the signal. RR interval computation failed.
2024-11-29 08:56:38,404 - ERROR - Error in get_nn function: No peaks detected in the signal. RR interval computation failed.
2024-11-29 08:56:38,412 - ERROR - Error in get_nn function: No peaks detected in the signal. RR interval computation failed.
2024-11-29 08:56:38,419 - ERROR - Error in get_nn function: No peaks detected in the signal. RR interval computation failed.
2024-11-29 08:56:38,427 - ERROR - Error in get_nn function: No peaks detected in the signal. RR interval computation failed.
2024-11-29 08:56:38,435 - ERROR - Error in get_nn function: No peaks detected in the signal. RR interval computation failed.
 90%|█████████ | 19/21 [00:02<00:00,  7.11it/s]2024-11-29 08:56:38,616 - ERROR - Error in get_nn function: No peaks detected in the signal. RR interval computation failed.
2024-11-29 08:56:38,624 - ERROR - Error in get_nn function: No peaks detected in the signal. RR interval computation failed.
2024-11-29 08:56:38,631 - ERROR - Error in get_nn function: No peaks detected in the signal. RR interval computation failed.
2024-11-29 08:56:38,639 - ERROR - Error in get_nn function: No peaks detected in the signal. RR interval computation failed.
2024-11-29 08:56:38,646 - ERROR - Error in get_nn function: No peaks detected in the signal. RR interval computation failed.
2024-11-29 08:56:38,654 - ERROR - Error in get_nn function: No peaks detected in the signal. RR interval computation failed.
2024-11-29 08:56:38,661 - ERROR - Error in get_nn function: No peaks detected in the signal. RR interval computation failed.
2024-11-29 08:56:38,669 - ERROR - Error in get_nn function: No peaks detected in the signal. RR interval computation failed.
2024-11-29 08:56:38,676 - ERROR - Error in get_nn function: No peaks detected in the signal. RR interval computation failed.
2024-11-29 08:56:38,684 - ERROR - Error in get_nn function: No peaks detected in the signal. RR interval computation failed.
2024-11-29 08:56:38,691 - ERROR - Error in get_nn function: No peaks detected in the signal. RR interval computation failed.
2024-11-29 08:56:38,698 - ERROR - Error in get_nn function: No peaks detected in the signal. RR interval computation failed.
2024-11-29 08:56:38,706 - ERROR - Error in get_nn function: No peaks detected in the signal. RR interval computation failed.
2024-11-29 08:56:38,713 - ERROR - Error in get_nn function: No peaks detected in the signal. RR interval computation failed.
2024-11-29 08:56:38,722 - ERROR - Error in get_nn function: No peaks detected in the signal. RR interval computation failed.
2024-11-29 08:56:38,729 - ERROR - Error in get_nn function: No peaks detected in the signal. RR interval computation failed.
100%|██████████| 21/21 [00:02<00:00,  7.08it/s]
[3]:
signal_obj.sqis[0].describe()
[3]:
perfusion kurtosis_1_mean_sqi kurtosis_1_median_sqi kurtosis_1_std_sqi skewness_mean_sqi skewness_median_sqi skewness_std_sqi entropy_mean_sqi entropy_median_sqi entropy_std_sqi ... sd1 sd2 area ratio kurtosis_1 skewness entropy dtw start_idx end_idx
count 21.000000 11.000000 11.000000 1.100000e+01 11.000000 11.000000 11.000000 11.000000 11.000000 11.0 ... 11.000000 9.000000 9.000000 9.000000 10.00000 10.000000 10.000000 10.000000 21.000000 21.000000
mean 187.404653 -0.482457 -0.482457 1.009294e-17 0.093488 0.081905 0.100155 4.457445 4.457445 0.0 ... 2.198122 1.428218 9.201789 2.113872 -inf -inf -inf -inf 30000.000000 32917.761905
std 20.712381 0.679086 0.679086 3.347448e-17 0.060373 0.056746 0.111606 0.051667 0.051667 0.0 ... 1.581353 0.551555 6.739287 2.757288 NaN NaN NaN NaN 18614.510469 18478.664053
min 112.313216 -1.377272 -1.377272 0.000000e+00 -0.047888 -0.047888 0.004433 4.360107 4.360107 0.0 ... 0.017795 0.452721 3.318527 0.595406 -inf -inf -inf -inf 0.000000 3000.000000
25% 183.541473 -0.859210 -0.859210 0.000000e+00 0.068493 0.054486 0.031351 4.451313 4.451313 0.0 ... 0.902263 1.155379 3.902413 0.690942 NaN NaN NaN NaN 15000.000000 18000.000000
50% 186.970824 -0.448372 -0.448372 0.000000e+00 0.091734 0.079710 0.062525 4.464563 4.464563 0.0 ... 1.913524 1.289028 7.749010 1.390623 NaN NaN NaN NaN 30000.000000 33000.000000
75% 195.615534 -0.346755 -0.346755 0.000000e+00 0.140014 0.124973 0.102512 4.489544 4.489544 0.0 ... 3.101990 1.782648 10.704902 1.519657 -inf -inf -inf -inf 45000.000000 48000.000000
max 215.095923 0.873035 0.873035 1.110223e-16 0.170472 0.151462 0.362694 4.518236 4.518236 0.0 ... 5.095000 2.340499 23.931841 9.278355 -1.12257 0.206787 4.423743 39.441812 60000.000000 61273.000000

8 rows × 49 columns

Get decision

[4]:
from vital_sqi.pipeline.pipeline_highlevel import get_qualified_ppg
rule_dict_filename = os.path.abspath('test_data/rule_dict_test.json') #  location dictionary contain threshold
ruleset_order = {3: 'skewness_1',
                  2: 'entropy',
                  1: 'perfusion'}
file_in = os.path.abspath('test_data/ppg_smartcare.csv') #FIle input location
sqi_dict = os.path.abspath('test_data/sqi_dict.json') #input dictionary -> which sqi features
signal_obj = get_qualified_ppg(file_in,
                                    signal_idx=['PLETH'],
                                    timestamp_idx=['TIMESTAMP_MS'],
                                    sqi_dict_filename = sqi_dict,
                                    rule_dict_filename = rule_dict_filename,
                                    ruleset_order=ruleset_order,
                                    output_dir=None)
2024-11-29 08:56:39,493 - ERROR - No valid timestamps column found. Generated 'timestamps' column based on index.
 29%|██▊       | 6/21 [00:00<00:02,  6.90it/s]2024-11-29 08:56:40,411 - ERROR - Error in get_nn function: No peaks detected in the signal. RR interval computation failed.
2024-11-29 08:56:40,419 - ERROR - Error in get_nn function: No peaks detected in the signal. RR interval computation failed.
2024-11-29 08:56:40,427 - ERROR - Error in get_nn function: No peaks detected in the signal. RR interval computation failed.
2024-11-29 08:56:40,434 - ERROR - Error in get_nn function: No peaks detected in the signal. RR interval computation failed.
2024-11-29 08:56:40,441 - ERROR - Error in get_nn function: No peaks detected in the signal. RR interval computation failed.
2024-11-29 08:56:40,449 - ERROR - Error in get_nn function: No peaks detected in the signal. RR interval computation failed.
2024-11-29 08:56:40,456 - ERROR - Error in get_nn function: No peaks detected in the signal. RR interval computation failed.
2024-11-29 08:56:40,463 - ERROR - Error in get_nn function: No peaks detected in the signal. RR interval computation failed.
2024-11-29 08:56:40,471 - ERROR - Error in get_nn function: No peaks detected in the signal. RR interval computation failed.
2024-11-29 08:56:40,478 - ERROR - Error in get_nn function: No peaks detected in the signal. RR interval computation failed.
2024-11-29 08:56:40,486 - ERROR - Error in get_nn function: No peaks detected in the signal. RR interval computation failed.
2024-11-29 08:56:40,493 - ERROR - Error in get_nn function: No peaks detected in the signal. RR interval computation failed.
2024-11-29 08:56:40,500 - ERROR - Error in get_nn function: No peaks detected in the signal. RR interval computation failed.
2024-11-29 08:56:40,508 - ERROR - Error in get_nn function: No peaks detected in the signal. RR interval computation failed.
2024-11-29 08:56:40,517 - ERROR - Error in get_nn function: No peaks detected in the signal. RR interval computation failed.
2024-11-29 08:56:40,524 - ERROR - Error in get_nn function: No peaks detected in the signal. RR interval computation failed.
 33%|███▎      | 7/21 [00:01<00:02,  6.84it/s]2024-11-29 08:56:40,560 - ERROR - Error in get_nn function: No peaks detected in the signal. RR interval computation failed.
2024-11-29 08:56:40,568 - ERROR - Error in get_nn function: No peaks detected in the signal. RR interval computation failed.
2024-11-29 08:56:40,575 - ERROR - Error in get_nn function: No peaks detected in the signal. RR interval computation failed.
2024-11-29 08:56:40,583 - ERROR - Error in get_nn function: No peaks detected in the signal. RR interval computation failed.
2024-11-29 08:56:40,590 - ERROR - Error in get_nn function: No peaks detected in the signal. RR interval computation failed.
2024-11-29 08:56:40,598 - ERROR - Error in get_nn function: No peaks detected in the signal. RR interval computation failed.
2024-11-29 08:56:40,605 - ERROR - Error in get_nn function: No peaks detected in the signal. RR interval computation failed.
2024-11-29 08:56:40,613 - ERROR - Error in get_nn function: No peaks detected in the signal. RR interval computation failed.
2024-11-29 08:56:40,620 - ERROR - Error in get_nn function: No peaks detected in the signal. RR interval computation failed.
2024-11-29 08:56:40,627 - ERROR - Error in get_nn function: No peaks detected in the signal. RR interval computation failed.
2024-11-29 08:56:40,635 - ERROR - Error in get_nn function: No peaks detected in the signal. RR interval computation failed.
2024-11-29 08:56:40,642 - ERROR - Error in get_nn function: No peaks detected in the signal. RR interval computation failed.
2024-11-29 08:56:40,650 - ERROR - Error in get_nn function: No peaks detected in the signal. RR interval computation failed.
2024-11-29 08:56:40,657 - ERROR - Error in get_nn function: No peaks detected in the signal. RR interval computation failed.
2024-11-29 08:56:40,664 - ERROR - Error in get_nn function: No peaks detected in the signal. RR interval computation failed.
2024-11-29 08:56:40,672 - ERROR - Error in get_nn function: No peaks detected in the signal. RR interval computation failed.
 81%|████████  | 17/21 [00:02<00:00,  7.28it/s]2024-11-29 08:56:41,972 - ERROR - Error in get_nn function: No peaks detected in the signal. RR interval computation failed.
2024-11-29 08:56:41,981 - ERROR - Error in get_nn function: No peaks detected in the signal. RR interval computation failed.
2024-11-29 08:56:41,989 - ERROR - Error in get_nn function: No peaks detected in the signal. RR interval computation failed.
2024-11-29 08:56:41,998 - ERROR - Error in get_nn function: No peaks detected in the signal. RR interval computation failed.
2024-11-29 08:56:42,006 - ERROR - Error in get_nn function: No peaks detected in the signal. RR interval computation failed.
2024-11-29 08:56:42,014 - ERROR - Error in get_nn function: No peaks detected in the signal. RR interval computation failed.
2024-11-29 08:56:42,022 - ERROR - Error in get_nn function: No peaks detected in the signal. RR interval computation failed.
2024-11-29 08:56:42,031 - ERROR - Error in get_nn function: No peaks detected in the signal. RR interval computation failed.
2024-11-29 08:56:42,039 - ERROR - Error in get_nn function: No peaks detected in the signal. RR interval computation failed.
2024-11-29 08:56:42,047 - ERROR - Error in get_nn function: No peaks detected in the signal. RR interval computation failed.
2024-11-29 08:56:42,056 - ERROR - Error in get_nn function: No peaks detected in the signal. RR interval computation failed.
2024-11-29 08:56:42,064 - ERROR - Error in get_nn function: No peaks detected in the signal. RR interval computation failed.
2024-11-29 08:56:42,073 - ERROR - Error in get_nn function: No peaks detected in the signal. RR interval computation failed.
2024-11-29 08:56:42,082 - ERROR - Error in get_nn function: No peaks detected in the signal. RR interval computation failed.
2024-11-29 08:56:42,091 - ERROR - Error in get_nn function: No peaks detected in the signal. RR interval computation failed.
2024-11-29 08:56:42,100 - ERROR - Error in get_nn function: No peaks detected in the signal. RR interval computation failed.
 90%|█████████ | 19/21 [00:02<00:00,  6.66it/s]2024-11-29 08:56:42,297 - ERROR - Error in get_nn function: No peaks detected in the signal. RR interval computation failed.
2024-11-29 08:56:42,305 - ERROR - Error in get_nn function: No peaks detected in the signal. RR interval computation failed.
2024-11-29 08:56:42,312 - ERROR - Error in get_nn function: No peaks detected in the signal. RR interval computation failed.
2024-11-29 08:56:42,320 - ERROR - Error in get_nn function: No peaks detected in the signal. RR interval computation failed.
2024-11-29 08:56:42,327 - ERROR - Error in get_nn function: No peaks detected in the signal. RR interval computation failed.
2024-11-29 08:56:42,335 - ERROR - Error in get_nn function: No peaks detected in the signal. RR interval computation failed.
2024-11-29 08:56:42,343 - ERROR - Error in get_nn function: No peaks detected in the signal. RR interval computation failed.
2024-11-29 08:56:42,351 - ERROR - Error in get_nn function: No peaks detected in the signal. RR interval computation failed.
2024-11-29 08:56:42,358 - ERROR - Error in get_nn function: No peaks detected in the signal. RR interval computation failed.
2024-11-29 08:56:42,366 - ERROR - Error in get_nn function: No peaks detected in the signal. RR interval computation failed.
2024-11-29 08:56:42,373 - ERROR - Error in get_nn function: No peaks detected in the signal. RR interval computation failed.
2024-11-29 08:56:42,381 - ERROR - Error in get_nn function: No peaks detected in the signal. RR interval computation failed.
2024-11-29 08:56:42,388 - ERROR - Error in get_nn function: No peaks detected in the signal. RR interval computation failed.
2024-11-29 08:56:42,396 - ERROR - Error in get_nn function: No peaks detected in the signal. RR interval computation failed.
2024-11-29 08:56:42,404 - ERROR - Error in get_nn function: No peaks detected in the signal. RR interval computation failed.
2024-11-29 08:56:42,412 - ERROR - Error in get_nn function: No peaks detected in the signal. RR interval computation failed.
100%|██████████| 21/21 [00:03<00:00,  6.97it/s]
Saving segments: 100%|██████████| 1/1 [00:00<00:00, 92.62it/s]
Saving segments: 100%|██████████| 20/20 [00:00<00:00, 112.72it/s]
[5]:
signal_obj.sqis[0].describe()
[5]:
perfusion kurtosis_1_mean_sqi kurtosis_1_median_sqi kurtosis_1_std_sqi skewness_mean_sqi skewness_median_sqi skewness_std_sqi entropy_mean_sqi entropy_median_sqi entropy_std_sqi ... sd1 sd2 area ratio kurtosis_1 skewness entropy dtw start_idx end_idx
count 21.000000 11.000000 11.000000 1.100000e+01 11.000000 11.000000 11.000000 11.000000 11.000000 11.0 ... 11.000000 9.000000 9.000000 9.000000 10.00000 10.000000 10.000000 10.000000 21.000000 21.000000
mean 187.404653 -0.482457 -0.482457 1.009294e-17 0.093488 0.081905 0.100155 4.457445 4.457445 0.0 ... 2.198122 1.428218 9.201789 2.113872 -inf -inf -inf -inf 30000.000000 32917.761905
std 20.712381 0.679086 0.679086 3.347448e-17 0.060373 0.056746 0.111606 0.051667 0.051667 0.0 ... 1.581353 0.551555 6.739287 2.757288 NaN NaN NaN NaN 18614.510469 18478.664053
min 112.313216 -1.377272 -1.377272 0.000000e+00 -0.047888 -0.047888 0.004433 4.360107 4.360107 0.0 ... 0.017795 0.452721 3.318527 0.595406 -inf -inf -inf -inf 0.000000 3000.000000
25% 183.541473 -0.859210 -0.859210 0.000000e+00 0.068493 0.054486 0.031351 4.451313 4.451313 0.0 ... 0.902263 1.155379 3.902413 0.690942 NaN NaN NaN NaN 15000.000000 18000.000000
50% 186.970824 -0.448372 -0.448372 0.000000e+00 0.091734 0.079710 0.062525 4.464563 4.464563 0.0 ... 1.913524 1.289028 7.749010 1.390623 NaN NaN NaN NaN 30000.000000 33000.000000
75% 195.615534 -0.346755 -0.346755 0.000000e+00 0.140014 0.124973 0.102512 4.489544 4.489544 0.0 ... 3.101990 1.782648 10.704902 1.519657 -inf -inf -inf -inf 45000.000000 48000.000000
max 215.095923 0.873035 0.873035 1.110223e-16 0.170472 0.151462 0.362694 4.518236 4.518236 0.0 ... 5.095000 2.340499 23.931841 9.278355 -1.12257 0.206787 4.423743 39.441812 60000.000000 61273.000000

8 rows × 49 columns