forked from NIVANorge/Mobius
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathincaview_compatibility.h
376 lines (326 loc) · 10.9 KB
/
incaview_compatibility.h
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
#if !defined(INCAVIEW_COMPATIBILITY_H)
#if !defined(INCAVIEW_INCLUDE_OPTIMIZER)
#define INCAVIEW_INCLUDE_OPTIMIZER 0
#endif
#if INCAVIEW_INCLUDE_OPTIMIZER
#include "Calibration/Optimizer/optimizer.h"
#endif
#if !defined(INCAVIEW_INCLUDE_GLUE)
#define INCAVIEW_INCLUDE_GLUE 0
#endif
#if INCAVIEW_INCLUDE_GLUE
#include "Calibration/GLUE/glue.h"
#endif
#if !defined(INCAVIEW_INCLUDE_MCMC)
#define INCAVIEW_INCLUDE_MCMC 0
#endif
#if INCAVIEW_INCLUDE_MCMC
#include "Calibration/MCMC/mobius_mcmc.h"
#endif
#include "mobius_json_io.cpp"
#include "mobius_sqlite3_io.cpp"
enum incaview_run_mode
{
IncaviewRunMode_Run,
IncaviewRunMode_ConvertParameters,
IncaviewRunMode_FillParameterFile,
IncaviewRunMode_RunOptimization,
IncaviewRunMode_RunGLUE,
IncaviewRunMode_RunMCMC,
};
struct incaview_commandline_arguments
{
incaview_run_mode Mode;
const char *InputFileName;
const char *ParameterInFileName;
const char *ParameterOutFileName;
const char *CalibrationScriptName;
const char *Exename;
};
static void
ParseIncaviewCommandline(int argc, char **argv, incaview_commandline_arguments *Args)
{
bool CorrectUse = false;
char *Exename = argv[0];
//NOTE: Very rudimentary attempt to trim off any leading directories. In case somebody called the exe from another directory while creating the database.
int LastSlashPos = -1;
int Pos = 0;
for(char *C = Exename; *C != 0; ++C)
{
if(*C == '\\' || *C == '/') LastSlashPos = Pos;
++Pos;
}
Exename = Exename + (LastSlashPos + 1);
Args->Exename = Exename;
if(argc == 4)
{
if(strcmp(argv[1], "run") == 0)
{
Args->Mode = IncaviewRunMode_Run;
Args->InputFileName = argv[2];
Args->ParameterInFileName = argv[3];
CorrectUse = true;
}
else if(strcmp(argv[1], "convert_parameters") == 0)
{
Args->Mode = IncaviewRunMode_ConvertParameters;
Args->ParameterInFileName = argv[2];
Args->ParameterOutFileName = argv[3];
CorrectUse = true;
}
else if(strcmp(argv[1], "fill_parameter_file") == 0)
{
Args->Mode = IncaviewRunMode_FillParameterFile;
Args->ParameterInFileName = argv[2];
Args->ParameterOutFileName = argv[3];
CorrectUse = true;
}
}
else if(argc == 6)
{
#if INCAVIEW_INCLUDE_OPTIMIZER
if(strcmp(argv[1], "run_optimizer") == 0)
{
Args->Mode = IncaviewRunMode_RunOptimization;
Args->InputFileName = argv[2];
Args->ParameterInFileName = argv[3];
Args->CalibrationScriptName = argv[4];
Args->ParameterOutFileName = argv[5];
CorrectUse = true;
}
#endif
#if INCAVIEW_INCLUDE_GLUE
if(strcmp(argv[1], "run_glue") == 0)
{
Args->Mode = IncaviewRunMode_RunGLUE;
Args->InputFileName = argv[2];
Args->ParameterInFileName = argv[3];
Args->CalibrationScriptName = argv[4];
Args->ParameterOutFileName = argv[5];
CorrectUse = true;
}
#endif
#if INCAVIEW_INCLUDE_GLUE
if(strcmp(argv[1], "run_mcmc") == 0)
{
Args->Mode = IncaviewRunMode_RunMCMC;
Args->InputFileName = argv[2];
Args->ParameterInFileName = argv[3];
Args->CalibrationScriptName = argv[4];
Args->ParameterOutFileName = argv[5];
CorrectUse = true;
}
#endif
}
if(!CorrectUse)
{
MOBIUS_PARTIAL_ERROR("Incorrect use of the executable. Correct use is one of: " << std::endl);
MOBIUS_PARTIAL_ERROR(" " << Args->Exename << " run <inputfile(.dat or .json)> <parameterfile(.db or .dat or .json)>" << std::endl);
MOBIUS_PARTIAL_ERROR(" " << Args->Exename << " convert_parameters <parameterfilein(.db or .dat or .json)> <parameterfileout(.db or .dat or .json)>" << std::endl);
MOBIUS_PARTIAL_ERROR(" " << Args->Exename << " fill_parameter_file <parameterfilein(.dat)> <parameterfileout(.dat)>" << std::endl);
#if INCAVIEW_INCLUDE_OPTIMIZER
MOBIUS_PARTIAL_ERROR(" " << Args->Exename << " run_optimizer <inputfile(.dat or .json)> <parameterfile(.db or .dat or .json)> <calibrationscript(.dat)> <parameterfileout(.dat or .db or .json)>" << std::endl);
#endif
#if INCAVIEW_INCLUDE_GLUE
MOBIUS_PARTIAL_ERROR(" " << Args->Exename << " run_glue <inputfile(.dat or .json)> <parameterfile(.db or .dat or .json)> <calibrationscript(.dat)> <calibrationresults(.db)>" << std::endl);
#endif
#if INCAVIEW_INCLUDE_MCMC
MOBIUS_PARTIAL_ERROR(" " << Args->Exename << " run_mcmc <inputfile(.dat or .json)> <parameterfile(.db or .dat or .json)> <calibrationscript(.dat)> <calibrationresults(.dat)>" << std::endl);
#endif
MOBIUS_FATAL_ERROR("");
}
}
static int
IncaviewParseFileType(const char *Filename)
{
int Len = strlen(Filename);
int At = Len - 1;
while(At >= 0)
{
char C = Filename[At];
if(C == '.') break;
At--;
}
//NOTE: At now points to the last '.' in the file name
const char *Extension = Filename + At + 1;
if(strcmp(Extension, "db") == 0) return 0;
if(strcmp(Extension, "dat") == 0) return 1;
if(strcmp(Extension, "json") == 0) return 2;
MOBIUS_FATAL_ERROR("ERROR: Unsupported file extension: " << Extension << " for file " << Filename << std::endl);
}
static void
ReadInputDependenciesFromFile_Ext(mobius_model *Model, const char *Filename)
{
int FileType = IncaviewParseFileType(Filename);
if(FileType == 0)
{
MOBIUS_FATAL_ERROR("ERROR: We do not currently support loading inputs from databases" << std::endl);
}
else if(FileType == 1)
{
ReadInputDependenciesFromFile(Model, Filename);
}
else if(FileType == 2)
{
ReadInputDependenciesFromJson(Model, Filename);
}
}
static void
ReadInputsFromFile_Ext(mobius_data_set *DataSet, const char *Filename)
{
int FileType = IncaviewParseFileType(Filename);
if(FileType == 0)
{
MOBIUS_FATAL_ERROR("ERROR: We do not currently support loading inputs from databases" << std::endl);
}
else if(FileType == 1)
{
ReadInputsFromFile(DataSet, Filename);
}
else if(FileType == 2)
{
ReadInputsFromJson(DataSet, Filename);
}
}
static void
ReadParametersFromFile_Ext(mobius_data_set *DataSet, const char *Filename)
{
int FileType = IncaviewParseFileType(Filename);
if(FileType == 0)
{
ReadParametersFromDatabase(DataSet, Filename);
}
else if(FileType == 1)
{
ReadParametersFromFile(DataSet, Filename);
}
else if(FileType == 2)
{
ReadParametersFromJson(DataSet, Filename);
}
}
static void
WriteParametersToFile_Ext(mobius_data_set *DataSet, const char *Filename, const char *Exename)
{
int FileType = IncaviewParseFileType(Filename);
if(FileType == 0)
{
WriteParametersToDatabase(DataSet, Filename, Exename);
}
else if(FileType == 1)
{
WriteParametersToFile(DataSet, Filename);
}
else if(FileType == 2)
{
WriteParametersToJson(DataSet, Filename);
}
}
static void
EnsureModelComplianceWithIncaviewCommandline(mobius_model *Model, incaview_commandline_arguments *Args)
{
if(Args->Mode == IncaviewRunMode_Run || Args->Mode == IncaviewRunMode_RunOptimization || Args->Mode == IncaviewRunMode_RunGLUE || Args->Mode == IncaviewRunMode_RunMCMC)
{
ReadInputDependenciesFromFile_Ext(Model, Args->InputFileName);
}
}
static void
RunDatasetAsSpecifiedByIncaviewCommandline(mobius_data_set *DataSet, incaview_commandline_arguments *Args)
{
if(Args->Mode == IncaviewRunMode_Run)
{
//TODO: would it be better to provide these as arguments too?
const char *ResultDbFileName = "results.db";
const char *InputDbFileName = "inputs.db"; //NOTE: This is only for writing inputs TO so that they can be read by INCAView. Inputs are always read in from the provided .dat file.
const char *ResultJsonFileName = "results.json";
const char *InputJsonFileName = "inputs.json";
int Type = IncaviewParseFileType(Args->ParameterInFileName);
ReadParametersFromFile_Ext(DataSet, Args->ParameterInFileName);
ReadInputsFromFile_Ext(DataSet, Args->InputFileName);
RunModel(DataSet);
//TODO: Delete existing databases if they exist? (right now it is handled by incaview, but it could be confusing if somebody runs the exe manually)
if(Type==0)
{
std::cout << "Model run finished. Writing result data to " << ResultDbFileName << std::endl;
WriteResultsToDatabase(DataSet, ResultDbFileName);
WriteInputsToDatabase(DataSet, InputDbFileName);
}
else
{
//TODO: This should be a command line option instead maybe.
std::cout << "Model run finished. Writing result data to " << ResultJsonFileName << std::endl;
WriteResultsToJson(DataSet, ResultJsonFileName);
WriteInputsToJson(DataSet, InputJsonFileName);
}
}
#if INCAVIEW_INCLUDE_OPTIMIZER
else if(Args->Mode == IncaviewRunMode_RunOptimization)
{
ReadParametersFromFile_Ext(DataSet, Args->ParameterInFileName);
ReadInputsFromFile_Ext(DataSet, Args->InputFileName);
optimization_setup Setup;
ReadOptimizationSetup(&Setup, Args->CalibrationScriptName);
auto Result = RunOptimizer(DataSet, &Setup);
std::cout << std::endl;
PrintOptimizationResult(&Setup, Result);
WriteOptimalParametersToDataSet(DataSet, &Setup, Result);
WriteParametersToFile_Ext(DataSet, Args->ParameterOutFileName, Args->Exename);
}
#endif
#if INCAVIEW_INCLUDE_GLUE
else if(Args->Mode == IncaviewRunMode_RunGLUE)
{
ReadParametersFromFile_Ext(DataSet, Args->ParameterInFileName);
ReadInputsFromFile_Ext(DataSet, Args->InputFileName);
glue_setup Setup;
glue_results Results;
ReadGLUESetupFromFile(&Setup, Args->CalibrationScriptName);
timer RunGlueTimer = BeginTimer();
RunGLUE(DataSet, &Setup, &Results);
u64 Ms = GetTimerMilliseconds(&RunGlueTimer);
std::cout << "GLUE finished. Running the model " << Setup.NumRuns << " times with " << Setup.NumThreads << " threads took " << Ms << " milliseconds." << std::endl;
WriteGLUEResultsToDatabase(Args->ParameterOutFileName, &Setup, &Results, DataSet); //NOTE: misleading name, it is not a parameter file, it is a GLUE results database.
}
#endif
#if INCAVIEW_INCLUDE_MCMC
else if(Args->Mode == IncaviewRunMode_RunMCMC)
{
ReadParametersFromFile_Ext(DataSet, Args->ParameterInFileName);
ReadInputsFromFile_Ext(DataSet, Args->InputFileName);
mcmc_setup Setup = {};
ReadMCMCSetupFromFile(&Setup, Args->CalibrationScriptName);
mcmc_results Results;
timer MCMCTimer = BeginTimer();
RunMCMC(DataSet, &Setup, &Results);
u64 Ms = GetTimerMilliseconds(&MCMCTimer);
std::cout << "Total MCMC run time : " << Ms << " milliseconds." << std::endl;
std::cout << "Acceptance rate: " << Results.AcceptanceRate << std::endl;
if(Setup.Algorithm == MCMCAlgorithm_DifferentialEvolution)
{
arma::cube& Draws = Results.DrawsOut;
Draws.save(Args->ParameterOutFileName, arma::arma_ascii);
}
else
{
arma::mat& Draws2 = Results.DrawsOut2;
Draws2.save(Args->ParameterOutFileName, arma::arma_ascii);
}
}
#endif
else if(Args->Mode == IncaviewRunMode_ConvertParameters)
{
ReadParametersFromFile_Ext(DataSet, Args->ParameterInFileName);
WriteParametersToFile_Ext(DataSet, Args->ParameterOutFileName, Args->Exename);
}
else if(Args->Mode == IncaviewRunMode_FillParameterFile)
{
ReadParametersFromFile(DataSet, Args->ParameterInFileName);
if(!DataSet->ParameterData)
{
AllocateParameterStorage(DataSet);
}
WriteParametersToFile(DataSet, Args->ParameterOutFileName);
}
}
#define INCAVIEW_COMPATIBILITY_H
#endif