-
Notifications
You must be signed in to change notification settings - Fork 5
/
test_all.m
executable file
·360 lines (321 loc) · 11.3 KB
/
test_all.m
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
355
356
357
358
359
360
%TEST_ALL Tests a selection of algorithms against all test scenarios
clear;
% shortcuts for true and false (used in algs and tests structs)
T = true;
F = false;
%% Algorithms and parameters
% Set the lines of the algs struct for the algorithms to be tested to T.
% The ones not to be tested have to be set to F.
algs = struct( ...
'RXD', {@rxd, T}, ...
'WSCF', {@wscf, T}, ...
'RSAD', {@rsad, T}, ...
'LAD_Q', {@lad_Q, T}, ...
'LAD_Q_S', {@lad_Q_S, T}, ...
'LAD_C', {@lad_C, T}, ...
'LAD_C_S', {@lad_C_S, T}, ...
'RXD_PCA', {@rxd_PCA, T}, ...
'LAD_Q_PCA', {@lad_Q_PCA, T}, ...
'LAD_Q_PCA_S', {@lad_Q_PCA_S, T}, ...
'LAD_C_PCA', {@lad_C_PCA, T}, ...
'LAD_C_PCA_S', {@lad_C_PCA_S, T} ...
);
algs_names = fieldnames(algs);
% Set the lines of the tests struct for the scenarios to be performed to T.
% The ones not to be performed have to be set to F.
tests = struct( ...
'IMPL4', T, ...
'IMPL14', T, ...
'REAL', T, ...
'URBAN1', T, ...
'URBAN2', T ...
);
% e is the energy to be preserved for the PCA-like methods
e = 0.99;
% alpha parameter for RSAD
alpha = 0.001;
% pp is an array containing all threshold to be tested as percentage of the
% max distance.
pp = 0:0.02:1;
%% Implanted anomaly 4
if tests.IMPL4
load('data/salinas/salinas_impl_4.mat');
load('data/salinas/salinas_impl_gt.mat');
all_res_IMPL4 = zeros([1 length(algs_names)]);
all_t_IMPL4 = zeros([1 length(algs_names)]);
all_p_IMPL4 = zeros([1 length(algs_names)]);
all_roc_IMPL4 = zeros([length(pp) length(algs_names) 2]);
disp('########### IMPLANTED 4');
for i = 1:length(algs_names)
alg_name = algs_names{i};
alg_f = algs(1).(alg_name);
alg_test = algs(2).(alg_name);
if alg_test
disp(['## ' alg_name]);
tic
if nargin(alg_f) == 1 % used for most algorithms
out = alg_f(X);
t = -1;
elseif nargin(alg_f) == 2 % used for PCA-like algorithms
[out, t] = alg_f(X, e);
else % used for RSAD
out = alg_f(X, alpha);
t = -1;
end
toc
if islogical(out)
this_p = -1;
this_res = metrics(out, gt);
this_roc = zeros([length(pp) 2]);
else
this_p = 0;
this_res = metrics(out>=0, gt);
this_roc = zeros([length(pp) 2]);
j = 1;
for p = pp
restemp = metrics(out>max(out(:))*p, gt);
this_roc(j,1) = restemp(4);
this_roc(j,2) = restemp(3);
j=j+1;
if restemp(7) > this_res(7)
this_res = restemp;
this_p = p;
end
% disp(['p: ' num2str(p) ' - SOI: ' num2str(restemp(7))]);
end
end
all_res_IMPL4(i) = this_res(7);
all_t_IMPL4(i) = t;
all_p_IMPL4(i) = this_p;
all_roc_IMPL4(:,i,:) = this_roc;
disp(['BEST: t = ' num2str(all_t_IMPL4(i)) '; p = ' ...
num2str(this_p) '; SOI = ' num2str(all_res_IMPL4(i))]);
end
end
end
%% Implanted anomaly 14
if tests.IMPL14
load('data/salinas/salinas_impl_14.mat');
load('data/salinas/salinas_impl_gt.mat');
all_res_IMPL14 = zeros([1 length(algs_names)]);
all_t_IMPL14 = zeros([1 length(algs_names)]);
all_p_IMPL14 = zeros([1 length(algs_names)]);
all_roc_IMPL14 = zeros([length(pp) length(algs_names) 2]);
disp('########### IMPLANTED 14');
for i = 1:length(algs_names)
alg_name = algs_names{i};
alg_f = algs(1).(alg_name);
alg_test = algs(2).(alg_name);
if alg_test
disp(['## ' alg_name]);
tic
if nargin(alg_f) == 1 % used for most algorithms
out = alg_f(X);
t = -1;
elseif nargin(alg_f) == 2 % used for PCA-like algorithms
[out, t] = alg_f(X, e);
else % used for RSAD
out = alg_f(X, alpha);
t = -1;
end
toc
if islogical(out)
this_p = -1;
this_res = metrics(out, gt);
this_roc = zeros([length(pp) 2]);
else
this_p = 0;
this_res = metrics(out>=0, gt);
this_roc = zeros([length(pp) 2]);
j = 1;
for p = pp
restemp = metrics(out>max(out(:))*p, gt);
this_roc(j,1) = restemp(4);
this_roc(j,2) = restemp(3);
j=j+1;
if restemp(7) > this_res(7)
this_res = restemp;
this_p = p;
end
% disp(['p: ' num2str(p) ' - SOI: ' num2str(restemp(7))]);
end
end
all_res_IMPL14(i) = this_res(7);
all_t_IMPL14(i) = t;
all_p_IMPL14(i) = this_p;
all_roc_IMPL14(:,i,:) = this_roc;
disp(['BEST: t = ' num2str(all_t_IMPL14(i)) '; p = ' ...
num2str(this_p) '; SOI = ' num2str(all_res_IMPL14(i))]);
end
end
end
%% Real anomaly
if tests.REAL
load('data/salinas/salinas.mat');
load('data/salinas/salinas_gt.mat');
all_res_REAL = zeros([1 length(algs_names)]);
all_t_REAL = zeros([1 length(algs_names)]);
all_p_REAL = zeros([1 length(algs_names)]);
all_roc_REAL = zeros([length(pp) length(algs_names) 2]);
disp('########### REAL');
for i = 1:length(algs_names)
alg_name = algs_names{i};
alg_f = algs(1).(alg_name);
alg_test = algs(2).(alg_name);
if alg_test
disp(['## ' alg_name]);
tic
if nargin(alg_f) == 1 % used for most algorithms
out = alg_f(X);
t = -1;
elseif nargin(alg_f) == 2 % used for PCA-like algorithms
[out, t] = alg_f(X, e);
else % used for RSAD
out = alg_f(X, alpha);
t = -1;
end
toc
if islogical(out)
this_p = -1;
this_res = metrics(out, gt);
this_roc = zeros([length(pp) 2]);
else
this_p = 0;
this_res = metrics(out>=0, gt);
this_roc = zeros([length(pp) 2]);
j = 1;
for p = pp
restemp = metrics(out>max(out(:))*p, gt);
this_roc(j,1) = restemp(4);
this_roc(j,2) = restemp(3);
j=j+1;
if restemp(7) > this_res(7)
this_res = restemp;
this_p = p;
end
% disp(['p: ' num2str(p) ' - SOI: ' num2str(restemp(7))]);
end
end
all_res_REAL(i) = this_res(7);
all_t_REAL(i) = t;
all_p_REAL(i) = this_p;
all_roc_REAL(:,i,:) = this_roc;
disp(['BEST: t = ' num2str(all_t_REAL(i)) '; p = ' ...
num2str(this_p) '; SOI = ' num2str(all_res_REAL(i))]);
end
end
end
%% Urban 1
if tests.URBAN1
load('data/ABU/urban-1.mat');
X = data;
gt = map;
all_res_ABU1 = zeros([1 length(algs_names)]);
all_t_ABU1 = zeros([1 length(algs_names)]);
all_p_ABU1 = zeros([1 length(algs_names)]);
all_roc_ABU1 = zeros([length(pp) length(algs_names) 2]);
disp('########### URBAN 1');
for i = 1:length(algs_names)
alg_name = algs_names{i};
alg_f = algs(1).(alg_name);
alg_test = algs(2).(alg_name);
if alg_test
disp(['## ' alg_name]);
tic
if nargin(alg_f) == 1 % used for most algorithms
out = alg_f(X);
t = -1;
elseif nargin(alg_f) == 2 % used for PCA-like algorithms
[out, t] = alg_f(X, e);
else % used for RSAD
out = alg_f(X, alpha);
t = -1;
end
toc
if islogical(out)
this_p = -1;
this_res = metrics(out, gt);
this_roc = zeros([length(pp) 2]);
else
this_p = 0;
this_res = metrics(out>=0, gt);
this_roc = zeros([length(pp) 2]);
j = 1;
for p = pp
restemp = metrics(out>max(out(:))*p, gt);
this_roc(j,1) = restemp(4);
this_roc(j,2) = restemp(3);
j=j+1;
if restemp(7) > this_res(7)
this_res = restemp;
this_p = p;
end
% disp(['p: ' num2str(p) ' - SOI: ' num2str(restemp(7))]);
end
end
all_res_ABU1(i) = this_res(7);
all_t_ABU1(i) = t;
all_p_ABU1(i) = this_p;
all_roc_ABU1(:,i,:) = this_roc;
disp(['BEST: t = ' num2str(all_t_ABU1(i)) '; p = ' ...
num2str(this_p) '; SOI = ' num2str(all_res_ABU1(i))]);
end
end
end
%% Urban 2
if tests.URBAN2
load('data/ABU/urban-2.mat');
X = data;
gt = map;
all_res_ABU2 = zeros([1 length(algs_names)]);
all_t_ABU2 = zeros([1 length(algs_names)]);
all_p_ABU2 = zeros([1 length(algs_names)]);
all_roc_ABU2 = zeros([length(pp) length(algs_names) 2]);
disp('########### URBAN 2');
for i = 1:length(algs_names)
alg_name = algs_names{i};
alg_f = algs(1).(alg_name);
alg_test = algs(2).(alg_name);
if alg_test
disp(['## ' alg_name]);
tic
if nargin(alg_f) == 1 % used for most algorithms
out = alg_f(X);
t = -1;
elseif nargin(alg_f) == 2 % used for PCA-like algorithms
[out, t] = alg_f(X, e);
else % used for RSAD
out = alg_f(X, alpha);
t = -1;
end
toc
if islogical(out)
this_p = -1;
this_res = metrics(out, gt);
this_roc = zeros([length(pp) 2]);
else
this_p = 0;
this_res = metrics(out>=0, gt);
this_roc = zeros([length(pp) 2]);
j = 1;
for p = pp
restemp = metrics(out>max(out(:))*p, gt);
this_roc(j,1) = restemp(4);
this_roc(j,2) = restemp(3);
j=j+1;
if restemp(7) > this_res(7)
this_res = restemp;
this_p = p;
end
% disp(['p: ' num2str(p) ' - SOI: ' num2str(restemp(7))]);
end
end
all_res_ABU2(i) = this_res(7);
all_t_ABU2(i) = t;
all_p_ABU2(i) = this_p;
all_roc_ABU2(:,i,:) = this_roc;
disp(['BEST: t = ' num2str(all_t_ABU2(i)) '; p = ' ...
num2str(this_p) '; SOI = ' num2str(all_res_ABU2(i))]);
end
end
end