# Configure Plotly to render inline figures via CDN so they display
# in the rendered Sphinx output (RTD). Safe to ignore when running
# locally in Jupyter.
import plotly.io as pio
pio.renderers.default = 'notebook_connected'
Import and load sample ECG & PPG
import vital_sqi
from vital_sqi.data.signal_io import ECG_reader,PPG_reader
import os
import plotly.graph_objects as go
from plotly.subplots import make_subplots
import numpy as np
import matplotlib.pyplot as plt
file_name = "example.edf"
ecg_data = ECG_reader(os.path.join("test_data",file_name),'edf')
file_name = "ppg_smartcare.csv"
ppg_data = PPG_reader(os.path.join("test_data",file_name),
signal_idx=6,
timestamp_idx= 1)
Matplotlib is building the font cache; this may take a moment.
Explain the signal object structure
The signal object contains the following attributes:
signals: a dataframe stores the raw signals mark by a timestamp column
sampling_rate: the signal sampling_rate
wave_type: either ‘ECG’ or ‘PPG’
infor: additional information retrieve from the csv (other columns) or edf file
sqis: a dataframe stores the scores of all sqis computation.
rules: the list of decision rule with the threshold to reject invalid signal
ruleset: the set of ruleset will be applied to determine valid/invalid signal
print("List of ECG object attributes: ",list(ecg_data.__dict__.keys()))
print("List of PPG object attributes: ",list(ppg_data.__dict__.keys()))
List of ECG object attributes: ['wave_type', 'signals', 'sampling_rate', 'start_datetime', 'info', 'sqis', 'rules', 'ruleset']
List of PPG object attributes: ['wave_type', 'signals', 'sampling_rate', 'start_datetime', 'info', 'sqis', 'rules', 'ruleset']
ecg_signals = ecg_data.signals
ecg_sampling_rate = int(ecg_data.sampling_rate)
print(ecg_signals.head())
ppg_signals = ppg_data.signals
ppg_sampling_rate = int(ppg_data.sampling_rate)
print(ppg_signals.head())
timestamps 0 1
0 2019-01-22 01:04:13.000000000 -13675.252155 -7651.251911
1 2019-01-22 01:04:13.003906250 -1351.679835 191.522423
2 2019-01-22 01:04:13.007812500 12283.614405 9414.911635
3 2019-01-22 01:04:13.011718750 10867.175189 7609.916136
4 2019-01-22 01:04:13.015625000 -2296.891218 -2713.004685
COUNTER PLETH
0 1970-01-01 00:00:00.009 34019
1 1970-01-01 00:00:00.010 33322
2 1970-01-01 00:00:00.011 32664
3 1970-01-01 00:00:00.012 32003
4 1970-01-01 00:00:00.013 31360
Explore the ECG signal with different channels
ppg_signals
| COUNTER | PLETH | |
|---|---|---|
| 0 | 1970-01-01 00:00:00.009 | 34019 |
| 1 | 1970-01-01 00:00:00.010 | 33322 |
| 2 | 1970-01-01 00:00:00.011 | 32664 |
| 3 | 1970-01-01 00:00:00.012 | 32003 |
| 4 | 1970-01-01 00:00:00.013 | 31360 |
| ... | ... | ... |
| 61268 | 1970-01-01 00:01:01.450 | 12298 |
| 61269 | 1970-01-01 00:01:01.451 | 11386 |
| 61270 | 1970-01-01 00:01:01.452 | 10529 |
| 61271 | 1970-01-01 00:01:01.453 | 9729 |
| 61272 | 1970-01-01 00:01:01.454 | 9020 |
61273 rows × 2 columns
fig = go.Figure()
fig.add_trace(go.Scatter(x= ecg_signals.iloc[:,0],
y= ecg_signals.iloc[:,1],
name='channel 1'))
fig.add_trace(go.Scatter(x= ecg_signals.iloc[:,0],
y= ecg_signals.iloc[:,2],
name='channel 2'))
fig.show()
fig = go.Figure()
fig.add_trace(go.Scatter(x= ppg_signals.iloc[:,0],
y= ppg_signals.iloc[:,1],
name='ppg signal'))
fig.show()