Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 8502 was 8502, checked in by ascheibe, 10 years ago

#1886 added a run collection view that shows statistical information for data table rows

File size: 7.2 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.AlgorithmBehavior {
33  [View("RunCollection Statistical Tabular View")]
34  [Content(typeof(RunCollection), false)]
35  public sealed partial class RunCollectionStatisticalTabularView : ItemView {
36    public RunCollectionStatisticalTabularView() {
37      InitializeComponent();
38    }
39
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
50    protected override void OnContentChanged() {
51      base.OnContentChanged();
52      dataTableComboBox.Items.Clear();
53      dataRowComboBox.Items.Clear();
54
55      if (Content != null) {
56        UpdateDataTableComboBox();
57      }
58    }
59
60    #region events
61    protected override void RegisterContentEvents() {
62      base.RegisterContentEvents();
63      Content.ItemsAdded += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_ItemsAdded);
64      Content.ItemsRemoved += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_ItemsRemoved);
65      Content.CollectionReset += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_CollectionReset);
66
67      RegisterRunEvents(Content);
68    }
69
70    private void RegisterRunEvents(IEnumerable<IRun> runs) {
71      foreach (IRun run in runs)
72        run.Changed += new EventHandler(run_Changed);
73    }
74
75    protected override void DeregisterContentEvents() {
76      base.DeregisterContentEvents();
77      Content.ItemsAdded -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_ItemsAdded);
78      Content.ItemsRemoved -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_ItemsRemoved);
79      Content.CollectionReset -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_CollectionReset);
80
81      DeregisterRunEvents(Content);
82    }
83
84    private void DeregisterRunEvents(IEnumerable<IRun> runs) {
85      foreach (IRun run in runs)
86        run.Changed -= new EventHandler(run_Changed);
87    }
88
89    private void Content_CollectionReset(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<IRun> e) {
90      DeregisterRunEvents(e.OldItems);
91      RegisterRunEvents(e.Items);
92      RebuildCombinedDataTable();
93    }
94
95    private void Content_ItemsRemoved(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<IRun> e) {
96      DeregisterRunEvents(e.Items);
97      RebuildCombinedDataTable();
98    }
99
100    private void Content_ItemsAdded(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<IRun> e) {
101      RegisterRunEvents(e.Items);
102      RebuildCombinedDataTable();
103    }
104
105    private void run_Changed(object sender, EventArgs e) {
106      if (InvokeRequired)
107        this.Invoke(new EventHandler(run_Changed), sender, e);
108      else {
109        IRun run = (IRun)sender;
110        UpdateRun(run);
111      }
112    }
113    #endregion
114
115    private void UpdateRun(IRun run) {
116      //TODO: hacky di hack... this is baaaadddd
117      RebuildCombinedDataTable();
118    }
119
120    private void dataTableComboBox_SelectedIndexChanged(object sender, EventArgs e) {
121      UpdateDataRowComboBox();
122    }
123
124    private void dataRowComboBox_SelectedIndexChanged(object sender, EventArgs e) {
125      RebuildCombinedDataTable();
126    }
127
128    private void UpdateDataRowComboBox() {
129      dataRowComboBox.Items.Clear();
130      var resultName = (string)dataTableComboBox.SelectedItem;
131      var dataTables = from run in Content
132                       where run.Results.ContainsKey(resultName)
133                       select run.Results[resultName] as DataTable;
134      var rowNames = (from dataTable in dataTables
135                      from row in dataTable.Rows
136                      select row.Name).Distinct().ToArray();
137
138      dataRowComboBox.Items.AddRange(rowNames);
139      if (dataRowComboBox.Items.Count > 0) dataRowComboBox.SelectedItem = dataRowComboBox.Items[0];
140    }
141
142    private void UpdateDataTableComboBox() {
143      dataTableComboBox.Items.Clear();
144      var dataTables = (from run in Content
145                        from result in run.Results
146                        where result.Value is DataTable
147                        select result.Key).Distinct().ToArray();
148
149      dataTableComboBox.Items.AddRange(dataTables);
150      if (dataTableComboBox.Items.Count > 0) dataTableComboBox.SelectedItem = dataTableComboBox.Items[0];
151    }
152
153    private void RebuildCombinedDataTable() {
154      string resultName = (string)dataTableComboBox.SelectedItem;
155      string rowName = (string)dataRowComboBox.SelectedItem;
156
157      string[] rowNames = new string[] { "Count", "Minimum", "Maximum", "Average", "Median", "Standard Deviation", "Variance", "25th Percentile", "75th Percentile" };
158
159      var runs = Content.Where(x => x.Results.ContainsKey(resultName));
160
161      StringMatrix dt = new StringMatrix(rowNames.Count(), runs.Count());
162      dt.RowNames = rowNames;
163      dt.ColumnNames = runs.Select(x => x.Name);
164
165      int i = 0;
166      foreach (Run run in runs) {
167        DataTable resTable = (DataTable)run.Results[resultName];
168        DataRow row = resTable.Rows[rowName];
169        var values = row.Values.AsEnumerable();
170
171        double cnt = values.Count();
172        double min = values.Min();
173        double max = values.Max();
174        double avg = values.Average();
175        double median = values.Median();
176        double stdDev = values.StandardDeviation();
177        double variance = values.Variance();
178        double percentile25 = values.Percentile(0.25);
179        double percentile75 = values.Percentile(0.75);
180
181        dt[0, i] = cnt.ToString();
182        dt[1, i] = min.ToString();
183        dt[2, i] = max.ToString();
184        dt[3, i] = avg.ToString();
185        dt[4, i] = median.ToString();
186        dt[5, i] = stdDev.ToString();
187        dt[6, i] = variance.ToString();
188        dt[7, i] = percentile25.ToString();
189        dt[8, i] = percentile75.ToString();
190
191        i++;
192      }
193
194      stringConvertibleMatrixView.Content = dt;
195    }
196  }
197}
Note: See TracBrowser for help on using the repository browser.