Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DataPreprocessing/HeuristicLab.DataPreprocessing.Views/3.3/ManipulationView.cs @ 10809

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