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
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 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  [View("Manipulation Chart View")]
34  [Content(typeof(ManipulationContent), true)]
35  public partial class ManipulationView : ItemView {
36    private Action[] validators;
37    private Action[] manipulations;
38
39    public new ManipulationContent Content {
40      get { return (ManipulationContent)base.Content; }
41      set { base.Content = value; }
42    }
43
44    public ManipulationView() {
45      InitializeComponent();
46      cmbReplaceWith.SelectedIndex = 0;
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
54      validators = new Action[] {
55        () => ValidateDeleteColumnsInfo(),
56        () => ValidateDeleteColumnsVariance(),
57        () => ValidateDeleteRowsInfo(),
58        () => ValidateReplaceWith(),
59        () => ValidateShuffle()
60      };
61
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)
68      };
69    }
70
71    protected override void OnContentChanged() {
72      base.OnContentChanged();
73      if (Content != null) {
74        cmbVariableNames.Items.Clear();
75        foreach (var name in Content.ManipulationLogic.VariableNames) {
76          cmbVariableNames.Items.Add(name);
77        }
78        cmbVariableNames.SelectedIndex = 0;
79        CheckFilters();
80      }
81    }
82
83    protected override void RegisterContentEvents() {
84      base.RegisterContentEvents();
85      Content.FilterLogic.FilterChanged += FilterLogic_FilterChanged;
86    }
87
88    protected override void DeregisterContentEvents() {
89      Content.FilterLogic.FilterChanged -= FilterLogic_FilterChanged;
90      base.DeregisterContentEvents();
91    }
92
93    private void FilterLogic_FilterChanged(object sender, EventArgs e) {
94      if (Content != null) {
95        CheckFilters();
96      }
97    }
98
99    private void CheckFilters() {
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
116    private double GetDeleteColumnsInfo() {
117      return double.Parse(txtDeleteColumnsInfo.Text);
118    }
119
120    private double GetDeleteColumnsVariance() {
121      return double.Parse(txtDeleteColumnsVariance.Text);
122    }
123
124    private double GetRowsColumnsInfo() {
125      return double.Parse(txtDeleteRowsInfo.Text);
126    }
127
128    private void ReplaceMissingValues() {
129      var missingValuesIndices = new List<int>();
130      var columnIndex = cmbVariableNames.SelectedIndex;
131      var columnIndices = new Dictionary<int, IList<int>> { { columnIndex, missingValuesIndices } };
132
133      for (int rowIndex = 0; rowIndex < Content.ManipulationLogic.PreProcessingData.Rows; rowIndex++)
134        if (Content.ManipulationLogic.PreProcessingData.IsCellEmpty(columnIndex, rowIndex))
135          missingValuesIndices.Add(rowIndex);
136
137      switch (cmbReplaceWith.SelectedIndex) {
138        case 0: //Value
139          Content.ManipulationLogic.ReplaceIndicesByValue(columnIndices, txtReplaceValue.Text);
140          break;
141        case 1: //Average
142          Content.ManipulationLogic.ReplaceIndicesByAverageValue(columnIndices);
143          break;
144        case 2: //Median
145          Content.ManipulationLogic.ReplaceIndicesByMedianValue(columnIndices);
146          break;
147        case 3: //Most Common
148          Content.ManipulationLogic.ReplaceIndicesByMostCommonValue(columnIndices);
149          break;
150        case 4: //Random
151          Content.ManipulationLogic.ReplaceIndicesByRandomValue(columnIndices);
152          break;
153      }
154    }
155
156    private void ValidateDeleteColumnsInfo() {
157      ValidateDoubleTextBox(txtDeleteColumnsInfo.Text);
158      if (btnApply.Enabled) {
159        var filteredColumns = Content.ManipulationLogic.ColumnsWithMissingValuesGreater(GetDeleteColumnsInfo());
160        int count = filteredColumns.Count;
161        int columnCount = Content.FilterLogic.PreprocessingData.Columns;
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) {
166          StringBuilder sb = new StringBuilder();
167          sb.Append(Environment.NewLine);
168          sb.Append("Columns: ");
169          sb.Append(Content.ManipulationLogic.VariableNames.ElementAt(filteredColumns.ElementAt(0)));
170          for (int i = 1; i < filteredColumns.Count; i++) {
171            string columnName = Content.ManipulationLogic.VariableNames.ElementAt(filteredColumns.ElementAt(i));
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();
179        }
180
181        btnApply.Enabled = count > 0;
182      } else {
183        lblPreviewColumnsInfo.Text = "Preview not possible yet - please input the limit above.";
184      }
185    }
186
187    private void ValidateDeleteColumnsVariance() {
188      ValidateDoubleTextBox(txtDeleteColumnsVariance.Text);
189      if (btnApply.Enabled) {
190        var filteredColumns = Content.ManipulationLogic.ColumnsWithVarianceSmaller(GetDeleteColumnsVariance());
191        int count = filteredColumns.Count;
192        int columnCount = Content.FilterLogic.PreprocessingData.Columns;
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) {
197          StringBuilder sb = new StringBuilder();
198          sb.Append(Environment.NewLine);
199          sb.Append("Columns: ");
200          sb.Append(Content.ManipulationLogic.VariableNames.ElementAt(filteredColumns.ElementAt(0)));
201          for (int i = 1; i < filteredColumns.Count; i++) {
202            string columnName = Content.ManipulationLogic.VariableNames.ElementAt(filteredColumns.ElementAt(i));
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();
210        }
211
212        btnApply.Enabled = count > 0;
213      } else {
214        lblPreviewColumnsVariance.Text = "Preview not possible yet - please input the limit for the variance above.";
215      }
216    }
217
218    private void ValidateDeleteRowsInfo() {
219      ValidateDoubleTextBox(txtDeleteRowsInfo.Text);
220      if (btnApply.Enabled) {
221        int count = Content.ManipulationLogic.RowsWithMissingValuesGreater(GetRowsColumnsInfo()).Count;
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.";
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
234    private void ValidateReplaceWith() {
235      btnApply.Enabled = false;
236      string replaceWith = (string)cmbReplaceWith.SelectedItem;
237      int columnIndex = cmbVariableNames.SelectedIndex;
238
239      if (cmbReplaceWith.SelectedIndex == 0) {
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.";
244        } else if (!Content.ManipulationLogic.PreProcessingData.Validate(txtReplaceValue.Text, out errorMessage, columnIndex)) {
245          lblPreviewReplaceMissingValues.Text = "Preview not possible yet - " + errorMessage;
246        } else {
247          btnApply.Enabled = true;
248        }
249        replaceWith = "\"" + replaceValue + "\"";
250      } else {
251        btnApply.Enabled = true;
252      }
253      if (btnApply.Enabled) {
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
258        int cellCount = Content.FilterLogic.PreprocessingData.Rows * Content.FilterLogic.PreprocessingData.Columns;
259        lblPreviewReplaceMissingValues.Text = count + " cell" + (count > 1 || count == 0 ? "s" : "")
260          + " of " + cellCount + " (" + string.Format("{0:F2}%", 100d / cellCount * count) + ") were detected with missing values which would be replaced with " + replaceWith;
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
269    private void ValidateShuffle() {
270      btnApply.Enabled = true;
271      lblShuffleProperties.Enabled = false;
272      lblShuffleProperties.Visible = false;
273      shuffleSeparatelyCheckbox.Enabled = true;
274      shuffleSeparatelyCheckbox.Visible = true;
275    }
276
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
283      //in order that button is enabled if necessary input was already entered
284      if (index >= 0) {
285        validators[index]();
286      }
287    }
288
289    private void btnApply_Click(object sender, System.EventArgs e) {
290      manipulations[lstMethods.SelectedIndex]();
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:
302          lblPreviewReplaceMissingValues.Text = "missing values successfully replaced.";
303          btnApply.Enabled = false;
304          break;
305        case 4:
306          lblPreviewShuffle.Text = "dataset shuffled successfully.";
307          btnApply.Enabled = false;
308          break;
309      }
310    }
311
312    private void ValidateDoubleTextBox(String text) {
313      btnApply.Enabled = false;
314      if (!string.IsNullOrEmpty(text)) {
315        double percent;
316        if (Double.TryParse(text, NumberStyles.Number ^ NumberStyles.AllowThousands, CultureInfo.CurrentCulture, out percent)) {
317          btnApply.Enabled = true;
318        }
319      }
320    }
321
322    private void txtDeleteColumnsInfo_TextChanged(object sender, EventArgs e) {
323      ValidateDeleteColumnsInfo();
324    }
325
326    private void txtDeleteColumnsVariance_TextChanged(object sender, EventArgs e) {
327      ValidateDeleteColumnsVariance();
328    }
329
330    private void txtDeleteRowsInfo_TextChanged(object sender, EventArgs e) {
331      ValidateDeleteRowsInfo();
332    }
333
334    private void cmbReplaceWith_SelectedIndexChanged(object sender, EventArgs e) {
335      bool isReplaceWithValueSelected = cmbReplaceWith.SelectedIndex == 0;
336      lblValueColon.Visible = isReplaceWithValueSelected;
337      txtReplaceValue.Visible = isReplaceWithValueSelected;
338      ValidateReplaceWith();
339    }
340
341    private void txtReplaceValue_TextChanged(object sender, EventArgs e) {
342      ValidateReplaceWith();
343    }
344  }
345}
Note: See TracBrowser for help on using the repository browser.