-
Notifications
You must be signed in to change notification settings - Fork 1
/
main_run.py
293 lines (244 loc) · 12.8 KB
/
main_run.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
# Kenny Schlegel, Peer Neubert, Peter Protzel
#
# HDC-MiniROCKET : Explicit Time Encoding in Time Series Classification with Hyperdimensional Computing
# Copyright (C) 2022 Chair of Automation Technology / TU Chemnitz
from data.dataset_utils import *
import logging
from models.HDC_MINIROCKET import HDC_MINIROCKET_model
from models.MINIROCKET import MINIROCKET_model
from sklearn.metrics import accuracy_score, roc_auc_score, classification_report, f1_score, confusion_matrix
# self.config logger
logger = logging.getLogger('log')
class NetTrial():
'''
high level class for training and evaluating
'''
def __init__(self,args):
# set self.config parameter
exec('self.config = ' + args.config + '()')
self.config.HDC_dim = args.HDC_dim
self.config.scale = args.scale
self.config.dataset = args.dataset
self.config.model_name = args.model
self.config.stat_iterations = args.stat_iterations
self.config.ensemble_idx = args.ensemble_idx
self.config.normalize_data = args.normalize
self.config.dataset_path = args.dataset_path
# load data at initialization
self.load_data()
def load_data(self):
# load dataset initially
self.data = load_dataset(self.config.dataset, self.config)
X_train = self.data[0]
X_test = self.data[1]
y_train = self.data[2]
y_test = self.data[3]
self.config.n_classes = len(np.unique(y_train))
self.config.n_inputs = X_train.shape[1]
self.config.n_steps = X_train.shape[2]
# if train test data not a list, create one (k-fold data set loading returns a list of splits - therefore we
# are handling all training set as lists, even if they only contain one set)
if type(X_train) == list:
print("given data is not a list")
self.X_train_list = X_train
self.X_test_list = X_test
self.y_train_list = y_train
self.y_test_list = y_test
else:
self.X_train_list = [X_train]
self.X_test_list = [X_test]
self.y_train_list = [y_train]
self.y_test_list = [y_test]
if self.config.model_name == "HDC_MINIROCKET":
self.model = HDC_MINIROCKET_model(self.config)
elif self.config.model_name == "MINIROCKET":
self.model = MINIROCKET_model(self.config)
def train(self):
'''
training procedure
@return:
'''
acc_stat = []
f1_stat = []
# statistical iteration
for stat_it in range(self.config.stat_iterations):
logger.info('Statistial iteration: ' + str(stat_it))
# train for each element in list (that is why we need list form, even if it contains only one element)
logger.info('Training data contains ' + str(len(self.X_train_list)) + ' training instances...')
f1s = []
accs = []
for it in range(len(self.X_train_list)):
logger.info(('.......'))
logger.info('instance ' + str(it) + ':')
X_train = self.X_train_list[it]
X_test = self.X_test_list[it]
y_train = self.y_train_list[it]
y_test = self.y_test_list[it]
# self.config.HDC_dim = X_train.shape[1]
logger.info('Training dataset shape: ' + str(X_train.shape) + str(y_train.shape))
logger.info('Test dataset shape: ' + str(X_test.shape) + str(y_test.shape))
self.config.train_count = len(X_train)
self.config.test_data_count = len(X_test)
self.model.config = self.config
# train the model
self.model.train_model(X_train,y_train,X_test,y_test)
# evaluate the model
y_pred, y_scores = self.model.eval_model(X_train, y_train, X_test, y_test)
# evaluate the results
logger.info('Results on test data: ')
report = classification_report(y_test.astype(int), y_pred, output_dict=True)
# accuracy and f1 score
acc = report['accuracy']
f1 = f1_score(y_test.astype(int), y_pred, average='weighted')
logger.info('Accuracy: ' + str(acc))
logger.info('F1 score: ' + str(f1))
accs.append(acc)
f1s.append(f1)
acc_stat.append(np.mean(accs))
f1_stat.append((np.mean(f1s)))
idx = self.config.ensemble_idx
try:
file_path = 'results/' + self.config.model_name + '_' + self.config.dataset + '_' + \
self.config.note
# write other results to excel
file = file_path + '_results.xlsx'
file_f1 = file_path + '_results_f1.xlsx'
file_time = file_path + '_time.xlsx'
acc_df = pd.DataFrame({'data': acc}, index=[0])
f1_df = pd.DataFrame({'data': f1}, index=[0])
idx_df = pd.DataFrame({'data': idx}, index=[0])
# write index
append_df_to_excel(file, pd.DataFrame({self.config.dataset + '_idx': []}),
startcol=0, index=False, header=True,
startrow=0)
append_df_to_excel(file_f1, pd.DataFrame({self.config.dataset + '_idx': []}),
startcol=0, index=False, header=True,
startrow=0)
append_df_to_excel(file_time, pd.DataFrame({self.config.dataset + '_idx': []}),
startcol=0, index=False, header=True,
startrow=0)
if self.config.best_scale:
header_name = pd.DataFrame({'acc_at_best_scale' :[],
'best_scale':[]})
else:
header_name = pd.DataFrame({'scale_idx' + str(self.config.scale_idx): []})
# files for the normal results
append_df_to_excel(file, header_name,
startcol=self.config.scale_idx + 1, index=False, header=True,
startrow=0)
append_df_to_excel(file, acc_df,
index=False, header=False, startrow=idx + 1,
startcol=self.config.scale_idx + 1)
if self.config.best_scale:
append_df_to_excel(file, pd.DataFrame({'best_scale': str(self.config.scale)}, index=[0]),
index=False, header=False, startrow=idx + 1,
startcol=2)
# files for the f1 results
append_df_to_excel(file_f1, header_name,
startcol=self.config.scale_idx + 1, index=False, header=True,
startrow=0)
append_df_to_excel(file_f1, f1_df,
index=False, header=False, startrow=idx + 1,
startcol=self.config.scale_idx + 1)
append_df_to_excel(file, idx_df,
startcol=0, index=False, header=False,
startrow=idx + 1)
append_df_to_excel(file_f1, idx_df,
startcol=0, index=False, header=False,
startrow=idx + 1)
# write run-time results to file
append_df_to_excel(file_time, idx_df,
startcol=0, index=False, header=False,
startrow=idx + 1)
append_df_to_excel(file_time, pd.DataFrame({'prep_time_train': []}),
startcol=1, index=False, header=True,
startrow=0)
append_df_to_excel(file_time, pd.DataFrame({'data': self.model.train_preproc}, index=[0]),
startcol=1, index=False, header=False,
startrow=idx + 1)
append_df_to_excel(file_time, pd.DataFrame({'prep_time_test': []}),
startcol=2, index=False, header=True,
startrow=0)
append_df_to_excel(file_time, pd.DataFrame({'data': self.model.test_preproc}, index=[0]),
startcol=2, index=False, header=False,
startrow=idx + 1)
append_df_to_excel(file_time, pd.DataFrame({'train_time': []}),
startcol=3, index=False, header=True,
startrow=0)
append_df_to_excel(file_time, pd.DataFrame({'data': self.model.training_time}, index=[0]),
startcol=3, index=False, header=False,
startrow=idx + 1)
append_df_to_excel(file_time, pd.DataFrame({'inf_time': []}),
startcol=4, index=False, header=True,
startrow=0)
append_df_to_excel(file_time, pd.DataFrame({'data': self.model.testing_time}, index=[0]),
startcol=4, index=False, header=False,
startrow=idx + 1)
except Exception as e:
logger.info(e)
def eval(self):
'''
evaluating procedure
@return:
'''
f1s = []
accs = []
for it in range(len(self.X_test_list)):
logger.info(('.......'))
logger.info('instance ' + str(it) + ':')
X_train = self.X_train_list[it]
X_test = self.X_test_list[it]
y_train = self.y_train_list[it]
y_test = self.y_test_list[it]
logger.info('Test dataset shape: ' + str(X_test.shape) + str(y_test.shape))
self.config.test_data_count = len(X_test)
self.model.config = self.config
# evaluate the model
acc, f1, confusion_matrix = self.model.eval_model(X_train,y_train,X_test,y_test,fold=it)
accs.append(acc)
f1s.append(f1)
with open('results/computing_time_inference_' + self.config.dataset + '_' + self.config.model_name + '.txt',
'a') as file:
file.write(str(self.config.ensemble_idx) + '\t' +
str(self.model.test_preproc) + '\t'
+ str(self.model.testing_time) + '\n'
)
logger.info('Accuracy results: ' + str(np.mean(accs)))
logger.info('F1 scores: ' + str(np.mean(f1s)))
def append_df_to_excel(filename, df, sheet_name='Sheet1', startrow=None,
truncate_sheet=False,
**to_excel_kwargs):
"""
code from https://gist.github.com/fredpedroso/590e54d4f07d0ae2d20d0ec0b190d5ff
Append a DataFrame [df] to existing Excel file [filename]
into [sheet_name] Sheet.
If [filename] doesn't exist, then this function will create it.
Parameters:
filename : File path or existing ExcelWriter
(Example: '/path/to/file.xlsx')
df : dataframe to save to workbook
sheet_name : Name of sheet which will contain DataFrame.
(default: 'Sheet1')
startrow : upper left cell row to dump data frame.
Per default (startrow=None) calculate the last row
in the existing DF and write to the next row...
truncate_sheet : truncate (remove and recreate) [sheet_name]
before writing DataFrame to Excel file
to_excel_kwargs : arguments which will be passed to `DataFrame.to_excel()`
[can be dictionary]
Returns: None
"""
# ignore [engine] parameter if it was passed
if 'engine' in to_excel_kwargs:
to_excel_kwargs.pop('engine')
if os.path.isfile(filename):
writer = pd.ExcelWriter(filename, mode='a', engine='openpyxl', if_sheet_exists="overlay")
else:
# check if directory exists
if not os.path.exists(os.path.dirname(filename)):
os.makedirs(os.path.dirname(filename))
writer = pd.ExcelWriter(filename, mode='w', engine='openpyxl')
# write out the new sheet
df.to_excel(writer, sheet_name, startrow=startrow, **to_excel_kwargs)
# save the workbook
writer.close()