Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Analysis.AlgorithmBehavior/HeuristicLab.Analysis.AlgorithmBehavior.Analyzers.Views/3.3/RunCollectionStatisticalTabularView.cs @ 9333

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

#1886 improved run collection views

File size: 12.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.Drawing;
25using System.Linq;
26using System.Windows.Forms;
27using HeuristicLab.Analysis.AlgorithmBehavior.Analyzers;
28using HeuristicLab.Common;
29using HeuristicLab.Core.Views;
30using HeuristicLab.Data;
31using HeuristicLab.MainForm;
32using HeuristicLab.Optimization;
33
34namespace HeuristicLab.Analysis.AlgorithmBehavior.Views {
35  [View("RunCollection Statistical Tabular View")]
36  [Content(typeof(RunCollection), false)]
37  public sealed partial class RunCollectionStatisticalTabularView : ItemView {
38    public RunCollectionStatisticalTabularView() {
39      InitializeComponent();
40      stringConvertibleMatrixView.DataGridView.RowHeaderMouseDoubleClick += new DataGridViewCellMouseEventHandler(DataGridView_RowHeaderMouseDoubleClick);
41    }
42
43    void DataGridView_RowHeaderMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e) {
44      if (e.RowIndex >= 0) {
45        IRun run = Content.ElementAt(e.RowIndex);
46        IContentView view = MainFormManager.MainForm.ShowContent(run);
47        if (view != null) {
48          view.ReadOnly = this.ReadOnly;
49          view.Locked = this.Locked;
50        }
51      }
52    }
53
54    /// <summary>
55    /// Clean up any resources being used.
56    /// </summary>
57    /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
58    protected override void Dispose(bool disposing) {
59      if (disposing && (components != null)) {
60        stringConvertibleMatrixView.DataGridView.RowHeaderMouseDoubleClick -= new DataGridViewCellMouseEventHandler(DataGridView_RowHeaderMouseDoubleClick);
61        components.Dispose();
62      }
63      base.Dispose(disposing);
64    }
65
66    public new RunCollection Content {
67      get { return (RunCollection)base.Content; }
68      set { base.Content = value; }
69    }
70
71    public override bool ReadOnly {
72      get { return true; }
73      set { /*not needed because results are always readonly */}
74    }
75
76    protected override void OnContentChanged() {
77      base.OnContentChanged();
78      dataTableComboBox.Items.Clear();
79      dataRowComboBox.Items.Clear();
80
81      if (Content != null) {
82        UpdateDataTableComboBox();
83      }
84    }
85
86    #region events
87    protected override void RegisterContentEvents() {
88      base.RegisterContentEvents();
89      Content.ItemsAdded += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_ItemsAdded);
90      Content.ItemsRemoved += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_ItemsRemoved);
91      Content.CollectionReset += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_CollectionReset);
92
93      RegisterRunEvents(Content);
94    }
95
96    private void RegisterRunEvents(IEnumerable<IRun> runs) {
97      foreach (IRun run in runs)
98        run.Changed += new EventHandler(run_Changed);
99    }
100
101    protected override void DeregisterContentEvents() {
102      base.DeregisterContentEvents();
103      Content.ItemsAdded -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_ItemsAdded);
104      Content.ItemsRemoved -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_ItemsRemoved);
105      Content.CollectionReset -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_CollectionReset);
106
107      DeregisterRunEvents(Content);
108    }
109
110    private void DeregisterRunEvents(IEnumerable<IRun> runs) {
111      foreach (IRun run in runs)
112        run.Changed -= new EventHandler(run_Changed);
113    }
114
115    private void Content_CollectionReset(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<IRun> e) {
116      DeregisterRunEvents(e.OldItems);
117      RegisterRunEvents(e.Items);
118      RebuildCombinedDataTable();
119    }
120
121    private void Content_ItemsRemoved(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<IRun> e) {
122      DeregisterRunEvents(e.Items);
123      RebuildCombinedDataTable();
124    }
125
126    private void Content_ItemsAdded(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<IRun> e) {
127      RegisterRunEvents(e.Items);
128      RebuildCombinedDataTable();
129    }
130
131    private void run_Changed(object sender, EventArgs e) {
132      if (InvokeRequired)
133        this.Invoke(new EventHandler(run_Changed), sender, e);
134      else {
135        if (!Content.UpdateOfRunsInProgress) {
136          IRun run = (IRun)sender;
137          UpdateRun(run);
138        }
139      }
140    }
141    #endregion
142
143    private void UpdateRun(IRun run) {
144      //TODO: hacky di hack... this is baaaadddd
145      RebuildCombinedDataTable();
146    }
147
148    private void dataTableComboBox_SelectedIndexChanged(object sender, EventArgs e) {
149      UpdateDataRowComboBox();
150    }
151
152    private void dataRowComboBox_SelectedIndexChanged(object sender, EventArgs e) {
153      RebuildCombinedDataTable();
154    }
155
156    private void UpdateDataRowComboBox() {
157      dataRowComboBox.Items.Clear();
158      var resultName = (string)dataTableComboBox.SelectedItem;
159      var dataTables = from run in Content
160                       where run.Results.ContainsKey(resultName)
161                       select run.Results[resultName] as DataTable;
162      var rowNames = (from dataTable in dataTables
163                      from row in dataTable.Rows
164                      select row.Name).Distinct().ToArray();
165
166      dataRowComboBox.Items.AddRange(rowNames);
167      if (dataRowComboBox.Items.Count > 0) dataRowComboBox.SelectedItem = dataRowComboBox.Items[0];
168    }
169
170    private void UpdateDataTableComboBox() {
171      dataTableComboBox.Items.Clear();
172      var dataTables = (from run in Content
173                        from result in run.Results
174                        where result.Value is DataTable
175                        select result.Key).Distinct().ToArray();
176
177      dataTableComboBox.Items.AddRange(dataTables);
178      if (dataTableComboBox.Items.Count > 0) dataTableComboBox.SelectedItem = dataTableComboBox.Items[0];
179    }
180
181    private void RebuildCombinedDataTable() {
182      string resultName = (string)dataTableComboBox.SelectedItem;
183      string rowName = (string)dataRowComboBox.SelectedItem;
184
185      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 %" };
186
187      var runs = Content.Where(x => x.Results.ContainsKey(resultName) && x.Visible);
188
189      StringMatrix dt = new StringMatrix(runs.Count(), columnNames.Count());
190      dt.RowNames = runs.Select(x => x.Name);
191      dt.ColumnNames = columnNames;
192
193      int i = 0;
194      foreach (Run run in runs) {
195        DataTable resTable = (DataTable)run.Results[resultName];
196        DataRow row = resTable.Rows[rowName];
197        var values = row.Values.AsEnumerable();
198
199        double cnt = values.Count();
200        double min = values.Min();
201        double max = values.Max();
202        double avg = values.Average();
203        double median = values.Median();
204        double stdDev = values.StandardDeviation();
205        double variance = values.Variance();
206        double percentile25 = values.Percentile(0.25);
207        double percentile75 = values.Percentile(0.75);
208        double k, d, r;
209        LinearLeastSquaresFitting.Calculate(values.ToArray(), out k, out d);
210        r = LinearLeastSquaresFitting.CalculateError(values.ToArray(), k, d);
211        double lowerAvg = values.OrderBy(x => x).Take((int)(values.Count() * 0.25)).Average();
212        double upperAvg = values.OrderByDescending(x => x).Take((int)(values.Count() * 0.25)).Average();
213        double firstAvg = values.Take((int)(values.Count() * 0.25)).Average();
214        double lastAvg = values.Skip((int)(values.Count() * 0.75)).Average();
215
216        dt[i, 0] = cnt.ToString();
217        dt[i, 1] = min.ToString();
218        dt[i, 2] = max.ToString();
219        dt[i, 3] = avg.ToString();
220        dt[i, 4] = median.ToString();
221        dt[i, 5] = stdDev.ToString();
222        dt[i, 6] = variance.ToString();
223        dt[i, 7] = percentile25.ToString();
224        dt[i, 8] = percentile75.ToString();
225        dt[i, 9] = k.ToString();
226        dt[i, 10] = r.ToString();
227        dt[i, 11] = upperAvg.ToString();
228        dt[i, 12] = lowerAvg.ToString();
229        dt[i, 13] = firstAvg.ToString();
230        dt[i, 14] = lastAvg.ToString();
231
232        i++;
233      }
234
235      stringConvertibleMatrixView.Content = dt;
236    }
237
238    private void addLineToChart_Click(object sender, EventArgs e) {
239      string resultName = (string)dataTableComboBox.SelectedItem;
240      string rowName = (string)dataRowComboBox.SelectedItem;
241      var runs = Content.Where(x => x.Results.ContainsKey(resultName) && x.Visible);
242
243      foreach (Run run in runs) {
244        DataTable resTable = (DataTable)run.Results[resultName];
245        DataRow row = resTable.Rows[rowName];
246        var values = row.Values.ToArray();
247        double k, d;
248        LinearLeastSquaresFitting.Calculate(values, out k, out d);
249
250        DataRow newRow = new DataRow(row.Name + " Fitted Line");
251        for (int i = 0; i < values.Count(); i++) {
252          newRow.Values.Add(k * i + d);
253        }
254
255        if (!resTable.Rows.ContainsKey(newRow.Name))
256          resTable.Rows.Add(newRow);
257      }
258    }
259
260    private void addValuesButton_Click(object sender, EventArgs e) {
261      string resultName = (string)dataTableComboBox.SelectedItem;
262      string rowName = (string)dataRowComboBox.SelectedItem;
263      var runs = Content.Where(x => x.Results.ContainsKey(resultName) && x.Visible);
264      StringMatrix sm = (StringMatrix)stringConvertibleMatrixView.Content;
265
266      Content.UpdateOfRunsInProgress = true;
267      for (int i = 0; i < runs.Count(); i++) {
268        IRun run = runs.ElementAt(i);
269
270        for (int j = 0; j < sm.ColumnNames.Count(); j++) {
271          string newResultName = resultName + " " + rowName + " " + sm.ColumnNames.ElementAt(j);
272          if (!run.Results.ContainsKey(newResultName))
273            run.Results.Add(new KeyValuePair<string, Core.IItem>(newResultName, new DoubleValue(double.Parse(sm[i, j]))));
274        }
275      }
276      Content.UpdateOfRunsInProgress = false;
277      //update column names of run collection
278      Content.Modify();
279    }
280
281    private void colorButton_Click(object sender, EventArgs e) {
282      string resultName = (string)dataTableComboBox.SelectedItem;
283      string rowName = (string)dataRowComboBox.SelectedItem;
284      var runs = Content.Where(x => x.Results.ContainsKey(resultName) && x.Visible);
285      Dictionary<int, double> values = new Dictionary<int, double>();
286
287      if (stringConvertibleMatrixView.DataGridView.CurrentCell != null) {
288        int curIndex = stringConvertibleMatrixView.DataGridView.CurrentCell.ColumnIndex;
289
290        for (int i = 0; i < stringConvertibleMatrixView.Content.Rows; i++) {
291          values[i] = double.Parse(stringConvertibleMatrixView.Content.GetValue(i, curIndex));
292        }
293
294        var orderedValues = values.OrderBy(x => x.Value);
295
296        for (int i = 0; i < orderedValues.Count(); i++) {
297          var row = stringConvertibleMatrixView.DataGridView.Rows[orderedValues.ElementAt(i).Key];
298
299          int r = (int)Math.Round((double)i / (double)orderedValues.Count() * 255.0);
300          int g = (int)Math.Round(((double)orderedValues.Count() - (double)i) / (double)orderedValues.Count() * 255.0);
301
302          Color color = Color.FromArgb(r, g, 0);
303          row.DefaultCellStyle.ForeColor = color;
304        }
305      }
306    }
307  }
308}
Note: See TracBrowser for help on using the repository browser.