Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.DataPreprocessing.Views/3.4/StatisticsView.cs @ 15405

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

#2709 Fixed some small issues and some default behavior.

File size: 10.2 KB
RevLine 
[10539]1#region License Information
2/* HeuristicLab
[14185]3 * Copyright (C) 2002-2016 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;
[15110]23using System.Collections;
[10369]24using System.Collections.Generic;
[15110]25using System.Linq;
[10369]26using System.Windows.Forms;
[10303]27using HeuristicLab.Core.Views;
[15110]28using HeuristicLab.Data;
[10303]29using HeuristicLab.MainForm;
[15110]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 {
[15210]36    private bool horizontal = false;
[15110]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) {
[15110]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() {
[15110]79      base.RegisterContentEvents();
[10551]80      Content.Changed += Content_Changed;
81    }
82
83    protected override void DeregisterContentEvents() {
84      Content.Changed -= Content_Changed;
[15110]85      base.DeregisterContentEvents();
[10551]86    }
87
[15110]88    private void UpdateData(Dictionary<string, bool> oldVisibility = null) {
[10370]89      var logic = Content.StatisticsLogic;
[15110]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();
[10369]96
[15110]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
[15110]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];
[10971]116        }
[10369]117      }
118
[15110]119      stringMatrixView.Parent.SuspendRepaint();
120      stringMatrixView.Content = statisticsMatrix;
[10369]121
[15110]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      }
[15110]131      if (horizontal)
132        stringMatrixView.UpdateColumnHeaders();
133      else
134        stringMatrixView.UpdateRowHeaders();
[10691]135
[15110]136      stringMatrixView.DataGridView.AutoResizeColumns();
137      stringMatrixView.Parent.ResumeRepaint(true);
[10369]138    }
139
[15110]140    private List<string> GetStatistics(int varIdx) {
[10551]141      List<string> list;
142      var logic = Content.StatisticsLogic;
[15110]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);
[10551]149      } else {
150        list = new List<string>();
[15110]151        for (int j = 0; j < StatisticsNames.Length; ++j) {
[10551]152          list.Add("unknown column type");
153        }
154      }
155      return list;
156    }
157
[15110]158    private List<string> GetDoubleColumns(int statIdx) {
[10369]159      var logic = Content.StatisticsLogic;
160      return new List<string> {
[15110]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()
[10369]173      };
174    }
175
[15110]176    private List<string> GetStringColumns(int statIdx) {
[10374]177      var logic = Content.StatisticsLogic;
178      return new List<string> {
[15110]179        logic.GetColumnTypeAsString(statIdx),
180        logic.GetMissingValueCount(statIdx).ToString(),
[10374]181        "", //min
182        "", //max
183        "", //median
184        "", //average
185        "", //standard deviation
186        "", //variance
[12889]187        "", //quarter percentile
188        "", //three quarter percentile
[15110]189        logic.GetMostCommonValue<string>(statIdx,string.Empty) ?? "",
190        logic.GetDifferentValuesCount<string>(statIdx).ToString()
[10374]191      };
[10369]192    }
[10374]193
[15110]194    private List<string> GetDateTimeColumns(int statIdx) {
[10374]195      var logic = Content.StatisticsLogic;
196      return new List<string> {
[15110]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()
[10374]209      };
[10369]210    }
211
[15110]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();
[10369]231      }
[15110]232
[10369]233    }
[15110]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
[10551]253
[15110]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        }
[10551]266      }
[15110]267      horizontal = horizontalRadioButton.Checked;
268      UpdateData(oldVisibility);
[10551]269    }
[15110]270    private void verticalRadioButton_CheckedChanged(object sender, EventArgs e) {
271      // everything is handled in horizontalRadioButton_CheckedChanged
272    }
273    #endregion
[10303]274  }
275}
Note: See TracBrowser for help on using the repository browser.