Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2709: merged branch to trunk

File size: 13.7 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() {
[10905]129      var allIndices = Content.SearchLogic.GetMissingValueIndices();
130      var columnIndex = cmbVariableNames.SelectedIndex;
131      var columnIndices = new Dictionary<int, IList<int>>{
132          {columnIndex,   allIndices[columnIndex]}
133      };
134
135      switch (cmbReplaceWith.SelectedIndex) {
[10776]136        case 0: //Value
[10905]137          Content.ManipulationLogic.ReplaceIndicesByValue(columnIndices, txtReplaceValue.Text);
[10776]138          break;
139        case 1: //Average
[10905]140          Content.ManipulationLogic.ReplaceIndicesByAverageValue(columnIndices);
[10776]141          break;
142        case 2: //Median
[10905]143          Content.ManipulationLogic.ReplaceIndicesByMedianValue(columnIndices);
[10776]144          break;
145        case 3: //Most Common
[10905]146          Content.ManipulationLogic.ReplaceIndicesByMostCommonValue(columnIndices);
[10776]147          break;
148        case 4: //Random
[10905]149          Content.ManipulationLogic.ReplaceIndicesByRandomValue(columnIndices);
[10776]150          break;
151      }
152    }
153
[15110]154    private void ValidateDeleteColumnsInfo() {
155      ValidateDoubleTextBox(txtDeleteColumnsInfo.Text);
[10737]156      if (btnApply.Enabled) {
[15110]157        var filteredColumns = Content.ManipulationLogic.ColumnsWithMissingValuesGreater(GetDeleteColumnsInfo());
[12676]158        int count = filteredColumns.Count;
159        int columnCount = Content.FilterLogic.PreprocessingData.Columns;
[13796]160        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);
161
162        //only display column names more than 0 and fewer than 50 are affected
163        if (count > 0 && count < 50) {
[12676]164          StringBuilder sb = new StringBuilder();
165          sb.Append(Environment.NewLine);
166          sb.Append("Columns: ");
167          sb.Append(Content.SearchLogic.VariableNames.ElementAt(filteredColumns.ElementAt(0)));
168          for (int i = 1; i < filteredColumns.Count; i++) {
169            string columnName = Content.SearchLogic.VariableNames.ElementAt(filteredColumns.ElementAt(i));
170            sb.Append(", ");
171            sb.Append(columnName);
172          }
173          sb.Append(Environment.NewLine);
174          sb.Append("Please press the button \"Apply Manipulation\" if you wish to delete those columns.");
175
176          lblPreviewColumnsInfo.Text += sb.ToString();
[10737]177        }
[13796]178
179        btnApply.Enabled = count > 0;
[10737]180      } else {
181        lblPreviewColumnsInfo.Text = "Preview not possible yet - please input the limit above.";
182      }
183    }
184
[15110]185    private void ValidateDeleteColumnsVariance() {
186      ValidateDoubleTextBox(txtDeleteColumnsVariance.Text);
[10737]187      if (btnApply.Enabled) {
[15110]188        var filteredColumns = Content.ManipulationLogic.ColumnsWithVarianceSmaller(GetDeleteColumnsVariance());
[12676]189        int count = filteredColumns.Count;
190        int columnCount = Content.FilterLogic.PreprocessingData.Columns;
[13796]191        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);
192
193        //only display column names more than 0 and fewer than 50 are affected
194        if (count > 0 && count < 50) {
[12676]195          StringBuilder sb = new StringBuilder();
196          sb.Append(Environment.NewLine);
197          sb.Append("Columns: ");
198          sb.Append(Content.SearchLogic.VariableNames.ElementAt(filteredColumns.ElementAt(0)));
199          for (int i = 1; i < filteredColumns.Count; i++) {
200            string columnName = Content.SearchLogic.VariableNames.ElementAt(filteredColumns.ElementAt(i));
201            sb.Append(", ");
202            sb.Append(columnName);
203          }
204          sb.Append(Environment.NewLine);
205          sb.Append("Please press the button \"Apply Manipulation\" if you wish to delete those columns.");
206
207          lblPreviewColumnsVariance.Text += sb.ToString();
[10737]208        }
[13796]209
210        btnApply.Enabled = count > 0;
[10737]211      } else {
212        lblPreviewColumnsVariance.Text = "Preview not possible yet - please input the limit for the variance above.";
213      }
214    }
215
[15110]216    private void ValidateDeleteRowsInfo() {
217      ValidateDoubleTextBox(txtDeleteRowsInfo.Text);
[10737]218      if (btnApply.Enabled) {
[15110]219        int count = Content.ManipulationLogic.RowsWithMissingValuesGreater(GetRowsColumnsInfo()).Count;
[11045]220        int rowCount = Content.FilterLogic.PreprocessingData.Rows;
221        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]222        if (count > 0) {
223          lblPreviewRowsInfo.Text += Environment.NewLine + Environment.NewLine + "Please press the button \"Apply Manipulation\" if you wish to delete those rows.";
224        } else {
225          btnApply.Enabled = false;
226        }
227      } else {
228        lblPreviewRowsInfo.Text = "Preview not possible yet - please input the limit above.";
229      }
230    }
231
[15110]232    private void ValidateReplaceWith() {
[10776]233      btnApply.Enabled = false;
[10905]234      string replaceWith = (string)cmbReplaceWith.SelectedItem;
235      int columnIndex = cmbVariableNames.SelectedIndex;
236
[10776]237      if (cmbReplaceWith.SelectedIndex == 0) {
[10905]238        string errorMessage;
239        string replaceValue = txtReplaceValue.Text;
240        if (string.IsNullOrEmpty(replaceValue)) {
241          lblPreviewReplaceMissingValues.Text = "Preview not possible yet - please input the text which will be used as replacement.";
[11002]242        } else if (!Content.ManipulationLogic.PreProcessingData.Validate(txtReplaceValue.Text, out errorMessage, columnIndex)) {
[10905]243          lblPreviewReplaceMissingValues.Text = "Preview not possible yet - " + errorMessage;
244        } else {
245          btnApply.Enabled = true;
246        }
247        replaceWith = "\"" + replaceValue + "\"";
[10776]248      } else {
249        btnApply.Enabled = true;
250      }
251      if (btnApply.Enabled) {
[10905]252        var allIndices = Content.SearchLogic.GetMissingValueIndices();
253        int count = allIndices[columnIndex].Count;
[11070]254        int cellCount = Content.FilterLogic.PreprocessingData.Rows * Content.FilterLogic.PreprocessingData.Columns;
[10905]255        lblPreviewReplaceMissingValues.Text = count + " cell" + (count > 1 || count == 0 ? "s" : "")
[11045]256          + " of " + cellCount + " (" + string.Format("{0:F2}%", 100d / cellCount * count) + ") were detected with missing values which would be replaced with " + replaceWith;
[10776]257        if (count > 0) {
258          lblPreviewReplaceMissingValues.Text += Environment.NewLine + Environment.NewLine + "Please press the button \"Apply Manipulation\" if you wish to perform the replacement.";
259        } else {
260          btnApply.Enabled = false;
261        }
262      }
263    }
264
[15110]265    private void ValidateShuffle() {
[11381]266      btnApply.Enabled = true;
267      lblShuffleProperties.Enabled = false;
268      lblShuffleProperties.Visible = false;
269      shuffleSeparatelyCheckbox.Enabled = true;
270      shuffleSeparatelyCheckbox.Visible = true;
271    }
272
[10709]273    private void lstMethods_SelectedIndexChanged(object sender, System.EventArgs e) {
274      int index = lstMethods.SelectedIndex;
275      tabsData.SelectedIndex = index + 1;
276      tabsPreview.SelectedIndex = index + 1;
277      btnApply.Enabled = false;
278
[10737]279      //in order that button is enabled if necessary input was already entered
[10710]280      if (index >= 0) {
281        validators[index]();
282      }
[10709]283    }
284
285    private void btnApply_Click(object sender, System.EventArgs e) {
286      manipulations[lstMethods.SelectedIndex]();
[10737]287      switch (lstMethods.SelectedIndex) {
288        case 0:
289          lblPreviewColumnsInfo.Text = "columns successfully deleted.";
290          break;
291        case 1:
292          lblPreviewColumnsVariance.Text = "columns successfully deleted.";
293          break;
294        case 2:
295          lblPreviewRowsInfo.Text = "rows successfully deleted.";
296          break;
297        case 3:
[10905]298          lblPreviewReplaceMissingValues.Text = "missing values successfully replaced.";
299          btnApply.Enabled = false;
300          break;
301        case 4:
[10737]302          lblPreviewShuffle.Text = "dataset shuffled successfully.";
303          btnApply.Enabled = false;
304          break;
305      }
[10709]306    }
307
[15110]308    private void ValidateDoubleTextBox(String text) {
[10709]309      btnApply.Enabled = false;
310      if (!string.IsNullOrEmpty(text)) {
311        double percent;
[12676]312        if (Double.TryParse(text, NumberStyles.Number ^ NumberStyles.AllowThousands, CultureInfo.CurrentCulture, out percent)) {
[10709]313          btnApply.Enabled = true;
314        }
315      }
316    }
317
318    private void txtDeleteColumnsInfo_TextChanged(object sender, EventArgs e) {
[15110]319      ValidateDeleteColumnsInfo();
[10709]320    }
321
322    private void txtDeleteColumnsVariance_TextChanged(object sender, EventArgs e) {
[15110]323      ValidateDeleteColumnsVariance();
[10709]324    }
325
326    private void txtDeleteRowsInfo_TextChanged(object sender, EventArgs e) {
[15110]327      ValidateDeleteRowsInfo();
[10709]328    }
[10737]329
[10776]330    private void cmbReplaceWith_SelectedIndexChanged(object sender, EventArgs e) {
331      bool isReplaceWithValueSelected = cmbReplaceWith.SelectedIndex == 0;
332      lblValueColon.Visible = isReplaceWithValueSelected;
333      txtReplaceValue.Visible = isReplaceWithValueSelected;
[15110]334      ValidateReplaceWith();
[10776]335    }
336
337    private void txtReplaceValue_TextChanged(object sender, EventArgs e) {
[15110]338      ValidateReplaceWith();
[10776]339    }
[10709]340  }
341}
Note: See TracBrowser for help on using the repository browser.