-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdataset.py
261 lines (183 loc) · 8.38 KB
/
dataset.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
import vitaldb
import pandas as pd
def get_vital(path,type_='or',sample_rate=300, duration=300*6*5):
vf = vitaldb.VitalFile(path,track_names=['Intellivue/PLETH','Intellivue/NIBP_SYS','Intellivue/ECG_II'])
ppg = vf.to_numpy('Intellivue/PLETH',1/sample_rate)
ecg = vf.to_numpy('Intellivue/ECG_II',1/sample_rate)
first_idx = 0
if type_ == 'or': # starts from nibp
nibp_or = vf.to_numpy('Intellivue/NIBP_SYS',1/sample_rate)
for i, value in enumerate(nibp_or):
if pd.isna(value):
continue
else:
first_idx = i
break
elif type_ == 'rec': # starts from non-nan
for i, value in enumerate(ppg):
if pd.isna(value):
continue
else:
first_idx = i
break
ecg = ecg[first_idx:first_idx+duration]
ppg = ppg[first_idx:first_idx+duration]
return ppg, ecg
def get_ppg(path,type_='or',sample_rate=300, duration=300*60*5):
vf = vitaldb.VitalFile(path,track_names=['Intellivue/PLETH','Intellivue/NIBP_SYS'])
ppg = vf.to_numpy('Intellivue/PLETH',1/sample_rate)
first_idx = 0
if type_ == 'or':
nibp_or = vf.to_numpy('Intellivue/NIBP_SYS',1/sample_rate)
for i, value in enumerate(nibp_or):
if pd.isna(value):
continue
else:
first_idx = i
break
elif type_ == 'rec':
for i, value in enumerate(ppg):
if pd.isna(value):
continue
else:
first_idx = i
break
ppg = ppg[first_idx:first_idx+duration]
return ppg
import os
import pandas as pd
import json
import numpy as np
import vitaldb
import torch
from torch.nn import functional as F
from preprocess import list2specgram, ecgs2specgram, ppgs2specgram
from preprocess import ppg_pre, ecg_pre
# from dataset import get_vital
class VitalDataset(torch.utils.data.Dataset):
def __init__(self,root_dir='../data/pd_gy',ann_path='../data/pd_gy/train.json',sample_rate=300,duration=300*60*5):
with open(ann_path,'r') as f:
jf = json.load(f)
self.jf = jf
self.root_dir = root_dir
self.sample_rate = sample_rate
self.duration = duration
def __getitem__(self,idx):
item = self.jf[idx]
orp, recp = self.get_path(item)
# print(f'{orp}\n{recp} \n===')
ppg_or, ecg_or = get_vital(orp, type_='or' ,sample_rate=self.sample_rate,duration=self.duration)
ppg_rec, ecg_rec = get_vital(recp, type_='rec',sample_rate=self.sample_rate,duration=self.duration)
# ppgecg = list2specgram([ppg_or, ppg_rec, ecg_or, ecg_rec],type_=['ppg','ppg','ecg','ecg'], sample_rate=self.sample_rate)
# ppg_or, ppg_rec, ecg_or, ecg_rec = ppgecg
ecg_or, ecg_rec = ecgs2specgram([ecg_or,ecg_rec],sample_rate=self.sample_rate)
ppg_or, ppg_rec = ppgs2specgram([ppg_or,ppg_rec],sample_rate=self.sample_rate)
#if all([sig.shape == (151,342) for sig in [ppg_or,ppg_rec,ecg_or,ecg_rec]]):
# vital = np.array([ppg_or,ecg_or,ppg_rec,ecg_rec])
#else:
# vital = np.random.rand(4,151,342)
# print('\033[32m \033[41m'+f'Error: {orp}\n{recp}\n\n '+ '\033[0m')
vital = np.array([ppg_or,ecg_or,ppg_rec,ecg_rec])
vital = torch.tensor(vital)
y = item['nrs_2']
y = torch.tensor(y)
y = F.one_hot(y,num_classes=2)
vital = vital.float()
y = y.type(torch.LongTensor)
return vital, y
def __len__(self):
return len(self.jf)
def get_path(self, item):
orp = item['or_path'][0].replace('\\','/')
orp = os.path.join(self.root_dir, orp)
recp = item['rec_path'][0].replace('\\','/')
recp = os.path.join(self.root_dir,recp)
return orp, recp
def getitem(self,idx):
item = self.jf[idx]
orp, recp = self.get_path(item)
# print(f'{orp}\n{recp} \n===')
ppg_or, ecg_or = get_vital(orp, type_='or' ,sample_rate=self.sample_rate,duration=self.duration)
ppg_rec, ecg_rec = get_vital(recp, type_='rec',sample_rate=self.sample_rate,duration=self.duration)
# ppgecg = list2specgram([ppg_or, ppg_rec, ecg_or, ecg_rec],type_=['ppg','ppg','ecg','ecg'], sample_rate=self.sample_rate)
# ppg_or, ppg_rec, ecg_or, ecg_rec = ppgecg
ecg_or, ecg_rec = ecgs2specgram([ecg_or,ecg_rec],sample_rate=self.sample_rate)
ppg_or, ppg_rec = ppgs2specgram([ppg_or,ppg_rec],sample_rate=self.sample_rate)
vital = np.array([ppg_or,ecg_or,ppg_rec,ecg_rec])
y = item['nrs_2']
# y = torch.tensor(y)
# y = F.one_hot(y,num_classes=2)
#
# vital = vital.float()
# y = y.float()
return vital, y
def get_raw(self,idx):
item = self.jf[idx]
orp, recp = self.get_path(item)
# print(f'{orp}\n{recp} \n===')
ppg_or, ecg_or = get_vital(orp, type_='or' ,sample_rate=self.sample_rate,duration=self.duration)
ppg_rec, ecg_rec = get_vital(recp, type_='rec',sample_rate=self.sample_rate,duration=self.duration)
vital = np.array([ppg_or,ecg_or,ppg_rec,ecg_rec])
return vital
def get_pre(self,idx):
item = self.jf[idx]
orp, recp = self.get_path(item)
# print(f'{orp}\n{recp} \n===')
ppg_or, ecg_or = get_vital(orp, type_='or' ,sample_rate=self.sample_rate,duration=self.duration)
ppg_rec, ecg_rec = get_vital(recp, type_='rec',sample_rate=self.sample_rate,duration=self.duration)
ppg_or = ppg_pre(ppg_or)
ppg_rec = ppg_pre(ppg_rec)
ecg_or = ecg_pre(ecg_or)
ecg_rec = ecg_pre(ecg_rec)
vital = np.array([ppg_or,ecg_or,ppg_rec,ecg_rec])
return vital
def get_info(self,idx):
return self.jf[idx]
def get_spectrogram(self,idx):
item = self.jf[idx]
orp, recp = self.get_path(item)
# print(f'{orp}\n{recp} \n===')
ppg_or, ecg_or = get_vital(orp, type_='or' ,sample_rate=self.sample_rate,duration=self.duration)
ppg_rec, ecg_rec = get_vital(recp, type_='rec',sample_rate=self.sample_rate,duration=self.duration)
from preprocess import ppg2specgram_all, ecg2specgram_all
f,t,ppg_or = ppg2specgram_all(ppg_or,self.sample_rate)
f,t,ecg_or = ecg2specgram_all(ecg_or,self.sample_rate)
f,t,ppg_rec = ppg2specgram_all(ppg_rec,self.sample_rate)
f,t,ecg_rec = ecg2specgram_all(ecg_rec,self.sample_rate)
vital = np.array([ppg_or,ecg_or,ppg_rec,ecg_rec])
return f, t, vital
class VitalDataset_fs(torch.utils.data.Dataset):
def __init__(self,root_dir='../data/all_3',ann_path='../data/pd_gy/train.json',sample_rate=300,duration=300*60*5):
with open(ann_path,'r') as f:
jf = json.load(f)
self.jf = jf
self.root_dir = root_dir
self.sample_rate = sample_rate
self.duration = duration
def __getitem__(self,idx):
item = self.jf[idx]
fname = item['pt_id_date']
vital = np.load(os.path.join(self.root_dir,f'{fname}.npy'))
y = item['nrs_2']
# y = F.one_hot(y,num_classes=2)
vital = torch.tensor(vital)
vital = vital.float()
y = torch.tensor(y)
y = y.type(torch.LongTensor)
return vital, y
def __len__(self):
return len(self.jf)
def get_npvital(self,idx):
pass
def get_path(self, item):
orp = item['or_path'][0].replace('\\','/')
orp = os.path.join(self.root_dir, orp)
recp = item['rec_path'][0].replace('\\','/')
recp = os.path.join(self.root_dir,recp)
return orp, recp
def getitem_np(self,idx):
item = self.jf[idx]
fname = item['pt_id_date']
vital = np.load(os.path.join(self.root_dir,f'{fname}.npy'))
y = item['nrs_2']
return vital, y