[9353] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[12012] | 3 | * Copyright (C) 2002-2015 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 |
|
---|
[11705] | 36 | namespace HeuristicLab.Analysis.Statistics.Views {
|
---|
| 37 | [View("Chart Analysis", "HeuristicLab.Analysis.Statistics.Views.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 |
|
---|
[11697] | 58 | stringConvertibleMatrixView.DataGridView.RowHeaderMouseDoubleClick += 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)) {
|
---|
[11697] | 69 | stringConvertibleMatrixView.DataGridView.RowHeaderMouseDoubleClick -= DataGridView_RowHeaderMouseDoubleClick;
|
---|
[9353] | 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 | if (Content != null) {
|
---|
| 89 | UpdateDataTableComboBox();
|
---|
| 90 | }
|
---|
| 91 | }
|
---|
| 92 |
|
---|
| 93 | protected override void RegisterContentEvents() {
|
---|
| 94 | base.RegisterContentEvents();
|
---|
[11697] | 95 | Content.ColumnsChanged += Content_ColumnsChanged;
|
---|
| 96 | Content.RowsChanged += Content_RowsChanged;
|
---|
[11376] | 97 | Content.CollectionReset += Content_CollectionReset;
|
---|
[9378] | 98 | Content.UpdateOfRunsInProgressChanged += Content_UpdateOfRunsInProgressChanged;
|
---|
[9353] | 99 | }
|
---|
| 100 |
|
---|
| 101 | protected override void DeregisterContentEvents() {
|
---|
| 102 | base.DeregisterContentEvents();
|
---|
[11697] | 103 | Content.ColumnsChanged -= Content_ColumnsChanged;
|
---|
| 104 | Content.RowsChanged -= Content_RowsChanged;
|
---|
[11376] | 105 | Content.CollectionReset -= Content_CollectionReset;
|
---|
[9378] | 106 | Content.UpdateOfRunsInProgressChanged -= Content_UpdateOfRunsInProgressChanged;
|
---|
[9353] | 107 | }
|
---|
| 108 |
|
---|
[11697] | 109 | void Content_RowsChanged(object sender, EventArgs e) {
|
---|
| 110 | RebuildDataTableAsync();
|
---|
[11376] | 111 | }
|
---|
| 112 |
|
---|
[11697] | 113 | void Content_ColumnsChanged(object sender, EventArgs e) {
|
---|
[12112] | 114 | if (!suppressUpdates) {
|
---|
| 115 | RebuildDataTableAsync();
|
---|
| 116 | }
|
---|
[11376] | 117 | }
|
---|
| 118 |
|
---|
| 119 | private void Content_CollectionReset(object sender, CollectionItemsChangedEventArgs<IRun> e) {
|
---|
| 120 | UpdateComboboxes();
|
---|
[9713] | 121 | RebuildDataTableAsync();
|
---|
[9353] | 122 | }
|
---|
| 123 |
|
---|
[11378] | 124 | private void Content_UpdateOfRunsInProgressChanged(object sender, EventArgs e) {
|
---|
[11376] | 125 | suppressUpdates = Content.UpdateOfRunsInProgress;
|
---|
| 126 |
|
---|
| 127 | if (!suppressUpdates && !valuesAdded) {
|
---|
[9713] | 128 | RebuildDataTableAsync();
|
---|
[9353] | 129 | }
|
---|
[9713] | 130 | if (valuesAdded) {
|
---|
| 131 | valuesAdded = false;
|
---|
| 132 | }
|
---|
[9353] | 133 | }
|
---|
| 134 | #endregion
|
---|
| 135 |
|
---|
[11697] | 136 | #region events
|
---|
[11378] | 137 | private void DataGridView_RowHeaderMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e) {
|
---|
[9377] | 138 | if (e.RowIndex >= 0) {
|
---|
[9378] | 139 | IRun run = runs[stringConvertibleMatrixView.GetRowIndex(e.RowIndex)];
|
---|
[9377] | 140 | IContentView view = MainFormManager.MainForm.ShowContent(run);
|
---|
| 141 | if (view != null) {
|
---|
| 142 | view.ReadOnly = this.ReadOnly;
|
---|
| 143 | view.Locked = this.Locked;
|
---|
| 144 | }
|
---|
| 145 | }
|
---|
[9353] | 146 | }
|
---|
| 147 |
|
---|
| 148 | private void dataTableComboBox_SelectedIndexChanged(object sender, EventArgs e) {
|
---|
| 149 | UpdateDataRowComboBox();
|
---|
| 150 | }
|
---|
| 151 |
|
---|
| 152 | private void dataRowComboBox_SelectedIndexChanged(object sender, EventArgs e) {
|
---|
[9713] | 153 | RebuildDataTableAsync();
|
---|
[9353] | 154 | }
|
---|
| 155 |
|
---|
[9377] | 156 | private void addLineToChart_Click(object sender, EventArgs e) {
|
---|
[11376] | 157 | MainFormManager.GetMainForm<MainForm.WindowsForms.MainForm>().AddOperationProgressToView(this, "Adding fitted lines to charts...");
|
---|
[9713] | 158 |
|
---|
[11379] | 159 | string resultName = (string)dataTableComboBox.SelectedItem;
|
---|
| 160 | string rowName = (string)dataRowComboBox.SelectedItem;
|
---|
[9713] | 161 |
|
---|
[11379] | 162 | var task = Task.Factory.StartNew(() => AddLineToChart(resultName, rowName));
|
---|
| 163 |
|
---|
[9713] | 164 | task.ContinueWith((t) => {
|
---|
[11376] | 165 | MainFormManager.GetMainForm<MainForm.WindowsForms.MainForm>().RemoveOperationProgressFromView(this);
|
---|
[9713] | 166 | ErrorHandling.ShowErrorDialog("An error occured while adding lines to charts. ", t.Exception);
|
---|
| 167 | }, TaskContinuationOptions.OnlyOnFaulted);
|
---|
| 168 |
|
---|
| 169 | task.ContinueWith((t) => {
|
---|
[11376] | 170 | MainFormManager.GetMainForm<MainForm.WindowsForms.MainForm>().RemoveOperationProgressFromView(this);
|
---|
[9713] | 171 | }, TaskContinuationOptions.OnlyOnRanToCompletion);
|
---|
| 172 | }
|
---|
| 173 |
|
---|
[11379] | 174 | private void AddLineToChart(string resultName, string rowName) {
|
---|
[9378] | 175 | foreach (IRun run in runs) {
|
---|
[9377] | 176 | DataTable resTable = (DataTable)run.Results[resultName];
|
---|
| 177 | DataRow row = resTable.Rows[rowName];
|
---|
| 178 | var values = row.Values.ToArray();
|
---|
| 179 |
|
---|
[9706] | 180 | var fittingAlg = fittingComboBox.SelectedItem as IFitting;
|
---|
[11914] | 181 | DataRow newRow = fittingAlg.CalculateFittedLine(values);
|
---|
| 182 | newRow.Name = row.Name + " (" + fittingAlg + ")";
|
---|
[9377] | 183 |
|
---|
| 184 | if (!resTable.Rows.ContainsKey(newRow.Name))
|
---|
| 185 | resTable.Rows.Add(newRow);
|
---|
| 186 | }
|
---|
| 187 | }
|
---|
| 188 |
|
---|
| 189 | private void addValuesButton_Click(object sender, EventArgs e) {
|
---|
| 190 | string resultName = (string)dataTableComboBox.SelectedItem;
|
---|
| 191 | string rowName = (string)dataRowComboBox.SelectedItem;
|
---|
[9378] | 192 | DoubleMatrix sm = (DoubleMatrix)stringConvertibleMatrixView.Content;
|
---|
[9377] | 193 |
|
---|
| 194 | Content.UpdateOfRunsInProgress = true;
|
---|
| 195 | for (int i = 0; i < runs.Count(); i++) {
|
---|
[9378] | 196 | IRun run = runs[i];
|
---|
[9377] | 197 |
|
---|
| 198 | for (int j = 0; j < sm.ColumnNames.Count(); j++) {
|
---|
[9378] | 199 | if (stringConvertibleMatrixView.DataGridView.Columns[j].Visible) {
|
---|
| 200 | string newResultName = resultName + " " + rowName + " " + sm.ColumnNames.ElementAt(j);
|
---|
| 201 | if (!run.Results.ContainsKey(newResultName)) {
|
---|
[11697] | 202 | run.Results.Add(new KeyValuePair<string, IItem>(newResultName, new DoubleValue(sm[i, j])));
|
---|
[9378] | 203 | }
|
---|
| 204 | }
|
---|
[9377] | 205 | }
|
---|
| 206 | }
|
---|
[9713] | 207 | valuesAdded = true;
|
---|
[9377] | 208 | Content.UpdateOfRunsInProgress = false;
|
---|
| 209 | }
|
---|
| 210 | #endregion
|
---|
| 211 |
|
---|
[9353] | 212 | private void UpdateDataRowComboBox() {
|
---|
[11379] | 213 | string selectedItem = (string)this.dataRowComboBox.SelectedItem;
|
---|
| 214 |
|
---|
[9353] | 215 | dataRowComboBox.Items.Clear();
|
---|
| 216 | var resultName = (string)dataTableComboBox.SelectedItem;
|
---|
| 217 | var dataTables = from run in Content
|
---|
| 218 | where run.Results.ContainsKey(resultName)
|
---|
| 219 | select run.Results[resultName] as DataTable;
|
---|
| 220 | var rowNames = (from dataTable in dataTables
|
---|
| 221 | from row in dataTable.Rows
|
---|
| 222 | select row.Name).Distinct().ToArray();
|
---|
| 223 |
|
---|
| 224 | dataRowComboBox.Items.AddRange(rowNames);
|
---|
[11379] | 225 | if (selectedItem != null && dataRowComboBox.Items.Contains(selectedItem)) {
|
---|
| 226 | dataRowComboBox.SelectedItem = selectedItem;
|
---|
| 227 | } else if (dataRowComboBox.Items.Count > 0) {
|
---|
| 228 | dataRowComboBox.SelectedItem = dataRowComboBox.Items[0];
|
---|
| 229 | }
|
---|
[9353] | 230 | }
|
---|
| 231 |
|
---|
| 232 | private void UpdateDataTableComboBox() {
|
---|
[11379] | 233 | string selectedItem = (string)this.dataTableComboBox.SelectedItem;
|
---|
| 234 |
|
---|
[9353] | 235 | dataTableComboBox.Items.Clear();
|
---|
| 236 | var dataTables = (from run in Content
|
---|
| 237 | from result in run.Results
|
---|
| 238 | where result.Value is DataTable
|
---|
| 239 | select result.Key).Distinct().ToArray();
|
---|
| 240 |
|
---|
| 241 | dataTableComboBox.Items.AddRange(dataTables);
|
---|
[11379] | 242 | if (selectedItem != null && dataTableComboBox.Items.Contains(selectedItem)) {
|
---|
| 243 | dataTableComboBox.SelectedItem = selectedItem;
|
---|
| 244 | } else if (dataTableComboBox.Items.Count > 0) {
|
---|
| 245 | dataTableComboBox.SelectedItem = dataTableComboBox.Items[0];
|
---|
| 246 | }
|
---|
[9353] | 247 | }
|
---|
| 248 |
|
---|
[9713] | 249 | private void RebuildDataTableAsync() {
|
---|
[11376] | 250 | progress = MainFormManager.GetMainForm<MainForm.WindowsForms.MainForm>().AddOperationProgressToView(this, "Calculating values...");
|
---|
[9713] | 251 |
|
---|
[11379] | 252 | string resultName = (string)dataTableComboBox.SelectedItem;
|
---|
| 253 | string rowName = (string)dataRowComboBox.SelectedItem;
|
---|
[9713] | 254 |
|
---|
[11379] | 255 | var task = Task.Factory.StartNew(() => RebuildDataTable(resultName, rowName));
|
---|
| 256 |
|
---|
[9713] | 257 | task.ContinueWith((t) => {
|
---|
[11376] | 258 | MainFormManager.GetMainForm<MainForm.WindowsForms.MainForm>().RemoveOperationProgressFromView(this);
|
---|
[9713] | 259 | ErrorHandling.ShowErrorDialog("An error occured while calculating values. ", t.Exception);
|
---|
| 260 | }, TaskContinuationOptions.OnlyOnFaulted);
|
---|
| 261 |
|
---|
| 262 | task.ContinueWith((t) => {
|
---|
[11376] | 263 | MainFormManager.GetMainForm<MainForm.WindowsForms.MainForm>().RemoveOperationProgressFromView(this);
|
---|
[9713] | 264 | }, TaskContinuationOptions.OnlyOnRanToCompletion);
|
---|
| 265 | }
|
---|
| 266 |
|
---|
[11379] | 267 | private void RebuildDataTable(string resultName, string rowName) {
|
---|
[9713] | 268 | LinearLeastSquaresFitting llsFitting = new LinearLeastSquaresFitting();
|
---|
[9908] | 269 | string[] columnNames = new string[] { "Count", "Minimum", "Maximum", "Average", "Median", "Standard Deviation", "Variance", "25th Percentile", "75th Percentile",
|
---|
[11914] | 270 | "Avg. of Upper 25 %", " Avg. of Lower 25 %", "Avg. of First 25 %", "Avg. of Last 25 %", "Slope", "Intercept", "Average Relative Error" };
|
---|
[9353] | 271 |
|
---|
[9378] | 272 | runs = Content.Where(x => x.Results.ContainsKey(resultName) && x.Visible).ToList();
|
---|
| 273 | DoubleMatrix dt = new DoubleMatrix(runs.Count(), columnNames.Count());
|
---|
[9353] | 274 | dt.RowNames = runs.Select(x => x.Name);
|
---|
| 275 | dt.ColumnNames = columnNames;
|
---|
| 276 |
|
---|
| 277 | int i = 0;
|
---|
| 278 | foreach (Run run in runs) {
|
---|
| 279 | DataTable resTable = (DataTable)run.Results[resultName];
|
---|
[9377] | 280 | dt.SortableView = true;
|
---|
[9353] | 281 | DataRow row = resTable.Rows[rowName];
|
---|
[11914] | 282 | var values = row.Values.ToArray();
|
---|
[9353] | 283 |
|
---|
| 284 | double cnt = values.Count();
|
---|
| 285 | double min = values.Min();
|
---|
| 286 | double max = values.Max();
|
---|
| 287 | double avg = values.Average();
|
---|
| 288 | double median = values.Median();
|
---|
| 289 | double stdDev = values.StandardDeviation();
|
---|
| 290 | double variance = values.Variance();
|
---|
| 291 | double percentile25 = values.Percentile(0.25);
|
---|
| 292 | double percentile75 = values.Percentile(0.75);
|
---|
[12117] | 293 | double lowerAvg = values.Count() > 4 ? values.OrderBy(x => x).Take((int)(values.Count() * 0.25)).Average() : double.NaN;
|
---|
| 294 | double upperAvg = values.Count() > 4 ? values.OrderByDescending(x => x).Take((int)(values.Count() * 0.25)).Average() : double.NaN;
|
---|
| 295 | double firstAvg = values.Count() > 4 ? values.Take((int)(values.Count() * 0.25)).Average() : double.NaN;
|
---|
| 296 | double lastAvg = values.Count() > 4 ? values.Skip((int)(values.Count() * 0.75)).Average() : double.NaN;
|
---|
[11914] | 297 | double slope, intercept, r;
|
---|
| 298 | llsFitting.Calculate(values, out slope, out intercept);
|
---|
| 299 | r = llsFitting.CalculateError(values, slope, intercept);
|
---|
[9353] | 300 |
|
---|
[9378] | 301 | dt[i, 0] = cnt;
|
---|
| 302 | dt[i, 1] = min;
|
---|
| 303 | dt[i, 2] = max;
|
---|
| 304 | dt[i, 3] = avg;
|
---|
| 305 | dt[i, 4] = median;
|
---|
| 306 | dt[i, 5] = stdDev;
|
---|
| 307 | dt[i, 6] = variance;
|
---|
| 308 | dt[i, 7] = percentile25;
|
---|
| 309 | dt[i, 8] = percentile75;
|
---|
[9713] | 310 | dt[i, 9] = upperAvg;
|
---|
| 311 | dt[i, 10] = lowerAvg;
|
---|
| 312 | dt[i, 11] = firstAvg;
|
---|
| 313 | dt[i, 12] = lastAvg;
|
---|
[11914] | 314 | dt[i, 13] = slope;
|
---|
| 315 | dt[i, 14] = intercept;
|
---|
| 316 | dt[i, 15] = r;
|
---|
[9353] | 317 |
|
---|
| 318 | i++;
|
---|
[11376] | 319 | progress.ProgressValue = ((double)runs.Count) / i;
|
---|
[9353] | 320 | }
|
---|
[9378] | 321 | stringConvertibleMatrixView.Content = dt;
|
---|
[9353] | 322 |
|
---|
[9378] | 323 | for (i = 0; i < runs.Count(); i++) {
|
---|
| 324 | stringConvertibleMatrixView.DataGridView.Rows[i].DefaultCellStyle.ForeColor = runs[i].Color;
|
---|
| 325 | }
|
---|
[9353] | 326 | }
|
---|
| 327 | }
|
---|
| 328 | }
|
---|