Free cookie consent management tool by TermsFeed Policy Generator

source: branches/StatisticalTesting/HeuristicLab.Analysis.Statistics/3.3/ChartAnalysisView.cs @ 9389

Last change on this file since 9389 was 9378, checked in by ascheibe, 11 years ago

#2031 fixed some bugs and the coloring in the chart analysis view

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