forked from abhishekdutta/healthcare-predictive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQualityOfSignal.py
366 lines (279 loc) · 11.9 KB
/
QualityOfSignal.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
"""
Adapted from Xiao Hu, PhD's MATLAB code for waveform quality checks
Dependencies: numpy, scipy
"""
from __future__ import division
import numpy as np
import scipy
from scipy import signal
import logging
from matplotlib import pyplot as plt
import scipy.io as sio
class QualityOfSignal():
# just need to run isPPGGoodQuality(signal, timestamp, samplingFreq)
# and it will output 1 or -1
def __init__(self):
pass
def isPPGGoodQuality(self, ppgSig, emptyVariable, fs, **kwargs):
# Check to see if opt specified, otherwise use default
if 'opt' in kwargs:
opt = kwargs.get('opt')
else:
opt = self.makeDefaultPPGSignalQualityParameter()
# Derek -- this throws div0 error sometimes, appears to be unused?
# dt = 1./np.diff(tsofSig)
onset = self.DetectPulseOnset(ppgSig, fs, opt['pulseWidth'])
if (len(onset) < 3):
qualityFlag = 0
return qualityFlag
sigMat, idx = self.formSignalMatrix(ppgSig, onset, fs)
if len(sigMat) != 0:
try:
u, s, v = np.linalg.svd(sigMat)
except np.linalg.linalg.LinAlgError:
logging.warn("Single value decomposition failed! Returning indeterminate (0)")
return 0
else:
qualityFlag = 0
return qualityFlag
ai = np.array([np.NaN, np.NaN, np.NaN])
for j in range(1, np.minimum(4,len(s))):
ai[j-1] = s[j-1]/s[j]
if ai[0] > opt['AI1Threshold'] or ai[1] > opt['AI2Threshold'] or ai[2] > opt['AI3Threshold']:
qualityFlag = 1
else:
qualityFlag = -1
return qualityFlag
def formSignalMatrix(self, sig, fiducialPnt, fs, **kwargs):
# Check to see if opt specified, otherwise use default
if 'algoParam' in kwargs:
algoParam = kwargs.get('algoParam')
else:
algoParam = self.makeDefaultSig2MatrixParam()
if type(fiducialPnt) == list:
fiducialPnt = np.array(fiducialPnt)
if type(sig) == list:
sig = np.array(sig)
if fiducialPnt.size == 0 or sig.size == 0:
sigMat = []
return sigMat
# make sure the signal is a column vector
if len(sig.shape) > 1:
if sig.shape[1] > sig.shape[0]:
sig = sig.T
maxBeatLeninMS = 60/algoParam['minHR'] * 1000
minBeatLeninMS = 60/algoParam['maxHR'] * 1000
# determine the appropriate length of the pulse
beatLeninMS = 1000*np.diff(fiducialPnt)/fs
# find beats with the length between the max and min beat length
idx_min = np.where(beatLeninMS > minBeatLeninMS)
idx_max = np.where(beatLeninMS < maxBeatLeninMS)
idx = np.intersect1d(idx_min, idx_max)
if idx.size == 0:
sigMat = []
return sigMat, idx
finalBeatLeninMS = np.percentile(beatLeninMS[idx], algoParam['prctile4BeatLength'])
minimalBeatLeninMS = np.percentile(beatLeninMS[idx], algoParam['prctile4MinimalBeatLength'])
# also remove pulses with a length less than minimal length
idxx = np.where(beatLeninMS[idx] < minimalBeatLeninMS)
idx[idxx] = []
beatLen = int(np.fix(finalBeatLeninMS * fs/1000))
sigMat = np.zeros((beatLen, len(idx)))
for i in range(0, len(idx)):
lenofPulse = fiducialPnt[idx[i]+1] - fiducialPnt[idx[i]]
if lenofPulse >= beatLen:
sigMat[:,i] = sig[fiducialPnt[idx[i]]:fiducialPnt[idx[i]]+beatLen]
else:
deltaW = beatLen - lenofPulse
if deltaW < 3:
samplesToFitData = 3
elif deltaW > 10:
samplesToFitData = 10
else:
samplesToFitData = deltaW
vv = self.PolyReSample(sig[fiducialPnt[idx[i]+1] - samplesToFitData: fiducialPnt[idx[i]+1]], np.r_[0:samplesToFitData].T, np.r_[samplesToFitData:samplesToFitData+deltaW].T, 1)
sigMat[:,i] = np.hstack((sig[fiducialPnt[idx[i]]:fiducialPnt[idx[i]+1]], vv))
# remove mean and normalized by standard deviation
sigMat[:,i] = self.NormalizeSig(sigMat[:,i],2)
return sigMat, idx
def makeDefaultPPGSignalQualityParameter(self):
opt = {}
#length of the window in milliseconds to form sum fraction for pulse onset detection
opt['pulseWidth'] = 120
# lower threshold for three alignment indices to just signal quality
opt['AI1Threshold'] = 2
opt['AI2Threshold'] = 2
opt['AI3Threshold'] = 2
return opt
# Is off by some values...
def DetectPulseOnset(self, asig, fs, wMS):
"""
Detect locations of onset for a pulsatile signal
sig: single-channel pulsatile signal
fs: sampling frequency
wMS: window in milliseconds to search for onset after a
threshold-crossing point is detected. This value will
also create a lag between the detected onset and the
actual onset. This lag is very desirable for using
our subsequent algorithm of finding the onset. As it can
be used a surrogate QRS R Peak position. Hence, we should
use the maximal onset latency as a window. ICP: 120 ms.
ABP: 220 ms. CBFV: 160 ms.
"""
# the percentage of the maximal value of the slope sum function
# to detect the onset
AmplitudeRatio = .01
# low pass filter
sig = self.zpIIR(asig, 3, .1, 20, 5 * 2/fs)
wSmp = int(np.round(wMS*fs/1000))
BlankWindowRatio = .9
# delta x
diffsig = np.diff(sig)
z = np.empty((sig.size - 1 - wSmp, 1))
z[:] = np.NaN
# calculate slope sum function
for i in range(wSmp,sig.size-1):
subsig = diffsig[i-wSmp:i]
z[i-wSmp] = np.sum(subsig[subsig>0])
z0 = np.mean(z)
onset = [0]
tPnt = []
zThres = 0
blankWin = int(np.round(400*fs/1000))
subIdx = np.r_[onset[0]: onset[0] + 4*blankWin + 1]
MedianArrayWinSize = 5
# this value controls the final acceptance
PrcofMaxAMP = .2
SSFAmpArray = np.ones((MedianArrayWinSize,1))*(np.max(z) - np.min(z)) * PrcofMaxAMP
# the percentage of maximal amplitude for threshold crossing
DetectionThreshold = .2
SSFCrossThresholdArray = np.ones((MedianArrayWinSize,1))*z0*DetectionThreshold
idx = 1
# Keep loop going while onsets detected
while(1):
# look for the first location where z > z0
try:
# Look in z[subIdx] (and make sure it doesn't go past z's size)
# find first index where z > the mean of z
tempIndex = np.trim_zeros(subIdx*(z.size>subIdx), 'b')
ix = np.amin(np.where(z[tempIndex] > z0)[0])
except:
break
ix = tempIndex[ix]
tPnt.append(ix)
srcWin = np.r_[np.maximum(0,ix - wSmp): ix + wSmp]
#if the window has passed the length of the data, then exit
if srcWin[-1] >= len(z):
break
# This section of code is to remove the initial zero-region in the SSF function before looking for onset (if such region exists)
zPnt = np.where(z[srcWin] == 0)
if zPnt[0].size != 0:
zPnt = srcWin[zPnt[0]]
if np.any(zPnt < ix):
srcWin = np.r_[zPnt[np.max(np.where(zPnt < ix))]: ix + wSmp]
# accept the window
if ( np.max(z[srcWin]) - np.min(z[srcWin]) > zThres):
# calculate the threshold for next cycle
SSFAmp = (np.max(z[srcWin]) - np.min(z[srcWin])) * PrcofMaxAMP
SSFAmpArray[np.remainder(idx, MedianArrayWinSize)] = SSFAmp
zThres = np.median(SSFAmpArray)
SSFCrossThresholdArray[np.remainder(idx, MedianArrayWinSize)] = np.mean(z[srcWin])*DetectionThreshold
z0 = np.median(SSFCrossThresholdArray)
minSSF = np.min(z[srcWin]) + SSFAmp *AmplitudeRatio
a = srcWin[0] + np.min(np.where(z[srcWin] >= minSSF))
onset.append(a)
# adaptively determine analysis window for next cycle
bw = blankWin
subIdx = np.round(np.r_[a + bw: a + 3*bw])
idx = idx + 1
else:
# no beat detected
subIdx = np.round(subIdx + blankWin)
return onset
def makeDefaultSig2MatrixParam(self):
algoParam = {}
# maximal HR allowed in BPM
algoParam['maxHR'] = 200
# minimal HR allowed in BPM
algoParam['minHR'] = 40
# the final length of the pulse will be set at the 80 percentile of the lengths
algoParam['prctile4BeatLength'] = 80
# pulse with minimal length should be excluded
algoParam['prctile4MinimalBeatLength'] = 10
return algoParam
def zpIIR(self, sig, p, rp, rs, wn, **kwargs):
#estimate the time constants
# Check to see if blowpass specified, otherwise use default
if 'blowpass' in kwargs:
blowpass = kwargs.get('blowpass')
else:
blowpass = 1
if blowpass == 1:
b,a = scipy.signal.ellip(p, rp, rs, wn)
else:
# This was "size", but probably should be len
if len(wn) > 1:
b,a = scipy.signal.ellip(p, rp, rs, wn, 'stop')
else:
b,a = scipy.signal.ellip(p, rp, rs, wn, 'high')
osig = scipy.signal.filtfilt(b, a, sig)
return osig
def NormalizeSig(self, sigMat, number):
# Subtract mean
meanValue = np.mean(sigMat)
sigMatFinal = sigMat - meanValue
# Divide by standard deviation
stdValue = np.std(sigMat)
sigMatFinal = sigMatFinal/stdValue
return sigMatFinal
def PolyReSample(self, y, T, newT, p):
"""
A resampling program for a nonunifromly sample series y
"""
pModel = np.polyfit(T, y, p)
newy = np.polyval(pModel, newT)
newy = newy.T
#return newy, newT
return newy
if __name__ == '__main__':
pass
## Import dataset from Xiao
# importing directly from csv files
# plethData = np.genfromtxt('10.25.2016_x00-11_ple_converted.csv', delimiter=',')
# QosData = np.genfromtxt('10.25.2016_x00-11_qos_converted.csv', delimiter=',')
# plethData = plethData[:,1]
# QosData = QosData[:,1]
#
# # trying to import from mat file - didn't work properly
# # import scipy.io as sio
# # signal = sio.loadmat('newestPlethData.mat')
# # plethData = signal['desiredPlethData']
# # QosData = signal['desiredQoS']
#
# # Initialize data relevant parameters
# fs = 125
# t0 = fs*10
# delt = 250 # ms
# winL = 7 # seconds
#
# ## Run our code and compare results
# SigCheck = QualityOfSignal()
#
# qualityFlag = []
# tofT = []
# # Loop through all the data
# t00 = t0
# for i in range(0, 500):
# qualityFlag.append(SigCheck.isPPGGoodQuality(plethData[t0:t0 + winL * fs],[], fs))
# t0 = np.floor(t0+fs*delt/1000)
# tofT.append((i)*delt/1000)
#
# t11 = t0;
# tofS = np.r_[0:(t11-t00)/fs:1/fs]
#
# maxData = int(np.ndarray.max(plethData[t00:t11]))
# plt.plot(tofS,plethData[t00:t11]/maxData, 'b')
# plt.plot(tofT,qualityFlag, 'ro', markersize=2)
# axes = plt.gca()
# axes.set_ylim([-1.2, 1.2])
# plt.show()