-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSensitivityViewWindow.cpp
373 lines (271 loc) · 11.2 KB
/
SensitivityViewWindow.cpp
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
#include "MobiView.h"
// NOTE: A little bad we have to re-include this here just to generate the Run icon.
#define IMAGECLASS IconImg3
#define IMAGEFILE <MobiView/images.iml>
#include <Draw/iml.h>
StatPlotCtrl::StatPlotCtrl()
{
CtrlLayout(*this);
}
SensitivityViewWindow::SensitivityViewWindow()
{
CtrlLayout(*this, "MobiView perturbation view");
Sizeable().Zoomable();
EditSteps.Min(2);
EditSteps.SetData(5);
PushRun.SetImage(IconImg3::Run());
PushRun.WhenPush = THISBACK(Run);
RunProgress.Hide();
StatPlot.SelectStat.Add("(none)");
#define SET_SETTING(Handle, Name, Type) \
StatPlot.SelectStat.Add(Name);
#define SET_RES_SETTING(Handle, Name, Type) SET_SETTING(Handle, Name, Type)
#include "SetStatSettings.h"
#undef SET_SETTING
#undef SET_RES_SETTING
StatPlot.SelectStat.SetIndex(0);
MainHorizontal.Horz();
MainHorizontal.Add(Plot);
MainHorizontal.Add(StatPlot);
MainHorizontal.SetPos(7500, 0);
Add(MainHorizontal.VSizePos(52, 32).HSizePos());
}
void
SensitivityViewWindow::Update()
{
ErrorLabel.SetText("");
ParamLabel.SetText("");
int SelectedRow = ParentWindow->FindSelectedParameterRow();
indexed_parameter &CurrentParameter = ParentWindow->CurrentSelectedParameter;
if((SelectedRow == -1) || !CurrentParameter.Valid)
{
ErrorLabel.SetText("Select a parameter in the main view");
EditMin.SetData(Null);
EditMax.SetData(Null);
return;
}
if(CurrentParameter.Type != ParameterType_Double)
{
//TODO: Allow UInts too?
ErrorLabel.SetText("Can only perturb parameters of type double");
EditMin.SetData(Null);
EditMax.SetData(Null);
return;
}
String ParUnit = ParentWindow->Params.ParameterView.Get(SelectedRow, Id("__unit"));
String LabelText = Format("%s [%s] ", CurrentParameter.Name.data(), ParUnit);
LabelText << MakeParameterIndexString(CurrentParameter);
ParamLabel.SetText(LabelText);
double Min = ParentWindow->Params.ParameterView.Get(SelectedRow, Id("__min"));
double Max = ParentWindow->Params.ParameterView.Get(SelectedRow, Id("__max"));
EditMin.SetData(Min);
EditMax.SetData(Max);
}
void
SensitivityViewWindow::Run()
{
double Min = EditMin.GetData();
double Max = EditMax.GetData();
int SelectedRow = ParentWindow->FindSelectedParameterRow();
ErrorLabel.SetText("");
indexed_parameter &CurrentParameter = ParentWindow->CurrentSelectedParameter;
if(SelectedRow == -1 || !CurrentParameter.Valid)
{
ErrorLabel.SetText("Select a parameter in the main view");
return;
}
if(IsNull(Min) || IsNull(Max))
{
ErrorLabel.SetText("Min and Max must be real values");
return;
}
if(Max <= Min)
{
ErrorLabel.SetText("Max must be larger than Min");
return;
}
ParentWindow->Plotter.GatherCurrentPlotSetup(Plot.PlotSetup);
if(Plot.PlotSetup.SelectedResults.size() != 1 || Plot.PlotSetup.SelectedInputs.size() > 1)
{
ErrorLabel.SetText("This currently only works with a single selected result series, and at most one input series");
return;
}
for(int Idx = 0; Idx < Plot.PlotSetup.SelectedIndexes.size(); ++Idx)
{
if(Plot.PlotSetup.SelectedIndexes[Idx].size() != 1 && Plot.PlotSetup.IndexSetIsActive[Idx])
{
ErrorLabel.SetText("This currently only works with a single index combination for the result");
return;
}
}
if(Plot.PlotSetup.YAxisMode == YAxis_Normalized)
Plot.PlotSetup.YAxisMode = YAxis_Regular;
Plot.PlotSetup.MajorMode = MajorMode_Regular;
int NSteps = EditSteps.GetData();
double Stride = (Max - Min) / (double) (NSteps - 1);
void *DataSetCopy = ParentWindow->ModelDll.CopyDataSet(ParentWindow->DataSet, true, true);
ParentWindow->CheckDllUserError();
char TimeStr[256];
ParentWindow->ModelDll.GetInputStartDate(DataSetCopy, TimeStr);
Time InputStartTime;
StrToTime(InputStartTime, TimeStr);
uint64 InputTimesteps = ParentWindow->ModelDll.GetInputTimesteps(DataSetCopy);
RunProgress.Set(0);
RunProgress.SetTotal(NSteps);
RunProgress.Show();
this->ProcessEvents();
int GUIUpdateFreq = std::min(10, NSteps / 10);
GUIUpdateFreq = std::max(1, GUIUpdateFreq);
Plot.ClearAll(false);
Plot.PlotData.Data.reserve(2*NSteps + 3);
StatPlot.Plot.RemoveAllSeries();
StatPlot.Plot.SetTitle(" ");
StatPlot.Plot.SetLabelX(" ");
StatPlot.Plot.SetLabelY(" ");
//TODO: This should say indexes too!
Plot.SetTitle(Format("Sensitivity of \"%s\" [%s] to %s",
Plot.PlotSetup.SelectedResults[0].data(),
ParentWindow->ModelDll.GetResultUnit(ParentWindow->DataSet, Plot.PlotSetup.SelectedResults[0].data()),
ParamLabel.GetText()));
bool HasInput = Plot.PlotSetup.SelectedInputs.size() == 1;
double *StatData = Plot.PlotData.Allocate(NSteps).data();
double *ParValues = Plot.PlotData.Allocate(NSteps).data();
for(int NStep = 0; NStep < NSteps; ++NStep)
{
StatData[NStep] = Null;
ParValues[NStep] = Min + ((double)NStep)*Stride; //TODO: Also allow selection of logarithmic spacing
}
double *InputYValues;
if(HasInput)
{
double *InputXValues = Plot.PlotData.Allocate(InputTimesteps).data();
ComputeXValues(InputStartTime, InputStartTime, InputTimesteps, ParentWindow->TimestepSize, InputXValues);
InputYValues = Plot.PlotData.Allocate(InputTimesteps).data();
String Legend;
String Unit;
ParentWindow->GetSingleSelectedInputSeries(Plot.PlotSetup, DataSetCopy, Plot.PlotSetup.SelectedInputs[0], Legend, Unit, InputYValues, false);
//NullifyNans(InputYValues, InputTimesteps);
Color InputColor(0, 130, 200);
Plot.AddPlot(Legend, Unit, InputXValues, InputYValues, InputTimesteps, true, InputStartTime, InputStartTime, ParentWindow->TimestepSize, 0.0, 0.0, InputColor);
//Plot.FormatAxes(MajorMode_Regular, 0, InputStartTime, ParentWindow->TimestepSize);
}
String StatName = StatPlot.SelectStat.Get();
if(StatName != "(none)")
{
Color StatColor(0, 130, 200);
StatPlot.Plot.AddSeries(ParValues, StatData, NSteps).MarkBorderColor(StatColor).MarkColor(StatColor).Stroke(1.5, StatColor).Opacity(0.5).MarkStyle<CircleMarkPlot>();
StatPlot.Plot.SetLabels(CurrentParameter.Name.data(), StatName);
StatPlot.Plot.ShowLegend(false);
StatPlot.Plot.SetMouseHandling(false, false);
//This is a little stupid, but whatever...
Size PlotReticleSize = GetTextSize("00000", StatPlot.Plot.GetReticleFont());
Size PlotUnitSize = GetTextSize("[dummy]", StatPlot.Plot.GetLabelsFont());
StatPlot.Plot.SetPlotAreaLeftMargin(PlotReticleSize.cx + PlotUnitSize.cy + 20);
StatPlot.Plot.SetPlotAreaBottomMargin(PlotReticleSize.cy + PlotUnitSize.cy + 20);
StatPlot.Plot.SetXYMin(Min);
StatPlot.Plot.SetRange(Max-Min);
StatPlot.Plot.SetMajorUnitsNum(std::min(NSteps-1, 9)); //TODO: This should be better!
}
else
{
StatPlot.Plot.SetTitle("Select a statistic to display");
}
Time GofStartTime;
Time GofEndTime;
bool HasResidual = false;
int NStep = 0;
for(int NStep = 0; NStep < NSteps; ++NStep)
{
double Val = ParValues[NStep];
// Write the value into the DataSet
SetParameterValue(CurrentParameter, DataSetCopy, Val, ParentWindow->ModelDll);
// Run the model
ParentWindow->ModelDll.RunModel(DataSetCopy, -1);
// Extract the result data and do the plotting
uint64 ResultTimesteps = ParentWindow->ModelDll.GetTimesteps(DataSetCopy);
ParentWindow->ModelDll.GetStartDate(DataSetCopy, TimeStr);
Time ResultStartTime;
StrToTime(ResultStartTime, TimeStr);
//TODO: It is wasteful to do this for each loop!!
double *ResultXValues = Plot.PlotData.Allocate(ResultTimesteps).data();
ComputeXValues(InputStartTime, ResultStartTime, ResultTimesteps, ParentWindow->TimestepSize, ResultXValues);
double *ResultYValues = Plot.PlotData.Allocate(ResultTimesteps).data();
String Legend;
String Unit;
ParentWindow->GetSingleSelectedResultSeries(Plot.PlotSetup, DataSetCopy, Plot.PlotSetup.SelectedResults[0], Legend, Unit, ResultYValues);
//NullifyNans(ResultYValues, ResultTimesteps);
//Note: override the legend:
Legend = Format("%g", Val);
Unit = Null; //NOTE: to avoid it showing in the legend (ideally we want to be able to turn that off).
Color GraphColor = GradientColor(Color(245, 130, 48), Color(145, 30, 180), NStep, NSteps);
//NOTE: It doesn't matter that we pass 0 as min and max since we disallow normalized Y
//axis above.
Plot.AddPlot(Legend, Unit, ResultXValues, ResultYValues, ResultTimesteps, false, InputStartTime, ResultStartTime, ParentWindow->TimestepSize, 0.0, 0.0, GraphColor);
//TODO: There is a lot of stuff in the following call that it is wasteful to do for
//each loop!
Plot.FormatAxes(MajorMode_Regular, 0, InputStartTime, ParentWindow->TimestepSize);
Plot.Refresh();
residual_stats ResidualStats = {};
timeseries_stats TimeseriesStats = {};
//TODO: It is a tiny bit inefficient to do the string comparison here every time, but
//it should be fine
if(StatName != "(none)")
{
int64 GofOffset;
int64 GofTimesteps;
ParentWindow->GetGofOffsets(ResultStartTime, ResultTimesteps, GofStartTime, GofEndTime, GofOffset, GofTimesteps);
int64 ResultOffset = TimestepsBetween(InputStartTime, ResultStartTime, ParentWindow->TimestepSize);
#define SET_SETTING(Handle, Name, Type) \
else if(Name == StatName) \
{ \
ComputeTimeseriesStats(TimeseriesStats, ResultYValues+GofOffset, GofTimesteps, ParentWindow->StatSettings, false); \
StatData[NStep] = TimeseriesStats.Handle; \
}
//TODO: Use the GOF interval!
#define SET_RES_SETTING(Handle, Name, Type) \
else if(Name == StatName) \
{ \
if(HasInput) \
{ \
ComputeResidualStats(ResidualStats, InputYValues+ResultOffset+GofOffset, ResultYValues+GofOffset, GofTimesteps); \
StatData[NStep] = ResidualStats.Handle; \
HasResidual = true; \
} \
else \
StatPlot.Plot.SetTitle("Select an input series to compute the residual stat"); \
}
if(StatName == "(none)") {}
#include "SetStatSettings.h"
#undef SET_SETTING
#undef SET_RES_SETTING
}
bool Error = ParentWindow->CheckDllUserError();
if(Error)
{
ErrorLabel.SetText("An error occurred while running the model. See the main log box");
break;
}
if(NStep % GUIUpdateFreq == 0)
{
RunProgress.Set(NStep+1);
StatPlot.Plot.ZoomToFit(true, true);
//StatPlot.Refresh();
SetBetterGridLinePositions(StatPlot.Plot, 1);
this->ProcessEvents();
}
}
//Plot.FormatAxes(MajorMode_Regular, 0, InputStartTime, ParentWindow->TimestepSize);
RunProgress.Hide();
StatPlot.Plot.ZoomToFit(true, true);
SetBetterGridLinePositions(StatPlot.Plot, 1);
if(StatName != "(none)")
{
double StartX = (double)(GofStartTime - InputStartTime);
double EndX = (double)(GofEndTime - InputStartTime);
Plot.AddLine(Null, StartX, StartX, Plot.GetYMin(), Plot.GetYMax(), Red());
Plot.AddLine(Null, EndX, EndX, Plot.GetYMin(), Plot.GetYMax(), Red());
}
Plot.Refresh();
ParentWindow->ModelDll.DeleteDataSet(DataSetCopy);
//PromptOK(Format("x: %g %g, y: %g, %g, ts: %d, resultstart: %s", ResultXValues[0], ResultXValues[10], ResultYValues[0], ResultYValues[10], (int)ResultTimesteps, ResultStartTime));
}