Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DataPreprocessing/HeuristicLab.DataPreprocessing.Views/3.4/ManipulationView.cs @ 10947

Last change on this file since 10947 was 10905, checked in by rstoll, 10 years ago
  • Manipulation View

missing values per column
validating per column
success message for missing values was missing

File size: 11.1 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.Drawing;
25using System.Windows.Forms;
26using HeuristicLab.Core.Views;
27using HeuristicLab.MainForm;
28
29namespace HeuristicLab.DataPreprocessing.Views {
30
31  [View("Manipulation Chart View")]
32  [Content(typeof(ManipulationContent), true)]
33  public partial class ManipulationView : ItemView {
34
35    private Action[] validators;
36    private Action[] manipulations;
37
38    public ManipulationView() {
39      InitializeComponent();
40      cmbReplaceWith.SelectedIndex = 0;
41      tabsData.Appearance = TabAppearance.FlatButtons;
42      tabsData.ItemSize = new Size(0, 1);
43      tabsData.SizeMode = TabSizeMode.Fixed;
44      tabsPreview.Appearance = TabAppearance.FlatButtons;
45      tabsPreview.ItemSize = new Size(0, 1);
46      tabsPreview.SizeMode = TabSizeMode.Fixed;
47
48      validators = new Action[] {
49        ()=>validateDeleteColumnsInfo(),
50        ()=>validateDeleteColumnsVariance(),
51        ()=>validateDeleteRowsInfo(),
52        ()=>validateReplaceWith(),
53        ()=>{btnApply.Enabled = true; lblPreviewShuffle.Text = "Data will be shuffled randomly - preview not possible";} //shuffle
54      };
55
56      manipulations = new Action[] {
57        ()=>Content.ManipulationLogic.DeleteColumnsWithMissingValuesGreater(getDeleteColumnsInfo()),
58        ()=>Content.ManipulationLogic.DeleteColumnsWithVarianceSmaller(getDeleteColumnsVariance()),
59        ()=>Content.ManipulationLogic.DeleteRowsWithMissingValuesGreater(getRowsColumnsInfo()),
60        ()=>replaceMissingValues(),
61        ()=>Content.ManipulationLogic.ShuffleWithRanges()
62      };
63
64    }
65
66    protected override void OnContentChanged() {
67      base.OnContentChanged();
68      if (Content != null) {
69        cmbVariableNames.Items.Clear();
70        foreach (var name in Content.DataGridLogic.ColumnNames) {
71          cmbVariableNames.Items.Add(name);
72        }
73        cmbVariableNames.SelectedIndex = 0;
74      }
75    }
76
77    protected override void RegisterContentEvents() {
78      base.RegisterContentEvents();
79      Content.ManipulationLogic.Changed += Content_Changed;
80    }
81
82    protected override void DeregisterContentEvents() {
83      base.DeregisterContentEvents();
84      Content.ManipulationLogic.Changed -= Content_Changed;
85    }
86
87    private double getDeleteColumnsInfo() {
88      return double.Parse(txtDeleteColumnsInfo.Text);
89    }
90
91    private double getDeleteColumnsVariance() {
92      return double.Parse(txtDeleteColumnsVariance.Text);
93    }
94
95    private double getRowsColumnsInfo() {
96      return double.Parse(txtDeleteRowsInfo.Text);
97    }
98
99    private void replaceMissingValues() {
100      var allIndices = Content.SearchLogic.GetMissingValueIndices();
101      var columnIndex = cmbVariableNames.SelectedIndex;
102      var columnIndices = new Dictionary<int, IList<int>>{
103          {columnIndex,   allIndices[columnIndex]}
104      };
105
106      switch (cmbReplaceWith.SelectedIndex) {
107        case 0: //Value
108          Content.ManipulationLogic.ReplaceIndicesByValue(columnIndices, txtReplaceValue.Text);
109          break;
110        case 1: //Average
111          Content.ManipulationLogic.ReplaceIndicesByAverageValue(columnIndices);
112          break;
113        case 2: //Median
114          Content.ManipulationLogic.ReplaceIndicesByMedianValue(columnIndices);
115          break;
116        case 3: //Most Common
117          Content.ManipulationLogic.ReplaceIndicesByMostCommonValue(columnIndices);
118          break;
119        case 4: //Random
120          Content.ManipulationLogic.ReplaceIndicesByRandomValue(columnIndices);
121          break;
122      }
123    }
124
125    private void validateDeleteColumnsInfo() {
126      validateDoubleTextBox(txtDeleteColumnsInfo.Text);
127      if (btnApply.Enabled) {
128        int count = Content.ManipulationLogic.ColumnsWithMissingValuesGreater(getDeleteColumnsInfo()).Count;
129        lblPreviewColumnsInfo.Text = count + " column" + (count > 1 || count == 0 ? "s" : "") + " were detected with more than " + txtDeleteColumnsInfo.Text + "% missing values.";
130        if (count > 0) {
131          lblPreviewColumnsInfo.Text += Environment.NewLine + Environment.NewLine + "Please press the button \"Apply Manipulation\" if you wish to delete those columns.";
132        } else {
133          btnApply.Enabled = false;
134        }
135      } else {
136        lblPreviewColumnsInfo.Text = "Preview not possible yet - please input the limit above.";
137      }
138    }
139
140    private void validateDeleteColumnsVariance() {
141      validateDoubleTextBox(txtDeleteColumnsVariance.Text);
142      if (btnApply.Enabled) {
143        int count = Content.ManipulationLogic.ColumnsWithVarianceSmaller(getDeleteColumnsVariance()).Count;
144        lblPreviewColumnsVariance.Text = count + " column" + (count > 1 || count == 0 ? "s" : "") + " were detected with a variance smaller than " + txtDeleteColumnsVariance.Text + ".";
145        if (count > 0) {
146          lblPreviewColumnsVariance.Text += Environment.NewLine + Environment.NewLine + "Please press the button \"Apply Manipulation\" if you wish to delete those columns.";
147        } else {
148          btnApply.Enabled = false;
149        }
150      } else {
151        lblPreviewColumnsVariance.Text = "Preview not possible yet - please input the limit for the variance above.";
152      }
153    }
154
155    private void validateDeleteRowsInfo() {
156      validateDoubleTextBox(txtDeleteRowsInfo.Text);
157      if (btnApply.Enabled) {
158        int count = Content.ManipulationLogic.RowsWithMissingValuesGreater(getRowsColumnsInfo()).Count;
159        lblPreviewRowsInfo.Text = count + " row" + (count > 1 || count == 0 ? "s" : "") + " were detected with more than " + txtDeleteRowsInfo.Text + "% missing values.";
160        if (count > 0) {
161          lblPreviewRowsInfo.Text += Environment.NewLine + Environment.NewLine + "Please press the button \"Apply Manipulation\" if you wish to delete those rows.";
162        } else {
163          btnApply.Enabled = false;
164        }
165      } else {
166        lblPreviewRowsInfo.Text = "Preview not possible yet - please input the limit above.";
167      }
168    }
169
170    private void validateReplaceWith() {
171      btnApply.Enabled = false;
172      string replaceWith = (string)cmbReplaceWith.SelectedItem;
173      int columnIndex = cmbVariableNames.SelectedIndex;
174
175      if (cmbReplaceWith.SelectedIndex == 0) {
176        string errorMessage;
177        string replaceValue = txtReplaceValue.Text;
178        if (string.IsNullOrEmpty(replaceValue)) {
179          lblPreviewReplaceMissingValues.Text = "Preview not possible yet - please input the text which will be used as replacement.";
180        } else if (!Content.DataGridLogic.Validate(txtReplaceValue.Text, out errorMessage, columnIndex)) {
181          lblPreviewReplaceMissingValues.Text = "Preview not possible yet - " + errorMessage;
182        } else {
183          btnApply.Enabled = true;
184        }
185        replaceWith = "\"" + replaceValue + "\"";
186      } else {
187        btnApply.Enabled = true;
188      }
189      if (btnApply.Enabled) {
190        var allIndices = Content.SearchLogic.GetMissingValueIndices();
191        int count = allIndices[columnIndex].Count;
192        lblPreviewReplaceMissingValues.Text = count + " cell" + (count > 1 || count == 0 ? "s" : "")
193          + " were detected with missing values which would be replaced with " + replaceWith;
194        if (count > 0) {
195          lblPreviewReplaceMissingValues.Text += Environment.NewLine + Environment.NewLine + "Please press the button \"Apply Manipulation\" if you wish to perform the replacement.";
196        } else {
197          btnApply.Enabled = false;
198        }
199      }
200    }
201
202    public new ManipulationContent Content {
203      get { return (ManipulationContent)base.Content; }
204      set { base.Content = value; }
205    }
206
207    private void lstMethods_SelectedIndexChanged(object sender, System.EventArgs e) {
208      int index = lstMethods.SelectedIndex;
209      tabsData.SelectedIndex = index + 1;
210      tabsPreview.SelectedIndex = index + 1;
211      btnApply.Enabled = false;
212
213      //in order that button is enabled if necessary input was already entered
214      if (index >= 0) {
215        validators[index]();
216      }
217    }
218
219    private void btnApply_Click(object sender, System.EventArgs e) {
220      manipulations[lstMethods.SelectedIndex]();
221      switch (lstMethods.SelectedIndex) {
222        case 0:
223          lblPreviewColumnsInfo.Text = "columns successfully deleted.";
224          break;
225        case 1:
226          lblPreviewColumnsVariance.Text = "columns successfully deleted.";
227          break;
228        case 2:
229          lblPreviewRowsInfo.Text = "rows successfully deleted.";
230          break;
231        case 3:
232          lblPreviewReplaceMissingValues.Text = "missing values successfully replaced.";
233          btnApply.Enabled = false;
234          break;
235        case 4:
236          lblPreviewShuffle.Text = "dataset shuffled successfully.";
237          btnApply.Enabled = false;
238          break;
239      }
240    }
241
242    private void validateDoubleTextBox(String text) {
243      btnApply.Enabled = false;
244      if (!string.IsNullOrEmpty(text)) {
245        double percent;
246        if (Double.TryParse(text, out percent)) {
247          btnApply.Enabled = true;
248        }
249      }
250    }
251
252    private void txtDeleteColumnsInfo_TextChanged(object sender, EventArgs e) {
253      validateDeleteColumnsInfo();
254    }
255
256    private void txtDeleteColumnsVariance_TextChanged(object sender, EventArgs e) {
257      validateDeleteColumnsVariance();
258    }
259
260    private void txtDeleteRowsInfo_TextChanged(object sender, EventArgs e) {
261      validateDeleteRowsInfo();
262    }
263
264    void Content_Changed(object sender, DataPreprocessingChangedEventArgs e) {
265      switch (lstMethods.SelectedIndex) {
266        case 0:
267          validateDeleteColumnsInfo();
268          break;
269        case 1:
270          validateDeleteColumnsVariance();
271          break;
272        case 2:
273          validateDeleteRowsInfo();
274          break;
275      }
276    }
277
278    private void cmbReplaceWith_SelectedIndexChanged(object sender, EventArgs e) {
279      bool isReplaceWithValueSelected = cmbReplaceWith.SelectedIndex == 0;
280      lblValueColon.Visible = isReplaceWithValueSelected;
281      txtReplaceValue.Visible = isReplaceWithValueSelected;
282      validateReplaceWith();
283    }
284
285    private void txtReplaceValue_TextChanged(object sender, EventArgs e) {
286      validateReplaceWith();
287    }
288  }
289}
Note: See TracBrowser for help on using the repository browser.