Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 8558 was 8558, checked in by ascheibe, 12 years ago

#1886 runs can now be opened in the statistical tabular view

File size: 8.9 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.Analysis.AlgorithmBehavior.Analyzers;
27using HeuristicLab.Common;
28using HeuristicLab.Core.Views;
29using HeuristicLab.Data;
30using HeuristicLab.MainForm;
31using HeuristicLab.Optimization;
32
33namespace HeuristicLab.Analysis.AlgorithmBehavior {
34  [View("RunCollection Statistical Tabular View")]
35  [Content(typeof(RunCollection), false)]
36  public sealed partial class RunCollectionStatisticalTabularView : ItemView {
37    public RunCollectionStatisticalTabularView() {
38      InitializeComponent();
39      stringConvertibleMatrixView.DataGridView.RowHeaderMouseDoubleClick += new DataGridViewCellMouseEventHandler(DataGridView_RowHeaderMouseDoubleClick);
40    }
41
42    void DataGridView_RowHeaderMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e) {
43      if (e.RowIndex >= 0) {
44        IRun run = Content.ElementAt(e.RowIndex);
45        IContentView view = MainFormManager.MainForm.ShowContent(run);
46        if (view != null) {
47          view.ReadOnly = this.ReadOnly;
48          view.Locked = this.Locked;
49        }
50      }
51    }
52
53    public new RunCollection Content {
54      get { return (RunCollection)base.Content; }
55      set { base.Content = value; }
56    }
57
58    public override bool ReadOnly {
59      get { return true; }
60      set { /*not needed because results are always readonly */}
61    }
62
63    protected override void OnContentChanged() {
64      base.OnContentChanged();
65      dataTableComboBox.Items.Clear();
66      dataRowComboBox.Items.Clear();
67
68      if (Content != null) {
69        UpdateDataTableComboBox();
70      }
71    }
72
73    #region events
74    protected override void RegisterContentEvents() {
75      base.RegisterContentEvents();
76      Content.ItemsAdded += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_ItemsAdded);
77      Content.ItemsRemoved += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_ItemsRemoved);
78      Content.CollectionReset += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_CollectionReset);
79
80      RegisterRunEvents(Content);
81    }
82
83    private void RegisterRunEvents(IEnumerable<IRun> runs) {
84      foreach (IRun run in runs)
85        run.Changed += new EventHandler(run_Changed);
86    }
87
88    protected override void DeregisterContentEvents() {
89      base.DeregisterContentEvents();
90      Content.ItemsAdded -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_ItemsAdded);
91      Content.ItemsRemoved -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_ItemsRemoved);
92      Content.CollectionReset -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_CollectionReset);
93
94      DeregisterRunEvents(Content);
95    }
96
97    private void DeregisterRunEvents(IEnumerable<IRun> runs) {
98      foreach (IRun run in runs)
99        run.Changed -= new EventHandler(run_Changed);
100    }
101
102    private void Content_CollectionReset(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<IRun> e) {
103      DeregisterRunEvents(e.OldItems);
104      RegisterRunEvents(e.Items);
105      RebuildCombinedDataTable();
106    }
107
108    private void Content_ItemsRemoved(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<IRun> e) {
109      DeregisterRunEvents(e.Items);
110      RebuildCombinedDataTable();
111    }
112
113    private void Content_ItemsAdded(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<IRun> e) {
114      RegisterRunEvents(e.Items);
115      RebuildCombinedDataTable();
116    }
117
118    private void run_Changed(object sender, EventArgs e) {
119      if (InvokeRequired)
120        this.Invoke(new EventHandler(run_Changed), sender, e);
121      else {
122        IRun run = (IRun)sender;
123        UpdateRun(run);
124      }
125    }
126    #endregion
127
128    private void UpdateRun(IRun run) {
129      //TODO: hacky di hack... this is baaaadddd
130      RebuildCombinedDataTable();
131    }
132
133    private void dataTableComboBox_SelectedIndexChanged(object sender, EventArgs e) {
134      UpdateDataRowComboBox();
135    }
136
137    private void dataRowComboBox_SelectedIndexChanged(object sender, EventArgs e) {
138      RebuildCombinedDataTable();
139    }
140
141    private void UpdateDataRowComboBox() {
142      dataRowComboBox.Items.Clear();
143      var resultName = (string)dataTableComboBox.SelectedItem;
144      var dataTables = from run in Content
145                       where run.Results.ContainsKey(resultName)
146                       select run.Results[resultName] as DataTable;
147      var rowNames = (from dataTable in dataTables
148                      from row in dataTable.Rows
149                      select row.Name).Distinct().ToArray();
150
151      dataRowComboBox.Items.AddRange(rowNames);
152      if (dataRowComboBox.Items.Count > 0) dataRowComboBox.SelectedItem = dataRowComboBox.Items[0];
153    }
154
155    private void UpdateDataTableComboBox() {
156      dataTableComboBox.Items.Clear();
157      var dataTables = (from run in Content
158                        from result in run.Results
159                        where result.Value is DataTable
160                        select result.Key).Distinct().ToArray();
161
162      dataTableComboBox.Items.AddRange(dataTables);
163      if (dataTableComboBox.Items.Count > 0) dataTableComboBox.SelectedItem = dataTableComboBox.Items[0];
164    }
165
166    private void RebuildCombinedDataTable() {
167      string resultName = (string)dataTableComboBox.SelectedItem;
168      string rowName = (string)dataRowComboBox.SelectedItem;
169
170      string[] columnNames = new string[] { "Count", "Minimum", "Maximum", "Average", "Median", "Standard Deviation", "Variance", "25th Percentile", "75th Percentile", "Gradient", "Relative Error" };
171
172      var runs = Content.Where(x => x.Results.ContainsKey(resultName));
173
174      StringMatrix dt = new StringMatrix(runs.Count(), columnNames.Count());
175      dt.RowNames = runs.Select(x => x.Name);
176      dt.ColumnNames = columnNames;
177
178      int i = 0;
179      foreach (Run run in runs) {
180        DataTable resTable = (DataTable)run.Results[resultName];
181        DataRow row = resTable.Rows[rowName];
182        var values = row.Values.AsEnumerable();
183
184        double cnt = values.Count();
185        double min = values.Min();
186        double max = values.Max();
187        double avg = values.Average();
188        double median = values.Median();
189        double stdDev = values.StandardDeviation();
190        double variance = values.Variance();
191        double percentile25 = values.Percentile(0.25);
192        double percentile75 = values.Percentile(0.75);
193        double k, d, r;
194        LinearLeastSquaresFitting.Calculate(values.ToArray(), out k, out d);
195        r = LinearLeastSquaresFitting.CalculateError(values.ToArray(), k, d);
196
197        dt[i, 0] = cnt.ToString();
198        dt[i, 1] = min.ToString();
199        dt[i, 2] = max.ToString();
200        dt[i, 3] = avg.ToString();
201        dt[i, 4] = median.ToString();
202        dt[i, 5] = stdDev.ToString();
203        dt[i, 6] = variance.ToString();
204        dt[i, 7] = percentile25.ToString();
205        dt[i, 8] = percentile75.ToString();
206        dt[i, 9] = k.ToString();
207        dt[i, 10] = r.ToString();
208
209        i++;
210      }
211
212      stringConvertibleMatrixView.Content = dt;
213    }
214
215    private void addLineToChart_Click(object sender, EventArgs e) {
216      string resultName = (string)dataTableComboBox.SelectedItem;
217      string rowName = (string)dataRowComboBox.SelectedItem;
218      var runs = Content.Where(x => x.Results.ContainsKey(resultName));
219
220      foreach (Run run in runs) {
221        DataTable resTable = (DataTable)run.Results[resultName];
222        DataRow row = resTable.Rows[rowName];
223        var values = row.Values.ToArray();
224        double k, d;
225        LinearLeastSquaresFitting.Calculate(values, out k, out d);
226
227        DataRow newRow = new DataRow(row.Name + " Fitted Line");
228        for (int i = 0; i < values.Count(); i++) {
229          newRow.Values.Add(k * i + d);
230        }
231
232        if (!resTable.Rows.ContainsKey(newRow.Name))
233          resTable.Rows.Add(newRow);
234      }
235    }
236  }
237}
Note: See TracBrowser for help on using the repository browser.