source: branches/DataPreprocessingImprovements/HeuristicLab.DataPreprocessing.Views/3.4/StatisticsView.cs @ 12545

Last change on this file since 12545 was 12545, checked in by ehopf, 4 years ago

#2335: Added the recalculation of the Statistics-View if a Filter gets applied (Defect 8) and minor fixes to the statistic calculations.

File size: 7.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 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.Generic;
24using System.Windows.Forms;
25using HeuristicLab.Core.Views;
26using HeuristicLab.MainForm;
27
28namespace HeuristicLab.DataPreprocessing.Views {
29
30  [View("Statistics View")]
31  [Content(typeof(StatisticsContent), true)]
32  public partial class StatisticsView : ItemView {
33
34    private List<List<string>> columnsRowsMatrix;
35    private readonly int COLUMNS = 10;
36
37    public new StatisticsContent Content {
38      get { return (StatisticsContent)base.Content; }
39      set { base.Content = value; }
40    }
41
42    public StatisticsView() {
43      InitializeComponent();
44    }
45
46    protected override void OnContentChanged() {
47      base.OnContentChanged();
48      if (Content == null) {
49        txtRows.Text = "";
50        txtColumns.Text = "";
51        txtNumericColumns.Text = "";
52        txtNominalColumns.Text = "";
53        txtMissingValuesTotal.Text = "";
54        dataGridView.Columns.Clear();
55      } else {
56        UpdateData();
57      }
58    }
59
60    /// <summary>
61    /// Adds eventhandlers to the current instance.
62    /// </summary>
63    protected override void RegisterContentEvents() {
64      Content.Changed += Content_Changed;
65    }
66
67
68    /// <summary>
69    /// Removes the eventhandlers from the current instance.
70    /// </summary>
71    protected override void DeregisterContentEvents() {
72      Content.Changed -= Content_Changed;
73    }
74
75    private void UpdateData() {
76      var logic = Content.StatisticsLogic;
77      var rowCount = logic.GetRowCount();
78      txtRows.Text = rowCount.ToString();
79      txtColumns.Text = logic.GetColumnCount().ToString();
80      txtNumericColumns.Text = logic.GetNumericColumnCount().ToString();
81      txtNominalColumns.Text = logic.GetNominalColumnCount().ToString();
82      txtMissingValuesTotal.Text = logic.GetMissingValueCount().ToString();
83
84      columnsRowsMatrix = new List<List<string>>();
85      DataGridViewColumn[] columns = new DataGridViewColumn[10];
86      for (int i = 0; i < COLUMNS; ++i) {
87        var column = new DataGridViewTextBoxColumn();
88        column.SortMode = DataGridViewColumnSortMode.Automatic;
89        column.FillWeight = 1;
90        columns[i] = column;
91      }
92
93      columns[0].HeaderCell.Value = "Type";
94      columns[1].HeaderCell.Value = "Missing Values";
95      columns[2].HeaderCell.Value = "Min";
96      columns[3].HeaderCell.Value = "Max";
97      columns[4].HeaderCell.Value = "Median";
98      columns[5].HeaderCell.Value = "Average";
99      columns[6].HeaderCell.Value = "std. Deviation";
100      columns[7].HeaderCell.Value = "Variance";
101      columns[8].HeaderCell.Value = "Most Common Value";
102      columns[9].HeaderCell.Value = "Num. diff. Values";
103
104      if (rowCount > 0) {
105        for (int i = 0; i < logic.GetColumnCount(); ++i) {
106          columnsRowsMatrix.Add(GetList(i));
107        }
108      }
109
110      dataGridView.Columns.Clear();
111      dataGridView.Columns.AddRange(columns);
112      dataGridView.RowCount = columnsRowsMatrix.Count;
113
114      for (int i = 0; i < columnsRowsMatrix.Count; ++i) {
115        dataGridView.Rows[i].HeaderCell.Value = logic.GetVariableName(i);
116      }
117
118      dataGridView.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.DisplayedCells);
119      dataGridView.AutoResizeRowHeadersWidth(DataGridViewRowHeadersWidthSizeMode.AutoSizeToDisplayedHeaders);
120      dataGridView.AllowUserToResizeColumns = true;
121    }
122
123    private List<string> GetList(int i) {
124      List<string> list;
125      var logic = Content.StatisticsLogic;
126      if (logic.VariableHasType<double>(i)) {
127        list = GetDoubleColumns(i);
128      } else if (logic.VariableHasType<string>(i)) {
129        list = GetStringColumns(i);
130      } else if (logic.VariableHasType<DateTime>(i)) {
131        list = GetDateTimeColumns(i);
132      } else {
133        list = new List<string>();
134        for (int j = 0; j < COLUMNS; ++j) {
135          list.Add("unknown column type");
136        }
137      }
138      return list;
139    }
140
141    private List<string> GetDoubleColumns(int columnIndex) {
142      var logic = Content.StatisticsLogic;
143      return new List<string> {
144        logic.GetColumnTypeAsString(columnIndex),
145        logic.GetMissingValueCount(columnIndex).ToString(),
146        logic.GetMin<double>(columnIndex).ToString(),
147        logic.GetMax<double>(columnIndex).ToString(),
148        logic.GetMedian(columnIndex).ToString(),
149        logic.GetAverage(columnIndex).ToString(),
150        logic.GetStandardDeviation(columnIndex).ToString(),
151        logic.GetVariance(columnIndex).ToString(),
152        logic.GetMostCommonValue<double>(columnIndex).ToString(),
153        logic.GetDifferentValuesCount<double>(columnIndex).ToString()
154      };
155    }
156
157    private List<string> GetStringColumns(int columnIndex) {
158      var logic = Content.StatisticsLogic;
159      return new List<string> {
160        logic.GetColumnTypeAsString(columnIndex),
161        logic.GetMissingValueCount(columnIndex).ToString(),
162        "", //min
163        "", //max
164        "", //median
165        "", //average
166        "", //standard deviation
167        "", //variance
168        logic.GetMostCommonValue<string>(columnIndex) ?? "",
169        logic.GetDifferentValuesCount<string>(columnIndex).ToString()
170      };
171    }
172
173    private List<string> GetDateTimeColumns(int columnIndex) {
174      var logic = Content.StatisticsLogic;
175      return new List<string> {
176        logic.GetColumnTypeAsString(columnIndex),
177        logic.GetMissingValueCount(columnIndex).ToString(),
178        logic.GetMin<DateTime>(columnIndex).ToString(),
179        logic.GetMax<DateTime>(columnIndex).ToString(),
180        logic.GetMedianDateTime(columnIndex).ToString(), 
181        logic.GetAverageDateTime(columnIndex).ToString(),
182        logic.GetStandardDeviation(columnIndex).ToString(),
183        logic.GetVariance(columnIndex).ToString(), //variance
184        logic.GetMostCommonValue<DateTime>(columnIndex).ToString(),
185        logic.GetDifferentValuesCount<DateTime>(columnIndex).ToString()
186      };
187    }
188
189    private void dataGridView_CellValueNeeded(object sender, DataGridViewCellValueEventArgs e) {
190      if (Content != null && e.RowIndex < columnsRowsMatrix.Count && e.ColumnIndex < columnsRowsMatrix[0].Count) {
191        e.Value = columnsRowsMatrix[e.RowIndex][e.ColumnIndex];
192      }
193    }
194
195    private void Content_Changed(object sender, DataPreprocessingChangedEventArgs e) {
196      switch (e.Type) {
197        case DataPreprocessingChangedEventType.DeleteColumn:
198          columnsRowsMatrix.RemoveAt(e.Column);
199          break;
200        case DataPreprocessingChangedEventType.AddColumn:
201          columnsRowsMatrix.Insert(e.Row, GetList(e.Column));
202          dataGridView.RowCount++;
203          break;
204        case DataPreprocessingChangedEventType.ChangeItem:
205          columnsRowsMatrix[e.Column] = GetList(e.Column);
206          break;
207        case DataPreprocessingChangedEventType.DeleteRow:
208        case DataPreprocessingChangedEventType.AddRow:
209        default:
210          for (int i = 0; i < columnsRowsMatrix.Count; ++i) {
211            columnsRowsMatrix[i] = GetList(e.Column);
212          }
213          break;
214      }
215      dataGridView.Refresh();
216    }
217  }
218}
Note: See TracBrowser for help on using the repository browser.