Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2990_VariableImpactBasedFeatureSelection/HeuristicLab.DataPreprocessing.Views/3.4/ManipulationView.cs @ 17366

Last change on this file since 17366 was 16565, checked in by gkronber, 6 years ago

#2520: merged changes from PersistenceOverhaul branch (r16451:16564) into trunk

File size: 9.5 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2019 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.Drawing;
24using System.Globalization;
25using System.Linq;
26using System.Text;
27using System.Windows.Forms;
28using HeuristicLab.Core.Views;
29using HeuristicLab.MainForm;
30
31namespace HeuristicLab.DataPreprocessing.Views {
32  [View("Manipulation Chart View")]
33  [Content(typeof(ManipulationContent), true)]
34  public partial class ManipulationView : ItemView {
35    private Action[] validators;
36    private Action[] manipulations;
37
38    public new ManipulationContent Content {
39      get { return (ManipulationContent)base.Content; }
40      set { base.Content = value; }
41    }
42
43    public ManipulationView() {
44      InitializeComponent();
45      tabsData.Appearance = TabAppearance.FlatButtons;
46      tabsData.ItemSize = new Size(0, 1);
47      tabsData.SizeMode = TabSizeMode.Fixed;
48      tabsPreview.Appearance = TabAppearance.FlatButtons;
49      tabsPreview.ItemSize = new Size(0, 1);
50      tabsPreview.SizeMode = TabSizeMode.Fixed;
51
52      validators = new Action[] {
53        () => ValidateDeleteColumnsInfo(),
54        () => ValidateDeleteColumnsVariance(),
55        () => ValidateDeleteRowsInfo(),
56      };
57
58      manipulations = new Action[] {
59        () => Content.DeleteColumnsWithMissingValuesGreater(GetDeleteColumnsInfo()),
60        () => Content.DeleteColumnsWithVarianceSmaller(GetDeleteColumnsVariance()),
61        () => Content.DeleteRowsWithMissingValuesGreater(GetRowsColumnsInfo()),
62      };
63    }
64
65    protected override void OnContentChanged() {
66      base.OnContentChanged();
67      if (Content != null) {
68        CheckFilters();
69      }
70    }
71
72    protected override void RegisterContentEvents() {
73      base.RegisterContentEvents();
74      Content.PreprocessingData.FilterChanged += FilterLogic_FilterChanged;
75    }
76
77    protected override void DeregisterContentEvents() {
78      Content.PreprocessingData.FilterChanged -= FilterLogic_FilterChanged;
79      base.DeregisterContentEvents();
80    }
81
82    private void FilterLogic_FilterChanged(object sender, EventArgs e) {
83      if (Content != null) {
84        CheckFilters();
85      }
86    }
87
88    private void CheckFilters() {
89      if (Content.PreprocessingData.IsFiltered) {
90        tabsPreview.SelectedIndex = 0;
91        lstMethods.Enabled = false;
92        tabsData.Enabled = false;
93        tabsPreview.Enabled = false;
94        lblPreviewInActive.Visible = true;
95        btnApply.Enabled = false;
96      } else {
97        lblPreviewInActive.Visible = false;
98        tabsData.Enabled = true;
99        tabsPreview.Enabled = true;
100        lstMethods.Enabled = true;
101        lstMethods_SelectedIndexChanged(null, null);
102      }
103    }
104
105    private double GetDeleteColumnsInfo() {
106      return double.Parse(txtDeleteColumnsInfo.Text);
107    }
108
109    private double GetDeleteColumnsVariance() {
110      return double.Parse(txtDeleteColumnsVariance.Text);
111    }
112
113    private double GetRowsColumnsInfo() {
114      return double.Parse(txtDeleteRowsInfo.Text);
115    }
116
117    private void ValidateDeleteColumnsInfo() {
118      ValidateDoubleTextBox(txtDeleteColumnsInfo.Text);
119      if (btnApply.Enabled) {
120        var filteredColumns = Content.ColumnsWithMissingValuesGreater(GetDeleteColumnsInfo());
121        int count = filteredColumns.Count;
122        int columnCount = Content.PreprocessingData.Columns;
123        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);
124
125        //only display column names more than 0 and fewer than 50 are affected
126        if (count > 0 && count < 50) {
127          StringBuilder sb = new StringBuilder();
128          sb.Append(Environment.NewLine);
129          sb.Append("Columns: ");
130          sb.Append(Content.PreprocessingData.VariableNames.ElementAt(filteredColumns.ElementAt(0)));
131          for (int i = 1; i < filteredColumns.Count; i++) {
132            string columnName = Content.PreprocessingData.VariableNames.ElementAt(filteredColumns.ElementAt(i));
133            sb.Append(", ");
134            sb.Append(columnName);
135          }
136          sb.Append(Environment.NewLine);
137          sb.Append("Please press the button \"Apply Manipulation\" if you wish to delete those columns.");
138
139          lblPreviewColumnsInfo.Text += sb.ToString();
140        }
141
142        btnApply.Enabled = count > 0;
143      } else {
144        lblPreviewColumnsInfo.Text = "Preview not possible yet - please input the limit above.";
145      }
146    }
147
148    private void ValidateDeleteColumnsVariance() {
149      ValidateDoubleTextBox(txtDeleteColumnsVariance.Text);
150      if (btnApply.Enabled) {
151        var filteredColumns = Content.ColumnsWithVarianceSmaller(GetDeleteColumnsVariance());
152        int count = filteredColumns.Count;
153        int columnCount = Content.PreprocessingData.Columns;
154        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);
155
156        //only display column names more than 0 and fewer than 50 are affected
157        if (count > 0 && count < 50) {
158          StringBuilder sb = new StringBuilder();
159          sb.Append(Environment.NewLine);
160          sb.Append("Columns: ");
161          sb.Append(Content.PreprocessingData.VariableNames.ElementAt(filteredColumns.ElementAt(0)));
162          for (int i = 1; i < filteredColumns.Count; i++) {
163            string columnName = Content.PreprocessingData.VariableNames.ElementAt(filteredColumns.ElementAt(i));
164            sb.Append(", ");
165            sb.Append(columnName);
166          }
167          sb.Append(Environment.NewLine);
168          sb.Append("Please press the button \"Apply Manipulation\" if you wish to delete those columns.");
169
170          lblPreviewColumnsVariance.Text += sb.ToString();
171        }
172
173        btnApply.Enabled = count > 0;
174      } else {
175        lblPreviewColumnsVariance.Text = "Preview not possible yet - please input the limit for the variance above.";
176      }
177    }
178
179    private void ValidateDeleteRowsInfo() {
180      ValidateDoubleTextBox(txtDeleteRowsInfo.Text);
181      if (btnApply.Enabled) {
182        int count = Content.RowsWithMissingValuesGreater(GetRowsColumnsInfo()).Count;
183        int rowCount = Content.PreprocessingData.Rows;
184        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.";
185        if (count > 0) {
186          lblPreviewRowsInfo.Text += Environment.NewLine + Environment.NewLine + "Please press the button \"Apply Manipulation\" if you wish to delete those rows.";
187        } else {
188          btnApply.Enabled = false;
189        }
190      } else {
191        lblPreviewRowsInfo.Text = "Preview not possible yet - please input the limit above.";
192      }
193    }
194
195    private void lstMethods_SelectedIndexChanged(object sender, System.EventArgs e) {
196      int index = lstMethods.SelectedIndex;
197      tabsData.SelectedIndex = index + 1;
198      tabsPreview.SelectedIndex = index + 1;
199      btnApply.Enabled = false;
200
201      //in order that button is enabled if necessary input was already entered
202      if (index >= 0) {
203        validators[index]();
204      }
205    }
206
207    private void btnApply_Click(object sender, System.EventArgs e) {
208      manipulations[lstMethods.SelectedIndex]();
209      switch (lstMethods.SelectedIndex) {
210        case 0:
211          lblPreviewColumnsInfo.Text = "columns successfully deleted.";
212          break;
213        case 1:
214          lblPreviewColumnsVariance.Text = "columns successfully deleted.";
215          break;
216        case 2:
217          lblPreviewRowsInfo.Text = "rows successfully deleted.";
218          break;
219      }
220    }
221
222    private void ValidateDoubleTextBox(String text) {
223      btnApply.Enabled = false;
224      if (!string.IsNullOrEmpty(text)) {
225        double percent;
226        if (Double.TryParse(text, NumberStyles.Number ^ NumberStyles.AllowThousands, CultureInfo.CurrentCulture, out percent)) {
227          btnApply.Enabled = true;
228        }
229      }
230    }
231
232    private void txtDeleteColumnsInfo_TextChanged(object sender, EventArgs e) {
233      ValidateDeleteColumnsInfo();
234    }
235
236    private void txtDeleteColumnsVariance_TextChanged(object sender, EventArgs e) {
237      ValidateDeleteColumnsVariance();
238    }
239
240    private void txtDeleteRowsInfo_TextChanged(object sender, EventArgs e) {
241      ValidateDeleteRowsInfo();
242    }
243  }
244}
Note: See TracBrowser for help on using the repository browser.