-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathOpenBDLM_main.m
More file actions
404 lines (352 loc) · 14.1 KB
/
Copy pathOpenBDLM_main.m
File metadata and controls
404 lines (352 loc) · 14.1 KB
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
function [data, model, estimation, misc] = OpenBDLM_main(UserInput)
%OPENBDLM_MAIN Process control for OpenBDLM
%
% SYNOPSIS:
% [data, model, estimation, misc] = OPENBDLM_MAIN(UserInput)
%
% INPUT:
% UserInput - character or cell array of characters (optional)
%
% If UserInput is not provided, the function runs
% in interactive mode, in which online user's
% interactions from the command line is
% required to perform the analysis.
%
% If UserInput is a character array, it should be
% the name of a configuration file name (see doc)
% The configuration file is only used to
% initialize the project. The function then
% switches to the interactive mode.
%
% If UserInput is a cell array, it should contain
% all the inputs required for analysis (see doc).
% The is the batch mode, in which the function runs
% silently by automatically reading pre-loaded
% commands.
%
% OUTPUT:
% data - structure
% see documentation for details about the fields
% in structure "data"
%
% model - structure
% see documentation for details about the fields
% in structure "model"
%
% estimation - structure
% see documentation for details about the fields
% in structure "estimation"
%
% misc - structure
% see documentation for details about the fields
% in structure "misc"
%
% DESCRIPTION:
% OPENBDLM_MAIN is the process control function for OpenBDLM.
% OpenBDLM is an open-source software for performing structural
% health monitoring using Bayesian Dynamic Linear Models
%
% EXAMPLES:
% [data, model, estimation, misc] = OPENBDLM_MAIN()
% [data, model, estimation, misc] = OPENBDLM_MAIN('CFG_TEST1.m')
% [data, model, estimation, misc] = OPENBDLM_MAIN({'''CFG_TEST1.m''', '31'})
%
%
% EXTERNAL FUNCTIONS CALLED:
% DataLoader, saveDataBinary, verificationDataStructure, SimulateData
% ModelConfiguration, buildModel, displayModelMatrices
% modifyInitialHiddenStates, modifyTrainingPeriod, learnModelParameters,
% modifyModelParameters, chooseIsDataSimulation, chooseProjectName
% displayProjects, initializeProject, printConfigurationFile,
% printProjectDateCreation, saveProject, testFileExistence,
% computeInitialHiddenStates, plotData, plotEstimations
%
% SUBFUNCTIONS:
% N/A
%
% See also
% DATALOADER, STATEESTIMATION, LEARNMODELPARAMETERS, SAVEDATABINARY,
% SIMULATEDATA, MODELCONFIGURATION, BUILDMODEL, DISPLAYMODELMATRICES
% MODIFYINITIALHIDDENSTATES, MODIFYTRAININGPERIOD,VERIFICATIONDATASTRUCTURE
% MODIFYMODELPARAMETERS, CHOOSEISDATASIMULATION, CHOOSEPROJECTNAME
% DISPLAYPROJECTS, INITIALIZEPROJECT, PRINTCONFIGURATIONFILE,
% PRINTPROJECTDATECREATION, SAVEPROJECT, TESTFILEEXISTENCE,
% COMPUTEINITIALHIDDENSTATES, PLOTDATA, PLOTESTIMATIONS
% AUTHORS:
% James-A Goulet, Luong Ha Nguyen, Ianis Gaudot
%
% Email: <james.goulet@polymtl.ca>
% Website: <http://www.polymtl.ca/expertises/goulet-james-alexandre>
%
% REFERENCES:
% [1] Goulet, J.-A., 2017, Bayesian dynamic linear models for
% structural health monitoring,
% Structural Control and Health Monitoring, Vol. 24, Issue 12.
%
% [2] Goulet, J.-A., Koo K., 2017, Empirical validation of Bayesian
% dynamic linear models in the context of structural health monitoring,
% Journal of Bridge Engineering, Vol.23, Issue 2.
%
% [3] Nguyen, L. H., Goulet J.-A., 2018, Anomaly detection with the
% Switching Kalman Filter for structural health monitoring,
% Structural Control and Health Monitoring, Vol. 25, Issue 4.
%
% MATLAB VERSION:
% Tested on 9.1.0.441655 (R2016b)
%
% DATE CREATED:
% June 27, 2018
%
% DATE LAST UPDATE:
% October 30, 2018
%--------------------BEGIN CODE ----------------------
%% Version of the program
OpenBDLMversion = '1.0';
%% Warning if multiple conflictous installations
%findDuplicateInstallation
%% Verify that the program is running from the right location
currentFolder = cd;
isFileExist = (exist(fullfile(currentFolder, mfilename), 'file') == 2);
if ~isFileExist
disp(' ');
error('%s is called from the wrong directory.', mfilename)
end
%% Read arguments and set internal variables
if exist('UserInput', 'var')
[misc]=setInternalVars(UserInput);
else
[misc]=setInternalVars();
end
if isempty(misc)
disp(' ');
error('Unrecognized input argument.')
end
%% Create log file to record messages during program run
[misc] = createLogFile(misc);
%% Set default options
[misc]=setDefaultOptions(misc);
if misc.internalVars.isQuiet
% output messages in a specific log file
fileID=fopen(misc.internalVars.logFileName, 'a');
else
% output message on screen
fileID=1;
end
if misc.internalVars.InteractiveMode.isInteractiveMode || ...
misc.internalVars.BatchMode.isBatchMode
%Set default font type
set(0,'DefaultAxesFontname','Helvetica')
%Set default font size
set(0,'DefaultAxesFontSize',20)
%Set display format
format short g
%% Display welcome menu
welcomeOpenBDLM(misc, 'version', OpenBDLMversion)
incTest=0;
MaxFailAttempts = 4;
isAnswerCorrect = false;
while ~isAnswerCorrect
incTest=incTest+1;
if incTest > MaxFailAttempts ; error(['Too many failed ', ...
'attempts (', num2str(MaxFailAttempts) ').']) ; end
fprintf(fileID, '\n');
fprintf(fileID,'- Start a new project: \n');
fprintf(fileID, '\n');
fprintf(fileID,[' * ', ...
'Enter a configuration filename \n']);
fprintf(fileID, ' 0 -> Interactive tool \n');
fprintf(fileID, '\n');
%% Display existing & saved projects
[~] = displayProjects(misc);
fprintf(fileID, ['- Type D to Delete project(s), ', ...
'V for Version control, Q to Quit.\n']);
if misc.internalVars.BatchMode.isBatchMode
UserChoice= ...
eval(char(misc.internalVars.BatchMode.Answers{...
misc.internalVars.BatchMode.AnswerIndex}));
UserChoice = num2str(UserChoice);
if ischar(UserChoice)
fprintf(fileID, ' %s \n', UserChoice);
else
fprintf(fileID, ' %s \n', num2str(UserChoice));
end
else
fprintf(fileID,'\n');
UserChoice = input(' choice >> ', 's');
end
fprintf(fileID,'\n');
% Remove space and quotes
UserChoice=strrep(UserChoice,'''',''); % remove quotes
UserChoice=strrep(UserChoice,'"','' ); % remove double quotes
UserChoice=strrep(UserChoice, ' ','' ); % remove spaces
if isempty(UserChoice)
continue
elseif isnan(str2double(UserChoice))
misc.internalVars.BatchMode.AnswerIndex = ...
misc.internalVars.BatchMode.AnswerIndex+1;
if strcmpi('D',UserChoice) && length(UserChoice) == 1
%% Delete project file(s)
piloteDeleteProject(misc)
incTest=0;
continue
elseif strcmpi('V', UserChoice) && length(UserChoice) == 1
%% Version control
piloteVersionControl(misc)
data=struct; model=struct; estimation=struct; misc=struct;
diary off
return
elseif strcmpi('Q', UserChoice) && length(UserChoice) == 1
%% Quit the program
disp(' Done ! See you soon !');
if misc.internalVars.isQuiet
fclose(fileID);
else
diary off
end
data=struct; model=struct; estimation=struct; misc=struct;
return
else
%% Load project from configuration file
[data, model, estimation, misc]= ...
loadConfigurationFile(misc, UserChoice);
isAnswerCorrect = true;
end
elseif round(str2double(UserChoice)) == 0
misc.internalVars.BatchMode.AnswerIndex = ...
misc.internalVars.BatchMode.AnswerIndex+1;
%% Load a project from interactive mode
[data, model, estimation, misc]=loadInteractive(misc);
isAnswerCorrect = true;
else
misc.internalVars.BatchMode.AnswerIndex = ...
misc.internalVars.BatchMode.AnswerIndex+1;
%% Load project from project file
UserChoice = round(str2double(UserChoice));
[data, model, estimation, misc]= ...
loadProjectFile(misc, UserChoice);
if ~isempty(model)
isAnswerCorrect = true;
end
end
end
elseif misc.internalVars.ReadFromConfigFileMode.isReadFromConfigFileMode
configFileName = ...
misc.internalVars.ReadFromConfigFileMode.ConfigFilename;
%% Load project from configuration file
[data, model, estimation, misc]= loadConfigurationFile(misc, ...
configFileName);
end
incTest=0;
MaxFailAttempts = 4;
while(1)
incTest=incTest+1;
if incTest > MaxFailAttempts ; error(['Too many failed ', ...
'attempts (', num2str(MaxFailAttempts) ').']) ; end
%% Display menu
[PossibleAnswers]=displayMenuOpenBDLM(misc);
%% Read user's choice
if misc.internalVars.BatchMode.isBatchMode
user_inputs=eval( ...
char(misc.internalVars.BatchMode.Answers{...
misc.internalVars.BatchMode.AnswerIndex}));
user_inputs = num2str(user_inputs);
if ischar(user_inputs)
fprintf(fileID, ' %s \n', user_inputs);
else
fprintf(fileID, ' %s \n', num2str(user_inputs));
end
else
user_inputs = input(' choice >> ', 's');
end
% Remove space and quotes
user_inputs=strrep(user_inputs,'''',''); % remove quotes
user_inputs=strrep(user_inputs,'"','' ); % remove double quotes
user_inputs=strrep(user_inputs, ' ','' ); % remove spaces
if isnan(str2double(user_inputs))
if strcmpi(user_inputs, 'Q')
%% Save project and quit
fprintf(fileID, '\n');
saveProject(model, estimation, misc, ...
'FilePath', misc.internalVars.ProjectPath)
[misc]=saveResultsMAT(data, model, estimation, ...
misc, 'FilePath', misc.internalVars.ResultsPath, ...
'isForceOverwrite', true);
fprintf(fileID, '\n');
disp(' Done ! See you soon !');
if misc.internalVars.isQuiet
fclose(fileID);
else
diary off
end
return
else
fprintf(fileID, '\n');
fprintf(fileID, ' wrong input \n');
continue
end
elseif ~ischar(user_inputs) && ...
~any(ismember(PossibleAnswers, user_inputs ))
fprintf(fileID, '\n');
fprintf(fileID, ' wrong input \n');
continue
else
misc.internalVars.BatchMode.AnswerIndex = ...
misc.internalVars.BatchMode.AnswerIndex+1;
user_inputs = round(str2double(user_inputs));
if user_inputs==1
%% Learn model parameters
[data, model, estimation, misc]= ...
piloteOptimization(data, model, estimation, misc);
incTest=0;
elseif user_inputs==2
%% Initial hidden states estimation
[data, model, estimation, misc]= ...
piloteInitialStateEstimation(data, model, estimation, misc);
incTest=0;
elseif user_inputs==3
%% Hidden states estimation
[data, model, estimation, misc]= ...
piloteStateEstimation(data, model, estimation, misc);
incTest=0;
elseif user_inputs==11
%% Modify model parameters
[model, misc]= ...
piloteModifyModelParameters(data, model, estimation, misc);
incTest=0;
elseif user_inputs==12
%% Modify initial hidden states
[model, misc]= ...
piloteModifyInitialStates(data, model, estimation, misc);
incTest=0;
elseif user_inputs==13
%% Modify training period
[misc]=piloteModifyTrainingPeriod(data, model, estimation, misc);
incTest=0;
elseif user_inputs==14
%% Plot tools
[misc] = pilotePlot(data, model, estimation, misc);
incTest=0;
elseif user_inputs==15
%% Display model matrices
piloteDisplayModelMatrices(data, model, estimation, misc)
incTest=0;
elseif user_inputs==16
%% Create synthetic data
[data, model, estimation, misc]= ...
piloteSimulateData(data, model, estimation, misc);
incTest=0;
elseif user_inputs==17
%% Export
[misc] = piloteExport(data, model, estimation, misc);
incTest=0;
elseif user_inputs==18
%% Export current options in a configuration file format
[misc] = printOptions(misc);
incTest=0;
else
disp(' ')
disp(' wrong input')
end
end
end
end