Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.DataPreprocessing.Views/3.4/StatisticsView.cs @ 15890

Last change on this file since 15890 was 15599, checked in by pfleck, 7 years ago

#2640 merged to stable

File size: 10.9 KB
RevLine 
[10539]1#region License Information
2/* HeuristicLab
[15584]3 * Copyright (C) 2002-2018 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[10539]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;
[15242]23using System.Collections;
[10369]24using System.Collections.Generic;
[15242]25using System.Linq;
[10369]26using System.Windows.Forms;
[10303]27using HeuristicLab.Core.Views;
[15242]28using HeuristicLab.Data;
[10303]29using HeuristicLab.MainForm;
[15242]30using HeuristicLab.MainForm.WindowsForms;
[10303]31
[10558]32namespace HeuristicLab.DataPreprocessing.Views {
[10303]33  [View("Statistics View")]
[10712]34  [Content(typeof(StatisticsContent), true)]
[10303]35  public partial class StatisticsView : ItemView {
[15242]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    };
[10303]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();
[10369]64      if (Content == null) {
[15242]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;
[10369]73      } else {
74        UpdateData();
[10320]75      }
[10303]76    }
[10551]77
78    protected override void RegisterContentEvents() {
[15242]79      base.RegisterContentEvents();
[10551]80      Content.Changed += Content_Changed;
81    }
82
83    protected override void DeregisterContentEvents() {
84      Content.Changed -= Content_Changed;
[15242]85      base.DeregisterContentEvents();
[10551]86    }
87
[15242]88    private void UpdateData(Dictionary<string, bool> oldVisibility = null) {
[15535]89      var data = Content.PreprocessingData;
90      rowsTextBox.Text = data.Rows.ToString();
[15599]91      columnsTextBox.Text = data.Columns.ToString();
[15535]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();
[10369]96
[15242]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        };
[10369]108
[15535]109      for (int i = 0; i < data.Columns; i++) {
110        var statistics = GetStatistics(i);
111        for (int j = 0; j < statistics.Count; j++) {
[15242]112          if (horizontal)
[15535]113            statisticsMatrix[j, i] = statistics[j];
[15242]114          else
[15535]115            statisticsMatrix[i, j] = statistics[j];
[10971]116        }
[10369]117      }
118
[15242]119      stringMatrixView.Parent.SuspendRepaint();
120      stringMatrixView.Content = statisticsMatrix;
[10369]121
[15242]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        }
[10534]130      }
[15242]131      if (horizontal)
132        stringMatrixView.UpdateColumnHeaders();
133      else
134        stringMatrixView.UpdateRowHeaders();
[10691]135
[15242]136      stringMatrixView.DataGridView.AutoResizeColumns();
137      stringMatrixView.Parent.ResumeRepaint(true);
[10369]138    }
139
[15535]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
[15242]150    private List<string> GetStatistics(int varIdx) {
[10551]151      List<string> list;
[15535]152      var data = Content.PreprocessingData;
153      if (data.VariableHasType<double>(varIdx)) {
[15242]154        list = GetDoubleColumns(varIdx);
[15535]155      } else if (data.VariableHasType<string>(varIdx)) {
[15242]156        list = GetStringColumns(varIdx);
[15535]157      } else if (data.VariableHasType<DateTime>(varIdx)) {
[15242]158        list = GetDateTimeColumns(varIdx);
[10551]159      } else {
160        list = new List<string>();
[15242]161        for (int j = 0; j < StatisticsNames.Length; ++j) {
[10551]162          list.Add("unknown column type");
163        }
164      }
165      return list;
166    }
167
[15242]168    private List<string> GetDoubleColumns(int statIdx) {
[15535]169      var data = Content.PreprocessingData;
[10369]170      return new List<string> {
[15535]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()
[10369]183      };
184    }
185
[15242]186    private List<string> GetStringColumns(int statIdx) {
[15535]187      var data = Content.PreprocessingData;
[10374]188      return new List<string> {
[15535]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
[10374]194        "", //average
195        "", //standard deviation
196        "", //variance
[15535]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()
[10374]201      };
[10369]202    }
[10374]203
[15242]204    private List<string> GetDateTimeColumns(int statIdx) {
[15535]205      var data = Content.PreprocessingData;
[10374]206      return new List<string> {
[15535]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()
[10374]219      };
[10369]220    }
221
[15242]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();
[10369]241      }
[15242]242
[10369]243    }
[15242]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
[10551]263
[15242]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        }
[10551]276      }
[15242]277      horizontal = horizontalRadioButton.Checked;
278      UpdateData(oldVisibility);
[10551]279    }
[15242]280    private void verticalRadioButton_CheckedChanged(object sender, EventArgs e) {
281      // everything is handled in horizontalRadioButton_CheckedChanged
282    }
283    #endregion
[10303]284  }
285}
Note: See TracBrowser for help on using the repository browser.