Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.DataPreprocessing.Views/3.4/ManipulationView.cs @ 13321

Last change on this file since 13321 was 12718, checked in by mkommend, 9 years ago

#2335: Merged all changes into stable.

File size: 13.5 KB
RevLine 
[10709]1#region License Information
2/* HeuristicLab
[12009]3 * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[10709]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;
[10905]23using System.Collections.Generic;
[10709]24using System.Drawing;
[12718]25using System.Globalization;
26using System.Linq;
27using System.Text;
[10709]28using System.Windows.Forms;
29using HeuristicLab.Core.Views;
30using HeuristicLab.MainForm;
31
32namespace HeuristicLab.DataPreprocessing.Views {
33
34  [View("Manipulation Chart View")]
[10712]35  [Content(typeof(ManipulationContent), true)]
[10709]36  public partial class ManipulationView : ItemView {
37
38    private Action[] validators;
39    private Action[] manipulations;
40
41    public ManipulationView() {
42      InitializeComponent();
[10776]43      cmbReplaceWith.SelectedIndex = 0;
[10709]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[] {
[10737]52        ()=>validateDeleteColumnsInfo(),
53        ()=>validateDeleteColumnsVariance(),
54        ()=>validateDeleteRowsInfo(),
[10776]55        ()=>validateReplaceWith(),
[11537]56        ()=>validateShuffle()
[10709]57      };
[10737]58
[10709]59      manipulations = new Action[] {
[10737]60        ()=>Content.ManipulationLogic.DeleteColumnsWithMissingValuesGreater(getDeleteColumnsInfo()),
61        ()=>Content.ManipulationLogic.DeleteColumnsWithVarianceSmaller(getDeleteColumnsVariance()),
62        ()=>Content.ManipulationLogic.DeleteRowsWithMissingValuesGreater(getRowsColumnsInfo()),
[10776]63        ()=>replaceMissingValues(),
[11537]64        ()=>Content.ManipulationLogic.Shuffle(shuffleSeparatelyCheckbox.Checked)
[10709]65      };
66    }
67
[10905]68    protected override void OnContentChanged() {
69      base.OnContentChanged();
70      if (Content != null) {
71        cmbVariableNames.Items.Clear();
[11002]72        foreach (var name in Content.ManipulationLogic.VariableNames) {
[10905]73          cmbVariableNames.Items.Add(name);
74        }
75        cmbVariableNames.SelectedIndex = 0;
[10977]76        CheckFilters();
[10905]77      }
78    }
79
[10737]80    protected override void RegisterContentEvents() {
81      base.RegisterContentEvents();
[10970]82      Content.FilterLogic.FilterChanged += FilterLogic_FilterChanged;
[10737]83    }
84
85    protected override void DeregisterContentEvents() {
[11070]86      Content.FilterLogic.FilterChanged -= FilterLogic_FilterChanged;
[10737]87      base.DeregisterContentEvents();
88    }
89
[10970]90    private void FilterLogic_FilterChanged(object sender, EventArgs e) {
91      if (Content != null) {
[10977]92        CheckFilters();
[10970]93      }
94    }
95
[10977]96    private void CheckFilters() {
[10970]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
[10737]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
[10776]125    private void replaceMissingValues() {
[10905]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) {
[10776]133        case 0: //Value
[10905]134          Content.ManipulationLogic.ReplaceIndicesByValue(columnIndices, txtReplaceValue.Text);
[10776]135          break;
136        case 1: //Average
[10905]137          Content.ManipulationLogic.ReplaceIndicesByAverageValue(columnIndices);
[10776]138          break;
139        case 2: //Median
[10905]140          Content.ManipulationLogic.ReplaceIndicesByMedianValue(columnIndices);
[10776]141          break;
142        case 3: //Most Common
[10905]143          Content.ManipulationLogic.ReplaceIndicesByMostCommonValue(columnIndices);
[10776]144          break;
145        case 4: //Random
[10905]146          Content.ManipulationLogic.ReplaceIndicesByRandomValue(columnIndices);
[10776]147          break;
148      }
149    }
150
[10737]151    private void validateDeleteColumnsInfo() {
152      validateDoubleTextBox(txtDeleteColumnsInfo.Text);
153      if (btnApply.Enabled) {
[12718]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.";
[10737]158        if (count > 0) {
[12718]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();
[10737]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) {
[12718]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 + ".";
[10737]187        if (count > 0) {
[12718]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();
[10737]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;
[11045]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.";
[10737]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
[10776]225    private void validateReplaceWith() {
226      btnApply.Enabled = false;
[10905]227      string replaceWith = (string)cmbReplaceWith.SelectedItem;
228      int columnIndex = cmbVariableNames.SelectedIndex;
229
[10776]230      if (cmbReplaceWith.SelectedIndex == 0) {
[10905]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.";
[11002]235        } else if (!Content.ManipulationLogic.PreProcessingData.Validate(txtReplaceValue.Text, out errorMessage, columnIndex)) {
[10905]236          lblPreviewReplaceMissingValues.Text = "Preview not possible yet - " + errorMessage;
237        } else {
238          btnApply.Enabled = true;
239        }
240        replaceWith = "\"" + replaceValue + "\"";
[10776]241      } else {
242        btnApply.Enabled = true;
243      }
244      if (btnApply.Enabled) {
[10905]245        var allIndices = Content.SearchLogic.GetMissingValueIndices();
246        int count = allIndices[columnIndex].Count;
[11070]247        int cellCount = Content.FilterLogic.PreprocessingData.Rows * Content.FilterLogic.PreprocessingData.Columns;
[10905]248        lblPreviewReplaceMissingValues.Text = count + " cell" + (count > 1 || count == 0 ? "s" : "")
[11045]249          + " of " + cellCount + " (" + string.Format("{0:F2}%", 100d / cellCount * count) + ") were detected with missing values which would be replaced with " + replaceWith;
[10776]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
[11537]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
[10709]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
[10737]277      //in order that button is enabled if necessary input was already entered
[10710]278      if (index >= 0) {
279        validators[index]();
280      }
[10709]281    }
282
283    private void btnApply_Click(object sender, System.EventArgs e) {
284      manipulations[lstMethods.SelectedIndex]();
[10737]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:
[10905]296          lblPreviewReplaceMissingValues.Text = "missing values successfully replaced.";
297          btnApply.Enabled = false;
298          break;
299        case 4:
[10737]300          lblPreviewShuffle.Text = "dataset shuffled successfully.";
301          btnApply.Enabled = false;
302          break;
303      }
[10709]304    }
305
306    private void validateDoubleTextBox(String text) {
307      btnApply.Enabled = false;
308      if (!string.IsNullOrEmpty(text)) {
309        double percent;
[12718]310        if (Double.TryParse(text, NumberStyles.Number ^ NumberStyles.AllowThousands, CultureInfo.CurrentCulture, out percent)) {
[10709]311          btnApply.Enabled = true;
312        }
313      }
314    }
315
316    private void txtDeleteColumnsInfo_TextChanged(object sender, EventArgs e) {
[10737]317      validateDeleteColumnsInfo();
[10709]318    }
319
320    private void txtDeleteColumnsVariance_TextChanged(object sender, EventArgs e) {
[10737]321      validateDeleteColumnsVariance();
[10709]322    }
323
324    private void txtDeleteRowsInfo_TextChanged(object sender, EventArgs e) {
[10737]325      validateDeleteRowsInfo();
[10709]326    }
[10737]327
[10776]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    }
[10709]338  }
339}
Note: See TracBrowser for help on using the repository browser.