-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathppg.py
155 lines (122 loc) · 5.25 KB
/
ppg.py
1
2
3
4
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# © 2024 Nokia
# Licensed under the BSD 3 Clause Clear License
# SPDX-License-Identifier: BSD-3-Clause-Clear
import pandas as pd
import torch
import os
import numpy as np
import pickle
import pyPPG.preproc as PP
from dotmap import DotMap
from vitaldb import VitalFile
def load_vitaldb_waveforms(path, name, frequency):
"""
Loading waveforms from vitaldb containing .vital files
Args:
path (string): path to .vital files
name (string): file name
frequency (int): loading frequency of the waveforms
Returns:
dict_waveforms (dictionary): Dictionary containing upto 3 wavesforms ECG_II, ECG_V5, and PLETH
"""
waveforms = np.array(['SNUADC/ECG_II', 'SNUADC/ECG_V5', 'SNUADC/PLETH'])
vf = VitalFile(os.path.join(path, name))
df = vf.to_pandas(waveforms, interval=1/frequency)
data_available = [waveforms[i] for i in range(len(waveforms)) if df.loc[:, waveforms[i]].isna().sum() != len(df)]
df = df.loc[:, data_available].dropna()
assert df.index.is_monotonic_increasing
columns = df.columns
dict_waveforms = {c.split('/')[1]: df.loc[:, c].values for c in columns}
return dict_waveforms
def preprocess_one_ppg_signal(waveform,
frequency,
fL=0.5,
fH=12,
order=4,
smoothing_windows={"ppg":50, "vpg":10, "apg":10, "jpg":10}):
"""
Preprocessing a single PPG waveform using py PPG.
https://pyppg.readthedocs.io/en/latest/Filters.html
Args:
waveform (numpy.array): PPG waveform for processing
frequency (int): waveform frequency
fL (float/int): high pass cut-off for chebyshev filter
fH (float/int): low pass cut-off for chebyshev filter
order (int): filter order
smoothing_windows (dictionary): smoothing window sizes in milliseconds as dictionary
Returns:
ppg (numpy.array): filtered ppg signal
ppg_d1 (numpy.array): first derivative of filtered ppg signal
ppg_d2 (numpy.array): second derivative of filtered ppg signal
ppg_d3 (numpy.array): third derivative of filtered ppg signal
"""
prep = PP.Preprocess(fL=fL,
fH=fH,
order=order,
sm_wins=smoothing_windows)
signal = DotMap()
signal.v = waveform
signal.fs = frequency
signal.filtering = True
ppg, ppg_d1, ppg_d2, ppg_d3 = prep.get_signals(signal)
return ppg, ppg_d1, ppg_d2, ppg_d3
def save_pickle_ppg(dict_waveforms, f_name):
"""
Saves the waveform dictionary as a pickle file
Args:
dict_waveforms (dictionary): Dictionary containing upto 3 wavesforms ECG_II, ECG_V, and PLETH
f_name (string): file name
"""
# Sometime PPG is not available
try:
ppg, ppg_d1, ppg_d2, ppg_d3 = preprocess_one_ppg_signal(waveform=dict_waveforms['PLETH'],
frequency=frequency)
dict_waveforms['ppg'] = ppg
dict_waveforms['ppg_d1'] = ppg_d1
dict_waveforms['ppg_d2'] = ppg_d2
dict_waveforms['ppg_d3'] = ppg_d3
pickle.dump(dict_waveforms, open(save_path + f_name.split(".")[0] + ".p", "wb"))
except KeyError as e:
print(f"PLETH not found for {f_name}")
def preprocess_ppg(vital_path, save_path, frequency, overwrite=True):
"""
Preprocess and filter all ppg signals in vitalDB
Args:
vital_path (string): Path to vital db files
save_path (string): directory to save the dictionary waveforms
frequency (int): waveform frequency
overwrite (boolean): True to overwrite existing .p files
"""
# pickle loading does not work sometimes
vital_files = os.listdir(vital_path)
for i in range(len(vital_files)):
f_name = vital_files[i]
print(f"[INFO] Processsing {f_name} | {i}/{len(vital_files)}")
if not f_name.endswith(".vital"):
continue
if not overwrite:
saved_files = os.listdir(save_path)
check_f_name = f_name.split(".")[0] + ".p"
if check_f_name in saved_files:
print(f"File {f_name} already exists -- Skipping")
else:
dict_waveforms = load_vitaldb_waveforms(path=vital_path,
name=f_name,
frequency=frequency)
save_pickle_ppg(dict_waveforms=dict_waveforms,
f_name=f_name)
else:
dict_waveforms = load_vitaldb_waveforms(path=vital_path,
name=f_name,
frequency=frequency)
save_pickle_ppg(dict_waveforms=dict_waveforms,
f_name=f_name)
if __name__ == "__main__":
vital_path = "/mnt/vitaldb/physionet.org/files/vitaldb/1.0.0/vital_files/"
save_path = "../../data/vitaldb/"
frequency = 500
overwrite = False
preprocess_ppg(vital_path=vital_path,
save_path=save_path,
frequency=frequency,
overwrite=overwrite)