Free cookie consent management tool by TermsFeed Policy Generator

source: branches/StatisticalTesting/HeuristicLab.Analysis.Statistics/3.3/ChartAnalysisView.cs @ 9706

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

#2031

  • added exponential fitting
  • added logarithmic fitting
  • refactored fitting code
  • updated license headers
File size: 10.5 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2013 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;
31using HeuristicLab.PluginInfrastructure;
32
33namespace HeuristicLab.Analysis.Statistics {
34  [View("RunCollection Chart Analysis View")]
35  [Content(typeof(RunCollection), false)]
36  public sealed partial class ChartAnalysisView : ItemView {
37    public new RunCollection Content {
38      get { return (RunCollection)base.Content; }
39      set { base.Content = value; }
40    }
41
42    public override bool ReadOnly {
43      get { return true; }
44      set { /*not needed because results are always readonly */}
45    }
46
47    private List<IRun> runs;
48
49    public ChartAnalysisView() {
50      InitializeComponent();
51      stringConvertibleMatrixView.DataGridView.RowHeaderMouseDoubleClick += new DataGridViewCellMouseEventHandler(DataGridView_RowHeaderMouseDoubleClick);
52
53      var fittingAlgs = ApplicationManager.Manager.GetInstances<IFitting>();
54      foreach (var fit in fittingAlgs) {
55        fittingComboBox.Items.Add(fit);
56      }
57      fittingComboBox.SelectedIndex = 0;
58    }
59
60    /// <summary>
61    /// Clean up any resources being used.
62    /// </summary>
63    /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
64    protected override void Dispose(bool disposing) {
65      if (disposing && (components != null)) {
66        stringConvertibleMatrixView.DataGridView.RowHeaderMouseDoubleClick -= new DataGridViewCellMouseEventHandler(DataGridView_RowHeaderMouseDoubleClick);
67        components.Dispose();
68      }
69      base.Dispose(disposing);
70    }
71
72    #region Content Events
73    protected override void OnContentChanged() {
74      base.OnContentChanged();
75      dataTableComboBox.Items.Clear();
76      dataRowComboBox.Items.Clear();
77
78      if (Content != null) {
79        UpdateDataTableComboBox();
80      }
81    }
82
83    protected override void RegisterContentEvents() {
84      base.RegisterContentEvents();
85      Content.ItemsAdded += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_ItemsAdded);
86      Content.ItemsRemoved += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_ItemsRemoved);
87      Content.CollectionReset += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_CollectionReset);
88      Content.UpdateOfRunsInProgressChanged += Content_UpdateOfRunsInProgressChanged;
89    }
90
91    protected override void DeregisterContentEvents() {
92      base.DeregisterContentEvents();
93      Content.ItemsAdded -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_ItemsAdded);
94      Content.ItemsRemoved -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_ItemsRemoved);
95      Content.CollectionReset -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_CollectionReset);
96      Content.UpdateOfRunsInProgressChanged -= Content_UpdateOfRunsInProgressChanged;
97    }
98
99    private void Content_CollectionReset(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<IRun> e) {
100      RebuildDataTable();
101    }
102
103    private void Content_ItemsRemoved(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<IRun> e) {
104      RebuildDataTable();
105    }
106
107    private void Content_ItemsAdded(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<IRun> e) {
108      RebuildDataTable();
109    }
110
111    void Content_UpdateOfRunsInProgressChanged(object sender, EventArgs e) {
112      if (!Content.UpdateOfRunsInProgress) {
113        RebuildDataTable();
114      }
115    }
116    #endregion
117
118    #region Events
119    void DataGridView_RowHeaderMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e) {
120      if (e.RowIndex >= 0) {
121        IRun run = runs[stringConvertibleMatrixView.GetRowIndex(e.RowIndex)];
122        IContentView view = MainFormManager.MainForm.ShowContent(run);
123        if (view != null) {
124          view.ReadOnly = this.ReadOnly;
125          view.Locked = this.Locked;
126        }
127      }
128    }
129
130    private void dataTableComboBox_SelectedIndexChanged(object sender, EventArgs e) {
131      UpdateDataRowComboBox();
132    }
133
134    private void dataRowComboBox_SelectedIndexChanged(object sender, EventArgs e) {
135      RebuildDataTable();
136    }
137
138    private void addLineToChart_Click(object sender, EventArgs e) {
139      string resultName = (string)dataTableComboBox.SelectedItem;
140      string rowName = (string)dataRowComboBox.SelectedItem;
141
142      foreach (IRun run in runs) {
143        DataTable resTable = (DataTable)run.Results[resultName];
144        DataRow row = resTable.Rows[rowName];
145        var values = row.Values.ToArray();
146
147        var fittingAlg = fittingComboBox.SelectedItem as IFitting;
148        DataRow newRow = fittingAlg.CalculateFittedLine(values, row.Name + " (" + fittingAlg.ToString() + ")");
149
150        if (!resTable.Rows.ContainsKey(newRow.Name))
151          resTable.Rows.Add(newRow);
152      }
153    }
154
155    private void addValuesButton_Click(object sender, EventArgs e) {
156      string resultName = (string)dataTableComboBox.SelectedItem;
157      string rowName = (string)dataRowComboBox.SelectedItem;
158      DoubleMatrix sm = (DoubleMatrix)stringConvertibleMatrixView.Content;
159
160      Content.UpdateOfRunsInProgress = true;
161      for (int i = 0; i < runs.Count(); i++) {
162        IRun run = runs[i];
163
164        for (int j = 0; j < sm.ColumnNames.Count(); j++) {
165          if (stringConvertibleMatrixView.DataGridView.Columns[j].Visible) {
166            string newResultName = resultName + " " + rowName + " " + sm.ColumnNames.ElementAt(j);
167            if (!run.Results.ContainsKey(newResultName)) {
168              run.Results.Add(new KeyValuePair<string, Core.IItem>(newResultName, new DoubleValue(sm[i, j])));
169            }
170          }
171        }
172      }
173      Content.UpdateOfRunsInProgress = false;
174    }
175    #endregion
176
177    private void UpdateDataRowComboBox() {
178      dataRowComboBox.Items.Clear();
179      var resultName = (string)dataTableComboBox.SelectedItem;
180      var dataTables = from run in Content
181                       where run.Results.ContainsKey(resultName)
182                       select run.Results[resultName] as DataTable;
183      var rowNames = (from dataTable in dataTables
184                      from row in dataTable.Rows
185                      select row.Name).Distinct().ToArray();
186
187      dataRowComboBox.Items.AddRange(rowNames);
188      if (dataRowComboBox.Items.Count > 0) dataRowComboBox.SelectedItem = dataRowComboBox.Items[0];
189    }
190
191    private void UpdateDataTableComboBox() {
192      dataTableComboBox.Items.Clear();
193      var dataTables = (from run in Content
194                        from result in run.Results
195                        where result.Value is DataTable
196                        select result.Key).Distinct().ToArray();
197
198      dataTableComboBox.Items.AddRange(dataTables);
199      if (dataTableComboBox.Items.Count > 0) dataTableComboBox.SelectedItem = dataTableComboBox.Items[0];
200    }
201
202    private void RebuildDataTable() {
203      string resultName = (string)dataTableComboBox.SelectedItem;
204      string rowName = (string)dataRowComboBox.SelectedItem;
205      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 %" };
206
207      runs = Content.Where(x => x.Results.ContainsKey(resultName) && x.Visible).ToList();
208      DoubleMatrix dt = new DoubleMatrix(runs.Count(), columnNames.Count());
209      dt.RowNames = runs.Select(x => x.Name);
210      dt.ColumnNames = columnNames;
211
212      int i = 0;
213      foreach (Run run in runs) {
214        DataTable resTable = (DataTable)run.Results[resultName];
215        dt.SortableView = true;
216        DataRow row = resTable.Rows[rowName];
217        var values = row.Values.AsEnumerable();
218
219        double cnt = values.Count();
220        double min = values.Min();
221        double max = values.Max();
222        double avg = values.Average();
223        double median = values.Median();
224        double stdDev = values.StandardDeviation();
225        double variance = values.Variance();
226        double percentile25 = values.Percentile(0.25);
227        double percentile75 = values.Percentile(0.75);
228        double k, d, r;
229        LinearLeastSquaresFitting llsf = new LinearLeastSquaresFitting();
230        llsf.Calculate(values.ToArray(), out k, out d);
231        r = llsf.CalculateError(values.ToArray(), k, d);
232        double lowerAvg = values.OrderBy(x => x).Take((int)(values.Count() * 0.25)).Average();
233        double upperAvg = values.OrderByDescending(x => x).Take((int)(values.Count() * 0.25)).Average();
234        double firstAvg = values.Take((int)(values.Count() * 0.25)).Average();
235        double lastAvg = values.Skip((int)(values.Count() * 0.75)).Average();
236
237        dt[i, 0] = cnt;
238        dt[i, 1] = min;
239        dt[i, 2] = max;
240        dt[i, 3] = avg;
241        dt[i, 4] = median;
242        dt[i, 5] = stdDev;
243        dt[i, 6] = variance;
244        dt[i, 7] = percentile25;
245        dt[i, 8] = percentile75;
246        dt[i, 9] = k;
247        dt[i, 10] = r;
248        dt[i, 11] = upperAvg;
249        dt[i, 12] = lowerAvg;
250        dt[i, 13] = firstAvg;
251        dt[i, 14] = lastAvg;
252
253        i++;
254      }
255      stringConvertibleMatrixView.Content = dt;
256
257      for (i = 0; i < runs.Count(); i++) {
258        stringConvertibleMatrixView.DataGridView.Rows[i].DefaultCellStyle.ForeColor = runs[i].Color;
259      }
260    }
261  }
262}
Note: See TracBrowser for help on using the repository browser.