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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Linq;
|
---|
25 | using System.Windows.Forms;
|
---|
26 | using HeuristicLab.Common;
|
---|
27 | using HeuristicLab.Core.Views;
|
---|
28 | using HeuristicLab.Data;
|
---|
29 | using HeuristicLab.MainForm;
|
---|
30 | using HeuristicLab.Optimization;
|
---|
31 |
|
---|
32 | namespace HeuristicLab.Analysis.Statistics {
|
---|
33 | [View("RunCollection Chart Analysis View")]
|
---|
34 | [Content(typeof(RunCollection), false)]
|
---|
35 | public sealed partial class ChartAnalysisView : ItemView {
|
---|
36 | public new RunCollection Content {
|
---|
37 | get { return (RunCollection)base.Content; }
|
---|
38 | set { base.Content = value; }
|
---|
39 | }
|
---|
40 |
|
---|
41 | public override bool ReadOnly {
|
---|
42 | get { return true; }
|
---|
43 | set { /*not needed because results are always readonly */}
|
---|
44 | }
|
---|
45 |
|
---|
46 | public ChartAnalysisView() {
|
---|
47 | InitializeComponent();
|
---|
48 | stringConvertibleMatrixView.DataGridView.RowHeaderMouseDoubleClick += new DataGridViewCellMouseEventHandler(DataGridView_RowHeaderMouseDoubleClick);
|
---|
49 | }
|
---|
50 |
|
---|
51 | /// <summary>
|
---|
52 | /// Clean up any resources being used.
|
---|
53 | /// </summary>
|
---|
54 | /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
|
---|
55 | protected override void Dispose(bool disposing) {
|
---|
56 | if (disposing && (components != null)) {
|
---|
57 | stringConvertibleMatrixView.DataGridView.RowHeaderMouseDoubleClick -= new DataGridViewCellMouseEventHandler(DataGridView_RowHeaderMouseDoubleClick);
|
---|
58 | components.Dispose();
|
---|
59 | }
|
---|
60 | base.Dispose(disposing);
|
---|
61 | }
|
---|
62 |
|
---|
63 | #region Content Events
|
---|
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 | 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 | if (!Content.UpdateOfRunsInProgress) {
|
---|
123 | IRun run = (IRun)sender;
|
---|
124 | UpdateRun(run);
|
---|
125 | }
|
---|
126 | }
|
---|
127 | }
|
---|
128 | #endregion
|
---|
129 |
|
---|
130 | #region Events
|
---|
131 | void DataGridView_RowHeaderMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e) {
|
---|
132 | if (e.RowIndex >= 0) {
|
---|
133 | IRun run = Content.ElementAt(stringConvertibleMatrixView.GetRowIndex(e.RowIndex));
|
---|
134 | IContentView view = MainFormManager.MainForm.ShowContent(run);
|
---|
135 | if (view != null) {
|
---|
136 | view.ReadOnly = this.ReadOnly;
|
---|
137 | view.Locked = this.Locked;
|
---|
138 | }
|
---|
139 | }
|
---|
140 | }
|
---|
141 |
|
---|
142 | private void dataTableComboBox_SelectedIndexChanged(object sender, EventArgs e) {
|
---|
143 | UpdateDataRowComboBox();
|
---|
144 | }
|
---|
145 |
|
---|
146 | private void dataRowComboBox_SelectedIndexChanged(object sender, EventArgs e) {
|
---|
147 | RebuildCombinedDataTable();
|
---|
148 | }
|
---|
149 |
|
---|
150 | private void addLineToChart_Click(object sender, EventArgs e) {
|
---|
151 | string resultName = (string)dataTableComboBox.SelectedItem;
|
---|
152 | string rowName = (string)dataRowComboBox.SelectedItem;
|
---|
153 | var runs = Content.Where(x => x.Results.ContainsKey(resultName) && x.Visible);
|
---|
154 |
|
---|
155 | foreach (Run run in runs) {
|
---|
156 | DataTable resTable = (DataTable)run.Results[resultName];
|
---|
157 | DataRow row = resTable.Rows[rowName];
|
---|
158 | var values = row.Values.ToArray();
|
---|
159 | double k, d;
|
---|
160 | LinearLeastSquaresFitting.Calculate(values, out k, out d);
|
---|
161 |
|
---|
162 | DataRow newRow = new DataRow(row.Name + " Fitted Line");
|
---|
163 | for (int i = 0; i < values.Count(); i++) {
|
---|
164 | newRow.Values.Add(k * i + d);
|
---|
165 | }
|
---|
166 |
|
---|
167 | if (!resTable.Rows.ContainsKey(newRow.Name))
|
---|
168 | resTable.Rows.Add(newRow);
|
---|
169 | }
|
---|
170 | }
|
---|
171 |
|
---|
172 | private void addValuesButton_Click(object sender, EventArgs e) {
|
---|
173 | string resultName = (string)dataTableComboBox.SelectedItem;
|
---|
174 | string rowName = (string)dataRowComboBox.SelectedItem;
|
---|
175 | var runs = Content.Where(x => x.Results.ContainsKey(resultName) && x.Visible);
|
---|
176 | StringMatrix sm = (StringMatrix)stringConvertibleMatrixView.Content;
|
---|
177 |
|
---|
178 | Content.UpdateOfRunsInProgress = true;
|
---|
179 | for (int i = 0; i < runs.Count(); i++) {
|
---|
180 | IRun run = runs.ElementAt(i);
|
---|
181 |
|
---|
182 | for (int j = 0; j < sm.ColumnNames.Count(); j++) {
|
---|
183 | string newResultName = resultName + " " + rowName + " " + sm.ColumnNames.ElementAt(j);
|
---|
184 | if (!run.Results.ContainsKey(newResultName))
|
---|
185 | run.Results.Add(new KeyValuePair<string, Core.IItem>(newResultName, new DoubleValue(double.Parse(sm[i, j]))));
|
---|
186 | }
|
---|
187 | }
|
---|
188 | Content.UpdateOfRunsInProgress = false;
|
---|
189 | //update column names of run collection
|
---|
190 | Content.Modify();
|
---|
191 | }
|
---|
192 | #endregion
|
---|
193 |
|
---|
194 | private void UpdateRun(IRun run) {
|
---|
195 | //TODO: hacky di hack... this is baaaadddd
|
---|
196 | RebuildCombinedDataTable();
|
---|
197 | }
|
---|
198 |
|
---|
199 | private void UpdateDataRowComboBox() {
|
---|
200 | dataRowComboBox.Items.Clear();
|
---|
201 | var resultName = (string)dataTableComboBox.SelectedItem;
|
---|
202 | var dataTables = from run in Content
|
---|
203 | where run.Results.ContainsKey(resultName)
|
---|
204 | select run.Results[resultName] as DataTable;
|
---|
205 | var rowNames = (from dataTable in dataTables
|
---|
206 | from row in dataTable.Rows
|
---|
207 | select row.Name).Distinct().ToArray();
|
---|
208 |
|
---|
209 | dataRowComboBox.Items.AddRange(rowNames);
|
---|
210 | if (dataRowComboBox.Items.Count > 0) dataRowComboBox.SelectedItem = dataRowComboBox.Items[0];
|
---|
211 | }
|
---|
212 |
|
---|
213 | private void UpdateDataTableComboBox() {
|
---|
214 | dataTableComboBox.Items.Clear();
|
---|
215 | var dataTables = (from run in Content
|
---|
216 | from result in run.Results
|
---|
217 | where result.Value is DataTable
|
---|
218 | select result.Key).Distinct().ToArray();
|
---|
219 |
|
---|
220 | dataTableComboBox.Items.AddRange(dataTables);
|
---|
221 | if (dataTableComboBox.Items.Count > 0) dataTableComboBox.SelectedItem = dataTableComboBox.Items[0];
|
---|
222 | }
|
---|
223 |
|
---|
224 | private void RebuildCombinedDataTable() {
|
---|
225 | string resultName = (string)dataTableComboBox.SelectedItem;
|
---|
226 | string rowName = (string)dataRowComboBox.SelectedItem;
|
---|
227 | 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 %" };
|
---|
228 |
|
---|
229 | var runs = Content.Where(x => x.Results.ContainsKey(resultName) && x.Visible);
|
---|
230 |
|
---|
231 | StringMatrix dt = new StringMatrix(runs.Count(), columnNames.Count());
|
---|
232 | dt.RowNames = runs.Select(x => x.Name);
|
---|
233 | dt.ColumnNames = columnNames;
|
---|
234 |
|
---|
235 | int i = 0;
|
---|
236 | foreach (Run run in runs) {
|
---|
237 | DataTable resTable = (DataTable)run.Results[resultName];
|
---|
238 | dt.SortableView = true;
|
---|
239 | DataRow row = resTable.Rows[rowName];
|
---|
240 | var values = row.Values.AsEnumerable();
|
---|
241 |
|
---|
242 | double cnt = values.Count();
|
---|
243 | double min = values.Min();
|
---|
244 | double max = values.Max();
|
---|
245 | double avg = values.Average();
|
---|
246 | double median = values.Median();
|
---|
247 | double stdDev = values.StandardDeviation();
|
---|
248 | double variance = values.Variance();
|
---|
249 | double percentile25 = values.Percentile(0.25);
|
---|
250 | double percentile75 = values.Percentile(0.75);
|
---|
251 | double k, d, r;
|
---|
252 | LinearLeastSquaresFitting.Calculate(values.ToArray(), out k, out d);
|
---|
253 | r = LinearLeastSquaresFitting.CalculateError(values.ToArray(), k, d);
|
---|
254 | double lowerAvg = values.OrderBy(x => x).Take((int)(values.Count() * 0.25)).Average();
|
---|
255 | double upperAvg = values.OrderByDescending(x => x).Take((int)(values.Count() * 0.25)).Average();
|
---|
256 | double firstAvg = values.Take((int)(values.Count() * 0.25)).Average();
|
---|
257 | double lastAvg = values.Skip((int)(values.Count() * 0.75)).Average();
|
---|
258 |
|
---|
259 | dt[i, 0] = cnt.ToString();
|
---|
260 | dt[i, 1] = min.ToString();
|
---|
261 | dt[i, 2] = max.ToString();
|
---|
262 | dt[i, 3] = avg.ToString();
|
---|
263 | dt[i, 4] = median.ToString();
|
---|
264 | dt[i, 5] = stdDev.ToString();
|
---|
265 | dt[i, 6] = variance.ToString();
|
---|
266 | dt[i, 7] = percentile25.ToString();
|
---|
267 | dt[i, 8] = percentile75.ToString();
|
---|
268 | dt[i, 9] = k.ToString();
|
---|
269 | dt[i, 10] = r.ToString();
|
---|
270 | dt[i, 11] = upperAvg.ToString();
|
---|
271 | dt[i, 12] = lowerAvg.ToString();
|
---|
272 | dt[i, 13] = firstAvg.ToString();
|
---|
273 | dt[i, 14] = lastAvg.ToString();
|
---|
274 |
|
---|
275 | i++;
|
---|
276 | }
|
---|
277 |
|
---|
278 | stringConvertibleMatrixView.Content = dt;
|
---|
279 | }
|
---|
280 | }
|
---|
281 | }
|
---|