/
data_wrangling.py
299 lines (233 loc) · 10.3 KB
/
data_wrangling.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
import os
import pickle
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from scipy import stats
import scipy.signal as scisig
import scipy.stats
import cvxEDA
# E4 (wrist) Sampling Frequencies
fs_dict = {'ACC': 32, 'BVP': 64, 'EDA': 4, 'TEMP': 4, 'label': 700, 'Resp': 700}
WINDOW_IN_SECONDS = 30
label_dict = {'baseline': 1, 'stress': 2, 'amusement': 0}
int_to_label = {1: 'baseline', 2: 'stress', 0: 'amusement'}
feat_names = None
savePath = 'data'
subject_feature_path = '/subject_feats'
if not os.path.exists(savePath):
os.makedirs(savePath)
if not os.path.exists(savePath + subject_feature_path):
os.makedirs(savePath + subject_feature_path)
# cvxEDA
def eda_stats(y):
Fs = fs_dict['EDA']
yn = (y - y.mean()) / y.std()
[r, p, t, l, d, e, obj] = cvxEDA.cvxEDA(yn, 1. / Fs)
return [r, p, t, l, d, e, obj]
class SubjectData:
def __init__(self, main_path, subject_number):
self.name = f'S{subject_number}'
self.subject_keys = ['signal', 'label', 'subject']
self.signal_keys = ['chest', 'wrist']
self.chest_keys = ['ACC', 'ECG', 'EMG', 'EDA', 'Temp', 'Resp']
self.wrist_keys = ['ACC', 'BVP', 'EDA', 'TEMP']
with open(os.path.join(main_path, self.name) + '/' + self.name + '.pkl', 'rb') as file:
self.data = pickle.load(file, encoding='latin1')
self.labels = self.data['label']
def get_wrist_data(self):
data = self.data['signal']['wrist']
data.update({'Resp': self.data['signal']['chest']['Resp']})
return data
def get_chest_data(self):
return self.data['signal']['chest']
def extract_features(self): # only wrist
results = \
{
key: get_statistics(self.get_wrist_data()[key].flatten(), self.labels, key)
for key in self.wrist_keys
}
return results
# https://github.com/MITMediaLabAffectiveComputing/eda-explorer/blob/master/load_files.py
def butter_lowpass(cutoff, fs, order=5):
# Filtering Helper functions
nyq = 0.5 * fs
normal_cutoff = cutoff / nyq
b, a = scisig.butter(order, normal_cutoff, btype='low', analog=False)
return b, a
def butter_lowpass_filter(data, cutoff, fs, order=5):
# Filtering Helper functions
b, a = butter_lowpass(cutoff, fs, order=order)
y = scisig.lfilter(b, a, data)
return y
def get_slope(series):
linreg = scipy.stats.linregress(np.arange(len(series)), series )
slope = linreg[0]
return slope
def get_window_stats(data, label=-1):
mean_features = np.mean(data)
std_features = np.std(data)
min_features = np.amin(data)
max_features = np.amax(data)
features = {'mean': mean_features, 'std': std_features, 'min': min_features, 'max': max_features,
'label': label}
return features
def get_net_accel(data):
return (data['ACC_x'] ** 2 + data['ACC_y'] ** 2 + data['ACC_z'] ** 2).apply(lambda x: np.sqrt(x))
def get_peak_freq(x):
f, Pxx = scisig.periodogram(x, fs=8)
psd_dict = {amp: freq for amp, freq in zip(Pxx, f)}
peak_freq = psd_dict[max(psd_dict.keys())]
return peak_freq
# https://github.com/MITMediaLabAffectiveComputing/eda-explorer/blob/master/AccelerometerFeatureExtractionScript.py
def filterSignalFIR(eda, cutoff=0.4, numtaps=64):
f = cutoff / (fs_dict['ACC'] / 2.0)
FIR_coeff = scisig.firwin(numtaps, f)
return scisig.lfilter(FIR_coeff, 1, eda)
def compute_features(e4_data_dict, labels, norm_type=None):
# Dataframes for each sensor type
eda_df = pd.DataFrame(e4_data_dict['EDA'], columns=['EDA'])
bvp_df = pd.DataFrame(e4_data_dict['BVP'], columns=['BVP'])
acc_df = pd.DataFrame(e4_data_dict['ACC'], columns=['ACC_x', 'ACC_y', 'ACC_z'])
temp_df = pd.DataFrame(e4_data_dict['TEMP'], columns=['TEMP'])
label_df = pd.DataFrame(labels, columns=['label'])
resp_df = pd.DataFrame(e4_data_dict['Resp'], columns=['Resp'])
# Filter EDA
eda_df['EDA'] = butter_lowpass_filter(eda_df['EDA'], 1.0, fs_dict['EDA'], 6)
# Filter ACM
for _ in acc_df.columns:
acc_df[_] = filterSignalFIR(acc_df.values)
# Adding indices for combination due to differing sampling frequencies
eda_df.index = [(1 / fs_dict['EDA']) * i for i in range(len(eda_df))]
bvp_df.index = [(1 / fs_dict['BVP']) * i for i in range(len(bvp_df))]
acc_df.index = [(1 / fs_dict['ACC']) * i for i in range(len(acc_df))]
temp_df.index = [(1 / fs_dict['TEMP']) * i for i in range(len(temp_df))]
label_df.index = [(1 / fs_dict['label']) * i for i in range(len(label_df))]
resp_df.index = [(1 / fs_dict['Resp']) * i for i in range(len(resp_df))]
# Change indices to datetime
eda_df.index = pd.to_datetime(eda_df.index, unit='s')
bvp_df.index = pd.to_datetime(bvp_df.index, unit='s')
temp_df.index = pd.to_datetime(temp_df.index, unit='s')
acc_df.index = pd.to_datetime(acc_df.index, unit='s')
label_df.index = pd.to_datetime(label_df.index, unit='s')
resp_df.index = pd.to_datetime(resp_df.index, unit='s')
# New EDA features
r, p, t, l, d, e, obj = eda_stats(eda_df['EDA'])
eda_df['EDA_phasic'] = r
eda_df['EDA_smna'] = p
eda_df['EDA_tonic'] = t
# Combined dataframe - not used yet
df = eda_df.join(bvp_df, how='outer')
df = df.join(temp_df, how='outer')
df = df.join(acc_df, how='outer')
df = df.join(resp_df, how='outer')
df = df.join(label_df, how='outer')
df['label'] = df['label'].fillna(method='bfill')
df.reset_index(drop=True, inplace=True)
if norm_type is 'std':
# std norm
df = (df - df.mean()) / df.std()
elif norm_type is 'minmax':
# minmax norm
df = (df - df.min()) / (df.max() - df.min())
# Groupby
grouped = df.groupby('label')
baseline = grouped.get_group(1)
stress = grouped.get_group(2)
amusement = grouped.get_group(3)
return grouped, baseline, stress, amusement
def get_samples(data, n_windows, label):
global feat_names
global WINDOW_IN_SECONDS
samples = []
# Using label freq (700 Hz) as our reference frequency due to it being the largest
# and thus encompassing the lesser ones in its resolution.
window_len = fs_dict['label'] * WINDOW_IN_SECONDS
for i in range(n_windows):
# Get window of data
w = data[window_len * i: window_len * (i + 1)]
# Add/Calc rms acc
# w['net_acc'] = get_net_accel(w)
w = pd.concat([w, get_net_accel(w)])
#w.columns = ['net_acc', 'ACC_x', 'ACC_y', 'ACC_z', 'BVP',
# 'EDA', 'EDA_phasic', 'EDA_smna', 'EDA_tonic', 'TEMP',
# 'label']
cols = list(w.columns)
cols[0] = 'net_acc'
w.columns = cols
# Calculate stats for window
wstats = get_window_stats(data=w, label=label)
# Seperating sample and label
x = pd.DataFrame(wstats).drop('label', axis=0)
y = x['label'][0]
x.drop('label', axis=1, inplace=True)
if feat_names is None:
feat_names = []
for row in x.index:
for col in x.columns:
feat_names.append('_'.join([row, col]))
# sample df
wdf = pd.DataFrame(x.values.flatten()).T
wdf.columns = feat_names
wdf = pd.concat([wdf, pd.DataFrame({'label': y}, index=[0])], axis=1)
# More feats
wdf['BVP_peak_freq'] = get_peak_freq(w['BVP'].dropna())
wdf['TEMP_slope'] = get_slope(w['TEMP'].dropna())
samples.append(wdf)
return pd.concat(samples)
def make_patient_data(subject_id):
global savePath
global WINDOW_IN_SECONDS
# Make subject data object for Sx
subject = SubjectData(main_path='data/WESAD', subject_number=subject_id)
# Empatica E4 data - now with resp
e4_data_dict = subject.get_wrist_data()
# norm type
norm_type = None
# The 3 classes we are classifying
grouped, baseline, stress, amusement = compute_features(e4_data_dict, subject.labels, norm_type)
# print(f'Available windows for {subject.name}:')
n_baseline_wdws = int(len(baseline) / (fs_dict['label'] * WINDOW_IN_SECONDS))
n_stress_wdws = int(len(stress) / (fs_dict['label'] * WINDOW_IN_SECONDS))
n_amusement_wdws = int(len(amusement) / (fs_dict['label'] * WINDOW_IN_SECONDS))
# print(f'Baseline: {n_baseline_wdws}\nStress: {n_stress_wdws}\nAmusement: {n_amusement_wdws}\n')
#
baseline_samples = get_samples(baseline, n_baseline_wdws, 1)
# Downsampling
# baseline_samples = baseline_samples[::2]
stress_samples = get_samples(stress, n_stress_wdws, 2)
amusement_samples = get_samples(amusement, n_amusement_wdws, 0)
all_samples = pd.concat([baseline_samples, stress_samples, amusement_samples])
all_samples = pd.concat([all_samples.drop('label', axis=1), pd.get_dummies(all_samples['label'])], axis=1)
# Selected Features
# all_samples = all_samples[['EDA_mean', 'EDA_std', 'EDA_min', 'EDA_max',
# 'BVP_mean', 'BVP_std', 'BVP_min', 'BVP_max',
# 'TEMP_mean', 'TEMP_std', 'TEMP_min', 'TEMP_max',
# 'net_acc_mean', 'net_acc_std', 'net_acc_min', 'net_acc_max',
# 0, 1, 2]]
# Save file as csv (for now)
all_samples.to_csv(f'{savePath}{subject_feature_path}/S{subject_id}_feats_4.csv')
# Does this save any space?
subject = None
def combine_files(subjects):
df_list = []
for s in subjects:
df = pd.read_csv(f'{savePath}{subject_feature_path}/S{s}_feats_4.csv', index_col=0)
df['subject'] = s
df_list.append(df)
df = pd.concat(df_list)
df['label'] = (df['0'].astype(str) + df['1'].astype(str) + df['2'].astype(str)).apply(lambda x: x.index('1'))
df.drop(['0', '1', '2'], axis=1, inplace=True)
df.reset_index(drop=True, inplace=True)
df.to_csv(f'{savePath}/may14_feats4.csv')
counts = df['label'].value_counts()
print('Number of samples per class:')
for label, number in zip(counts.index, counts.values):
print(f'{int_to_label[label]}: {number}')
if __name__ == '__main__':
subject_ids = [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 14, 15, 16, 17]
for patient in subject_ids:
print(f'Processing data for S{patient}...')
make_patient_data(patient)
combine_files(subject_ids)
print('Processing complete.')