Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 15269 was 15269, checked in by pfleck, 7 years ago

#2809: Removed SearchLogic

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