-
Notifications
You must be signed in to change notification settings - Fork 0
/
surrogate.m
431 lines (397 loc) · 15.1 KB
/
surrogate.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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
%%
% surrogate command line tool
function surrogate(varargin)
% set version number
versionNumber = '0.1';
% add script path
if ~isdeployed % checking MATLAB mode or stand-alone mode.
[st,ind] = dbstack('-completenames');
relpath = st(ind).file;
[exedir,exename,ext] = fileparts(relpath);
if exist([exedir '/util'],'dir')
addpath([exedir '/util']);
addpath([exedir '/lib']);
end
end
% get exe file full path
global exePath;
global exeName;
[exePath, exeName, ext] = exeFilename();
% init command line input
handles.commandError = 0;
handles.csvFiles = {};
handles.outpath = 'results';
handles.lag = 3;
handles.transform = 0;
handles.transopt = NaN;
handles.format = 2;
handles.showSig = 0;
handles.rg = 0;
handles.rs = 0;
handles.ft = 0;
handles.aaft = 0;
handles.iaaft = 0;
handles.var = 0;
handles.pcvar = 0;
handles.vardnn = 0;
handles.lazy = 0;
handles.multi = 0;
handles.uni = 0;
handles.noiseType = 'gaussian';
handles.surrNum = 1;
handles.maxEpochs = 1000;
handles.L2Regularization = 0.05;
handles.noCache = 0;
handles.nn = 2;
% load command line input
i = 1;
while true
if i > size(varargin, 2)
break;
end
switch varargin{i}
case {'-g','--rg'}
handles.rg = 1;
case {'-s','--rs'}
handles.rs = 1;
case {'-f','--ft'}
handles.ft = 1;
case {'-a','--aaft'}
handles.aaft = 1;
case {'-i','--iaaft'}
handles.iaaft = 1;
case {'-v','--var'}
handles.var = 1;
case {'-p','--pcvar'}
handles.pcvar = 1;
case {'-d','--vardnn'}
handles.vardnn = 1;
case {'-l','--lazy'}
handles.lazy = 1;
case {'--multi'}
handles.multi = 1;
case {'--uni'}
handles.uni = 1;
case {'--noise'}
handles.noiseType = varargin{i+1};
i = i + 1;
case {'--surrnum'}
handles.surrNum = str2num(varargin{i+1});
i = i + 1;
case {'--outpath'}
handles.outpath = varargin{i+1};
i = i + 1;
case {'--lag'}
handles.lag = str2num(varargin{i+1});
i = i + 1;
case {'--transform'}
handles.transform = str2num(varargin{i+1});
i = i + 1;
case {'--transopt'}
handles.transopt = str2num(varargin{i+1});
i = i + 1;
case {'--format'}
handles.format = str2num(varargin{i+1});
i = i + 1;
case {'--epoch'}
handles.maxEpochs = str2num(varargin{i+1});
i = i + 1;
case {'--l2'}
handles.L2Regularization = str2num(varargin{i+1});
i = i + 1;
case {'--nn'}
handles.nn = str2num(varargin{i+1});
i = i + 1;
case {'--nocache'}
handles.noCache = 1;
case {'--showsig'}
handles.showSig = 1;
case {'-h','--help'}
showUsage();
return;
case {'--version'}
disp([exeName ' version : ' num2str(versionNumber)]);
return;
otherwise
if strcmp(varargin{i}(1), '-')
disp(['bad option : ' varargin{i}]);
i = size(varargin, 2);
handles.commandError = 1;
else
handles.csvFiles = [handles.csvFiles varargin{i}];
end
end
i = i + 1;
end
if handles.multi == 0 && handles.uni == 0
handles.multi = 1;
end
% check command input
if handles.commandError
showUsage();
return;
elseif isempty(handles.csvFiles)
disp('no input files. please specify time-series files.');
showUsage();
return;
end
% process input files
processInputFiles(handles);
end
%%
% show usage function
function showUsage()
global exePath;
global exeName;
disp(['usage: ' exeName ' [options] filename.csv ...']);
disp(' -g, --rg output Random Gaussian (RG) surrogate (<filename>_rg_<variate>_<num>.csv)');
disp(' -s, --rs output Random Shuffling (RS) surrogate (<filename>_rs_<variate>_<num>.csv)');
disp(' -f, --ft output Fourier Transform (FT) surrogate (<filename>_ft_<variate>_<num>.csv)');
disp(' -a, --aaft output Amplitude Adjusted FT (AAFT) surrogate (<filename>_aaft_<variate>_<num>.csv)');
disp(' -i, --iaaft output Iterated AAFT (IAAFT) surrogate (<filename>_iaaft_<variate>_<num>.csv)');
disp(' -v, --var output Vector Auto-Regression (VAR) surrogate (<filename>_var_<variate>_<num>.csv)');
disp(' -p, --pcvar output Principal Component VAR (PCVAR) surrogate (<filename>_pcvar_<variate>_<num>.csv)');
disp(' -d, --vardnn output VAR Deep Neural Network (VARDNN) surrogate (<filename>_vardnn_<variate>_<num>.csv)');
disp(' -l, --lazy output Lazy Learning (LL) surrogate (<filename>_lazy_<variate>_<num>.csv)');
disp(' --multi output multivariate surrogate (default:on)');
disp(' --uni output univariate surrogate (default:off)');
disp(' --noise type noise type for VAR, PCVAR, VARDNN, LL surrogate (default:"gaussian")');
disp(' --surrnum num output surrogate sample number <num> (default:1)');
disp(' --outpath path output files <path> (default:"results")');
disp(' --format type save file format <type> 0:csv, 1:mat(each), 2:mat(all) (default:2)');
disp(' --transform type input signal transform <type> 0:raw, 1:sigmoid (default:0)');
disp(' --transopt num signal transform option <num> (for type 1:centroid value)');
disp(' --lag num time lag <num> for VAR, PCVAR, VARDNN, LL (default:3)');
disp(' --epoch num VARDNN training epoch number <num> (default:1000)');
disp(' --l2 num VARDNN training L2Regularization <num> (default:0.05)');
disp(' --nn num <num>-nearest neighbor for Lazy Learning (default:2)');
disp(' --showsig show input time-series data of <filename>.csv');
disp(' --nocache do not use cache file for VARDNN training');
disp(' --version show version number');
disp(' -h, --help show command line help');
end
%%
% process input files (mail rutine)
%
function processInputFiles(handles)
global exePath;
global exeName;
% init
N = length(handles.csvFiles);
% process each file
for i = 1:N
% init data
X = [];
exSignal = [];
nodeControl = [];
exControl = [];
% load time-series csv or mat file
fname = handles.csvFiles{i};
if ~exist(fname,'file')
disp(['file is not found. ignoring : ' fname]);
continue;
end
[path,name,ext] = fileparts(fname);
if strcmp(ext,'.mat')
f = load(fname);
if isfield(f,'X')
X = f.X;
else
disp(['file does not contain "X" matrix. ignoring : ' fname]);
end
else
T = readtable(fname);
X = table2array(T);
end
nodeNum = size(X,1);
sigLen = size(X,2);
if handles.format==2 % if save format is mat(all)
if i==1
savename = name;
end
else
savename = name;
end
% signal transform raw or not
if handles.transform == 1
[X, sig, c, maxsi, minsi] = convert2SigmoidSignal(X, handles.transopt);
end
% show input time-series
if handles.showSig > 0
figure; plot(X.');
title(['Input Signals : ' strrep(name,'_','-')]);
xlabel('Time Series');
ylabel('Signal Value');
end
% calc VARDNN surrogate
if handles.vardnn > 0
% train VARDNN network
if ~exist('results/cache','dir')
mkdir('results/cache');
end
vardnnFile = [exePath '/results/cache/vardnn-' name '.mat'];
if exist(vardnnFile, 'file') && handles.noCache == 0
disp(['read cache file : ' vardnnFile]);
load(vardnnFile);
else
% layer parameters
net = initMvarDnnNetwork(X, exSignal, nodeControl, exControl, handles.lag);
% training VARDNN network
miniBatchSize = ceil(sigLen / 3);
options = trainingOptions('adam', ...
'ExecutionEnvironment','cpu', ...
'MaxEpochs', handles.maxEpochs, ...
'MiniBatchSize', miniBatchSize, ...
'Shuffle', 'every-epoch', ...
'GradientThreshold', 5,...
'L2Regularization', handles.L2Regularization, ...
'Verbose',false);
disp('start training');
net = trainMvarDnnNetwork(X, exSignal, nodeControl, exControl, net, options);
[time, loss, rsme] = getMvarDnnTrainingResult(net);
disp(['VARDNN training result : rsme=' num2str(rsme)]);
if handles.noCache == 0
save(vardnnFile, 'net', '-v7.3');
end
end
% output result matrix files
if handles.multi > 0
Y = surrogateMvarDnn(X, exSignal, nodeControl, exControl, net, handles.noiseType, handles.surrNum);
saveResultFiles(handles, Y, [savename '_vardnn_multi']);
end
if handles.uni > 0
disp('vardnn_uni combination is not supported.');
end
end
% calc VAR surrogate
if handles.var > 0
if handles.multi > 0
net = initMvarNetwork(X, exSignal, nodeControl, exControl, handles.lag);
Y = surrogateMVAR(X, exSignal, nodeControl, exControl, net, handles.noiseType, handles.surrNum);
saveResultFiles(handles, Y, [savename '_var_multi']);
end
if handles.uni > 0
Y = surrogateAR(X, handles.lag, handles.noiseType, handles.surrNum);
saveResultFiles(handles, Y, [savename '_ar_uni']);
end
end
% calc PCVAR surrogate
if handles.pcvar > 0
if handles.multi > 0
net = initMpcvarNetwork(X, exSignal, nodeControl, exControl, handles.lag);
Y = surrogateMpcvar(X, exSignal, nodeControl, exControl, net, handles.noiseType, handles.surrNum);
saveResultFiles(handles, Y, [savename '_pcvar_multi']);
end
if handles.uni > 0
disp('pcvar_uni combination is not supported.');
end
end
% calc LL surrogate
if handles.lazy > 0
if handles.multi > 0
LL = initLazyLearning(X, exSignal, nodeControl, exControl, handles.lag);
Y = surrogateLazyLearning(X, exSignal, nodeControl, exControl, LL, handles.nn, handles.noiseType, handles.surrNum);
saveResultFiles(handles, Y, [savename '_lazy_multi']);
end
if handles.uni > 0
disp('lazy_uni combination is not supported.');
end
end
% calc Random Gaussian surrogate
if handles.rg > 0
if handles.multi > 0
Y = surrogateMRG(X, handles.surrNum);
saveResultFiles(handles, Y, [savename '_rg_multi']);
end
if handles.uni > 0
Y = surrogateRG(X, handles.surrNum);
saveResultFiles(handles, Y, [savename '_rg_uni']);
end
end
% calc Random Shuffling surrogate
if handles.rs > 0
if handles.multi > 0
Y = surrogateMRS(X, handles.surrNum);
saveResultFiles(handles, Y, [savename '_rs_multi']);
end
if handles.uni > 0
Y = surrogateRS(X, handles.surrNum);
saveResultFiles(handles, Y, [savename '_rs_uni']);
end
end
% calc Fourier Transform surrogate
if handles.ft > 0
if handles.multi > 0
Y = surrogateMFT(X, handles.surrNum);
saveResultFiles(handles, Y, [savename '_ft_multi']);
end
if handles.uni > 0
Y = surrogateFT(X, handles.surrNum);
saveResultFiles(handles, Y, [savename '_ft_uni']);
end
end
% calc AAFT surrogate
if handles.aaft > 0
if handles.multi > 0
Y = surrogateMAAFT(X, handles.surrNum);
saveResultFiles(handles, Y, [savename '_aaft_multi']);
end
if handles.uni > 0
Y = surrogateAAFT(X, handles.surrNum);
saveResultFiles(handles, Y, [savename '_aaft_uni']);
end
end
% calc IAAFT surrogate
if handles.iaaft > 0
if handles.multi > 0
Y = surrogateMIAAFT(X, 100, handles.surrNum);
saveResultFiles(handles, Y, [savename '_iaaft_multi']);
end
if handles.uni > 0
Y = surrogateIAAFT(X, 100, handles.surrNum);
saveResultFiles(handles, Y, [savename '_iaaft_uni']);
end
end
end
end
%%
% output result matrix files
%
function saveResultFiles(handles, Y, outname)
if handles.format == 1
for i=1:size(Y,3)
X = single(squeeze(Y(:,:,i)));
outfname = [handles.outpath '/' outname '_' num2str(i) '.mat'];
save(outfname,'X');
disp(['output mat file : ' outfname]);
end
elseif handles.format == 2
CX = cell(1,size(Y,3)); names = cell(1,size(Y,3));
for i=1:size(Y,3), CX{i}=single(squeeze(Y(:,:,i))); names{i} = [outname '_' num2str(i)]; end
outfname = [handles.outpath '/' outname '_all.mat'];
save(outfname, 'CX', 'names', '-v7.3');
disp(['output mat file : ' outfname]);
else
% output result matrix csv file
for i=1:size(Y,3)
X = squeeze(Y(:,:,i));
outputCsvFile(X, [handles.outpath '/' outname '_' num2str(i) '.csv']);
end
end
% show first sample of output signals
if handles.showSig > 0
X = squeeze(Y(:,:,1));
figure; plot(X.');
title(['First sample of output signals : ' strrep(outname,'_','-')]);
xlabel('Time Series');
ylabel('Signal Value');
end
end
%%
% output csv file function
%
function outputCsvFile(mat, outfname)
T = array2table(mat);
writetable(T,outfname,'WriteVariableNames',false);
disp(['output csv file : ' outfname]);
end