forked from s-wagner/HeuristicLab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChartAnalysisView.cs
352 lines (301 loc) · 13.2 KB
/
ChartAnalysisView.cs
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
#region License Information
/* HeuristicLab
* Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
*
* This file is part of HeuristicLab.
*
* HeuristicLab is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* HeuristicLab is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
*/
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using HeuristicLab.Collections;
using HeuristicLab.Common;
using HeuristicLab.Core;
using HeuristicLab.Core.Views;
using HeuristicLab.Data;
using HeuristicLab.MainForm;
using HeuristicLab.Optimization;
using HeuristicLab.PluginInfrastructure;
namespace HeuristicLab.Analysis.Statistics.Views {
[View("Chart Analysis", "HeuristicLab.Analysis.Statistics.Views.InfoResources.ChartAnalysisInfo.rtf")]
[Content(typeof(RunCollection), false)]
public sealed partial class ChartAnalysisView : ItemView {
public new RunCollection Content {
get { return (RunCollection)base.Content; }
set { base.Content = value; }
}
public override bool ReadOnly {
get { return true; }
set { /*not needed because results are always readonly */}
}
private List<IRun> runs;
private IProgress progress;
private bool valuesAdded = false;
private bool suppressUpdates = false;
private SemaphoreSlim sem = new SemaphoreSlim(1, 1);
public ChartAnalysisView() {
InitializeComponent();
stringConvertibleMatrixView.DataGridView.RowHeaderMouseDoubleClick += DataGridView_RowHeaderMouseDoubleClick;
var fittingAlgs = ApplicationManager.Manager.GetInstances<IFitting>();
foreach (var fit in fittingAlgs) {
fittingComboBox.Items.Add(fit);
}
fittingComboBox.SelectedIndex = 0;
}
protected override void Dispose(bool disposing) {
if (disposing && (components != null)) {
stringConvertibleMatrixView.DataGridView.RowHeaderMouseDoubleClick -= DataGridView_RowHeaderMouseDoubleClick;
components.Dispose();
}
base.Dispose(disposing);
}
#region Content Events
protected override void OnContentChanged() {
base.OnContentChanged();
UpdateComboboxes();
UpdateCaption();
}
private void UpdateCaption() {
Caption = Content != null ? Content.OptimizerName + " Chart Analysis" : ViewAttribute.GetViewName(GetType());
}
private void UpdateComboboxes() {
if (Content != null) {
UpdateDataTableComboBox();
}
}
protected override void RegisterContentEvents() {
base.RegisterContentEvents();
Content.ColumnsChanged += Content_ColumnsChanged;
Content.RowsChanged += Content_RowsChanged;
Content.CollectionReset += Content_CollectionReset;
Content.UpdateOfRunsInProgressChanged += Content_UpdateOfRunsInProgressChanged;
}
protected override void DeregisterContentEvents() {
base.DeregisterContentEvents();
Content.ColumnsChanged -= Content_ColumnsChanged;
Content.RowsChanged -= Content_RowsChanged;
Content.CollectionReset -= Content_CollectionReset;
Content.UpdateOfRunsInProgressChanged -= Content_UpdateOfRunsInProgressChanged;
}
void Content_RowsChanged(object sender, EventArgs e) {
if (suppressUpdates) return;
if (InvokeRequired) Invoke((Action<object, EventArgs>)Content_RowsChanged, sender, e);
else {
RebuildDataTableAsync();
}
}
void Content_ColumnsChanged(object sender, EventArgs e) {
if (suppressUpdates) return;
if (InvokeRequired) Invoke((Action<object, EventArgs>)Content_ColumnsChanged, sender, e);
else {
UpdateDataTableComboBox();
RebuildDataTableAsync();
}
}
private void Content_CollectionReset(object sender, CollectionItemsChangedEventArgs<IRun> e) {
if (suppressUpdates) return;
if (InvokeRequired) Invoke((Action<object, CollectionItemsChangedEventArgs<IRun>>)Content_CollectionReset, sender, e);
else {
UpdateComboboxes();
RebuildDataTableAsync();
}
}
private void Content_UpdateOfRunsInProgressChanged(object sender, EventArgs e) {
if (InvokeRequired) Invoke((Action<object, EventArgs>)Content_UpdateOfRunsInProgressChanged, sender, e);
else {
suppressUpdates = Content.UpdateOfRunsInProgress;
if (!suppressUpdates && !valuesAdded) {
UpdateDataTableComboBox();
RebuildDataTableAsync();
}
if (valuesAdded) {
valuesAdded = false;
}
}
}
#endregion
#region events
private void DataGridView_RowHeaderMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e) {
if (e.RowIndex >= 0) {
IRun run = runs[stringConvertibleMatrixView.GetRowIndex(e.RowIndex)];
IContentView view = MainFormManager.MainForm.ShowContent(run);
if (view != null) {
view.ReadOnly = this.ReadOnly;
view.Locked = this.Locked;
}
}
}
private void dataTableComboBox_SelectedIndexChanged(object sender, EventArgs e) {
UpdateDataRowComboBox();
}
private void dataRowComboBox_SelectedIndexChanged(object sender, EventArgs e) {
if (suppressUpdates) return;
RebuildDataTableAsync();
}
private void addLineToChart_Click(object sender, EventArgs e) {
Progress.Show(this, "Adding fitted lines to charts...", ProgressMode.Indeterminate);
string resultName = (string)dataTableComboBox.SelectedItem;
string rowName = (string)dataRowComboBox.SelectedItem;
var task = Task.Factory.StartNew(() => AddLineToChart(resultName, rowName));
task.ContinueWith((t) => {
Progress.Hide(this);
ErrorHandling.ShowErrorDialog("An error occured while adding lines to charts. ", t.Exception);
}, TaskContinuationOptions.OnlyOnFaulted);
task.ContinueWith((t) => {
Progress.Hide(this);
}, TaskContinuationOptions.OnlyOnRanToCompletion);
}
private void AddLineToChart(string resultName, string rowName) {
foreach (IRun run in runs) {
DataTable resTable = (DataTable)run.Results[resultName];
DataRow row = resTable.Rows[rowName];
var values = row.Values.ToArray();
var fittingAlg = fittingComboBox.SelectedItem as IFitting;
DataRow newRow = fittingAlg.CalculateFittedLine(values);
newRow.Name = row.Name + " (" + fittingAlg + ")";
if (!resTable.Rows.ContainsKey(newRow.Name))
resTable.Rows.Add(newRow);
}
}
private void addValuesButton_Click(object sender, EventArgs e) {
string resultName = (string)dataTableComboBox.SelectedItem;
string rowName = (string)dataRowComboBox.SelectedItem;
DoubleMatrix sm = (DoubleMatrix)stringConvertibleMatrixView.Content;
Content.UpdateOfRunsInProgress = true;
for (int i = 0; i < runs.Count(); i++) {
IRun run = runs[i];
for (int j = 0; j < sm.ColumnNames.Count(); j++) {
if (stringConvertibleMatrixView.DataGridView.Columns[j].Visible) {
string newResultName = resultName + " " + rowName + " " + sm.ColumnNames.ElementAt(j);
if (!run.Results.ContainsKey(newResultName)) {
run.Results.Add(new KeyValuePair<string, IItem>(newResultName, new DoubleValue(sm[i, j])));
}
}
}
}
valuesAdded = true;
Content.UpdateOfRunsInProgress = false;
}
#endregion
private void UpdateDataRowComboBox() {
string selectedItem = (string)this.dataRowComboBox.SelectedItem;
dataRowComboBox.Items.Clear();
var resultName = (string)dataTableComboBox.SelectedItem;
var dataTables = from run in Content
where run.Results.ContainsKey(resultName)
select run.Results[resultName] as DataTable;
var rowNames = (from dataTable in dataTables
from row in dataTable.Rows
select row.Name).Distinct().ToArray();
dataRowComboBox.Items.AddRange(rowNames);
if (selectedItem != null && dataRowComboBox.Items.Contains(selectedItem)) {
dataRowComboBox.SelectedItem = selectedItem;
} else if (dataRowComboBox.Items.Count > 0) {
dataRowComboBox.SelectedItem = dataRowComboBox.Items[0];
}
}
private void UpdateDataTableComboBox() {
string selectedItem = (string)this.dataTableComboBox.SelectedItem;
dataTableComboBox.Items.Clear();
var dataTables = (from run in Content
from result in run.Results
where result.Value is DataTable
select result.Key).Distinct().ToArray();
dataTableComboBox.Items.AddRange(dataTables);
if (selectedItem != null && dataTableComboBox.Items.Contains(selectedItem)) {
dataTableComboBox.SelectedItem = selectedItem;
} else if (dataTableComboBox.Items.Count > 0) {
dataTableComboBox.SelectedItem = dataTableComboBox.Items[0];
}
}
private void RebuildDataTableAsync() {
string resultName = (string)dataTableComboBox.SelectedItem;
if (string.IsNullOrEmpty(resultName)) return;
string rowName = (string)dataRowComboBox.SelectedItem;
var task = Task.Factory.StartNew(() => {
sem.Wait();
progress = Progress.Show(this, "Calculating values...");
RebuildDataTable(resultName, rowName);
});
task.ContinueWith((t) => {
Progress.Hide(this);
ErrorHandling.ShowErrorDialog("An error occured while calculating values. ", t.Exception);
sem.Release();
}, TaskContinuationOptions.OnlyOnFaulted);
task.ContinueWith((t) => {
Progress.Hide(this);
sem.Release();
}, TaskContinuationOptions.OnlyOnRanToCompletion);
}
private void RebuildDataTable(string resultName, string rowName) {
LinearLeastSquaresFitting llsFitting = new LinearLeastSquaresFitting();
string[] columnNames = new string[] { "Count", "Minimum", "Maximum", "Average", "Median", "Standard Deviation", "Variance", "25th Percentile", "75th Percentile",
"Avg. of Upper 25 %", " Avg. of Lower 25 %", "Avg. of First 25 %", "Avg. of Last 25 %", "Slope", "Intercept", "Average Relative Error" };
runs = Content.Where(x => x.Results.ContainsKey(resultName) && x.Visible).ToList();
DoubleMatrix dt = new DoubleMatrix(runs.Count(), columnNames.Count());
dt.RowNames = runs.Select(x => x.Name);
dt.ColumnNames = columnNames;
int i = 0;
foreach (Run run in runs) {
DataTable resTable = (DataTable)run.Results[resultName];
dt.SortableView = true;
DataRow row = resTable.Rows[rowName];
var values = row.Values.ToArray();
double cnt = values.Count();
double min = values.Min();
double max = values.Max();
double avg = values.Average();
double median = values.Median();
double stdDev = values.StandardDeviation();
double variance = values.Variance();
double percentile25 = values.Quantile(0.25);
double percentile75 = values.Quantile(0.75);
double lowerAvg = values.Count() > 4 ? values.OrderBy(x => x).Take((int)(values.Count() * 0.25)).Average() : double.NaN;
double upperAvg = values.Count() > 4 ? values.OrderByDescending(x => x).Take((int)(values.Count() * 0.25)).Average() : double.NaN;
double firstAvg = values.Count() > 4 ? values.Take((int)(values.Count() * 0.25)).Average() : double.NaN;
double lastAvg = values.Count() > 4 ? values.Skip((int)(values.Count() * 0.75)).Average() : double.NaN;
double slope, intercept, r;
llsFitting.Calculate(values, out slope, out intercept);
r = llsFitting.CalculateError(values, slope, intercept);
dt[i, 0] = cnt;
dt[i, 1] = min;
dt[i, 2] = max;
dt[i, 3] = avg;
dt[i, 4] = median;
dt[i, 5] = stdDev;
dt[i, 6] = variance;
dt[i, 7] = percentile25;
dt[i, 8] = percentile75;
dt[i, 9] = upperAvg;
dt[i, 10] = lowerAvg;
dt[i, 11] = firstAvg;
dt[i, 12] = lastAvg;
dt[i, 13] = slope;
dt[i, 14] = intercept;
dt[i, 15] = r;
i++;
progress.ProgressValue = ((double)runs.Count) / i;
}
stringConvertibleMatrixView.Content = dt;
for (i = 0; i < runs.Count(); i++) {
stringConvertibleMatrixView.DataGridView.Rows[i].DefaultCellStyle.ForeColor = runs[i].Color;
}
}
}
}