-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
354 lines (297 loc) · 18.3 KB
/
main.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
import numpy as np
import pandas as pd
import csv
import datetime
import sys
import argparse
import pickle
import shap
from contextlib import redirect_stdout
from sklearn.model_selection import train_test_split
from utils.readData import readData
from utils.model import two_layer_CV, model_with_paramSelection, underSampleParticipants
from Config import config
import warnings
warnings.filterwarnings('ignore')
def main(args):
#argName = args.argName
dataset = args.dataset
samplingMethod = args.samplingMethod
windowLength = args.windowLength
modelType = args.modelType
group = args.group
upsample = args.upsample
dataFileName = dataset + '_' + samplingMethod + '_' + str(windowLength) + '_' + group if len(args.dataFileName)==0 else dataset + '_' + samplingMethod + '_' + str(windowLength) + '_' + group + '_' + args.dataFileName
try:
with open('./data/' + dataFileName + '_anonymous.pkl', 'rb') as fp:
(X, y, days, participants, timeOfDay, removedData) = pickle.load(fp)
except:
with open('./data/creationOfFeatureMatrix' + dataFileName + '_anonymous.log', 'wb') as f:
with redirect_stdout(f):
X, y, days, participants, timeOfDay, removedData = readData(samplingMethod, windowLength, group=group)
out = (X, y, days, participants, timeOfDay, removedData)
with open('./data/' + dataFileName + '_anonymous.pkl', 'wb') as fp:
pickle.dump(out, fp)
timeOfDay = np.array(timeOfDay)
participants = np.array(participants)
n,m = X.shape
N = len(np.unique(participants))
print(f'Number of observations: {n}')
print(f'Number of features: {m}')
print(f'Number of unique participants: {N}')
assert n == len(y)
assert n == len(days)
assert n == len(participants)
assert n == len(timeOfDay)
assert m == len(config.featuresNames)
upsampleStr = '_upsampled' if upsample else ''
if modelType=='Personal':
for i in range(1,11):
model = model_with_paramSelection('Temporal', 1, upsample=upsample)
CV = two_layer_CV('Temporal', 1, verbose=1, upsample=upsample)
for id_ in np.unique(participants):
filter_ = participants==id_
avg_acc, avg_f1score, avg_auc, all_accuracies, all_f1scores, rocInfo, precisionRecall, selected_models, participantResults, shapInfo = CV.estimateTestACC(X[filter_,:], y[filter_], np.zeros((len(participants[filter_])),dtype=int), days[filter_])
print(f'The average accuracy on the outer layer is {avg_acc}')
print(f'The average F1 score on the outer layer is {avg_f1score}')
print(f'The average ROC AUC on the outer layer is {avg_auc}')
resultsFileName = dataset + '_' + samplingMethod + '_' + str(windowLength) + '_' + modelType + '_' + group + upsampleStr
out = {
'avgAcc': avg_acc,
'avgF1Score': avg_f1score,
'avgAUC': avg_auc,
'accuracies': all_accuracies,
'f1Scores': all_f1scores,
'rocLists': rocInfo,
'precisionRecall': precisionRecall,
'selectedModels': selected_models,
'participantResults': participantResults,
'shapInfo': shapInfo
}
with open('./results/' + resultsFileName + '_ID' + str(id_) + '_' + str(i) + '.pkl', 'wb') as fp:
pickle.dump(out, fp)
if modelType=='PersonalDownSample':
for i in range(1,11):
model = model_with_paramSelection('PersonalDownSample', 1, upsample=upsample)
CV = two_layer_CV('PersonalDownSample', 1, verbose=1, upsample=upsample)
filter_ = participants==7
for percentage in [0.1, 0.25, 0.5, 0.75, 0.9]:
#No statefied sampling
#n_sample = int(percentage*(X[filter_,:].shape[0]))
#sample_idxs = sorted(np.random.choice(np.arange(X[filter_,:].shape[0]), size=n_sample, replace=False))
#stratified sampling
#sample_idxs, _ = train_test_split(np.arange(X[filter_,:].shape[0]), test_size=1-percentage, stratify=y[filter_])
#sample_idxs = sorted(sample_idxs)
X_sample = X[filter_,:]#[sample_idxs,:]
y_sample = y[filter_]#[sample_idxs]
participant_sample = np.zeros((len(y[filter_])),dtype=int)
days_sample = days[filter_]#[sample_idxs]
avg_acc, avg_f1score, avg_auc, all_accuracies, all_f1scores, rocInfo, precisionRecall, selected_models, participantResults, shapInfo = CV.estimateTestACC(X_sample, y_sample, participant_sample, days_sample, percentage=percentage)
print(f'The average accuracy on the outer layer is {avg_acc}')
print(f'The average F1 score on the outer layer is {avg_f1score}')
print(f'The average ROC AUC on the outer layer is {avg_auc}')
ytest = y_sample[shapInfo[1]] #test indicies are found in shapInfo[1]
resultsFileName = dataset + '_' + samplingMethod + '_' + str(windowLength) + '_' + modelType + '_' + group + upsampleStr
out = {
'avgAcc': avg_acc,
'avgF1Score': avg_f1score,
'avgAUC': avg_auc,
'accuracies': all_accuracies,
'f1Scores': all_f1scores,
'rocLists': rocInfo,
'precisionRecall': precisionRecall,
'selectedModels': selected_models,
'participantResults': participantResults,
'shapInfo': shapInfo,
'ytest': ytest
}
with open('./results/' + resultsFileName + '_ID7_percentageData' + str(percentage) + '_' + str(i) + '.pkl', 'wb') as fp:
pickle.dump(out, fp)
if modelType=='PersonalDownSampleRandomCV':
for i in range(1,11):
model = model_with_paramSelection('Random', 1, upsample=upsample)
CV = two_layer_CV('Random', 1, verbose=1, upsample=upsample)
filter_ = participants==7
for percentage in [0.1, 0.25, 0.5, 0.75, 0.9, 1.0]:
#No statefied sampling
#n_sample = int(percentage*(X[filter_,:].shape[0]))
#sample_idxs = sorted(np.random.choice(np.arange(X[filter_,:].shape[0]), size=n_sample, replace=False))
#stratified sampling
if percentage<1.0:
sample_idxs, _ = train_test_split(np.arange(X[filter_,:].shape[0]), test_size=1-percentage, stratify=y[filter_])
sample_idxs = sorted(sample_idxs)
else:
sample_idxs = np.arange(X[filter_,:].shape[0])
X_sample = X[filter_,:][sample_idxs,:]
y_sample = y[filter_][sample_idxs]
participant_sample = np.zeros((len(y_sample)),dtype=int)
days_sample = days[filter_][sample_idxs]
print(X_sample.shape)
avg_acc, avg_f1score, avg_auc, all_accuracies, all_f1scores, rocInfo, precisionRecall, selected_models, participantResults, shapInfo = CV.estimateTestACC(X_sample, y_sample, participant_sample, days_sample)
print(f'The average accuracy on the outer layer is {avg_acc}')
print(f'The average F1 score on the outer layer is {avg_f1score}')
print(f'The average ROC AUC on the outer layer is {avg_auc}')
ytest = [y_sample[test_idx] for test_idx in shapInfo[1]] #test indicies are found in shapInfo[1]
resultsFileName = dataset + '_' + samplingMethod + '_' + str(windowLength) + '_' + modelType + '_' + group + upsampleStr
out = {
'avgAcc': avg_acc,
'avgF1Score': avg_f1score,
'avgAUC': avg_auc,
'accuracies': all_accuracies,
'f1Scores': all_f1scores,
'rocLists': rocInfo,
'precisionRecall': precisionRecall,
'selectedModels': selected_models,
'participantResults': participantResults,
'shapInfo': shapInfo,
'ytest': ytest
}
with open('./results/' + resultsFileName + '_ID7_percentageData' + str(percentage) + '_' + str(i) + '.pkl', 'wb') as fp:
pickle.dump(out, fp)
elif modelType=='TemporalDownSample':
for i in range(1,11):
model = model_with_paramSelection('TemporalDownSample', 1, upsample=upsample)
CV = two_layer_CV('TemporalDownSample', 1, verbose=1, upsample=upsample)
for id_ in np.unique(participants):
#Downsample participants with more data
X_sample, y_sample, participant_sample, days_sample = underSampleParticipants(id_, X, y, participants, days)
avg_acc, avg_f1score, avg_auc, all_accuracies, all_f1scores, rocInfo, precisionRecall, selected_models, participantResults, shapInfo = CV.estimateTestACC(X_sample, y_sample, participant_sample, days_sample, evaluationId=id_)
print(f'The average accuracy on the outer layer is {avg_acc}')
print(f'The average F1 score on the outer layer is {avg_f1score}')
print(f'The average ROC AUC on the outer layer is {avg_auc}')
resultsFileName = dataset + '_' + samplingMethod + '_' + str(windowLength) + '_' + modelType + '_' + group + upsampleStr
out = {
'avgAcc': avg_acc,
'avgF1Score': avg_f1score,
'avgAUC': avg_auc,
'accuracies': all_accuracies,
'f1Scores': all_f1scores,
'rocLists': rocInfo,
'precisionRecall': precisionRecall,
'selectedModels': selected_models,
'participantResults': participantResults,
'shapInfo': shapInfo
}
with open('./results/' + resultsFileName + '_ID' + str(id_) + '_' + str(i) + '.pkl', 'wb') as fp:
pickle.dump(out, fp)
elif modelType=='RandomDownSample':
for i in range(1,2):
model = model_with_paramSelection('Random', N, upsample=upsample)
CV = two_layer_CV('Random', N, verbose=1, upsample=upsample)
#Downsample participants with more data
X_sample, y_sample, participant_sample, days_sample = underSampleParticipants(1, X, y, participants, days) #participant 3 has the lowest data amount with 30, 0 is the median with 213 #Random DownSample. Does not account for class distribution
avg_acc, avg_f1score, avg_auc, all_accuracies, all_f1scores, rocInfo, precisionRecall, selected_models, participantResults, shapInfo = CV.estimateTestACC(X_sample, y_sample, participant_sample, days_sample)
print(f'The average accuracy on the outer layer is {avg_acc}')
print(f'The average F1 score on the outer layer is {avg_f1score}')
print(f'The average ROC AUC on the outer layer is {avg_auc}')
resultsFileName = dataset + '_' + samplingMethod + '_' + str(windowLength) + '_' + modelType + '_' + group + upsampleStr
out = {
'avgAcc': avg_acc,
'avgF1Score': avg_f1score,
'avgAUC': avg_auc,
'accuracies': all_accuracies,
'f1Scores': all_f1scores,
'rocLists': rocInfo,
'precisionRecall': precisionRecall,
'selectedModels': selected_models,
'participantResults': participantResults,
'shapInfo': shapInfo
}
with open('./results/' + resultsFileName + str(i) + '.pkl', 'wb') as fp:
pickle.dump(out, fp)
elif modelType=='topShapFeatures':
for i in range(1,2):
model = model_with_paramSelection('Random', N, upsample=upsample)
CV = two_layer_CV('Random', N, verbose=1, upsample=upsample)
#Filter the top shap values
fnames = np.array(config.featuresNames)
cols = np.hstack([
np.where(fnames=='BVP_max_slope')[0],
np.where(fnames=='BVP_min_slope')[0],
np.where(fnames=='BVP_freq_mean_real')[0],
np.where(fnames=='BVP_time_min')[0],
np.where(fnames=='BVP_freq_min_real')[0],
np.where(fnames=='BVP_time_max')[0],
np.where(fnames=='BVP_freq_sma_real')[0],
np.where(fnames=='BVP_freq_max_real')[0],
np.where(fnames=='BVP_time_std')[0],
np.where(fnames=='BVP_freq_mean_in')[0],
np.where(fnames=='Phasic_BandPower_0.4_1.0')[0],
np.where(fnames=='HR_std')[0],
np.where(fnames=='BVP_freq_iqr_real')[0],
np.where(fnames=='BVP_freq_iqr_im')[0],
np.where(fnames=='BVP_avg_slopq')[0],
np.where(fnames=='TEMP_max')[0],
np.where(fnames=='HR_25quantile')[0],
np.where(fnames=='BVP_freq_sma_im')[0],
np.where(fnames=='HR_max')[0],
np.where(fnames=='Phasic_BandPower_0.04_0.15')[0],
])
cols = sorted(cols)
X_sample = X[:,cols]
avg_acc, avg_f1score, avg_auc, all_accuracies, all_f1scores, rocInfo, precisionRecall, selected_models, participantResults, shapInfo = CV.estimateTestACC(X_sample, y, participants, days)
print(f'The average accuracy on the outer layer is {avg_acc}')
print(f'The average F1 score on the outer layer is {avg_f1score}')
print(f'The average ROC AUC on the outer layer is {avg_auc}')
resultsFileName = dataset + '_' + samplingMethod + '_' + str(windowLength) + '_' + modelType + '_' + group + upsampleStr
out = {
'avgAcc': avg_acc,
'avgF1Score': avg_f1score,
'avgAUC': avg_auc,
'accuracies': all_accuracies,
'f1Scores': all_f1scores,
'rocLists': rocInfo,
'precisionRecall': precisionRecall,
'selectedModels': selected_models,
'participantResults': participantResults,
'shapInfo': shapInfo
}
with open('./results/' + resultsFileName + str(i) + '.pkl', 'wb') as fp:
pickle.dump(out, fp)
else:
n_repeats = 10 if modelType=='Temporal' else 1
for i in range(1,n_repeats+1):
model = model_with_paramSelection(modelType, N, upsample=upsample)
CV = two_layer_CV(modelType, N, verbose=1, upsample=upsample)
avg_acc, avg_f1score, avg_auc, all_accuracies, all_f1scores, rocInfo, precisionRecall, selected_models, participantResults, shapInfo = CV.estimateTestACC(X, y, participants, days)
print(f'The average accuracy on the outer layer is {avg_acc}')
print(f'The average F1 score on the outer layer is {avg_f1score}')
print(f'The average ROC AUC on the outer layer is {avg_auc}')
resultsFileName = dataset + '_' + samplingMethod + '_' + str(windowLength) + '_' + modelType + '_' + group + upsampleStr
out = {
'avgAcc': avg_acc,
'avgF1Score': avg_f1score,
'avgAUC': avg_auc,
'accuracies': all_accuracies,
'f1Scores': all_f1scores,
'rocLists': rocInfo,
'precisionRecall': precisionRecall,
'selectedModels': selected_models,
'participantResults': participantResults,
'shapInfo': shapInfo
}
with open('./results/' + resultsFileName + '_' + str(i) + '.pkl', 'wb') as fp:
pickle.dump(out, fp)
#model.fit(X, y, participants, days)
def parse_arguments(argv):
"""Command line parser.
Use like:
python main.py --arg1 string --arg2 value --arg4
For help:
python main.py -h
"""
parser = argparse.ArgumentParser()
#parser.add_argument('--arg1', type=str, default='String', help='String value. Default = "String"')
#parser.add_argument('--arg2', type=int, default=50, choices=[50, 100, 200], help='Integer value with limited choices. Default = 50')
#parser.add_argument('--arg3', type=float, default=0.001, help='Float value. Default = 0.001')
#parser.add_argument('--arg4', type=bool, default=False, help='Bool value. Default = False')
#parser.add_argument("--optional", action="store_true", help="Optional argument")
parser.add_argument("--upsample", action="store_true", help="Whether to upsample the positive class")
parser.add_argument('--dataset', type=str, default='wristAngel', choices=['wristAngel'], help='The data set to use. Choices are DTU ("DTU") or Wrist Angel ("wristAngel"). Default = "DTU"')
parser.add_argument('--samplingMethod', type=str, default='extractTags', choices=['rollingWindows', 'extractTags'], help='The method to extract observations. Choices are rolling windows ("rollingWindows") or windows sampled around tags ("extractTags"). Default = "extractTags"')
parser.add_argument('--windowLength', type=int, default=300, help='Window length in seconds to use for feature extraction. Default=120')
parser.add_argument('--modelType', type=str, choices=['Temporal', 'TemporalDownSample', 'Participant', 'Personal','PersonalDownSample', 'Random','RandomDownSample','topShapFeatures','PersonalDownSampleRandomCV'], help='The model type to train. Choices are Temporal ("Temporal") Participant ("Participant") or Personal ("Personal"). Required')
parser.add_argument('--group', type=str, default='patients', choices=['all', 'patients','controls'], help='The group which data to use. Choices are all ("all") or patients ("patients") or controls ("controls"). Default = "all"')
return parser.parse_args()
if __name__ == '__main__':
main(parse_arguments(sys.argv[1:]))