Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 15190 was 14186, checked in by swagner, 8 years ago

#2526: Updated year of copyrights in license headers

File size: 13.7 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
34  [View("Manipulation Chart View")]
35  [Content(typeof(ManipulationContent), true)]
36  public partial class ManipulationView : ItemView {
37
38    private Action[] validators;
39    private Action[] manipulations;
40
41    public ManipulationView() {
42      InitializeComponent();
43      cmbReplaceWith.SelectedIndex = 0;
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[] {
52        ()=>validateDeleteColumnsInfo(),
53        ()=>validateDeleteColumnsVariance(),
54        ()=>validateDeleteRowsInfo(),
55        ()=>validateReplaceWith(),
56        ()=>validateShuffle()
57      };
58
59      manipulations = new Action[] {
60        ()=>Content.ManipulationLogic.DeleteColumnsWithMissingValuesGreater(getDeleteColumnsInfo()),
61        ()=>Content.ManipulationLogic.DeleteColumnsWithVarianceSmaller(getDeleteColumnsVariance()),
62        ()=>Content.ManipulationLogic.DeleteRowsWithMissingValuesGreater(getRowsColumnsInfo()),
63        ()=>replaceMissingValues(),
64        ()=>Content.ManipulationLogic.Shuffle(shuffleSeparatelyCheckbox.Checked)
65      };
66    }
67
68    protected override void OnContentChanged() {
69      base.OnContentChanged();
70      if (Content != null) {
71        cmbVariableNames.Items.Clear();
72        foreach (var name in Content.ManipulationLogic.VariableNames) {
73          cmbVariableNames.Items.Add(name);
74        }
75        cmbVariableNames.SelectedIndex = 0;
76        CheckFilters();
77      }
78    }
79
80    protected override void RegisterContentEvents() {
81      base.RegisterContentEvents();
82      Content.FilterLogic.FilterChanged += FilterLogic_FilterChanged;
83    }
84
85    protected override void DeregisterContentEvents() {
86      Content.FilterLogic.FilterChanged -= FilterLogic_FilterChanged;
87      base.DeregisterContentEvents();
88    }
89
90    private void FilterLogic_FilterChanged(object sender, EventArgs e) {
91      if (Content != null) {
92        CheckFilters();
93      }
94    }
95
96    private void CheckFilters() {
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
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
125    private void replaceMissingValues() {
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) {
133        case 0: //Value
134          Content.ManipulationLogic.ReplaceIndicesByValue(columnIndices, txtReplaceValue.Text);
135          break;
136        case 1: //Average
137          Content.ManipulationLogic.ReplaceIndicesByAverageValue(columnIndices);
138          break;
139        case 2: //Median
140          Content.ManipulationLogic.ReplaceIndicesByMedianValue(columnIndices);
141          break;
142        case 3: //Most Common
143          Content.ManipulationLogic.ReplaceIndicesByMostCommonValue(columnIndices);
144          break;
145        case 4: //Random
146          Content.ManipulationLogic.ReplaceIndicesByRandomValue(columnIndices);
147          break;
148      }
149    }
150
151    private void validateDeleteColumnsInfo() {
152      validateDoubleTextBox(txtDeleteColumnsInfo.Text);
153      if (btnApply.Enabled) {
154        var filteredColumns = Content.ManipulationLogic.ColumnsWithMissingValuesGreater(getDeleteColumnsInfo());
155        int count = filteredColumns.Count;
156        int columnCount = Content.FilterLogic.PreprocessingData.Columns;
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) {
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();
174        }
175
176        btnApply.Enabled = count > 0;
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) {
185        var filteredColumns = Content.ManipulationLogic.ColumnsWithVarianceSmaller(getDeleteColumnsVariance());
186        int count = filteredColumns.Count;
187        int columnCount = Content.FilterLogic.PreprocessingData.Columns;
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) {
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();
205        }
206
207        btnApply.Enabled = count > 0;
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;
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.";
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
229    private void validateReplaceWith() {
230      btnApply.Enabled = false;
231      string replaceWith = (string)cmbReplaceWith.SelectedItem;
232      int columnIndex = cmbVariableNames.SelectedIndex;
233
234      if (cmbReplaceWith.SelectedIndex == 0) {
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.";
239        } else if (!Content.ManipulationLogic.PreProcessingData.Validate(txtReplaceValue.Text, out errorMessage, columnIndex)) {
240          lblPreviewReplaceMissingValues.Text = "Preview not possible yet - " + errorMessage;
241        } else {
242          btnApply.Enabled = true;
243        }
244        replaceWith = "\"" + replaceValue + "\"";
245      } else {
246        btnApply.Enabled = true;
247      }
248      if (btnApply.Enabled) {
249        var allIndices = Content.SearchLogic.GetMissingValueIndices();
250        int count = allIndices[columnIndex].Count;
251        int cellCount = Content.FilterLogic.PreprocessingData.Rows * Content.FilterLogic.PreprocessingData.Columns;
252        lblPreviewReplaceMissingValues.Text = count + " cell" + (count > 1 || count == 0 ? "s" : "")
253          + " of " + cellCount + " (" + string.Format("{0:F2}%", 100d / cellCount * count) + ") were detected with missing values which would be replaced with " + replaceWith;
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
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
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
281      //in order that button is enabled if necessary input was already entered
282      if (index >= 0) {
283        validators[index]();
284      }
285    }
286
287    private void btnApply_Click(object sender, System.EventArgs e) {
288      manipulations[lstMethods.SelectedIndex]();
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:
300          lblPreviewReplaceMissingValues.Text = "missing values successfully replaced.";
301          btnApply.Enabled = false;
302          break;
303        case 4:
304          lblPreviewShuffle.Text = "dataset shuffled successfully.";
305          btnApply.Enabled = false;
306          break;
307      }
308    }
309
310    private void validateDoubleTextBox(String text) {
311      btnApply.Enabled = false;
312      if (!string.IsNullOrEmpty(text)) {
313        double percent;
314        if (Double.TryParse(text, NumberStyles.Number ^ NumberStyles.AllowThousands, CultureInfo.CurrentCulture, out percent)) {
315          btnApply.Enabled = true;
316        }
317      }
318    }
319
320    private void txtDeleteColumnsInfo_TextChanged(object sender, EventArgs e) {
321      validateDeleteColumnsInfo();
322    }
323
324    private void txtDeleteColumnsVariance_TextChanged(object sender, EventArgs e) {
325      validateDeleteColumnsVariance();
326    }
327
328    private void txtDeleteRowsInfo_TextChanged(object sender, EventArgs e) {
329      validateDeleteRowsInfo();
330    }
331
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    }
342  }
343}
Note: See TracBrowser for help on using the repository browser.