Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 14876 was 14185, checked in by swagner, 8 years ago

#2526: Updated year of copyrights in license headers

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