Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2817-BinPackingSpeedup/HeuristicLab.DataPreprocessing.Views/3.4/StatisticsView.cs @ 15448

Last change on this file since 15448 was 15210, checked in by pfleck, 7 years ago

#2709 Fixed some small issues and some default behavior.

File size: 10.2 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 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;
24using System.Collections.Generic;
25using System.Linq;
26using System.Windows.Forms;
27using HeuristicLab.Core.Views;
28using HeuristicLab.Data;
29using HeuristicLab.MainForm;
30using HeuristicLab.MainForm.WindowsForms;
31
32namespace HeuristicLab.DataPreprocessing.Views {
33  [View("Statistics View")]
34  [Content(typeof(StatisticsContent), true)]
35  public partial class StatisticsView : ItemView {
36    private bool horizontal = false;
37    private StringMatrix statisticsMatrix;
38    private static readonly string[] StatisticsNames = new[] {
39      "Type",
40      "Missing Values",
41      "Min",
42      "Max",
43      "Median",
44      "Average",
45      "Std. Deviation",
46      "Variance",
47      "25th Percentile",
48      "75th Percentile",
49      "Most Common Value",
50      "Num. diff. Values"
51    };
52
53    public new StatisticsContent Content {
54      get { return (StatisticsContent)base.Content; }
55      set { base.Content = value; }
56    }
57
58    public StatisticsView() {
59      InitializeComponent();
60    }
61
62    protected override void OnContentChanged() {
63      base.OnContentChanged();
64      if (Content == null) {
65        rowsTextBox.Text = string.Empty;
66        columnsTextBox.Text = string.Empty;
67        numericColumnsTextBox.Text = string.Empty;
68        nominalColumnsTextBox5.Text = string.Empty;
69        missingValuesTextBox.Text = string.Empty;
70        totalValuesTextBox.Text = string.Empty;
71        stringMatrixView.Content = null;
72        statisticsMatrix = null;
73      } else {
74        UpdateData();
75      }
76    }
77
78    protected override void RegisterContentEvents() {
79      base.RegisterContentEvents();
80      Content.Changed += Content_Changed;
81    }
82
83    protected override void DeregisterContentEvents() {
84      Content.Changed -= Content_Changed;
85      base.DeregisterContentEvents();
86    }
87
88    private void UpdateData(Dictionary<string, bool> oldVisibility = null) {
89      var logic = Content.StatisticsLogic;
90      rowsTextBox.Text = logic.GetRowCount().ToString();
91      columnsTextBox.Text = logic.GetColumnCount().ToString();
92      numericColumnsTextBox.Text = logic.GetNumericColumnCount().ToString();
93      nominalColumnsTextBox5.Text = logic.GetNominalColumnCount().ToString();
94      missingValuesTextBox.Text = logic.GetMissingValueCount().ToString();
95      totalValuesTextBox.Text = (logic.GetColumnCount() * logic.GetRowCount() - logic.GetMissingValueCount()).ToString();
96
97      var variableNames = Content.PreprocessingData.VariableNames.ToList();
98      if (horizontal)
99        statisticsMatrix = new StringMatrix(StatisticsNames.Length, Content.PreprocessingData.Columns) {
100          RowNames = StatisticsView.StatisticsNames,
101          ColumnNames = variableNames
102        };
103      else
104        statisticsMatrix = new StringMatrix(Content.PreprocessingData.Columns, StatisticsNames.Length) {
105          RowNames = variableNames,
106          ColumnNames = StatisticsView.StatisticsNames
107        };
108
109      for (int i = 0; i < logic.GetColumnCount(); i++) {
110        var data = GetStatistics(i);
111        for (int j = 0; j < data.Count; j++) {
112          if (horizontal)
113            statisticsMatrix[j, i] = data[j];
114          else
115            statisticsMatrix[i, j] = data[j];
116        }
117      }
118
119      stringMatrixView.Parent.SuspendRepaint();
120      stringMatrixView.Content = statisticsMatrix;
121
122      var grid = stringMatrixView.DataGridView;
123      int idx = 0;
124      var list = horizontal ? grid.Columns : grid.Rows as IList;
125      foreach (DataGridViewBand band in list) {
126        var variable = variableNames[idx++];
127        if (oldVisibility != null) {
128          band.Visible = !oldVisibility.ContainsKey(variable) || oldVisibility[variable];
129        }
130      }
131      if (horizontal)
132        stringMatrixView.UpdateColumnHeaders();
133      else
134        stringMatrixView.UpdateRowHeaders();
135
136      stringMatrixView.DataGridView.AutoResizeColumns();
137      stringMatrixView.Parent.ResumeRepaint(true);
138    }
139
140    private List<string> GetStatistics(int varIdx) {
141      List<string> list;
142      var logic = Content.StatisticsLogic;
143      if (logic.VariableHasType<double>(varIdx)) {
144        list = GetDoubleColumns(varIdx);
145      } else if (logic.VariableHasType<string>(varIdx)) {
146        list = GetStringColumns(varIdx);
147      } else if (logic.VariableHasType<DateTime>(varIdx)) {
148        list = GetDateTimeColumns(varIdx);
149      } else {
150        list = new List<string>();
151        for (int j = 0; j < StatisticsNames.Length; ++j) {
152          list.Add("unknown column type");
153        }
154      }
155      return list;
156    }
157
158    private List<string> GetDoubleColumns(int statIdx) {
159      var logic = Content.StatisticsLogic;
160      return new List<string> {
161        logic.GetColumnTypeAsString(statIdx),
162        logic.GetMissingValueCount(statIdx).ToString(),
163        logic.GetMin<double>(statIdx, double.NaN).ToString(),
164        logic.GetMax<double>(statIdx, double.NaN).ToString(),
165        logic.GetMedian(statIdx).ToString(),
166        logic.GetAverage(statIdx).ToString(),
167        logic.GetStandardDeviation(statIdx).ToString(),
168        logic.GetVariance(statIdx).ToString(),
169        logic.GetOneQuarterPercentile(statIdx).ToString(),
170        logic.GetThreeQuarterPercentile(statIdx).ToString(),
171        logic.GetMostCommonValue<double>(statIdx, double.NaN).ToString(),
172        logic.GetDifferentValuesCount<double>(statIdx).ToString()
173      };
174    }
175
176    private List<string> GetStringColumns(int statIdx) {
177      var logic = Content.StatisticsLogic;
178      return new List<string> {
179        logic.GetColumnTypeAsString(statIdx),
180        logic.GetMissingValueCount(statIdx).ToString(),
181        "", //min
182        "", //max
183        "", //median
184        "", //average
185        "", //standard deviation
186        "", //variance
187        "", //quarter percentile
188        "", //three quarter percentile
189        logic.GetMostCommonValue<string>(statIdx,string.Empty) ?? "",
190        logic.GetDifferentValuesCount<string>(statIdx).ToString()
191      };
192    }
193
194    private List<string> GetDateTimeColumns(int statIdx) {
195      var logic = Content.StatisticsLogic;
196      return new List<string> {
197        logic.GetColumnTypeAsString(statIdx),
198        logic.GetMissingValueCount(statIdx).ToString(),
199        logic.GetMin<DateTime>(statIdx, DateTime.MinValue).ToString(),
200        logic.GetMax<DateTime>(statIdx, DateTime.MinValue).ToString(),
201        logic.GetMedianDateTime(statIdx).ToString(),
202        logic.GetAverageDateTime(statIdx).ToString(),
203        logic.GetStandardDeviation(statIdx).ToString(),
204        logic.GetVariance(statIdx).ToString(),
205        logic.GetOneQuarterPercentile(statIdx).ToString(),
206        logic.GetThreeQuarterPercentile(statIdx).ToString(),
207        logic.GetMostCommonValue<DateTime>(statIdx, DateTime.MinValue).ToString(),
208        logic.GetDifferentValuesCount<DateTime>(statIdx).ToString()
209      };
210    }
211
212    private void Content_Changed(object sender, DataPreprocessingChangedEventArgs e) {
213      UpdateData();
214    }
215
216    #region Show/Hide Variables
217    private void checkInputsTargetButton_Click(object sender, EventArgs e) {
218      var grid = stringMatrixView.DataGridView;
219      var list = horizontal ? grid.Columns : grid.Rows as IList;
220      var variableNames = Content.PreprocessingData.VariableNames.ToList();
221      int idx = 0;
222      foreach (DataGridViewBand band in list) {
223        var variable = variableNames[idx++];
224        bool isInputTarget = Content.PreprocessingData.InputVariables.Contains(variable)
225                             || Content.PreprocessingData.TargetVariable == variable;
226        band.Visible = isInputTarget;
227        if (horizontal)
228          stringMatrixView.UpdateColumnHeaders();
229        else
230          stringMatrixView.UpdateRowHeaders();
231      }
232
233    }
234    private void checkAllButton_Click(object sender, EventArgs e) {
235      var grid = stringMatrixView.DataGridView;
236      var list = horizontal ? grid.Columns : grid.Rows as IList;
237      foreach (DataGridViewBand band in list) {
238        band.Visible = true;
239      }
240      if (horizontal)
241        stringMatrixView.UpdateColumnHeaders();
242      else
243        stringMatrixView.UpdateRowHeaders();
244    }
245    private void uncheckAllButton_Click(object sender, EventArgs e) {
246      var grid = stringMatrixView.DataGridView;
247      var list = horizontal ? grid.Columns : grid.Rows as IList;
248      foreach (DataGridViewBand band in list) {
249        band.Visible = false;
250      }
251    }
252    #endregion
253
254    #region Orientation
255    private void horizontalRadioButton_CheckedChanged(object sender, EventArgs e) {
256      var grid = stringMatrixView.DataGridView;
257      var oldVisibility = new Dictionary<string, bool>();
258      var variableNames = Content.PreprocessingData.VariableNames.ToList();
259      if (stringMatrixView.Content != null) {
260        var list = horizontal ? grid.Columns : grid.Rows as IList;
261        int idx = 0;
262        foreach (DataGridViewBand band in list) {
263          var variable = variableNames[idx++];
264          oldVisibility.Add(variable, band.Visible);
265        }
266      }
267      horizontal = horizontalRadioButton.Checked;
268      UpdateData(oldVisibility);
269    }
270    private void verticalRadioButton_CheckedChanged(object sender, EventArgs e) {
271      // everything is handled in horizontalRadioButton_CheckedChanged
272    }
273    #endregion
274  }
275}
Note: See TracBrowser for help on using the repository browser.