Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1886

  • fixed a cloning bug
  • fixed forgotten event handler deregistration
  • added a run collection view for analyzing parameter influences
File size: 12.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.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        IRun run = (IRun)sender;
136        UpdateRun(run);
137      }
138    }
139    #endregion
140
141    private void UpdateRun(IRun run) {
142      //TODO: hacky di hack... this is baaaadddd
143      RebuildCombinedDataTable();
144    }
145
146    private void dataTableComboBox_SelectedIndexChanged(object sender, EventArgs e) {
147      UpdateDataRowComboBox();
148    }
149
150    private void dataRowComboBox_SelectedIndexChanged(object sender, EventArgs e) {
151      RebuildCombinedDataTable();
152    }
153
154    private void UpdateDataRowComboBox() {
155      dataRowComboBox.Items.Clear();
156      var resultName = (string)dataTableComboBox.SelectedItem;
157      var dataTables = from run in Content
158                       where run.Results.ContainsKey(resultName)
159                       select run.Results[resultName] as DataTable;
160      var rowNames = (from dataTable in dataTables
161                      from row in dataTable.Rows
162                      select row.Name).Distinct().ToArray();
163
164      dataRowComboBox.Items.AddRange(rowNames);
165      if (dataRowComboBox.Items.Count > 0) dataRowComboBox.SelectedItem = dataRowComboBox.Items[0];
166    }
167
168    private void UpdateDataTableComboBox() {
169      dataTableComboBox.Items.Clear();
170      var dataTables = (from run in Content
171                        from result in run.Results
172                        where result.Value is DataTable
173                        select result.Key).Distinct().ToArray();
174
175      dataTableComboBox.Items.AddRange(dataTables);
176      if (dataTableComboBox.Items.Count > 0) dataTableComboBox.SelectedItem = dataTableComboBox.Items[0];
177    }
178
179    private void RebuildCombinedDataTable() {
180      string resultName = (string)dataTableComboBox.SelectedItem;
181      string rowName = (string)dataRowComboBox.SelectedItem;
182
183      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 %" };
184
185      var runs = Content.Where(x => x.Results.ContainsKey(resultName) && x.Visible);
186
187      StringMatrix dt = new StringMatrix(runs.Count(), columnNames.Count());
188      dt.RowNames = runs.Select(x => x.Name);
189      dt.ColumnNames = columnNames;
190
191      int i = 0;
192      foreach (Run run in runs) {
193        DataTable resTable = (DataTable)run.Results[resultName];
194        DataRow row = resTable.Rows[rowName];
195        var values = row.Values.AsEnumerable();
196
197        double cnt = values.Count();
198        double min = values.Min();
199        double max = values.Max();
200        double avg = values.Average();
201        double median = values.Median();
202        double stdDev = values.StandardDeviation();
203        double variance = values.Variance();
204        double percentile25 = values.Percentile(0.25);
205        double percentile75 = values.Percentile(0.75);
206        double k, d, r;
207        LinearLeastSquaresFitting.Calculate(values.ToArray(), out k, out d);
208        r = LinearLeastSquaresFitting.CalculateError(values.ToArray(), k, d);
209        double lowerAvg = values.OrderBy(x => x).Take((int)(values.Count() * 0.25)).Average();
210        double upperAvg = values.OrderByDescending(x => x).Take((int)(values.Count() * 0.25)).Average();
211        double firstAvg = values.Take((int)(values.Count() * 0.25)).Average();
212        double lastAvg = values.Skip((int)(values.Count() * 0.75)).Average();
213
214        dt[i, 0] = cnt.ToString();
215        dt[i, 1] = min.ToString();
216        dt[i, 2] = max.ToString();
217        dt[i, 3] = avg.ToString();
218        dt[i, 4] = median.ToString();
219        dt[i, 5] = stdDev.ToString();
220        dt[i, 6] = variance.ToString();
221        dt[i, 7] = percentile25.ToString();
222        dt[i, 8] = percentile75.ToString();
223        dt[i, 9] = k.ToString();
224        dt[i, 10] = r.ToString();
225        dt[i, 11] = upperAvg.ToString();
226        dt[i, 12] = lowerAvg.ToString();
227        dt[i, 13] = firstAvg.ToString();
228        dt[i, 14] = lastAvg.ToString();
229
230        i++;
231      }
232
233      stringConvertibleMatrixView.Content = dt;
234    }
235
236    private void addLineToChart_Click(object sender, EventArgs e) {
237      string resultName = (string)dataTableComboBox.SelectedItem;
238      string rowName = (string)dataRowComboBox.SelectedItem;
239      var runs = Content.Where(x => x.Results.ContainsKey(resultName) && x.Visible);
240
241      foreach (Run run in runs) {
242        DataTable resTable = (DataTable)run.Results[resultName];
243        DataRow row = resTable.Rows[rowName];
244        var values = row.Values.ToArray();
245        double k, d;
246        LinearLeastSquaresFitting.Calculate(values, out k, out d);
247
248        DataRow newRow = new DataRow(row.Name + " Fitted Line");
249        for (int i = 0; i < values.Count(); i++) {
250          newRow.Values.Add(k * i + d);
251        }
252
253        if (!resTable.Rows.ContainsKey(newRow.Name))
254          resTable.Rows.Add(newRow);
255      }
256    }
257
258    private void addValuesButton_Click(object sender, EventArgs e) {
259      string resultName = (string)dataTableComboBox.SelectedItem;
260      string rowName = (string)dataRowComboBox.SelectedItem;
261      var runs = Content.Where(x => x.Results.ContainsKey(resultName) && x.Visible);
262      StringMatrix sm = (StringMatrix)stringConvertibleMatrixView.Content;
263
264      for (int i = 0; i < runs.Count(); i++) {
265        IRun run = runs.ElementAt(i);
266
267        for (int j = 0; j < sm.ColumnNames.Count(); j++) {
268          string newResultName = resultName + " " + rowName + " " + sm.ColumnNames.ElementAt(j);
269          if (!run.Results.ContainsKey(newResultName))
270            run.Results.Add(new KeyValuePair<string, Core.IItem>(newResultName, new DoubleValue(double.Parse(sm[i, j]))));
271        }
272      }
273      //update column names of run collection
274      Content.Modify();
275    }
276
277    private void colorButton_Click(object sender, EventArgs e) {
278      string resultName = (string)dataTableComboBox.SelectedItem;
279      string rowName = (string)dataRowComboBox.SelectedItem;
280      var runs = Content.Where(x => x.Results.ContainsKey(resultName) && x.Visible);
281      Dictionary<int, double> values = new Dictionary<int, double>();
282
283      if (stringConvertibleMatrixView.DataGridView.CurrentCell != null) {
284        int curIndex = stringConvertibleMatrixView.DataGridView.CurrentCell.ColumnIndex;
285
286        for (int i = 0; i < stringConvertibleMatrixView.Content.Rows; i++) {
287          values[i] = double.Parse(stringConvertibleMatrixView.Content.GetValue(i, curIndex));
288        }
289
290        var orderedValues = values.OrderBy(x => x.Value);
291
292        for (int i = 0; i < orderedValues.Count(); i++) {
293          var row = stringConvertibleMatrixView.DataGridView.Rows[orderedValues.ElementAt(i).Key];
294
295          int r = (int)Math.Round((double)i / (double)orderedValues.Count() * 255.0);
296          int g = (int)Math.Round(((double)orderedValues.Count() - (double)i) / (double)orderedValues.Count() * 255.0);
297
298          Color color = Color.FromArgb(r, g, 0);
299          row.DefaultCellStyle.ForeColor = color;
300        }
301      }
302    }
303  }
304}
Note: See TracBrowser for help on using the repository browser.