forked from Icewater1337/pythonDataInterpreter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PhysiologicalAnalysis.py
360 lines (288 loc) · 12.6 KB
/
PhysiologicalAnalysis.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
from os import listdir
from os.path import isfile, join
import numpy as np
import pandas as pd
from scipy.stats import ttest_ind, stats
import HRVCalcs as hrvCalc
from downloadedUtils import empaticaHRV
# This class contains the methods to do the physiological analysis.
# Its constructor takes two arguments:
# 1. The folder where the data lies. For now this folder has to contain folders that follow this naming convention:
# baseFolder/empatica_ep_X/splitParts Whereas splitParts subfolder contains the data for only first experiment
# And the data for the second experiment.
# The naming conventions for the single files are the following: Part + Type + light setting
# Example: 1BVPnoLight or 2IBIblue Where the part is whether it was MAT one or MAT two
# 2. epNbrs which contains the episodes to look at. Episodes not in there will be ignored.
class PhysiologicalAnalysis:
def __init__(self, baseFolder, epNbrs):
# After this you can chose what plots to create in the main method.
self.baseFolder = baseFolder
self.epNbrs = epNbrs
def calculateHRVRMSSDforFirstAndSecondIbi(self, folder):
global hrv1, hrv2
onlyfiles = [f for f in listdir(folder) if isfile(join(folder, f))]
colnames = ['IBI', 'Time']
for i in onlyfiles:
if "IBIblue" in i:
ibiBlue = pd.read_csv(folder + i, names=colnames, header=0)
hrv2 = hrvCalc.calculateRMSSDFromIbi(ibiBlue)
if "IBInoLight" in i:
ibiNoLight = pd.read_csv(folder + i, names=colnames, header=0)
hrv1 = hrvCalc.calculateRMSSDFromIbi(ibiNoLight)
return hrv1, hrv2
def calculateHRVRMSSDforBaseIbi(self, folder):
global hrv2
onlyfiles = [f for f in listdir(folder) if isfile(join(folder, f))]
colnames = ['IBI', 'Time']
for i in onlyfiles:
if "IBIBase" in i:
ibiBlue = pd.read_csv(folder + i, names=colnames, header=0)
hrv2 = hrvCalc.calculateRMSSDFromIbi(ibiBlue)
return hrv2
def calculateHRVSDRRforFirstAndSecondIbi(self, folder):
global hrv1, hrv2
onlyfiles = [f for f in listdir(folder) if isfile(join(folder, f))]
colnames = ['IBI', 'Time']
for i in onlyfiles:
if "IBIblue" in i:
ibiBlue = pd.read_csv(folder + i, names=colnames, header=0)
hrv2 = hrvCalc.calculateSDRRFromIbi(ibiBlue)
if "IBInoLight" in i:
ibiNoLight = pd.read_csv(folder + i, names=colnames, header=0)
hrv1 = hrvCalc.calculateSDRRFromIbi(ibiNoLight)
return hrv1, hrv2
def getIBIFromHRAndBVP(self, HR_DF, BVP_DF):
column = list(HR_DF)[0]
temp = HR_DF.drop(0, axis=0)
HR = temp[column]
HR = HR.tolist()
column2 = list(BVP_DF)[0]
sample_rate = BVP_DF[column2][0]
temp = BVP_DF.drop(0, axis=0)
temp['spData'] = 0
temp.loc[temp[column2] > 0, 'spData'] = temp[column2]
signal = temp['spData'].tolist()
return empaticaHRV.getRRI(signal, column2, sample_rate)
def calculateHRVWithFirstTest(self, folder):
global hrv1
onlyfiles = [f for f in listdir(folder) if isfile(join(folder, f))]
colnames = ['IBI', 'Time']
for i in onlyfiles:
if "1IBI" in i:
ibi = pd.read_csv(folder + i, names=colnames, header=0)
hrv1 = hrvCalc.calculateRMSSDFromIbi(ibi)
return hrv1
def getEDA(self, folder):
global eda1, eda2
onlyfiles = [f for f in listdir(folder) if isfile(join(folder, f))]
colnames = ['Time', 'EDA']
for i in onlyfiles:
if "EDAblue" in i:
eda2 = pd.read_csv(folder + i, names=colnames, header=0)
if "EDAnoLight" in i:
eda1 = pd.read_csv(folder + i, names=colnames, header=0)
return eda1, eda2
def getHR(self, folder):
global hr1, hr2
onlyfiles = [f for f in listdir(folder) if isfile(join(folder, f))]
colnames = ['Time', 'HR']
for i in onlyfiles:
if "HRblue" in i:
hr2 = pd.read_csv(folder + i, names=colnames, header=0)
if "HRnoLight" in i:
hr1 = pd.read_csv(folder + i, names=colnames, header=0)
return hr1, hr2
def getBaseHR(self, folder):
global hr2
onlyfiles = [f for f in listdir(folder) if isfile(join(folder, f))]
colnames = ['Time', 'HR']
for i in onlyfiles:
if "HRbasePart" in i:
hr2 = pd.read_csv(folder + i, names=colnames, header=0)
return hr2
# This method takes all HR data and creates a T-test with it
def getHRAvgAndTTest(self):
hr_no_light = []
hr_with_light = []
even = 0
uneven = 0
for epNbr in self.epNbrs:
baseFolder = self.baseFolder + "empatica_ep_" + str(epNbr).zfill(
2) + "/splitParts/"
hr1, hr2 = self.getHR(baseFolder)
hr1Avg = np.average(hr1['HR'])
hr2Avg = np.average(hr2['HR'])
if (hr1Avg > 0 and hr2Avg > 0):
if epNbr % 2 == 0:
even = even + 1
if epNbr % 2 != 0:
uneven = uneven + 1
# print("Add EP: " + str(epNbr))
# print("Avg No Light: " + str(hr1Avg))
# print("Avg With Light: " + str(hr2Avg))
hr_no_light.append(hr1Avg)
hr_with_light.append(hr2Avg)
print("Uneven:" + str(uneven))
print("even:" + str(even))
print("Average With light:" + str(np.average(hr_with_light)))
print("Average Without light:" + str(np.average(hr_no_light)))
return ttest_ind(hr_no_light, hr_with_light)
# This method takes the EDA and calculates a t-test on it
def getEDAAvgsAndTTest(self):
eda_no_light = []
eda_with_light = []
even = 0
uneven = 0
for epNbr in self.epNbrs:
baseFolder = self.baseFolder + "empatica_ep_" + str(epNbr).zfill(
2) + "/splitParts/"
eda1, eda2 = self.getEDA(baseFolder)
eda1Avg = np.average(eda1['EDA'])
eda2Avg = np.average(eda2['EDA'])
if (eda1Avg > 0 and eda2Avg > 0):
if epNbr % 2 == 0:
even = even + 1
if epNbr % 2 != 0:
uneven = uneven + 1
# print("Add EP: " + str(epNbr))
normalizedEda1 = (eda1['EDA'] - np.min(eda1['EDA'])) / (np.max(eda1['EDA']) - np.min(eda1['EDA']))
normalizedEda2 = (eda2['EDA'] - np.min(eda2['EDA'])) / (np.max(eda2['EDA']) - np.min(eda2['EDA']))
# print("Avg No Light: " + str(np.average(normalizedEda1)))
# print("Avg With Light: " + str(np.average(normalizedEda2)))
eda_no_light.append(np.average((normalizedEda1)))
eda_with_light.append(np.average((normalizedEda2)))
print("Uneven:" + str(uneven))
print("even:" + str(even))
print("Average With light:" + str(np.average(eda_with_light)))
print("Average Without light:" + str(np.average(eda_no_light)))
return ttest_ind(eda_with_light, eda_no_light)
def getHRVRMSSDAvgsAndTTest(self):
hrv_no_light = []
hrv_with_light = []
# Calculate one ibi
even = 0
uneven = 0
for epNbr in self.epNbrs:
baseFolder = self.baseFolder + "empatica_ep_" + str(epNbr).zfill(
2) + "/splitParts/"
# hrv1, hrv2 = calculateHRVforFirstAndSecondIbi(baseFolder)
hrv1, hrv2 = self.calculateHRVRMSSDforFirstAndSecondIbi(baseFolder)
# Read EDA
if hrv1 > 0 and hrv2 > 0:
# print("add: " + str(epNbr))
# print(hrv1)
# print(hrv2)
if epNbr % 2 == 0:
even = even + 1
if epNbr % 2 != 0:
uneven = uneven + 1
hrv_no_light.append(hrv1)
hrv_with_light.append(hrv2)
# epNbr = "04"
# print(hrv_no_light)
# print(hrv_with_light)
print("Uneven:" + str(len(hrv_no_light)))
print("even:" + str(len(hrv_with_light)))
print("Average With light:" + str(np.average(hrv_with_light)))
print("Average Without light:" + str(np.average(hrv_no_light)))
return ttest_ind(hrv_no_light, hrv_with_light)
def getHRVSDRRAvgsAndTTest(self):
hrv_no_light = []
hrv_with_light = []
# Calculate one ibi
even = 0
uneven = 0
for epNbr in self.epNbrs:
baseFolder = self.baseFolder + "empatica_ep_" + str(epNbr).zfill(
2) + "/splitParts/"
# hrv1, hrv2 = calculateHRVforFirstAndSecondIbi(baseFolder)
hrv1, hrv2 = self.calculateHRVSDRRforFirstAndSecondIbi(baseFolder)
# Read EDA
if hrv1 > 0 and hrv2 > 0:
# print("add: " + str(epNbr))
# print(hrv1)
# print(hrv2)
if epNbr % 2 == 0:
even = even + 1
if epNbr % 2 != 0:
uneven = uneven + 1
hrv_no_light.append(hrv1)
hrv_with_light.append(hrv2)
# epNbr = "04"
# print(hrv_no_light)
# print(hrv_with_light)
print("Uneven:" + str(len(hrv_no_light)))
print("even:" + str(len(hrv_with_light)))
print("Average With light:" + str(np.average(hrv_with_light)))
print("Average Without light:" + str(np.average(hrv_no_light)))
return ttest_ind(hrv_no_light, hrv_with_light)
# This method only takes the first part of the experiment. The first MAT task.
def useOnlyPartOneFromTestGetHRV(self):
hrv_no_light = []
hrv_with_light = []
# Calculate one ibi
even = 0
uneven = 0
for epNbr in self.epNbrs:
baseFolder = self.baseFolder + "empatica_ep_" + str(epNbr).zfill(
2) + "/splitParts/"
hrv = self.calculateHRVWithFirstTest(baseFolder)
if hrv > 0:
# print("add: " + str(epNbr))
# print(hrv)
if epNbr % 2 == 0:
even = even + 1
hrv_with_light.append(hrv)
if epNbr % 2 != 0:
uneven = uneven + 1
hrv_no_light.append(hrv)
print("Uneven:" + str(uneven))
print("even:" + str(even))
print("Average With light:" + str(np.average(hrv_with_light)))
print("Average Without light:" + str(np.average(hrv_no_light)))
return ttest_ind(hrv_no_light, hrv_with_light)
def executeAnovaOnHrv(self):
hrv_no_light = []
hrv_with_light = []
# Calculate one ibi
even = 0
uneven = 0
for epNbr in self.epNbrs:
baseFolder = self.baseFolder + "empatica_ep_" + str(epNbr).zfill(
2) + "/splitParts/"
# hrv1, hrv2 = calculateHRVforFirstAndSecondIbi(baseFolder)
hrv1, hrv2 = self.calculateHRVRMSSDforFirstAndSecondIbi(baseFolder)
# Read EDA
if hrv1 > 0 and hrv2 > 0:
# print("add: " + str(epNbr))
# print(hrv1)
# print(hrv2)
if epNbr % 2 == 0:
even = even + 1
if epNbr % 2 != 0:
uneven = uneven + 1
hrv_no_light.append(hrv1)
hrv_with_light.append(hrv2)
# epNbr = "04"
# print(hrv_no_light)
# print(hrv_with_light)
print("Uneven:" + str(len(hrv_no_light)))
print("even:" + str(len(hrv_with_light)))
print("Variance With light:" + str(np.var(hrv_with_light)))
print("Variance Without light:" + str(np.var(hrv_no_light)))
return stats.f_oneway(hrv_no_light, hrv_with_light)
def getHRandHRVBaseAvg(self):
hr_base = []
hrv_base = []
for epNbr in self.epNbrs:
baseFolder = self.baseFolder + "empatica_ep_" + str(epNbr).zfill(
2) + "/splitParts/"
hr2 = self.getBaseHR(baseFolder)
hr2Avg = np.average(hr2['HR'])
hrv = self.calculateHRVRMSSDforBaseIbi(baseFolder)
if (hr2Avg > 0):
hr_base.append(hr2Avg)
hrv_base.append(hrv)
print ( "EPisode: "+ str(epNbr) + " Has HR: "+ str(hr2Avg) + " and HRV: " + str(hrv))
print("Average base HR" + str(np.average(hr_base)))
print("Average base HR" + str(np.average(hrv_base)))