[9353] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[11375] | 3 | * Copyright (C) 2002-2014 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[9353] | 4 | *
|
---|
| 5 | * This file is part of HeuristicLab.
|
---|
| 6 | *
|
---|
| 7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
| 8 | * it under the terms of the GNU General Public License as published by
|
---|
| 9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
| 10 | * (at your option) any later version.
|
---|
| 11 | *
|
---|
| 12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 15 | * GNU General Public License for more details.
|
---|
| 16 | *
|
---|
| 17 | * You should have received a copy of the GNU General Public License
|
---|
| 18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
| 19 | */
|
---|
| 20 | #endregion
|
---|
| 21 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.Collections.Generic;
|
---|
| 24 | using System.Linq;
|
---|
[9713] | 25 | using System.Threading.Tasks;
|
---|
[9353] | 26 | using System.Windows.Forms;
|
---|
[11376] | 27 | using HeuristicLab.Collections;
|
---|
[9353] | 28 | using HeuristicLab.Common;
|
---|
[11376] | 29 | using HeuristicLab.Core;
|
---|
[9353] | 30 | using HeuristicLab.Core.Views;
|
---|
| 31 | using HeuristicLab.Data;
|
---|
| 32 | using HeuristicLab.MainForm;
|
---|
| 33 | using HeuristicLab.Optimization;
|
---|
[9706] | 34 | using HeuristicLab.PluginInfrastructure;
|
---|
[9353] | 35 |
|
---|
| 36 | namespace HeuristicLab.Analysis.Statistics {
|
---|
[9917] | 37 | [View("Chart Analysis", "HeuristicLab.Analysis.Statistics.InfoResources.ChartAnalysisInfo.rtf")]
|
---|
[9353] | 38 | [Content(typeof(RunCollection), false)]
|
---|
[9377] | 39 | public sealed partial class ChartAnalysisView : ItemView {
|
---|
| 40 | public new RunCollection Content {
|
---|
| 41 | get { return (RunCollection)base.Content; }
|
---|
| 42 | set { base.Content = value; }
|
---|
| 43 | }
|
---|
| 44 |
|
---|
| 45 | public override bool ReadOnly {
|
---|
| 46 | get { return true; }
|
---|
| 47 | set { /*not needed because results are always readonly */}
|
---|
| 48 | }
|
---|
| 49 |
|
---|
[9378] | 50 | private List<IRun> runs;
|
---|
[9904] | 51 | private IProgress progress;
|
---|
[9713] | 52 | private bool valuesAdded = false;
|
---|
[11376] | 53 | private bool suppressUpdates = false;
|
---|
[9378] | 54 |
|
---|
[9377] | 55 | public ChartAnalysisView() {
|
---|
[9353] | 56 | InitializeComponent();
|
---|
[9713] | 57 |
|
---|
[9353] | 58 | stringConvertibleMatrixView.DataGridView.RowHeaderMouseDoubleClick += new DataGridViewCellMouseEventHandler(DataGridView_RowHeaderMouseDoubleClick);
|
---|
[9706] | 59 |
|
---|
| 60 | var fittingAlgs = ApplicationManager.Manager.GetInstances<IFitting>();
|
---|
| 61 | foreach (var fit in fittingAlgs) {
|
---|
| 62 | fittingComboBox.Items.Add(fit);
|
---|
| 63 | }
|
---|
| 64 | fittingComboBox.SelectedIndex = 0;
|
---|
[9353] | 65 | }
|
---|
| 66 |
|
---|
| 67 | protected override void Dispose(bool disposing) {
|
---|
| 68 | if (disposing && (components != null)) {
|
---|
| 69 | stringConvertibleMatrixView.DataGridView.RowHeaderMouseDoubleClick -= new DataGridViewCellMouseEventHandler(DataGridView_RowHeaderMouseDoubleClick);
|
---|
| 70 | components.Dispose();
|
---|
| 71 | }
|
---|
[9904] | 72 |
|
---|
[9353] | 73 | base.Dispose(disposing);
|
---|
| 74 | }
|
---|
| 75 |
|
---|
[9377] | 76 | #region Content Events
|
---|
[9353] | 77 | protected override void OnContentChanged() {
|
---|
| 78 | base.OnContentChanged();
|
---|
[11376] | 79 | UpdateComboboxes();
|
---|
| 80 | UpdateCaption();
|
---|
| 81 | }
|
---|
| 82 |
|
---|
| 83 | private void UpdateCaption() {
|
---|
| 84 | Caption = Content != null ? Content.OptimizerName + " Chart Analysis" : ViewAttribute.GetViewName(GetType());
|
---|
| 85 | }
|
---|
| 86 |
|
---|
| 87 | private void UpdateComboboxes() {
|
---|
[9353] | 88 | dataTableComboBox.Items.Clear();
|
---|
| 89 | dataRowComboBox.Items.Clear();
|
---|
| 90 |
|
---|
| 91 | if (Content != null) {
|
---|
| 92 | UpdateDataTableComboBox();
|
---|
| 93 | }
|
---|
| 94 | }
|
---|
| 95 |
|
---|
| 96 | protected override void RegisterContentEvents() {
|
---|
| 97 | base.RegisterContentEvents();
|
---|
[11376] | 98 | Content.ItemsAdded += Content_ItemsAdded;
|
---|
| 99 | Content.ItemsRemoved += Content_ItemsRemoved;
|
---|
| 100 | Content.CollectionReset += Content_CollectionReset;
|
---|
[9378] | 101 | Content.UpdateOfRunsInProgressChanged += Content_UpdateOfRunsInProgressChanged;
|
---|
[11376] | 102 | RegisterRunEvents(Content);
|
---|
[9353] | 103 | }
|
---|
| 104 |
|
---|
| 105 | protected override void DeregisterContentEvents() {
|
---|
| 106 | base.DeregisterContentEvents();
|
---|
[11376] | 107 | Content.ItemsAdded -= Content_ItemsAdded;
|
---|
| 108 | Content.ItemsRemoved -= Content_ItemsRemoved;
|
---|
| 109 | Content.CollectionReset -= Content_CollectionReset;
|
---|
[9378] | 110 | Content.UpdateOfRunsInProgressChanged -= Content_UpdateOfRunsInProgressChanged;
|
---|
[11376] | 111 | DeregisterRunEvents(Content);
|
---|
[9353] | 112 | }
|
---|
| 113 |
|
---|
[11376] | 114 | private void RegisterRunEvents(IEnumerable<IRun> runs) {
|
---|
| 115 | foreach (IRun run in runs) {
|
---|
| 116 | RegisterRunResultsEvents(run);
|
---|
| 117 | }
|
---|
| 118 | }
|
---|
| 119 |
|
---|
| 120 | private void DeregisterRunEvents(IEnumerable<IRun> runs) {
|
---|
| 121 | foreach (IRun run in runs) {
|
---|
| 122 | DeregisterRunResultsEvents(run);
|
---|
| 123 | }
|
---|
| 124 | }
|
---|
| 125 |
|
---|
| 126 | private void RegisterRunResultsEvents(IRun run) {
|
---|
| 127 | IObservableDictionary<string, IItem> dict = run.Results;
|
---|
| 128 | dict.ItemsAdded += run_Changed;
|
---|
| 129 | dict.ItemsRemoved += run_Changed;
|
---|
| 130 | dict.ItemsReplaced += run_Changed;
|
---|
| 131 | dict.CollectionReset += run_Changed;
|
---|
| 132 | }
|
---|
| 133 |
|
---|
| 134 | private void DeregisterRunResultsEvents(IRun run) {
|
---|
| 135 | IObservableDictionary<string, IItem> dict = run.Results;
|
---|
| 136 | dict.ItemsAdded -= run_Changed;
|
---|
| 137 | dict.ItemsRemoved -= run_Changed;
|
---|
| 138 | dict.ItemsReplaced -= run_Changed;
|
---|
| 139 | dict.CollectionReset -= run_Changed;
|
---|
| 140 | }
|
---|
| 141 |
|
---|
| 142 | private void Content_CollectionReset(object sender, CollectionItemsChangedEventArgs<IRun> e) {
|
---|
| 143 | DeregisterRunEvents(e.OldItems);
|
---|
| 144 | RegisterRunEvents(e.Items);
|
---|
| 145 | UpdateComboboxes();
|
---|
[9713] | 146 | RebuildDataTableAsync();
|
---|
[9353] | 147 | }
|
---|
| 148 |
|
---|
[11376] | 149 | private void Content_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IRun> e) {
|
---|
| 150 | DeregisterRunEvents(e.Items);
|
---|
[9713] | 151 | RebuildDataTableAsync();
|
---|
[9353] | 152 | }
|
---|
| 153 |
|
---|
[11376] | 154 | private void Content_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IRun> e) {
|
---|
| 155 | RegisterRunEvents(e.Items);
|
---|
[9713] | 156 | RebuildDataTableAsync();
|
---|
[9353] | 157 | }
|
---|
| 158 |
|
---|
[11378] | 159 | private void Content_UpdateOfRunsInProgressChanged(object sender, EventArgs e) {
|
---|
[11376] | 160 | suppressUpdates = Content.UpdateOfRunsInProgress;
|
---|
| 161 |
|
---|
| 162 | if (!suppressUpdates && !valuesAdded) {
|
---|
[9713] | 163 | RebuildDataTableAsync();
|
---|
[9353] | 164 | }
|
---|
[9713] | 165 | if (valuesAdded) {
|
---|
| 166 | valuesAdded = false;
|
---|
| 167 | }
|
---|
[9353] | 168 | }
|
---|
[11376] | 169 |
|
---|
| 170 | private void run_Changed(object sender, EventArgs e) {
|
---|
| 171 | if (InvokeRequired)
|
---|
| 172 | Invoke(new EventHandler(run_Changed), sender, e);
|
---|
| 173 | else if (!suppressUpdates) {
|
---|
| 174 | UpdateComboboxes();
|
---|
| 175 | }
|
---|
| 176 | }
|
---|
[9353] | 177 | #endregion
|
---|
| 178 |
|
---|
[9377] | 179 | #region Events
|
---|
[11378] | 180 | private void DataGridView_RowHeaderMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e) {
|
---|
[9377] | 181 | if (e.RowIndex >= 0) {
|
---|
[9378] | 182 | IRun run = runs[stringConvertibleMatrixView.GetRowIndex(e.RowIndex)];
|
---|
[9377] | 183 | IContentView view = MainFormManager.MainForm.ShowContent(run);
|
---|
| 184 | if (view != null) {
|
---|
| 185 | view.ReadOnly = this.ReadOnly;
|
---|
| 186 | view.Locked = this.Locked;
|
---|
| 187 | }
|
---|
| 188 | }
|
---|
[9353] | 189 | }
|
---|
| 190 |
|
---|
| 191 | private void dataTableComboBox_SelectedIndexChanged(object sender, EventArgs e) {
|
---|
| 192 | UpdateDataRowComboBox();
|
---|
| 193 | }
|
---|
| 194 |
|
---|
| 195 | private void dataRowComboBox_SelectedIndexChanged(object sender, EventArgs e) {
|
---|
[9713] | 196 | RebuildDataTableAsync();
|
---|
[9353] | 197 | }
|
---|
| 198 |
|
---|
[9377] | 199 | private void addLineToChart_Click(object sender, EventArgs e) {
|
---|
[11376] | 200 | MainFormManager.GetMainForm<MainForm.WindowsForms.MainForm>().AddOperationProgressToView(this, "Adding fitted lines to charts...");
|
---|
[9713] | 201 |
|
---|
[11376] | 202 | var task = Task.Factory.StartNew(AddLineToChart);
|
---|
[9713] | 203 |
|
---|
| 204 | task.ContinueWith((t) => {
|
---|
[11376] | 205 | MainFormManager.GetMainForm<MainForm.WindowsForms.MainForm>().RemoveOperationProgressFromView(this);
|
---|
[9713] | 206 | ErrorHandling.ShowErrorDialog("An error occured while adding lines to charts. ", t.Exception);
|
---|
| 207 | }, TaskContinuationOptions.OnlyOnFaulted);
|
---|
| 208 |
|
---|
| 209 | task.ContinueWith((t) => {
|
---|
[11376] | 210 | MainFormManager.GetMainForm<MainForm.WindowsForms.MainForm>().RemoveOperationProgressFromView(this);
|
---|
[9713] | 211 | }, TaskContinuationOptions.OnlyOnRanToCompletion);
|
---|
| 212 | }
|
---|
| 213 |
|
---|
| 214 | private void AddLineToChart() {
|
---|
[9377] | 215 | string resultName = (string)dataTableComboBox.SelectedItem;
|
---|
| 216 | string rowName = (string)dataRowComboBox.SelectedItem;
|
---|
| 217 |
|
---|
[9378] | 218 | foreach (IRun run in runs) {
|
---|
[9377] | 219 | DataTable resTable = (DataTable)run.Results[resultName];
|
---|
| 220 | DataRow row = resTable.Rows[rowName];
|
---|
| 221 | var values = row.Values.ToArray();
|
---|
| 222 |
|
---|
[9706] | 223 | var fittingAlg = fittingComboBox.SelectedItem as IFitting;
|
---|
| 224 | DataRow newRow = fittingAlg.CalculateFittedLine(values, row.Name + " (" + fittingAlg.ToString() + ")");
|
---|
[9377] | 225 |
|
---|
| 226 | if (!resTable.Rows.ContainsKey(newRow.Name))
|
---|
| 227 | resTable.Rows.Add(newRow);
|
---|
| 228 | }
|
---|
| 229 | }
|
---|
| 230 |
|
---|
| 231 | private void addValuesButton_Click(object sender, EventArgs e) {
|
---|
| 232 | string resultName = (string)dataTableComboBox.SelectedItem;
|
---|
| 233 | string rowName = (string)dataRowComboBox.SelectedItem;
|
---|
[9378] | 234 | DoubleMatrix sm = (DoubleMatrix)stringConvertibleMatrixView.Content;
|
---|
[9377] | 235 |
|
---|
| 236 | Content.UpdateOfRunsInProgress = true;
|
---|
| 237 | for (int i = 0; i < runs.Count(); i++) {
|
---|
[9378] | 238 | IRun run = runs[i];
|
---|
[9377] | 239 |
|
---|
| 240 | for (int j = 0; j < sm.ColumnNames.Count(); j++) {
|
---|
[9378] | 241 | if (stringConvertibleMatrixView.DataGridView.Columns[j].Visible) {
|
---|
| 242 | string newResultName = resultName + " " + rowName + " " + sm.ColumnNames.ElementAt(j);
|
---|
| 243 | if (!run.Results.ContainsKey(newResultName)) {
|
---|
| 244 | run.Results.Add(new KeyValuePair<string, Core.IItem>(newResultName, new DoubleValue(sm[i, j])));
|
---|
| 245 | }
|
---|
| 246 | }
|
---|
[9377] | 247 | }
|
---|
| 248 | }
|
---|
[9713] | 249 | valuesAdded = true;
|
---|
[9377] | 250 | Content.UpdateOfRunsInProgress = false;
|
---|
| 251 | }
|
---|
| 252 | #endregion
|
---|
| 253 |
|
---|
[9353] | 254 | private void UpdateDataRowComboBox() {
|
---|
| 255 | dataRowComboBox.Items.Clear();
|
---|
| 256 | var resultName = (string)dataTableComboBox.SelectedItem;
|
---|
| 257 | var dataTables = from run in Content
|
---|
| 258 | where run.Results.ContainsKey(resultName)
|
---|
| 259 | select run.Results[resultName] as DataTable;
|
---|
| 260 | var rowNames = (from dataTable in dataTables
|
---|
| 261 | from row in dataTable.Rows
|
---|
| 262 | select row.Name).Distinct().ToArray();
|
---|
| 263 |
|
---|
| 264 | dataRowComboBox.Items.AddRange(rowNames);
|
---|
| 265 | if (dataRowComboBox.Items.Count > 0) dataRowComboBox.SelectedItem = dataRowComboBox.Items[0];
|
---|
| 266 | }
|
---|
| 267 |
|
---|
| 268 | private void UpdateDataTableComboBox() {
|
---|
| 269 | dataTableComboBox.Items.Clear();
|
---|
| 270 | var dataTables = (from run in Content
|
---|
| 271 | from result in run.Results
|
---|
| 272 | where result.Value is DataTable
|
---|
| 273 | select result.Key).Distinct().ToArray();
|
---|
| 274 |
|
---|
| 275 | dataTableComboBox.Items.AddRange(dataTables);
|
---|
| 276 | if (dataTableComboBox.Items.Count > 0) dataTableComboBox.SelectedItem = dataTableComboBox.Items[0];
|
---|
| 277 | }
|
---|
| 278 |
|
---|
[9713] | 279 | private void RebuildDataTableAsync() {
|
---|
[11376] | 280 | progress = MainFormManager.GetMainForm<MainForm.WindowsForms.MainForm>().AddOperationProgressToView(this, "Calculating values...");
|
---|
[9713] | 281 |
|
---|
[9904] | 282 | var task = Task.Factory.StartNew(RebuildDataTable);
|
---|
[9713] | 283 |
|
---|
| 284 | task.ContinueWith((t) => {
|
---|
[11376] | 285 | MainFormManager.GetMainForm<MainForm.WindowsForms.MainForm>().RemoveOperationProgressFromView(this);
|
---|
[9713] | 286 | ErrorHandling.ShowErrorDialog("An error occured while calculating values. ", t.Exception);
|
---|
| 287 | }, TaskContinuationOptions.OnlyOnFaulted);
|
---|
| 288 |
|
---|
| 289 | task.ContinueWith((t) => {
|
---|
[11376] | 290 | MainFormManager.GetMainForm<MainForm.WindowsForms.MainForm>().RemoveOperationProgressFromView(this);
|
---|
[9713] | 291 | }, TaskContinuationOptions.OnlyOnRanToCompletion);
|
---|
| 292 | }
|
---|
| 293 |
|
---|
[9378] | 294 | private void RebuildDataTable() {
|
---|
[9353] | 295 | string resultName = (string)dataTableComboBox.SelectedItem;
|
---|
| 296 | string rowName = (string)dataRowComboBox.SelectedItem;
|
---|
[9713] | 297 | LinearLeastSquaresFitting llsFitting = new LinearLeastSquaresFitting();
|
---|
| 298 | LogFitting logFitting = new LogFitting();
|
---|
[9908] | 299 | string[] columnNames = new string[] { "Count", "Minimum", "Maximum", "Average", "Median", "Standard Deviation", "Variance", "25th Percentile", "75th Percentile",
|
---|
| 300 | "Avg. of Upper 25 %", " Avg. of Lower 25 %", "Avg. of First 25 %", "Avg. of Last 25 %", "Linear Gradient", "Relative Error", "a (log fitted)", "b (log fitted)" };
|
---|
[9353] | 301 |
|
---|
[9378] | 302 | runs = Content.Where(x => x.Results.ContainsKey(resultName) && x.Visible).ToList();
|
---|
| 303 | DoubleMatrix dt = new DoubleMatrix(runs.Count(), columnNames.Count());
|
---|
[9353] | 304 | dt.RowNames = runs.Select(x => x.Name);
|
---|
| 305 | dt.ColumnNames = columnNames;
|
---|
| 306 |
|
---|
| 307 | int i = 0;
|
---|
| 308 | foreach (Run run in runs) {
|
---|
| 309 | DataTable resTable = (DataTable)run.Results[resultName];
|
---|
[9377] | 310 | dt.SortableView = true;
|
---|
[9353] | 311 | DataRow row = resTable.Rows[rowName];
|
---|
| 312 | var values = row.Values.AsEnumerable();
|
---|
| 313 |
|
---|
| 314 | double cnt = values.Count();
|
---|
| 315 | double min = values.Min();
|
---|
| 316 | double max = values.Max();
|
---|
| 317 | double avg = values.Average();
|
---|
| 318 | double median = values.Median();
|
---|
| 319 | double stdDev = values.StandardDeviation();
|
---|
| 320 | double variance = values.Variance();
|
---|
| 321 | double percentile25 = values.Percentile(0.25);
|
---|
| 322 | double percentile75 = values.Percentile(0.75);
|
---|
| 323 | double lowerAvg = values.OrderBy(x => x).Take((int)(values.Count() * 0.25)).Average();
|
---|
| 324 | double upperAvg = values.OrderByDescending(x => x).Take((int)(values.Count() * 0.25)).Average();
|
---|
| 325 | double firstAvg = values.Take((int)(values.Count() * 0.25)).Average();
|
---|
| 326 | double lastAvg = values.Skip((int)(values.Count() * 0.75)).Average();
|
---|
[9713] | 327 | double k, d, r, a, b;
|
---|
| 328 | llsFitting.Calculate(values.ToArray(), out k, out d);
|
---|
| 329 | r = llsFitting.CalculateError(values.ToArray(), k, d);
|
---|
| 330 | logFitting.Calculate(values.ToArray(), out a, out b);
|
---|
[9353] | 331 |
|
---|
[9378] | 332 | dt[i, 0] = cnt;
|
---|
| 333 | dt[i, 1] = min;
|
---|
| 334 | dt[i, 2] = max;
|
---|
| 335 | dt[i, 3] = avg;
|
---|
| 336 | dt[i, 4] = median;
|
---|
| 337 | dt[i, 5] = stdDev;
|
---|
| 338 | dt[i, 6] = variance;
|
---|
| 339 | dt[i, 7] = percentile25;
|
---|
| 340 | dt[i, 8] = percentile75;
|
---|
[9713] | 341 | dt[i, 9] = upperAvg;
|
---|
| 342 | dt[i, 10] = lowerAvg;
|
---|
| 343 | dt[i, 11] = firstAvg;
|
---|
| 344 | dt[i, 12] = lastAvg;
|
---|
| 345 | dt[i, 13] = k;
|
---|
| 346 | dt[i, 14] = r;
|
---|
| 347 | dt[i, 15] = a;
|
---|
| 348 | dt[i, 16] = b;
|
---|
[9353] | 349 |
|
---|
| 350 | i++;
|
---|
[11376] | 351 | progress.ProgressValue = ((double)runs.Count) / i;
|
---|
[9353] | 352 | }
|
---|
[9378] | 353 | stringConvertibleMatrixView.Content = dt;
|
---|
[9353] | 354 |
|
---|
[9378] | 355 | for (i = 0; i < runs.Count(); i++) {
|
---|
| 356 | stringConvertibleMatrixView.DataGridView.Rows[i].DefaultCellStyle.ForeColor = runs[i].Color;
|
---|
| 357 | }
|
---|
[9353] | 358 | }
|
---|
| 359 | }
|
---|
| 360 | }
|
---|