Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1886 added a project for views

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