-
Notifications
You must be signed in to change notification settings - Fork 3
/
util.py
149 lines (129 loc) · 4.48 KB
/
util.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
import numpy as np
data_path = 'data/'
def convert_to_2d(arr, index=None):
array = []
if index != None:
for subject_index in range(arr.shape[0]):
array.extend(arr[subject_index][index])
else:
for subject_index in range(arr.shape[0]):
array.extend(arr[subject_index])
return np.array(array)
def find_max_min(arr):
arr_min = np.min(arr)
arr_max = np.max(arr)
return arr_min, arr_max
def to_timeseries(X, y, is_train, max_len):
"""
generate data for one user.
"""
seq_x = []
seqs_x = []
seqs_y = []
seq_y = []
min_max = np.load(data_path+'min_max.npy')
min_max_train = np.load(data_path+'min_max_train.npy')
ppg_all_min, ppg_all_max = min_max[0]
ecg_all_min, ecg_all_max = min_max[1]
abp_all_min, abp_all_max = min_max[2]
ppg_train_min, ppg_train_max = min_max_train[0]
ecg_train_min, ecg_train_max = min_max_train[1]
abp_train_min, abp_train_max = min_max_train[2]
for i in range(X[0].shape[0]):
if len(seq_x) < max_len:
if is_train:
seq_x.append([
(X[0][i]-ppg_train_min)/(ppg_train_max-ppg_train_min),
(X[1][i]-ecg_train_min)/(ecg_train_max-ecg_train_min)
])
seq_y.append([(y[i]-abp_train_min)/(abp_train_max-abp_train_min)])
else:
seq_x.append([
(X[0][i]-ppg_all_min)/(ppg_all_max-ppg_all_min),
(X[1][i]-ecg_all_min)/(ecg_all_max-ecg_all_min)
])
seq_y.append([(y[i]-abp_all_min)/(abp_all_max-abp_all_min)])
else:
seq_x = seq_x[1:]
seq_y = seq_y[1:]
if is_train:
seq_x.append([
(X[0][i]-ppg_train_min)/(ppg_train_max-ppg_train_min),
(X[1][i]-ecg_train_min)/(ecg_train_max-ecg_train_min)
])
seq_y.append([(y[i]-abp_train_min)/(abp_train_max-abp_train_min)])
else:
seq_x.append([(X[0][i]-ppg_all_min)/(ppg_all_max-ppg_all_min), (X[1][i]-ecg_all_min)/(ecg_all_max-ecg_all_min)])
seq_y.append([(y[i]-abp_all_min)/(abp_all_max-abp_all_min)])
if len(seq_x) < max_len:
continue
else:
seqs_x.append(seq_x)
seqs_y.append(seq_y)
zeros_y = np.zeros((len(seqs_y), max_len, 1))
return seqs_x, zeros_y, seqs_y
def find_abp_peak_sys(abp, delay):
index_max_abp = []
loop_count = int(len(abp)/delay)
index = 0
for i in range(loop_count):
max_value = 0
max_index = 0
for j in abp[i*delay:(i+1)*delay]:
if j > max_value:
max_value = j
max_index = index
index = index + 1
index_max_abp.append(max_index)
return index_max_abp
def find_abp_peak_dia(abp, delay):
index_min_abp = []
loop_count = int(len(abp)/delay)
index = 0
for i in range(loop_count):
min_value = 9999999999
min_index = 0
for j in abp[i*delay:(i+1)*delay]:
if j < min_value:
min_value = j
min_index = index
index = index + 1
index_min_abp.append(min_index)
return index_min_abp
def zero_order_holding_first(abp, delay, is_sys):
if is_sys:
index_max_abp = find_abp_peak_sys(abp, delay)
else:
index_max_abp = find_abp_peak_dia(abp, delay)
peak_abp = np.zeros(len(abp))
j = 0
for i in range(len(abp)):
if i < index_max_abp[j]:
if j == 1:
peak_abp[i] = abp[index_max_abp[0]]
else:
peak_abp[i] = abp[index_max_abp[j-1]]
else:
j = j + 1
if j >= len(index_max_abp):
j = j - 1
if j == 1:
peak_abp[i] = abp[index_max_abp[0]]
else:
peak_abp[i] = abp[index_max_abp[j-1]]
return peak_abp
def zero_order_second(signal):
change = 0
num = len(signal)
for i in range(0, num-10):
counter = 0
if signal[i+1] != signal[i]:
change = 1
for j in range(1, 12):
if signal[i+j] == signal[i]:
counter = counter + 1
if change == 1 and counter > 0:
for j in range(0, 11):
signal[i+j] = signal[i]
change = 0
return signal