Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2389-EpsLexicase/HeuristicLab.DataPreprocessing.Views/3.4/StatisticsView.cs @ 16300

Last change on this file since 16300 was 15598, checked in by pfleck, 7 years ago

#2640 Another datapreprocessing hotfix (row-column changed)

File size: 10.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2018 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 data = Content.PreprocessingData;
90      rowsTextBox.Text = data.Rows.ToString();
91      columnsTextBox.Text = data.Columns.ToString();
92      numericColumnsTextBox.Text = GetColumnCount<double>().ToString();
93      nominalColumnsTextBox5.Text = GetColumnCount<string>().ToString();
94      missingValuesTextBox.Text = data.GetMissingValueCount().ToString();
95      totalValuesTextBox.Text = (data.Rows * data.Rows - data.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 < data.Columns; i++) {
110        var statistics = GetStatistics(i);
111        for (int j = 0; j < statistics.Count; j++) {
112          if (horizontal)
113            statisticsMatrix[j, i] = statistics[j];
114          else
115            statisticsMatrix[i, j] = statistics[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    public int GetColumnCount<T>() {
141      int count = 0;
142      for (int i = 0; i < Content.PreprocessingData.Columns; ++i) {
143        if (Content.PreprocessingData.VariableHasType<T>(i)) {
144          ++count;
145        }
146      }
147      return count;
148    }
149
150    private List<string> GetStatistics(int varIdx) {
151      List<string> list;
152      var data = Content.PreprocessingData;
153      if (data.VariableHasType<double>(varIdx)) {
154        list = GetDoubleColumns(varIdx);
155      } else if (data.VariableHasType<string>(varIdx)) {
156        list = GetStringColumns(varIdx);
157      } else if (data.VariableHasType<DateTime>(varIdx)) {
158        list = GetDateTimeColumns(varIdx);
159      } else {
160        list = new List<string>();
161        for (int j = 0; j < StatisticsNames.Length; ++j) {
162          list.Add("unknown column type");
163        }
164      }
165      return list;
166    }
167
168    private List<string> GetDoubleColumns(int statIdx) {
169      var data = Content.PreprocessingData;
170      return new List<string> {
171        data.GetVariableType(statIdx).Name,
172        data.GetMissingValueCount(statIdx).ToString(),
173        data.GetMin<double>(statIdx, emptyValue: double.NaN).ToString(),
174        data.GetMax<double>(statIdx, emptyValue: double.NaN).ToString(),
175        data.GetMedian<double>(statIdx, emptyValue: double.NaN).ToString(),
176        data.GetMean<double>(statIdx, emptyValue: double.NaN).ToString(),
177        data.GetStandardDeviation<double>(statIdx, emptyValue: double.NaN).ToString(),
178        data.GetVariance<double>(statIdx, emptyValue: double.NaN).ToString(),
179        data.GetQuantile<double>(0.25, statIdx, emptyValue: double.NaN).ToString(),
180        data.GetQuantile<double>(0.75, statIdx, emptyValue: double.NaN).ToString(),
181        data.GetMode<double>(statIdx, emptyValue: double.NaN).ToString(),
182        data.GetDistinctValues<double>(statIdx).ToString()
183      };
184    }
185
186    private List<string> GetStringColumns(int statIdx) {
187      var data = Content.PreprocessingData;
188      return new List<string> {
189        data.GetVariableType(statIdx).Name,
190        data.GetMissingValueCount(statIdx).ToString(),
191        "", // data.GetMin<string>(statIdx, emptyValue: string.Empty), //min
192        "", // data.GetMax<string>(statIdx, emptyValue: string.Empty), //max
193        "", // data.GetMedian<string>(statIdx, emptyValue: string.Empty), //median
194        "", //average
195        "", //standard deviation
196        "", //variance
197        "", // data.GetQuantile<string>(0.25, statIdx, emptyValue: string.Empty), //quarter percentile
198        "", // data.GetQuantile<string>(0.75, statIdx, emptyValue: string.Empty), //three quarter percentile
199        data.GetMode<string>(statIdx, emptyValue: string.Empty),
200        data.GetDistinctValues<string>(statIdx).ToString()
201      };
202    }
203
204    private List<string> GetDateTimeColumns(int statIdx) {
205      var data = Content.PreprocessingData;
206      return new List<string> {
207        data.GetVariableType(statIdx).Name,
208        data.GetMissingValueCount(statIdx).ToString(),
209        data.GetMin<DateTime>(statIdx).ToString(),
210        data.GetMax<DateTime>(statIdx).ToString(),
211        data.GetMedian<DateTime>(statIdx).ToString(),
212        data.GetMean<DateTime>(statIdx).ToString(),
213        "", // should be of type TimeSpan //data.GetStandardDeviation<DateTime>(statIdx).ToString(),
214        "", // should be of type TimeSpan //data.GetVariance<DateTime>(statIdx).ToString(),
215        data.GetQuantile<DateTime>(0.25, statIdx).ToString(),
216        data.GetQuantile<DateTime>(0.75, statIdx).ToString(),
217        data.GetMode<DateTime>(statIdx).ToString(),
218        data.GetDistinctValues<DateTime>(statIdx).ToString()
219      };
220    }
221
222    private void Content_Changed(object sender, DataPreprocessingChangedEventArgs e) {
223      UpdateData();
224    }
225
226    #region Show/Hide Variables
227    private void checkInputsTargetButton_Click(object sender, EventArgs e) {
228      var grid = stringMatrixView.DataGridView;
229      var list = horizontal ? grid.Columns : grid.Rows as IList;
230      var variableNames = Content.PreprocessingData.VariableNames.ToList();
231      int idx = 0;
232      foreach (DataGridViewBand band in list) {
233        var variable = variableNames[idx++];
234        bool isInputTarget = Content.PreprocessingData.InputVariables.Contains(variable)
235                             || Content.PreprocessingData.TargetVariable == variable;
236        band.Visible = isInputTarget;
237        if (horizontal)
238          stringMatrixView.UpdateColumnHeaders();
239        else
240          stringMatrixView.UpdateRowHeaders();
241      }
242
243    }
244    private void checkAllButton_Click(object sender, EventArgs e) {
245      var grid = stringMatrixView.DataGridView;
246      var list = horizontal ? grid.Columns : grid.Rows as IList;
247      foreach (DataGridViewBand band in list) {
248        band.Visible = true;
249      }
250      if (horizontal)
251        stringMatrixView.UpdateColumnHeaders();
252      else
253        stringMatrixView.UpdateRowHeaders();
254    }
255    private void uncheckAllButton_Click(object sender, EventArgs e) {
256      var grid = stringMatrixView.DataGridView;
257      var list = horizontal ? grid.Columns : grid.Rows as IList;
258      foreach (DataGridViewBand band in list) {
259        band.Visible = false;
260      }
261    }
262    #endregion
263
264    #region Orientation
265    private void horizontalRadioButton_CheckedChanged(object sender, EventArgs e) {
266      var grid = stringMatrixView.DataGridView;
267      var oldVisibility = new Dictionary<string, bool>();
268      var variableNames = Content.PreprocessingData.VariableNames.ToList();
269      if (stringMatrixView.Content != null) {
270        var list = horizontal ? grid.Columns : grid.Rows as IList;
271        int idx = 0;
272        foreach (DataGridViewBand band in list) {
273          var variable = variableNames[idx++];
274          oldVisibility.Add(variable, band.Visible);
275        }
276      }
277      horizontal = horizontalRadioButton.Checked;
278      UpdateData(oldVisibility);
279    }
280    private void verticalRadioButton_CheckedChanged(object sender, EventArgs e) {
281      // everything is handled in horizontalRadioButton_CheckedChanged
282    }
283    #endregion
284  }
285}
Note: See TracBrowser for help on using the repository browser.