/
wfdb_local_lin_svr_olclstr.py
449 lines (342 loc) · 20.3 KB
/
wfdb_local_lin_svr_olclstr.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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
import matplotlib.pyplot as plt
import numpy as np
from scipy import signal as sps
from sklearn.svm import SVR, NuSVR, NuSVC
from sklearn.neighbors import NearestNeighbors, DistanceMetric
from scipy.spatial import distance_matrix
from scipy.spatial import distance
# from sklearn.neighbors import DistanceMetric
# from sklearn.cluster import KMeans
from sklearn.linear_model import SGDRegressor
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler
import os
import shutil
import posixpath
import time
from timeit import default_timer as timer
# from pynput import mouse
import plotly.express as px
import plotly.graph_objects as go
from plotly.subplots import make_subplots
import sys
sys.path.append("/home/pdp1145/fetal_ecg_det/wfdb_python_master/")
# import kymatio
import wfdb
import pywt
n_taps = 101
f = 0.015
fir_hipass = sps.firwin(n_taps, f, pass_zero=False)
# n_taps = 11
# f_hipass = 5.0
# fir_hipass = sps.firwin(n_taps, f_hipass, width=None, window='hamming', pass_zero='highpass', scale=False, nyq=None, fs=1000.0)
# Show high pass filter & freq resp:
#
x_idxs = np.arange(n_taps)
figz = make_subplots(rows=2, cols=1, subplot_titles=("FIR Coefs", "Freq Resp"))
figz.append_trace(go.Scatter(x=x_idxs, y=fir_hipass), row=1, col=1)
# figz.append_trace(go.Scatter(x=x_idxs, y=fetal_lead[init_record_skip: (init_record_skip + overlap_wdw_idx)]), row=2, col=1)
figz.show()
# record = wfdb.rdrecord('/home/pdp1145/fetal_ecg_det/wfdb_python_master/sample-data/a103l')
record = wfdb.rdrecord('/home/pdp1145/fetal_ecg_det/fetal_ecg_data/ARR_01')
# record = wfdb.rdrecord('/home/pdp1145/fetal_ecg_det/fetal_ecg_data/NR_06')
# wfdb.plot_wfdb(record=record, title='Record a103l from Physionet Challenge 2015')
rem_record_lth = 20000
n_bpfs = 64
scale_fac = 0.05 # 256/n_bpfs
cwt_wdw_lth_h = 1
svr_wdw_lth = 128
svr_wdw_lth_inv = 1.0/float(svr_wdw_lth)
n_coef_tpls = 8000
init_record_skip = 1000
wdw_shift = 1
plot_freq = 4000
mat_lead_r = np.float32(record.p_signal[0:rem_record_lth,0])
mat_lead = sps.convolve(mat_lead_r, fir_hipass, mode='same', method='direct')
fetal_lead_r = np.float32(record.p_signal[0:rem_record_lth,1])
fetal_lead = sps.convolve(fetal_lead_r, fir_hipass, mode='same', method='direct')
x = np.arange(len(mat_lead))
fig = make_subplots(rows=2, cols=1, subplot_titles=("Raw Maternal Lead", "Filtered Maternal Lead"))
fig.append_trace(go.Scatter(x=x, y=mat_lead_r), row=1, col=1)
fig.append_trace(go.Scatter(x=x, y=mat_lead), row=2, col=1)
fig.show()
fig = make_subplots(rows=2, cols=1, subplot_titles=("Raw Fetal Lead", "Filtered Fetal Lead"))
fig.append_trace(go.Scatter(x=x, y=fetal_lead_r), row=1, col=1)
fig.append_trace(go.Scatter(x=x, y=fetal_lead), row=2, col=1)
fig.show()
fig = make_subplots(rows=2, cols=1, subplot_titles=("Filtered Maternal Lead", "Filtered Fetal Lead"))
fig.append_trace(go.Scatter(x=x, y=mat_lead), row=1, col=1)
fig.append_trace(go.Scatter(x=x, y=fetal_lead), row=2, col=1)
fig.show()
widths = np.arange(1, (n_bpfs+1))*scale_fac
cwt_maternal_lead = np.float32(sps.cwt(mat_lead, sps.ricker, widths))
cwt_fetal_lead = np.float32(sps.cwt(fetal_lead, sps.ricker, widths))
cwt_trans = np.float32(np.transpose(cwt_maternal_lead))
cwt_trans_fetal = np.float32(np.transpose(cwt_fetal_lead))
fig_cwt_mat, ax_cwt_mat = plt.subplots()
ax_cwt_mat.imshow(cwt_maternal_lead[:, 0 : 1000], aspect='auto')
time.sleep(5.0)
fig_cwt_fetal, ax_cwt_fetal = plt.subplots()
ax_cwt_fetal.imshow(cwt_fetal_lead[:, 0 : 1000], aspect='auto')
time.sleep(5.0)
# SVR w/ single CWT vector -> fetal ECG:
#
n_feats = (cwt_wdw_lth_h*2 -1)*n_bpfs
n_feats_maternal = n_feats*svr_wdw_lth
n_feats_maternal_fetal = n_feats*2*svr_wdw_lth
maternal_feature_vectors = np.float32(np.zeros([n_coef_tpls, n_feats_maternal]))
maternal_fetal_feature_vectors = np.float32(np.zeros([n_coef_tpls, n_feats_maternal_fetal]))
linear_regression_coefs = np.float32(np.zeros([n_coef_tpls, n_feats]))
linear_regression_intercepts = np.float32(np.zeros([n_coef_tpls,]))
mat_lead_wdw_hist = np.float32(np.zeros([n_coef_tpls, svr_wdw_lth]))
n_maternal_fetal_feature_vectors = 0
init_delay = init_record_skip
mat_lead_wdw_hist_arr = np.float32(np.zeros(rem_record_lth,))
abdominal_est = np.float32(np.zeros(rem_record_lth,))
abdominal_est_idxs = np.arange(0, n_coef_tpls)
dist_arr = np.float32(np.zeros(rem_record_lth,))
n_svrs = 0
overlap_wdw_idx = 0
init = 0
if(init == 1): # Load initialized template library and regressors if already initialized
maternal_fetal_feature_vectors = np.load('maternal_fetal_feature_vectors1k.npy')
maternal_feature_vectors = np.float32(np.load('maternal_feature_vectors1k.npy'))
linear_regression_coefs = np.float32(np.load('linear_regression_coefs1k.npy'))
linear_regression_intercepts = np.float32(np.load('linear_regression_intercepts1k.npy'))
n_svrs = n_coef_tpls*wdw_shift # Skip past template library initialization
init_delay = init_delay + n_svrs
overlap_wdw_idx = overlap_wdw_idx + n_svrs
for svr_wdw_beg in np.arange(init_delay, init_delay + rem_record_lth - svr_wdw_lth -1, wdw_shift):
init_sect_beg = timer()
wdw_beg = svr_wdw_beg
wdw_end = wdw_beg + svr_wdw_lth
fetal_lead_wdw = np.float32(np.zeros([(wdw_end - wdw_beg),]))
mat_lead_wdw = np.float32(np.zeros([(wdw_end - wdw_beg),]))
cwt_wdw = np.float32(np.zeros([(wdw_end - wdw_beg), n_feats]))
cwt_wdw_fetal = np.float32(np.zeros([(wdw_end - wdw_beg), n_feats]))
regr_idx = 0
init_sect_end = timer()
# print(" Init sect elapsed time: @ " + str(svr_wdw_beg) + " " + str(init_sect_end - init_sect_beg))
init_sect_beg = timer()
for wdw_idx in np.arange(wdw_beg, wdw_end):
fetal_lead_wdw[regr_idx] = fetal_lead[wdw_idx] # Extract lead windows for regression
mat_lead_wdw[regr_idx] = mat_lead[wdw_idx]
cwt_wdw[regr_idx,:] = cwt_trans[wdw_idx - cwt_wdw_lth_h : wdw_idx + cwt_wdw_lth_h -1, :].flatten() # Extract feature vectors for regression & knn
cwt_wdw_fetal[regr_idx,:] = cwt_trans_fetal[wdw_idx - cwt_wdw_lth_h : wdw_idx + cwt_wdw_lth_h -1, :].flatten() # Extract feature vectors for regression & knn
regr_idx = regr_idx +1
init_sect_end = timer()
# print(" Array collection sect elapsed time: @ " + str(svr_wdw_beg) + " " + str(init_sect_end - init_sect_beg))
if(n_svrs < n_coef_tpls): # Initialization phase (fill template library)
init_sect_beg = timer()
maternal_feature_vectors[n_svrs, :] = cwt_wdw.flatten()
maternal_fetal_feature_vectors[n_svrs, :] = np.concatenate((cwt_wdw.flatten(), cwt_wdw_fetal.flatten()), axis = None)
nusv_res = NuSVR(nu=0.95, C=10.0, kernel='linear', degree=3, gamma='scale', coef0=0.0, shrinking=True, tol=0.001, cache_size=200, verbose=False, max_iter=10000)
z_rbf = nusv_res.fit(cwt_wdw, fetal_lead_wdw).predict(cwt_wdw)
# z_rbf = nusv_res.fit(cwt_wdw, mat_lead_wdw).predict(cwt_wdw)
nusv_lin_coef = np.float32(nusv_res.coef_)
linear_regression_coefs[n_svrs, :] = np.float32(nusv_lin_coef)
linear_regression_intercepts[n_svrs] = np.float32(nusv_res.intercept_)
nusv_intercept = np.float32(nusv_res.intercept_)
mat_lead_wdw_hist[n_svrs, :] = mat_lead_wdw # Save maternal lead for this window
init_sect_end = timer()
# print(" NuSVR sect elapsed time: @ " + str(svr_wdw_beg) + " " + str(init_sect_end - init_sect_beg))
else: # Estimates based on retrieved templates / update templates
# Maternal & fetal CWT templates centered on this sample:
maternal_feature_vector_s = cwt_wdw.flatten()
maternal_feature_vector_rs = np.reshape(maternal_feature_vector_s, (1, maternal_feature_vector_s.size))
maternal_fetal_feature_vector_s = np.concatenate((cwt_wdw.flatten(), cwt_wdw_fetal.flatten()), axis=None)
# Get k-nn maternal lead templates:
token_dists_knn = distance.cdist(maternal_feature_vector_rs, maternal_feature_vectors, metric='cityblock')
token_dists_knn_sorted_idxs = np.argsort(token_dists_knn).flatten()
token_dists_knn_fl = token_dists_knn.flatten()
token_dists_knn_sorted = token_dists_knn_fl[token_dists_knn_sorted_idxs]
dist_arr[init_record_skip + overlap_wdw_idx + int(svr_wdw_lth / 2)] = token_dists_knn_sorted[0]
mat_wdw_knn = mat_lead_wdw_hist[token_dists_knn_sorted_idxs[0],:]
mat_lead_wdw_hist_arr[(init_record_skip + overlap_wdw_idx): (init_record_skip + overlap_wdw_idx + svr_wdw_lth)] = \
np.add(mat_wdw_knn, mat_lead_wdw_hist_arr[(init_record_skip + overlap_wdw_idx): (init_record_skip + overlap_wdw_idx + svr_wdw_lth)])
#
# token_dist_knn_idxs = np.arange(len(token_dists_knn_sorted))
# fig = make_subplots(rows=1, cols=1)
# fig.append_trace(go.Scatter(x=token_dist_knn_idxs, y=token_dists_knn_sorted), row=1, col=1)
# fig.show()
# Retrieve regression coef's from best matches:
#
nusv_lin_coef = linear_regression_coefs[token_dists_knn_sorted_idxs[0], :]
nusv_intercept = linear_regression_intercepts[token_dists_knn_sorted_idxs[0]]
# # Generate abdominal signal estimates:
# #
# cwt_wdw_trans = np.transpose(cwt_wdw)
# z_cwt_xcoef = np.matmul(nusv_lin_coef, cwt_wdw_trans)
# z_cwt_xcoef_rs = z_cwt_xcoef + nusv_intercept
# Generate abdominal signal estimate for this window:
#
cwt_wdw_trans = np.transpose(cwt_wdw)
z_cwt_xcoef = np.matmul(nusv_lin_coef, cwt_wdw_trans)
z_cwt_xcoef_rs = (np.reshape(z_cwt_xcoef, (svr_wdw_lth,)) + nusv_intercept)*svr_wdw_lth_inv
abdominal_est[(init_record_skip + overlap_wdw_idx) : (init_record_skip + overlap_wdw_idx + svr_wdw_lth)] = \
np.add(z_cwt_xcoef_rs, abdominal_est[(init_record_skip + overlap_wdw_idx) : (init_record_skip + overlap_wdw_idx + svr_wdw_lth)])
# abdominal_est[init_record_skip + overlap_wdw_idx + int(svr_wdw_lth/2)] = \
# np.add(z_cwt_xcoef_rs[int(svr_wdw_lth/2)], abdominal_est[init_record_skip + overlap_wdw_idx + int(svr_wdw_lth/2)])
overlap_wdw_idx = overlap_wdw_idx +1
n_svrs = n_svrs +1
if((n_svrs % 50) == 1214):
figz = make_subplots(rows=3, cols=1, subplot_titles=("Maternal", "Abdominal",
"Maternal NuSVR Estimate: nu=0.75, Linear, C=1.0, CWT Window Length = 4, Training Record Length = 5000", "Abdominal Estimate"))
figz.append_trace(go.Scatter(x = x_idxs, y = mat_lead_wdw), row=1, col=1)
figz.append_trace(go.Scatter(x = x_idxs, y = fetal_lead_wdw), row=2, col=1)
figz.append_trace(go.Scatter(x = x_idxs, y = z_cwt_xcoef_rs), row=3, col=1)
figz.append_trace(go.Scatter(x = x_idxs, y = z_rbf), row=3, col=1)
figz.show()
time.sleep(5.0)
if ((n_svrs % plot_freq) == 0):
x_idxs = np.arange(init_record_skip, (init_record_skip + overlap_wdw_idx))
figz = make_subplots(rows=4, cols=1, subplot_titles=("Maternal", "Abdominal", "Abdominal Estimate"))
figz.append_trace(go.Scatter(x=x_idxs, y=mat_lead[init_record_skip : (init_record_skip + overlap_wdw_idx)]), row=1, col=1)
figz.append_trace(go.Scatter(x=x_idxs, y=fetal_lead[init_record_skip : (init_record_skip + overlap_wdw_idx)]), row=2, col=1)
figz.append_trace(go.Scatter(x=x_idxs, y=abdominal_est[init_record_skip : (init_record_skip + overlap_wdw_idx)]), row=3, col=1)
# figz.append_trace(go.Scatter(x=x_idxs, y=dist_arr[init_record_skip : (init_record_skip + overlap_wdw_idx)]), row=4, col=1)
figz.append_trace(go.Scatter(x=x_idxs, y=mat_lead_wdw_hist_arr[init_record_skip : (init_record_skip + overlap_wdw_idx)]), row=4, col=1)
figz.show()
time.sleep(10.0)
if(init == 0):
np.save('maternal_fetal_feature_vectors1k', maternal_fetal_feature_vectors, allow_pickle=False)
np.save('maternal_feature_vectors1k', maternal_feature_vectors, allow_pickle=False)
np.save('linear_regression_coefs1k', linear_regression_coefs, allow_pickle=False)
np.save('linear_regression_intercepts1k', linear_regression_intercepts, allow_pickle=False)
# figz.data = []
if ((n_svrs % 25) == 0):
print(['n_svrs: ' + str(n_svrs)])
# Get histogram of token - token distances for clustering:
#
dist = DistanceMetric.get_metric('manhattan')
token_dists = dist.pairwise(maternal_fetal_feature_vectors[0:200,:])
# token_dists = distance_matrix(maternal_fetal_feature_vectors, maternal_fetal_feature_vectors, p=1, threshold=100000000)
# token_dists = distance.cdist(maternal_fetal_feature_vectors[0:5,:], maternal_fetal_feature_vectors[0:5,:], metric='cityblock')
token_dists = distance.pdist(maternal_fetal_feature_vectors, metric='cityblock')
# token_dist_hist = np.histogram(token_dists, bins=1000)
# token_dist_hist_idxs = np.arange(len(token_dist_hist))
token_dists_sorted = np.sort(token_dists)
token_dist_idxs = np.arange(len(token_dists_sorted))
fig = make_subplots(rows=1, cols=1)
fig.append_trace(go.Scatter(x=token_dist_idxs, y=token_dists_sorted), row=1, col=1)
fig.show()
# kmeans_maternal_fetal = KMeans(n_clusters = 100, init = 'k-means++').fit(maternal_fetal_feature_vectors)
post_init = init_delay + n_coef_tpls - svr_wdw_lth # Post-init processing w/ no gap
for svr_wdw_beg in np.arange(post_init, post_init + rem_record_lth - svr_wdw_lth, wdw_shift):
wdw_beg = svr_wdw_beg
wdw_end = wdw_beg + svr_wdw_lth
fetal_lead_wdw = np.float32(np.zeros([(wdw_end - wdw_beg), ]))
mat_lead_wdw = np.float32(np.zeros([(wdw_end - wdw_beg), ]))
cwt_wdw = np.float32(np.zeros([(wdw_end - wdw_beg), n_feats]))
cwt_wdw_fetal = np.float32(np.zeros([(wdw_end - wdw_beg), n_feats]))
regr_idx = 0
# Snapshot of maternal and fetal CWT contexts (templates) and CWT feature vectors for regression
for wdw_idx in np.arange(wdw_beg, wdw_end):
fetal_lead_wdw[regr_idx] = fetal_lead[wdw_idx] # Extract lead windows for regression
mat_lead_wdw[regr_idx] = mat_lead[wdw_idx]
cwt_wdw[regr_idx, :] = cwt_trans[wdw_idx - cwt_wdw_lth_h: wdw_idx + cwt_wdw_lth_h -1,:].flatten() # Extract feature vectors for regression & knn
cwt_wdw_fetal[regr_idx, :] = cwt_trans[wdw_idx - cwt_wdw_lth_h: wdw_idx + cwt_wdw_lth_h -1,:].flatten() # Extract feature vectors for regression & knn
regr_idx = regr_idx + 1
# Maternal & fetal CWT templates centered on this sample:
maternal_feature_vector_s = cwt_wdw.flatten()
maternal_feature_vector_rs = np.reshape(maternal_feature_vector_s, (1, maternal_feature_vector_s.size))
maternal_fetal_feature_vector_s = np.concatenate((cwt_wdw.flatten(), cwt_wdw_fetal.flatten()), axis=None)
# Get k-nn maternal lead templates:
token_dists_knn = distance.cdist(maternal_feature_vector_rs, maternal_feature_vectors, metric='cityblock')
token_dists_knn_fl = token_dists_knn.flatten()
token_dists_knn_sorted_idxs = np.argsort(token_dists_knn).flatten()
token_dists_knn_fl = token_dists_knn.flatten()
token_dists_knn_sorted = token_dists_knn_fl[token_dists_knn_sorted_idxs]
token_dist_knn_idxs = np.arange(len(token_dists_knn_sorted))
#
# fig = make_subplots(rows=1, cols=1)
# fig.append_trace(go.Scatter(x=token_dist_knn_idxs, y=token_dists_knn_sorted), row=1, col=1)
# fig.show()
# Retrieve regression coef's from best matches:
#
nusv_lin_coef = linear_regression_coefs[token_dists_knn_sorted_idxs[0], :]
nusv_intercept = linear_regression_intercepts[token_dists_knn_sorted_idxs[0]]
# Generate abdominal signal estimates:
#
cwt_wdw_trans = np.transpose(cwt_wdw)
z_cwt_xcoef = np.matmul(nusv_lin_coef, cwt_wdw_trans)
z_cwt_xcoef_rs = z_cwt_xcoef + nusv_intercept
# Update abdominal signal estimate:
try:
abdominal_est[overlap_wdw_idx : (overlap_wdw_idx + svr_wdw_lth)] = np.add(z_cwt_xcoef_rs, abdominal_est[overlap_wdw_idx: (overlap_wdw_idx + svr_wdw_lth)])
except:
arf = 22
# x_idxs = np.arange(len(fetal_lead_wdw))
# figz = make_subplots(rows=3, cols=1, subplot_titles=("Maternal", "Abdominal", "Abdominal Estimate"))
# figz.append_trace(go.Scatter(x=x_idxs, y=mat_lead_wdw), row=1, col=1)
# figz.append_trace(go.Scatter(x=x_idxs, y=fetal_lead_wdw), row=2, col=1)
# figz.append_trace(go.Scatter(x=x_idxs, y=z_cwt_xcoef_rs), row=3, col=1)
# figz.show()
# time.sleep(5.0)
# maternal_feature_vectors[n_svrs, :] = cwt_wdw.flatten()
# maternal_fetal_feature_vectors[n_svrs, :] = np.concatenate((cwt_wdw.flatten(), cwt_wdw_fetal.flatten()), axis=None)
# nusv_res = NuSVR(nu=0.75, C=1.0, kernel='linear', degree=3, gamma='scale', coef0=0.0, shrinking=True, tol=0.001,
# cache_size=200, verbose=False, max_iter=-1)
# z_rbf = nusv_res.fit(cwt_wdw, fetal_lead_wdw).predict(cwt_wdw)
#
# nusv_lin_coef = np.float32(nusv_res.coef_)
# cwt_wdw_trans = np.transpose(cwt_wdw)
# z_cwt_xcoef = np.matmul(nusv_lin_coef, cwt_wdw_trans)
# z_cwt_xcoef_rs = np.reshape(z_cwt_xcoef, (svr_wdw_lth,)) + np.float32(nusv_res.intercept_)
#
# linear_regression_coefs[n_svrs, :] = np.float32(nusv_lin_coef)
# linear_regression_intercepts[n_svrs] = np.float32(nusv_res.intercept_)
if ((n_svrs % 50) == 1214):
figz = make_subplots(rows=3, cols=1, subplot_titles=("Maternal", "Abdominal",
"Maternal NuSVR Estimate: nu=0.75, Linear, C=1.0, CWT Window Length = 4, Training Record Length = 5000",
"Abdominal Estimate"))
# x_idxs = np.arange(len(fetal_lead))
figz.append_trace(go.Scatter(x=x_idxs, y=mat_lead_wdw), row=1, col=1)
figz.append_trace(go.Scatter(x=x_idxs, y=fetal_lead_wdw), row=2, col=1)
figz.append_trace(go.Scatter(x=x_idxs, y=z_cwt_xcoef_rs), row=3, col=1)
figz.append_trace(go.Scatter(x=x_idxs, y=z_rbf), row=3, col=1)
figz.show()
time.sleep(5.0)
if ((n_svrs % 250) == 0):
np.save('abdominal_est1k', abdominal_est, allow_pickle=False)
x_idxs = np.arange(overlap_wdw_idx)
figz = make_subplots(rows=3, cols=1, subplot_titles=("Maternal", "Abdominal", "Abdominal Estimate"))
figz.append_trace(go.Scatter(x=x_idxs, y=mat_lead[0 : overlap_wdw_idx]), row=1, col=1)
figz.append_trace(go.Scatter(x=x_idxs, y=fetal_lead[0 : overlap_wdw_idx]), row=2, col=1)
figz.append_trace(go.Scatter(x=x_idxs, y=abdominal_est[0 : overlap_wdw_idx]), row=3, col=1)
figz.show()
time.sleep(5.0)
if ((n_svrs % 25) == 0):
print(['n_svrs: ' + str(n_svrs)])
overlap_wdw_idx = overlap_wdw_idx + 1
n_svrs = n_svrs + 1
arf = 12
# figz = make_subplots(rows=2, cols=1, subplot_titles=("Maternal", "Maternal NuSVR Estimate: nu=0.75, Linear, C=1.0, CWT Window Length = 4, Training Record Length = 5000"))
# figz.append_trace(go.Scatter(x = x_idxs, y = mat_lead_wdw), row=1, col=1)
# figz.append_trace(go.Scatter(x = x_idxs, y = mat_lead_wdw), row=2, col=1)
# figz.append_trace(go.Scatter(x = x_idxs, y = z_rbf), row=2, col=1)
# figz.show()
# matplotlib.pyplot.close()
# Run trained SVR on full record:
#
wdw_beg = 1
wdw_end = 15000
regr_idx = 0
fetal_lead_wdw = np.zeros([(wdw_end - wdw_beg),])
mat_lead_wdw = np.zeros([(wdw_end - wdw_beg),])
cwt_wdw = np.zeros([(wdw_end - wdw_beg), n_feats])
for wdw_idx in np.arange(wdw_beg, wdw_end):
fetal_lead_wdw[regr_idx] = fetal_lead[wdw_idx]
mat_lead_wdw[regr_idx] = mat_lead[wdw_idx]
blef = cwt_trans[wdw_idx - cwt_wdw_lth_h : wdw_idx + cwt_wdw_lth_h -1, :]
cwt_wdw[regr_idx,:] = blef.flatten()
regr_idx = regr_idx +1
z_rbf = nusv_res.predict(cwt_wdw)
figz = make_subplots(rows=2, cols=1)
figz.append_trace(go.Scatter(x = x_idxs, y = mat_lead_wdw), row=1, col=1)
figz.append_trace(go.Scatter(x = x_idxs, y = fetal_lead_wdw), row=2, col=1)
figz.append_trace(go.Scatter(x = x_idxs, y = z_rbf), row=2, col=1)
figz.show()
# plt.plot(fetal_lead[500:700])
# plt.plot(svr_rbf.predict(cwt_trans[500:700,:]))
arf = 12