-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathevaluate.py
332 lines (263 loc) · 12 KB
/
evaluate.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
"""
File: evaluate.py
Project: analysis
Last Modified: 2021-8-2
Created Date: 2021-8-2
Copyright (c) 2021
Author: AHMA project (Univ Rennes, CNRS, Inria, IRISA)
"""
################################################################################
import sys, os, glob
import logging
import numpy as np
import joblib
import argparse
import time
from tqdm import tqdm
from datetime import datetime
from sklearn.svm import SVC
from sklearn.naive_bayes import GaussianNB
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
from sklearn.preprocessing import LabelEncoder
from sklearn.metrics import classification_report
sys.path.append(os.path.join (os.path.dirname (__file__), "../pre-processings/"))
from nicv import compute_nicv
from corr import compute_corr
from list_manipulation import get_tag
################################################################################
import numpy as np
import matplotlib, sys
import logging
## to avoid bug when it is run without graphic interfaces
try:
matplotlib.use('GTK3Agg')
import matplotlib.pyplot as plt
except ImportError:
# print ('Warning importing GTK3Agg: ', sys.exc_info()[0])
matplotlib.use('Agg')
import matplotlib.pyplot as plt
################################################################################
def load_traces (files_list, bandwidth, time_limit):
################################################################################
# load_traces
# load all the traces listed in 'files_lists', the 2D-traces are flattened
#
# input:
# + files_list: list of the filenames
# + bandwidth: selected bandwidth
# + time_limit: percentage of the trace to concerve
#
# output:
# + traces: array containing the traces (dimension: DxQ, D number of features,
# Q number of samples)
################################################################################
## get dimension
tmp_trace = np.load (files_list [0], allow_pickle = True)[-1][bandwidth, :]
## takes only half of the features
D = int (tmp_trace.shape [1]*time_limit)
tmp_trace = tmp_trace [:, :D].flatten ()
traces = np.zeros ((len (tmp_trace), len (files_list)))
traces [:, 0] = tmp_trace
for i in tqdm (range (1, traces.shape [1])):
traces [:, i] = np.load (files_list [i], allow_pickle = True)[-1][bandwidth, :D].flatten ()
return traces
################################################################################
def mean_by_label (traces, labels, mean_size):
################################################################################
# mean_by_label
# mean traces per label, it means the input traces are mean by batch of 'mean_size'
# of the same label
#
# input:
# + traces: array of traces (DxQ)
#
# output:
# + traces: mean traces (dimension: DxQ, D number of features,
# Q number of samples)
################################################################################
unique = np.unique (labels)
tmp_res = []
tmp_labels = []
count = 0
for i in tqdm (range (len (unique)), desc = 'meaning (%s)'%mean_size):
idx = np.where (labels == unique [i])[0]
for j in range (0, len (idx) - mean_size, mean_size):
tmp_labels.append (unique [i])
current_res = 0.
for k in range (mean_size):
current_res += traces [:, idx [j + k]]
tmp_res.append (current_res/mean_size)
return np.array (tmp_res).T, tmp_labels
################################################################################
def evaluate (path_lists, log_file, mean_sizes, nb_of_bd, path_acc,
time_limit, metric):
################################################################################
# mean_by_label
# compute the LDA + BN and LDA + SVM machine learning algorithm
#
# input:
# + path_lists: path of the lists
# + log_file: where the results are saved
# + mean_sizes: numbers of mean sizes to try
# + nb_of_bd: nb of frequency band to conserve
# + path_acc: directory where acculmulators are
# + time_limit: percentage of the trace (from the begining)
# - metric: metric to use for the bandwidth selection
################################################################################
## logging exp in file
today = datetime.now ()
d1 = today.strftime ("%d.%m.%Y - %H:%M:%S")
file_log = open (log_file, 'a')
file_log.write ('-'*80 + '\n')
file_log.write (d1 + '\n')
file_log.write ('path_lists: %s\n'%str (path_lists)\
+ 'log_file: %s\n'%str (log_file)\
+ 'model_lda: None\n'\
+ 'model_svm: None\n'\
+ 'model_nb: None\n'\
+ 'means: %s\n'%str (mean_sizes)\
+ 'nb_of_bd: %s\n'%str (nb_of_bd)\
+ 'path_acc: %s\n'%str (path_acc)\
+ 'time_limit: %s\n'%str (time_limit)\
+ 'metric: %s\n'%str (metric))
file_log.write ('-'*80 + '\n')
file_log.close ()
## get indexes
if ('nicv' in metric):
t, f, nicv, bandwidth = compute_nicv (path_lists, path_acc, None,\
bandwidth_nb = nb_of_bd,
time_limit = time_limit,
metric = metric)
else:
t, f, nicv, bandwidth = compute_corr (path_lists, path_acc, None,\
bandwidth_nb = nb_of_bd,
time_limit = time_limit,
metric = metric)
## load lists
[x_train_filelist, x_val_filelist, x_test_filelist, y_train, y_val, y_test] \
= np.load (path_lists, allow_pickle = True)
learning_traces = load_traces (x_train_filelist, bandwidth, time_limit)
validating_traces = load_traces (x_val_filelist, bandwidth, time_limit)
## learning
traces = np.zeros ((learning_traces.shape [0], learning_traces.shape [1] + validating_traces.shape [1]))
traces [:, :learning_traces.shape [1]] = learning_traces
traces [:, learning_traces.shape [1]:] = validating_traces
learning_labels = y_train
validating_labels = y_val
labels = np.concatenate ((learning_labels, validating_labels))
## projection
t0 = time.time ()
clf = LinearDiscriminantAnalysis ()
transformed_traces = clf.fit_transform (traces.T, list (labels))
## save LDA
tmp = path_lists.split ('=')[-1].split ('.')[0]
joblib.dump (clf, '/'.join (path_lists.split ('/')[:-1]) + f'/LDA_tagmpas={tmp}_{nb_of_bd}bd.jl')
file_log = open (log_file, 'a')
file_log.write ('LDA (compuation): %s seconds\n'%(time.time () - t0))
file_log.close ()
## learning on projection
t0 = time.time ()
gnb = GaussianNB ()
gnb.fit (transformed_traces, labels)
## save GN
tmp = path_lists.split ('=')[-1].split ('.')[0]
joblib.dump (gnb, '/'.join (path_lists.split ('/')[:-1]) + f'/NB_tagmpas={tmp}_{nb_of_bd}bd.jl')
file_log = open (log_file, 'a')
file_log.write ('NB (computation): %s seconds\n'%(time.time () - t0))
file_log.close ()
t0 = time.time ()
svc = SVC ()
svc.fit (transformed_traces, labels)
## save SVM
tmp = path_lists.split ('=')[-1].split ('.')[0]
joblib.dump (svc, '/'.join (path_lists.split ('/')[:-1]) + f'/SVM_tagmpas={tmp}_{nb_of_bd}bd.jl')
file_log = open (log_file, 'a')
file_log.write ('SVM (computation): %s seconds\n'%(time.time () - t0))
file_log.close ()
## now and testing
testing_traces = load_traces (x_test_filelist, bandwidth, time_limit)
testing_labels = y_test
## no means
## projection LDA
t0 = time.time ()
X = clf.transform (testing_traces.T)
# save transformed traces
tmp = path_lists.split ('=')[-1].split ('.')[0]
np.save ('/'.join (path_lists.split ('/')[:-1]) + f'/transformed_traces_tagmaps={tmp}_{nb_of_bd}bd.npy',
X, allow_pickle = True)
file_log = open (log_file, 'a')
file_log.write ('transform (size: %s): %s seconds\n'%(str(testing_traces.shape), str (time.time () - t0)))
file_log.close ()
## NB
t0 = time.time ()
predicted_labels = gnb.predict (X)
file_log = open (log_file, 'a')
file_log.write ('Test NB (size: %s) [%s seconds]:\n'%(str (X.shape), str (time.time () - t0)))
file_log.write (f'{classification_report (list (testing_labels), predicted_labels, digits = 4, zero_division = 0)}')
file_log.close ()
## SVM
t0 = time.time ()
predicted_labels = svc.predict (X)
file_log = open (log_file, 'a')
file_log.write ('Test SVM (size: %s) [%s seconds]:\n'%(str (X.shape), str (time.time () - t0)))
file_log.write (f'{classification_report (list (testing_labels), predicted_labels, digits = 4, zero_division = 0)}')
file_log.close ()
for mean_size in mean_sizes:
file_log = open (log_file, 'a')
file_log.write ('compute with %s per mean\n'%mean_size)
file_log.close ()
X, y = mean_by_label (testing_traces, np.array (testing_labels), mean_size)
## LDA on means
t0 = time.time ()
X = clf.transform (X.T)
file_log = open (log_file, 'a')
file_log.write ('transform (size: %s): %s seconds\n'%(str(testing_traces.shape), str (time.time () - t0)))
file_log.close ()
## NB
t0 = time.time ()
predicted_labels = gnb.predict (X)
file_log = open (log_file, 'a')
file_log.write (f'NB - mean {mean_size}:\n {classification_report (list (y), predicted_labels, digits = 4, zero_division = 0)}')
file_log.close ()
# SVM
t0 = time.time ()
predicted_labels = svc.predict (X)
file_log = open (log_file, 'a')
file_log.write (f'SVM - mean {mean_size}:\n {classification_report (list (y), predicted_labels, digits = 4, zero_division = 0)}')
file_log.close ()
################################################################################
if __name__ == '__main__':
################################################################################
parser = argparse.ArgumentParser()
parser.add_argument ('--lists', action = 'store',
type = str, dest = 'path_lists',
help = 'Absolute path to a file containing the lists')
parser.add_argument("--mean_size", default = [2,3,4,5,6,7,9,10],
action = 'append', dest = 'mean_sizes',
help = 'Size of each means')
parser.add_argument('--log-file', default = 'log-evaluation.txt',
dest = 'log_file',
help = 'Absolute path to the file to save results')
parser.add_argument ('--acc', action='store', type=str,
dest='path_acc',
help='Absolute path of the accumulators directory')
parser.add_argument('--nb_of_bandwidth', action='store', type=int,
default=20,
dest='nb_of_bandwidth',
help='number of bandwidth to extract')
parser.add_argument ('--time_limit', action ='store', type = float, default = 1,
dest = 'time_limit',
help = 'percentage of time to concerve (from the begining)')
parser.add_argument('--metric', action='store', type=str, default='nicv_max',
dest='metric', help='Metric to use for select bandwidth: {nicv, corr}_{mean, max} ')
args, unknown = parser.parse_known_args ()
assert len (unknown) == 0, f"[WARNING] Unknown arguments:\n{unknown}\n"
args, unknown = parser.parse_known_args ()
# test (args, GaussianNB (), False)
evaluate (args.path_lists,
args.log_file,
args.mean_sizes,
args.nb_of_bandwidth,
args.path_acc,
args.time_limit,
args.metric)