-
Notifications
You must be signed in to change notification settings - Fork 0
/
read_data_hrv_anal.py
317 lines (228 loc) · 8.07 KB
/
read_data_hrv_anal.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
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
Created on Wed Oct 25 11:57:24 2017
@author: Rebeca
"""
import numpy as np
import matplotlib.pyplot as plt
#from qrs_detector import *
from HRV import HRV
from HRV_Entropy import HRV_entropy
from pat_class import pat
from tcx import *
from easygui import *
import os
import glob
from qrs_detector import *
def get_folder(tit = "Choose A Subject folder with subfolders 'activo' and 'reposo' "):
"""
Functions tha opens a dialog box to select a folder
"""
dir_path = diropenbox(title=tit, default = './')
return dir_path
def folder_processing():
"""
Function that process folder for a given subject in order to read the
hr files
"""
dir_path = get_folder()
os.chdir(dir_path)
#run over folders
for folder in glob.iglob('./*'):
os.chdir(folder)
kind_test = os.path.basename(folder) #indicate which kind
#get tcx
tcx_file = os.path.basename(glob.glob('./*.tcx')[0])
#HRV analysis tcx
pat_tcx_hrv_dict = HRV_analysis_tcx(tcx_file)
np.save(kind_test+'_'+pat_tcx_hrv_dict['id']+'_tcx',pat_tcx_hrv_dict)
#get txt
txt_file = os.path.basename(glob.glob('./*.txt')[0])
#HRV analysis bitalino
pat_bitalino_hrv_dict,ecginf = HRV_analysis_bitalino(txt_file)
np.save(kind_test+'_'+pat_bitalino_hrv_dict['id']+'_bitalino',pat_bitalino_hrv_dict)
os.chdir("..")
os.chdir("..")
return tcx_file,ecginf
def get_pat_data():
msg = "Enter subject information"
title = "Run app"
fieldNames = ["Pat_Id", "Gender", "Age"]
fieldValues = [] # we start with blanks for the values
fieldValues = multenterbox(msg, title, fieldNames)
return fieldValues
def remove_zeros(hr):
"""
Function that removes zeros from hr signal from POLAR
"""
#first check zero positions
ind_zeros = (hr == 0)
x_p = np.arange(0,len(hr))
x_p = x_p[~ind_zeros] #get valuse diff from zero
hr_p = hr[~ind_zeros]
hr_c = np.interp(np.arange(0,len(hr)),x_p,hr_p) #may be change linear interpolation?
return hr_c
def HRV_analysis_tcx(fname):
"""
Function that performs HRV analysis on tcx (polar signal)
"""
#get data pat
fields = get_pat_data()
idf = fields[0]
gender = fields[1]
age = fields[2]
my_pat = pat(idf,age,gender)
#read example.tcx
t,hr = my_pat.read_txc(fname)
hr = np.array(hr)
is_bpm = True #boolean falg to convert bpm heart rate signal (from Polar)
#remove 0 from hr
if np.sum(hr == 0) > 0:
hr = remove_zeros(hr)
#HRV Analysis
if is_bpm:
rr = 60. * 1000/(hr) #rr intervals
plt.close('all')
plt.figure(1)
plt.title("RR Polar")
plt.plot(rr)
labels=['N']*len(rr)
hrv_anal = HRV()
prct = 0.2
ind_not_N_beats=hrv_anal.artifact_ectopic_detection(rr, labels, prct, numBeatsAfterV = 4)
valid = hrv_anal.is_valid(ind_not_N_beats,perct_valid = 0.2)
#if every beat is Normal (sum(ind_not_N_beats) == 0), then no correction
if ind_not_N_beats.sum() > 0:
rr_corrected = hrv_anal.artifact_ectopic_correction(rr, ind_not_N_beats, method='linear')
else:
rr_corrected = rr.copy()
plt.figure(2)
plt.title("RR Corregidos Polar")
plt.plot(rr_corrected)
plt.figure(3)
plt.title("HR Polar")
plt.plot(hr)
hrv_pat = hrv_anal.load_HRV_variables(rr_corrected)
r = np.std(rr_corrected)*0.2
hrv_en = HRV_entropy()
hrv_pat['sampen'] = hrv_en.SampEn(rr_corrected,m = 2,r=r,kernel = 'Heaviside')
hrv_pat['id'] = idf
hrv_pat['gender'] = gender
hrv_pat['age'] = age
hrv_pat['rr'] = rr_corrected
hrv_pat['hr'] = hr
return hrv_pat
#pat()
def HRV_analysis_bitalino(fname):
"""
Function that performs HRV analysis on txt (bitalino signal)
"""
#get data pat
f = np.loadtxt(fname)
fields = get_pat_data()
idf = fields[0]
gender = fields[1]
age = fields[2]
my_pat = pat(idf,age,gender)
#read bitalino file
ecg = f[:,6] #verify the channel with ECG
#plt.plot(ecg)
# get rr from ecg
fs=1000.;
ecg_d,t = detrendSpline(ecg,fs,l_w = 1.2)
ecg_filtered = bandpass_qrs_filter(ecg_d, fs, fc1 = 12,fc2 = 20)
beat, th, qrs_index= exp_beat_detection(ecg_filtered,fs,Tr = .220,a = .75,b = 0.9998)
r_peak, rr = r_peak_detection(ecg_filtered,ecg,fs,beat,th,qrs_index,Tr = .220)
plt.close('all')
plt.figure(1)
plt.title("RR BiTalino")
plt.plot(rr)
t = np.arange(0,len(ecg))/fs
plt.figure(2)
plt.title("ECG Filtered")
plt.plot(t,ecg_filtered)
plt.plot(t,th)
plt.plot(t[r_peak],ecg_filtered[r_peak],'r*')
plt.figure(3)
plt.title("ECG")
plt.plot(t,ecg)
plt.plot(t[r_peak],ecg[r_peak],'r*')
labels=['N']*len(rr)
hrv_anal = HRV()
prct = 0.2
ind_not_N_beats=hrv_anal.artifact_ectopic_detection(rr, labels, prct, numBeatsAfterV = 4)
valid = hrv_anal.is_valid(ind_not_N_beats,perct_valid = 0.2)
#if every beat is Normal (sum(ind_not_N_beats) == 0), then no correction
if ind_not_N_beats.sum() > 0:
rr_corrected = hrv_anal.artifact_ectopic_correction(rr, ind_not_N_beats, method='linear')
rr_corrected = rr.copy()
else:
rr_corrected = rr.copy()
plt.figure(4)
plt.title("RR Corregidos BiTalino")
plt.plot(rr_corrected)
hrv_pat = hrv_anal.load_HRV_variables(rr_corrected)
r = np.std(rr_corrected)*0.2
hrv_en = HRV_entropy()
hrv_pat['sampen'] = hrv_en.SampEn(rr_corrected,m = 2,r=r,kernel = 'Heaviside')
hrv_pat['id'] = idf
hrv_pat['gender'] = gender
hrv_pat['age'] = age
hrv_pat['rr'] = rr_corrected
ecginf = {'ecg':ecg,'r':r_peak}
return hrv_pat,ecginf
#pat()
foo,ecginfo = folder_processing() #Hay que elegir la carpeta de paciente, que contiene las subcarpetas
#andando y sentado
"""
f = np.loadtxt("sigs/opensignals_201607181603_2018-01-09_13-14-55.txt")
#f = np.loadtxt("opensignals_Oscar_2017-10-17_12-27-32.txt")
ecg=f[:,6];
#eda=f[:,6];
#marcas=f[:,1];
fs=1000;
ecg_d,t = detrendSpline(ecg,fs,l_w = 1.2)
ecg_filtered = bandpass_qrs_filter(ecg_d, fs, fc1 = 5,fc2 = 15)
beat, th, qrs_index= exp_beat_detection(ecg_filtered,fs,Tr = .180,a = .7,b = 0.999)
r_peak, rr = r_peak_detection(ecg_filtered,ecg,fs,beat,th,qrs_index,Tr = .100)
t=np.arange(0,len(ecg_d))*fs
plt.figure(), plt.plot(t,ecg_d)
plt.figure(), plt.plot(rr)
#t_eda=np.arange(0,len(eda))*fs
#plt.figure(), plt.plot(t_eda,eda)
"""
"""
Analysis with polar
"""
#create a pat
"""
idf = '01'
age = 38
gender = 'M'
my_pat = pat(idf,age,gender)
#read example.tcx
t,hr = my_pat.read_txc('example.tcx')
hr = np.array(hr)
is_bpm = True #boolean falg to convert bpm heart rate signal (from Polar)
#HRV Analysis
if is_bpm:
rr = 60. * 1000/(hr) #rr intervals
labels=['N']*len(rr)
hrv_anal = HRV()
prct = 0.2
ind_not_N_beats=hrv_anal.artifact_ectopic_detection(rr, labels, prct, numBeatsAfterV = 4)
valid = hrv_anal.is_valid(ind_not_N_beats,perct_valid = 0.2)
#if every beat is Normal (sum(ind_not_N_beats) == 0), then no correction
if ind_not_N_beats.sum() > 0:
rr_corrected = hrv_anal.artifact_ectopic_correction(rr, ind_not_N_beats, method='linear')
else:
rr_corrected = rr.copy()
plt.figure()
plt.plot(rr_corrected)
hrv_pat = hrv_anal.load_HRV_variables(rr_corrected)
r = np.std(rr_corrected)*0.2
hrv_en = HRV_entropy()
hrv_pat['sampen'] = hrv_en.SampEn(rr_corrected,m = 2,r=r,kernel = 'Heaviside')
#hrv_pat['ti']=hrv_en.TimeIrreversibility(rr_corrected,tau=1)
"""