Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2335: Corrected the preview column count in the Manipulation-View (Defect 5) and added some additional information.

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