Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DataPreprocessing/HeuristicLab.DataPreprocessing.Views/3.3/StatisticsView.cs @ 10637

Last change on this file since 10637 was 10558, checked in by pfleck, 11 years ago
  • removed unnecessary references and using
  • added missing license header
  • formatted code according to HL standards
File size: 7.6 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2013 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), false)]
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      txtRows.Text = logic.GetRowCount().ToString();
78      txtColumns.Text = logic.GetColumnCount().ToString();
79      txtNumericColumns.Text = logic.GetNumericColumnCount().ToString();
80      txtNominalColumns.Text = logic.GetNominalColumnCount().ToString();
81      txtMissingValuesTotal.Text = logic.GetMissingValueCount().ToString();
82
83      columnsRowsMatrix = new List<List<string>>();
84      DataGridViewColumn[] columns = new DataGridViewColumn[10];
85      for (int i = 0; i < COLUMNS; ++i) {
86        var column = new DataGridViewTextBoxColumn();
87        column.FillWeight = 1;
88        columns[i] = column;
89      }
90
91      columns[0].HeaderCell.Value = "Type";
92      columns[1].HeaderCell.Value = "Missing Values";
93      columns[2].HeaderCell.Value = "Min";
94      columns[3].HeaderCell.Value = "Max";
95      columns[4].HeaderCell.Value = "Median";
96      columns[5].HeaderCell.Value = "Average";
97      columns[6].HeaderCell.Value = "std. Deviation";
98      columns[7].HeaderCell.Value = "Variance";
99      columns[8].HeaderCell.Value = "Most Common Value";
100      columns[9].HeaderCell.Value = "Num. diff. Values";
101
102      for (int i = 0; i < logic.GetColumnCount(); ++i) {
103        columnsRowsMatrix.Add(GetList(i));
104      }
105
106      dataGridView.Columns.Clear();
107      dataGridView.Columns.AddRange(columns);
108      dataGridView.RowCount = columnsRowsMatrix.Count;
109
110      for (int i = 0; i < columnsRowsMatrix.Count; ++i) {
111        dataGridView.Rows[i].HeaderCell.Value = logic.GetVariableName(i);
112      }
113      dataGridView.AllowUserToResizeColumns = true;
114    }
115
116    private List<string> GetList(int i) {
117      List<string> list;
118      var logic = Content.StatisticsLogic;
119      if (logic.IsType<double>(i)) {
120        list = GetDoubleColumns(i);
121      } else if (logic.IsType<string>(i)) {
122        list = GetStringColumns(i);
123      } else if (logic.IsType<DateTime>(i)) {
124        list = GetDateTimeColumns(i);
125      } else {
126        list = new List<string>();
127        for (int j = 0; j < COLUMNS; ++j) {
128          list.Add("unknown column type");
129        }
130      }
131      return list;
132    }
133
134    private List<string> GetDoubleColumns(int columnIndex) {
135      var logic = Content.StatisticsLogic;
136      return new List<string> {
137        logic.GetColumnTypeAsString(columnIndex),
138        logic.GetMissingValueCount(columnIndex).ToString(),
139        logic.GetMin<double>(columnIndex).ToString(),
140        logic.GetMax<double>(columnIndex).ToString(),
141        logic.GetMedian(columnIndex).ToString(),
142        logic.GetAverage(columnIndex).ToString(),
143        logic.GetStandardDeviation(columnIndex).ToString(),
144        logic.GetVariance(columnIndex).ToString(),
145        logic.GetMostCommonValue<double>(columnIndex).ToString(),
146        logic.GetDifferentValuesCount<double>(columnIndex).ToString()
147      };
148    }
149
150    private List<string> GetStringColumns(int columnIndex) {
151      var logic = Content.StatisticsLogic;
152      return new List<string> {
153        logic.GetColumnTypeAsString(columnIndex),
154        logic.GetMissingValueCount(columnIndex).ToString(),
155        "", //min
156        "", //max
157        "", //median
158        "", //average
159        "", //standard deviation
160        "", //variance
161        logic.GetMostCommonValue<string>(columnIndex).ToString(),
162        logic.GetDifferentValuesCount<string>(columnIndex).ToString()
163      };
164    }
165
166    private List<string> GetDateTimeColumns(int columnIndex) {
167      var logic = Content.StatisticsLogic;
168      return new List<string> {
169        logic.GetColumnTypeAsString(columnIndex),
170        logic.GetMissingValueCount(columnIndex).ToString(),
171        logic.GetMin<DateTime>(columnIndex).ToString(),
172        logic.GetMax<DateTime>(columnIndex).ToString(),
173        logic.GetMedianDateTime(columnIndex).ToString(),
174        logic.GetAverageDateTime(columnIndex).ToString(),
175        logic.GetStandardDeviation(columnIndex).ToString(),
176        logic.GetVariance(columnIndex).ToString(), //variance
177        logic.GetMostCommonValue<DateTime>(columnIndex).ToString(),
178        logic.GetDifferentValuesCount<DateTime>(columnIndex).ToString()
179      };
180    }
181
182    private List<string> GetDoubleColums(int i) {
183      throw new NotImplementedException();
184    }
185
186    private void dataGridView_CellValueNeeded(object sender, DataGridViewCellValueEventArgs e) {
187      if (Content != null && e.RowIndex < columnsRowsMatrix.Count && e.ColumnIndex < columnsRowsMatrix[0].Count) {
188        e.Value = columnsRowsMatrix[e.RowIndex][e.ColumnIndex];
189      }
190    }
191
192    private void Content_Changed(object sender, DataPreprocessingChangedEventArgs e) {
193      switch (e.Type) {
194        case DataPreprocessingChangedEventType.DeleteColumn:
195          columnsRowsMatrix.RemoveAt(e.Column);
196          break;
197        case DataPreprocessingChangedEventType.AddColumn:
198          columnsRowsMatrix.Insert(e.Row, GetList(e.Column));
199          dataGridView.RowCount++;
200          break;
201        case DataPreprocessingChangedEventType.ChangeItem:
202          columnsRowsMatrix[e.Column] = GetList(e.Column);
203          break;
204        case DataPreprocessingChangedEventType.DeleteRow:
205        case DataPreprocessingChangedEventType.AddRow:
206          for (int i = 0; i < columnsRowsMatrix.Count; ++i) {
207            columnsRowsMatrix[i] = GetList(e.Column);
208          }
209          break;
210      }
211      dataGridView.Refresh();
212    }
213  }
214}
Note: See TracBrowser for help on using the repository browser.