Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.DataPreprocessing.Views/3.4/ManipulationView.cs @ 12921

Last change on this file since 12921 was 12676, checked in by mkommend, 9 years ago

#2335: Merged changes into trunk.

File size: 13.5 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.Globalization;
26using System.Linq;
27using System.Text;
28using System.Windows.Forms;
29using HeuristicLab.Core.Views;
30using HeuristicLab.MainForm;
31
32namespace HeuristicLab.DataPreprocessing.Views {
33
34  [View("Manipulation Chart View")]
35  [Content(typeof(ManipulationContent), true)]
36  public partial class ManipulationView : ItemView {
37
38    private Action[] validators;
39    private Action[] manipulations;
40
41    public ManipulationView() {
42      InitializeComponent();
43      cmbReplaceWith.SelectedIndex = 0;
44      tabsData.Appearance = TabAppearance.FlatButtons;
45      tabsData.ItemSize = new Size(0, 1);
46      tabsData.SizeMode = TabSizeMode.Fixed;
47      tabsPreview.Appearance = TabAppearance.FlatButtons;
48      tabsPreview.ItemSize = new Size(0, 1);
49      tabsPreview.SizeMode = TabSizeMode.Fixed;
50
51      validators = new Action[] {
52        ()=>validateDeleteColumnsInfo(),
53        ()=>validateDeleteColumnsVariance(),
54        ()=>validateDeleteRowsInfo(),
55        ()=>validateReplaceWith(),
56        ()=>validateShuffle()
57      };
58
59      manipulations = new Action[] {
60        ()=>Content.ManipulationLogic.DeleteColumnsWithMissingValuesGreater(getDeleteColumnsInfo()),
61        ()=>Content.ManipulationLogic.DeleteColumnsWithVarianceSmaller(getDeleteColumnsVariance()),
62        ()=>Content.ManipulationLogic.DeleteRowsWithMissingValuesGreater(getRowsColumnsInfo()),
63        ()=>replaceMissingValues(),
64        ()=>Content.ManipulationLogic.Shuffle(shuffleSeparatelyCheckbox.Checked)
65      };
66    }
67
68    protected override void OnContentChanged() {
69      base.OnContentChanged();
70      if (Content != null) {
71        cmbVariableNames.Items.Clear();
72        foreach (var name in Content.ManipulationLogic.VariableNames) {
73          cmbVariableNames.Items.Add(name);
74        }
75        cmbVariableNames.SelectedIndex = 0;
76        CheckFilters();
77      }
78    }
79
80    protected override void RegisterContentEvents() {
81      base.RegisterContentEvents();
82      Content.FilterLogic.FilterChanged += FilterLogic_FilterChanged;
83    }
84
85    protected override void DeregisterContentEvents() {
86      Content.FilterLogic.FilterChanged -= FilterLogic_FilterChanged;
87      base.DeregisterContentEvents();
88    }
89
90    private void FilterLogic_FilterChanged(object sender, EventArgs e) {
91      if (Content != null) {
92        CheckFilters();
93      }
94    }
95
96    private void CheckFilters() {
97      if (Content.FilterLogic.IsFiltered) {
98        tabsPreview.SelectedIndex = 0;
99        lstMethods.Enabled = false;
100        tabsData.Enabled = false;
101        tabsPreview.Enabled = false;
102        lblPreviewInActive.Visible = true;
103        btnApply.Enabled = false;
104      } else {
105        lblPreviewInActive.Visible = false;
106        tabsData.Enabled = true;
107        tabsPreview.Enabled = true;
108        lstMethods.Enabled = true;
109        lstMethods_SelectedIndexChanged(null, null);
110      }
111    }
112
113    private double getDeleteColumnsInfo() {
114      return double.Parse(txtDeleteColumnsInfo.Text);
115    }
116
117    private double getDeleteColumnsVariance() {
118      return double.Parse(txtDeleteColumnsVariance.Text);
119    }
120
121    private double getRowsColumnsInfo() {
122      return double.Parse(txtDeleteRowsInfo.Text);
123    }
124
125    private void replaceMissingValues() {
126      var allIndices = Content.SearchLogic.GetMissingValueIndices();
127      var columnIndex = cmbVariableNames.SelectedIndex;
128      var columnIndices = new Dictionary<int, IList<int>>{
129          {columnIndex,   allIndices[columnIndex]}
130      };
131
132      switch (cmbReplaceWith.SelectedIndex) {
133        case 0: //Value
134          Content.ManipulationLogic.ReplaceIndicesByValue(columnIndices, txtReplaceValue.Text);
135          break;
136        case 1: //Average
137          Content.ManipulationLogic.ReplaceIndicesByAverageValue(columnIndices);
138          break;
139        case 2: //Median
140          Content.ManipulationLogic.ReplaceIndicesByMedianValue(columnIndices);
141          break;
142        case 3: //Most Common
143          Content.ManipulationLogic.ReplaceIndicesByMostCommonValue(columnIndices);
144          break;
145        case 4: //Random
146          Content.ManipulationLogic.ReplaceIndicesByRandomValue(columnIndices);
147          break;
148      }
149    }
150
151    private void validateDeleteColumnsInfo() {
152      validateDoubleTextBox(txtDeleteColumnsInfo.Text);
153      if (btnApply.Enabled) {
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, NumberStyles.Number ^ NumberStyles.AllowThousands, CultureInfo.CurrentCulture, 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.