[9353] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[9706] | 3 | * Copyright (C) 2002-2013 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;
|
---|
| 25 | using System.Windows.Forms;
|
---|
| 26 | using HeuristicLab.Common;
|
---|
| 27 | using HeuristicLab.Core.Views;
|
---|
| 28 | using HeuristicLab.Data;
|
---|
| 29 | using HeuristicLab.MainForm;
|
---|
| 30 | using HeuristicLab.Optimization;
|
---|
[9706] | 31 | using HeuristicLab.PluginInfrastructure;
|
---|
[9353] | 32 |
|
---|
| 33 | namespace HeuristicLab.Analysis.Statistics {
|
---|
| 34 | [View("RunCollection Chart Analysis View")]
|
---|
| 35 | [Content(typeof(RunCollection), false)]
|
---|
[9377] | 36 | public sealed partial class ChartAnalysisView : ItemView {
|
---|
| 37 | public new RunCollection Content {
|
---|
| 38 | get { return (RunCollection)base.Content; }
|
---|
| 39 | set { base.Content = value; }
|
---|
| 40 | }
|
---|
| 41 |
|
---|
| 42 | public override bool ReadOnly {
|
---|
| 43 | get { return true; }
|
---|
| 44 | set { /*not needed because results are always readonly */}
|
---|
| 45 | }
|
---|
| 46 |
|
---|
[9378] | 47 | private List<IRun> runs;
|
---|
| 48 |
|
---|
[9377] | 49 | public ChartAnalysisView() {
|
---|
[9353] | 50 | InitializeComponent();
|
---|
| 51 | stringConvertibleMatrixView.DataGridView.RowHeaderMouseDoubleClick += new DataGridViewCellMouseEventHandler(DataGridView_RowHeaderMouseDoubleClick);
|
---|
[9706] | 52 |
|
---|
| 53 | var fittingAlgs = ApplicationManager.Manager.GetInstances<IFitting>();
|
---|
| 54 | foreach (var fit in fittingAlgs) {
|
---|
| 55 | fittingComboBox.Items.Add(fit);
|
---|
| 56 | }
|
---|
| 57 | fittingComboBox.SelectedIndex = 0;
|
---|
[9353] | 58 | }
|
---|
| 59 |
|
---|
| 60 | /// <summary>
|
---|
| 61 | /// Clean up any resources being used.
|
---|
| 62 | /// </summary>
|
---|
| 63 | /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
|
---|
| 64 | protected override void Dispose(bool disposing) {
|
---|
| 65 | if (disposing && (components != null)) {
|
---|
| 66 | stringConvertibleMatrixView.DataGridView.RowHeaderMouseDoubleClick -= new DataGridViewCellMouseEventHandler(DataGridView_RowHeaderMouseDoubleClick);
|
---|
| 67 | components.Dispose();
|
---|
| 68 | }
|
---|
| 69 | base.Dispose(disposing);
|
---|
| 70 | }
|
---|
| 71 |
|
---|
[9377] | 72 | #region Content Events
|
---|
[9353] | 73 | protected override void OnContentChanged() {
|
---|
| 74 | base.OnContentChanged();
|
---|
| 75 | dataTableComboBox.Items.Clear();
|
---|
| 76 | dataRowComboBox.Items.Clear();
|
---|
| 77 |
|
---|
| 78 | if (Content != null) {
|
---|
| 79 | UpdateDataTableComboBox();
|
---|
| 80 | }
|
---|
| 81 | }
|
---|
| 82 |
|
---|
| 83 | protected override void RegisterContentEvents() {
|
---|
| 84 | base.RegisterContentEvents();
|
---|
| 85 | Content.ItemsAdded += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_ItemsAdded);
|
---|
| 86 | Content.ItemsRemoved += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_ItemsRemoved);
|
---|
| 87 | Content.CollectionReset += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_CollectionReset);
|
---|
[9378] | 88 | Content.UpdateOfRunsInProgressChanged += Content_UpdateOfRunsInProgressChanged;
|
---|
[9353] | 89 | }
|
---|
| 90 |
|
---|
| 91 | protected override void DeregisterContentEvents() {
|
---|
| 92 | base.DeregisterContentEvents();
|
---|
| 93 | Content.ItemsAdded -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_ItemsAdded);
|
---|
| 94 | Content.ItemsRemoved -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_ItemsRemoved);
|
---|
| 95 | Content.CollectionReset -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_CollectionReset);
|
---|
[9378] | 96 | Content.UpdateOfRunsInProgressChanged -= Content_UpdateOfRunsInProgressChanged;
|
---|
[9353] | 97 | }
|
---|
| 98 |
|
---|
| 99 | private void Content_CollectionReset(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<IRun> e) {
|
---|
[9378] | 100 | RebuildDataTable();
|
---|
[9353] | 101 | }
|
---|
| 102 |
|
---|
| 103 | private void Content_ItemsRemoved(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<IRun> e) {
|
---|
[9378] | 104 | RebuildDataTable();
|
---|
[9353] | 105 | }
|
---|
| 106 |
|
---|
| 107 | private void Content_ItemsAdded(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<IRun> e) {
|
---|
[9378] | 108 | RebuildDataTable();
|
---|
[9353] | 109 | }
|
---|
| 110 |
|
---|
[9378] | 111 | void Content_UpdateOfRunsInProgressChanged(object sender, EventArgs e) {
|
---|
| 112 | if (!Content.UpdateOfRunsInProgress) {
|
---|
| 113 | RebuildDataTable();
|
---|
[9353] | 114 | }
|
---|
| 115 | }
|
---|
| 116 | #endregion
|
---|
| 117 |
|
---|
[9377] | 118 | #region Events
|
---|
| 119 | void DataGridView_RowHeaderMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e) {
|
---|
| 120 | if (e.RowIndex >= 0) {
|
---|
[9378] | 121 | IRun run = runs[stringConvertibleMatrixView.GetRowIndex(e.RowIndex)];
|
---|
[9377] | 122 | IContentView view = MainFormManager.MainForm.ShowContent(run);
|
---|
| 123 | if (view != null) {
|
---|
| 124 | view.ReadOnly = this.ReadOnly;
|
---|
| 125 | view.Locked = this.Locked;
|
---|
| 126 | }
|
---|
| 127 | }
|
---|
[9353] | 128 | }
|
---|
| 129 |
|
---|
| 130 | private void dataTableComboBox_SelectedIndexChanged(object sender, EventArgs e) {
|
---|
| 131 | UpdateDataRowComboBox();
|
---|
| 132 | }
|
---|
| 133 |
|
---|
| 134 | private void dataRowComboBox_SelectedIndexChanged(object sender, EventArgs e) {
|
---|
[9378] | 135 | RebuildDataTable();
|
---|
[9353] | 136 | }
|
---|
| 137 |
|
---|
[9377] | 138 | private void addLineToChart_Click(object sender, EventArgs e) {
|
---|
| 139 | string resultName = (string)dataTableComboBox.SelectedItem;
|
---|
| 140 | string rowName = (string)dataRowComboBox.SelectedItem;
|
---|
| 141 |
|
---|
[9378] | 142 | foreach (IRun run in runs) {
|
---|
[9377] | 143 | DataTable resTable = (DataTable)run.Results[resultName];
|
---|
| 144 | DataRow row = resTable.Rows[rowName];
|
---|
| 145 | var values = row.Values.ToArray();
|
---|
| 146 |
|
---|
[9706] | 147 | var fittingAlg = fittingComboBox.SelectedItem as IFitting;
|
---|
| 148 | DataRow newRow = fittingAlg.CalculateFittedLine(values, row.Name + " (" + fittingAlg.ToString() + ")");
|
---|
[9377] | 149 |
|
---|
| 150 | if (!resTable.Rows.ContainsKey(newRow.Name))
|
---|
| 151 | resTable.Rows.Add(newRow);
|
---|
| 152 | }
|
---|
| 153 | }
|
---|
| 154 |
|
---|
| 155 | private void addValuesButton_Click(object sender, EventArgs e) {
|
---|
| 156 | string resultName = (string)dataTableComboBox.SelectedItem;
|
---|
| 157 | string rowName = (string)dataRowComboBox.SelectedItem;
|
---|
[9378] | 158 | DoubleMatrix sm = (DoubleMatrix)stringConvertibleMatrixView.Content;
|
---|
[9377] | 159 |
|
---|
| 160 | Content.UpdateOfRunsInProgress = true;
|
---|
| 161 | for (int i = 0; i < runs.Count(); i++) {
|
---|
[9378] | 162 | IRun run = runs[i];
|
---|
[9377] | 163 |
|
---|
| 164 | for (int j = 0; j < sm.ColumnNames.Count(); j++) {
|
---|
[9378] | 165 | if (stringConvertibleMatrixView.DataGridView.Columns[j].Visible) {
|
---|
| 166 | string newResultName = resultName + " " + rowName + " " + sm.ColumnNames.ElementAt(j);
|
---|
| 167 | if (!run.Results.ContainsKey(newResultName)) {
|
---|
| 168 | run.Results.Add(new KeyValuePair<string, Core.IItem>(newResultName, new DoubleValue(sm[i, j])));
|
---|
| 169 | }
|
---|
| 170 | }
|
---|
[9377] | 171 | }
|
---|
| 172 | }
|
---|
| 173 | Content.UpdateOfRunsInProgress = false;
|
---|
| 174 | }
|
---|
| 175 | #endregion
|
---|
| 176 |
|
---|
[9353] | 177 | private void UpdateDataRowComboBox() {
|
---|
| 178 | dataRowComboBox.Items.Clear();
|
---|
| 179 | var resultName = (string)dataTableComboBox.SelectedItem;
|
---|
| 180 | var dataTables = from run in Content
|
---|
| 181 | where run.Results.ContainsKey(resultName)
|
---|
| 182 | select run.Results[resultName] as DataTable;
|
---|
| 183 | var rowNames = (from dataTable in dataTables
|
---|
| 184 | from row in dataTable.Rows
|
---|
| 185 | select row.Name).Distinct().ToArray();
|
---|
| 186 |
|
---|
| 187 | dataRowComboBox.Items.AddRange(rowNames);
|
---|
| 188 | if (dataRowComboBox.Items.Count > 0) dataRowComboBox.SelectedItem = dataRowComboBox.Items[0];
|
---|
| 189 | }
|
---|
| 190 |
|
---|
| 191 | private void UpdateDataTableComboBox() {
|
---|
| 192 | dataTableComboBox.Items.Clear();
|
---|
| 193 | var dataTables = (from run in Content
|
---|
| 194 | from result in run.Results
|
---|
| 195 | where result.Value is DataTable
|
---|
| 196 | select result.Key).Distinct().ToArray();
|
---|
| 197 |
|
---|
| 198 | dataTableComboBox.Items.AddRange(dataTables);
|
---|
| 199 | if (dataTableComboBox.Items.Count > 0) dataTableComboBox.SelectedItem = dataTableComboBox.Items[0];
|
---|
| 200 | }
|
---|
| 201 |
|
---|
[9378] | 202 | private void RebuildDataTable() {
|
---|
[9353] | 203 | string resultName = (string)dataTableComboBox.SelectedItem;
|
---|
| 204 | string rowName = (string)dataRowComboBox.SelectedItem;
|
---|
| 205 | string[] columnNames = new string[] { "Count", "Minimum", "Maximum", "Average", "Median", "Standard Deviation", "Variance", "25th Percentile", "75th Percentile", "Gradient", "Relative Error", "Avg. of Upper 25 %", " Avg. of Lower 25 %", "Avg. of First 25 %", "Avg. of Last 25 %" };
|
---|
| 206 |
|
---|
[9378] | 207 | runs = Content.Where(x => x.Results.ContainsKey(resultName) && x.Visible).ToList();
|
---|
| 208 | DoubleMatrix dt = new DoubleMatrix(runs.Count(), columnNames.Count());
|
---|
[9353] | 209 | dt.RowNames = runs.Select(x => x.Name);
|
---|
| 210 | dt.ColumnNames = columnNames;
|
---|
| 211 |
|
---|
| 212 | int i = 0;
|
---|
| 213 | foreach (Run run in runs) {
|
---|
| 214 | DataTable resTable = (DataTable)run.Results[resultName];
|
---|
[9377] | 215 | dt.SortableView = true;
|
---|
[9353] | 216 | DataRow row = resTable.Rows[rowName];
|
---|
| 217 | var values = row.Values.AsEnumerable();
|
---|
| 218 |
|
---|
| 219 | double cnt = values.Count();
|
---|
| 220 | double min = values.Min();
|
---|
| 221 | double max = values.Max();
|
---|
| 222 | double avg = values.Average();
|
---|
| 223 | double median = values.Median();
|
---|
| 224 | double stdDev = values.StandardDeviation();
|
---|
| 225 | double variance = values.Variance();
|
---|
| 226 | double percentile25 = values.Percentile(0.25);
|
---|
| 227 | double percentile75 = values.Percentile(0.75);
|
---|
| 228 | double k, d, r;
|
---|
[9706] | 229 | LinearLeastSquaresFitting llsf = new LinearLeastSquaresFitting();
|
---|
| 230 | llsf.Calculate(values.ToArray(), out k, out d);
|
---|
| 231 | r = llsf.CalculateError(values.ToArray(), k, d);
|
---|
[9353] | 232 | double lowerAvg = values.OrderBy(x => x).Take((int)(values.Count() * 0.25)).Average();
|
---|
| 233 | double upperAvg = values.OrderByDescending(x => x).Take((int)(values.Count() * 0.25)).Average();
|
---|
| 234 | double firstAvg = values.Take((int)(values.Count() * 0.25)).Average();
|
---|
| 235 | double lastAvg = values.Skip((int)(values.Count() * 0.75)).Average();
|
---|
| 236 |
|
---|
[9378] | 237 | dt[i, 0] = cnt;
|
---|
| 238 | dt[i, 1] = min;
|
---|
| 239 | dt[i, 2] = max;
|
---|
| 240 | dt[i, 3] = avg;
|
---|
| 241 | dt[i, 4] = median;
|
---|
| 242 | dt[i, 5] = stdDev;
|
---|
| 243 | dt[i, 6] = variance;
|
---|
| 244 | dt[i, 7] = percentile25;
|
---|
| 245 | dt[i, 8] = percentile75;
|
---|
| 246 | dt[i, 9] = k;
|
---|
| 247 | dt[i, 10] = r;
|
---|
| 248 | dt[i, 11] = upperAvg;
|
---|
| 249 | dt[i, 12] = lowerAvg;
|
---|
| 250 | dt[i, 13] = firstAvg;
|
---|
| 251 | dt[i, 14] = lastAvg;
|
---|
[9353] | 252 |
|
---|
| 253 | i++;
|
---|
| 254 | }
|
---|
[9378] | 255 | stringConvertibleMatrixView.Content = dt;
|
---|
[9353] | 256 |
|
---|
[9378] | 257 | for (i = 0; i < runs.Count(); i++) {
|
---|
| 258 | stringConvertibleMatrixView.DataGridView.Rows[i].DefaultCellStyle.ForeColor = runs[i].Color;
|
---|
| 259 | }
|
---|
[9353] | 260 | }
|
---|
| 261 | }
|
---|
| 262 | }
|
---|