-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
175 lines (162 loc) · 7.03 KB
/
Copy pathutils.py
File metadata and controls
175 lines (162 loc) · 7.03 KB
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
#!/usr/bin/env python
""" Data reader and feature extracter modules for E4 offline processing
__Author__='Guangtao Nie'
__Institution__='RASL Lab, Vanderbilt Univ'
__version__='0.1'
"""
import pandas as pd
import numpy as np
from scipy.signal import find_peaks
import scipy
import heartpy as hp
import datetime
from datetime import timezone
from biosppy.signals.eda import kbk_scr
import os
from datetime import datetime
class Reader():
def __init__(self, freqs, step_ratio, window):
self.acc_freq = freqs['acc']
self.bvp_freq = freqs['bvp']
self.eda_freq = freqs['eda']
self.temp_freq = freqs['temp']
self.step_ratio = step_ratio
self.window = window
def date_converter(self, year, month, day, hour, minute, second, msec, season):
if season.startswith('s'):
lag = 5
elif season.startswith('w'):
lag = 6
else:
raise ValueError(
'season should be input, either winter/w or summer/s for timezone calculation.')
try:
dt = datetime(year, month, day, hour, minute, second, msec)
# converted to GMT-5, Nashville local time zone
timestamp = dt.replace(tzinfo=timezone.utc).timestamp()+lag*60*60
return timestamp
except:
raise ValueError('input datetime not valid!')
def get_data(self, direc, start, end):
acc = pd.read_csv(os.path.join(direc, 'ACC.csv'),
header=None).to_numpy()
baseline = acc[0][0]
print('E4 recording start at:', baseline)
if start < baseline:
raise ValueError('session started before E4 recording!')
if (end-baseline)*self.acc_freq > acc.shape[0]-2:
raise ValueError('E4 recording finished before session end!')
start, end = int(start-baseline), int(end-baseline)
# add 2 because frst 2 rows in a file are timestamp and freq
acc = acc[int(start*self.acc_freq)+2:int(end*self.acc_freq)+2]
bvp = pd.read_csv(os.path.join(direc, 'BVP.csv'),
header=None).to_numpy()
bvp = bvp[int(start*self.bvp_freq)+2:int(end*self.bvp_freq)+2]
eda = pd.read_csv(os.path.join(direc, 'EDA.csv'),
header=None).to_numpy()
eda = eda[int(start*self.eda_freq)+2:int(end*self.eda_freq)+2]
temp = pd.read_csv(os.path.join(direc, 'TEMP.csv'),
header=None).to_numpy()
temp = temp[int(start*self.temp_freq)+2:int(end*self.temp_freq)+2]
ACC_data, BVP_data, EDA_data, TEMP_data = [], [], [], []
for i in np.arange(0, end-start-self.window, int(self.step_ratio*self.window)):
ACC_data.append(
acc[i*self.acc_freq:(i+self.window)*self.acc_freq])
BVP_data.append(
bvp[i*self.bvp_freq:(i+self.window)*self.bvp_freq])
EDA_data.append(
eda[i*self.eda_freq:(i+self.window)*self.eda_freq])
TEMP_data.append(
temp[i*self.temp_freq:(i+self.window)*self.temp_freq])
return np.asarray(ACC_data), np.asarray(BVP_data), np.asarray(EDA_data), np.asarray(TEMP_data)
class Extract_Features():
def __init__(self, freqs):
self.acc_freq = freqs['acc']
self.bvp_freq = freqs['bvp']
self.eda_freq = freqs['eda']
self.temp_freq = freqs['temp']
def acc_features(self, acc):
# Mu, SD, Integral, Peaks = [], [], [], []
# features = []
# for acc in ACC: # ACC shape (3,), acc shape (,320,3)
M = np.zeros((acc.shape[0], 15))
# print('acc shape:{}'.format(acc.shape))
M[:, 0:3] = acc.mean(axis=1)
# print('mu shape:{}'.format(mu.shape))
M[:, 3:6] = acc.std(axis=1)
# print('std shape:{}'.format(sd.shape))
M[:, 6:9] = M[:, 0:3]*acc.shape[1]
num_peaks = np.zeros((acc.shape[0], 3))
for i in np.arange(acc.shape[0]):
for j in np.arange(3):
p, _ = find_peaks(
acc[i, :, j], distance=0.25*self.acc_freq)
num_peaks[i, j] = p.size
M[:, 9:12] = num_peaks
M[:, 12] = M[:, 0:3].sum(axis=1) # summed mu
M[:, 13] = M[:, 3:6].sum(axis=1) # summed sd
M[:, 14] = M[:, 6:9].sum(axis=1) # summed integral
return M
def bvp_features(self, bvp):
M = np.zeros((bvp.shape[0], 16))
for i in np.arange(bvp.shape[0]):
try:
# calc_freq=True,freq_method='fft',interp_clipping=True)
_, m = hp.process(bvp[i], self.bvp_freq)
M[i] = np.asarray(list(m.values()))
except:
M[i] = np.zeros(16)
return M
def eda_features(self, eda_):
M = np.zeros((eda_.shape[0], 12))
eda_ = np.squeeze(eda_, axis=2)
for i in np.arange(eda_.shape[0]):
# _,filtered,onsets,peaks,amplitudes=eda(eda_[i],eda_freq)
# onsets,peaks,amplitudes=basic_scr(eda_[i],eda_freq)
mean_eda = eda_[i].mean()
sd_eda = eda_[i].std()
minx = eda_[i].min()
maxx = eda_[i].max()
slope, _, _, _, _ = scipy.stats.linregress(
np.arange(eda_[i].size), eda_[i])
rang = maxx-minx
while (eda_[i] < 0).any():
eda_[i][eda_[i] < 0] = 0
try:
onsets, peaks, amplitudes = kbk_scr(eda_[i], self.eda_freq)
num_scr = peaks.size
for peak, amp in zip(peaks, amplitudes):
scl = eda_[peak]-amp
mean_scl = scl.mean()
sd_scl = scl.std()
mean_scr = amplitudes.mean()
sd_scr = amplitudes.std()
corr_scl = scipy.stats.pearsonr(
np.arange(eda_[i].size), eda_[i])[0]
except:
print('{}th segment in eda of length:{} failed to extract eda features and all set to be 0'.format(
i, eda_[i].size))
num_scr, mean_scl, sd_scl, mean_scr, sd_scr, corr_scl = 0, 0, 0, 0, 0, 0
M[i] = np.asarray([mean_eda, sd_eda, minx, maxx, slope, rang,
num_scr, mean_scl, sd_scl, mean_scr, sd_scr, corr_scl])
return M
def temp_features(self, temp):
M = np.zeros((temp.shape[0], 6))
temp = np.squeeze(temp, axis=2)
for i in np.arange(temp.shape[0]):
mean_t = temp[i].mean()
sd_t = temp[i].std()
min_t = temp[i].min()
max_t = temp[i].max()
rang = max_t-min_t
slope, _, _, _, _ = scipy.stats.linregress(
np.arange(temp[i].size), temp[i])
M[i] = np.asarray([mean_t, sd_t, min_t, max_t, rang, slope])
return M
def eda_bvp_nan(self, eda_features):
for i in np.arange(eda_features.shape[0]):
k = np.argwhere(np.isnan(eda_features[i]))
eda_features[i][k] = 0
for eda in eda_features:
if np.isnan(eda).any():
raise ValueError('nan still exists!')