Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DataPreprocessing/HeuristicLab.DataPreprocessing.Views/3.4/ManipulationView.cs @ 11068

Last change on this file since 11068 was 11045, checked in by rstoll, 10 years ago
  • preview info for manipulation view completed with total row count and percentage
File size: 12.6 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2013 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.Windows.Forms;
26using HeuristicLab.Core.Views;
27using HeuristicLab.MainForm;
28
29namespace HeuristicLab.DataPreprocessing.Views {
30
31  [View("Manipulation Chart View")]
32  [Content(typeof(ManipulationContent), true)]
33  public partial class ManipulationView : ItemView {
34
35    private Action[] validators;
36    private Action[] manipulations;
37
38    public ManipulationView() {
39      InitializeComponent();
40      cmbReplaceWith.SelectedIndex = 0;
41      tabsData.Appearance = TabAppearance.FlatButtons;
42      tabsData.ItemSize = new Size(0, 1);
43      tabsData.SizeMode = TabSizeMode.Fixed;
44      tabsPreview.Appearance = TabAppearance.FlatButtons;
45      tabsPreview.ItemSize = new Size(0, 1);
46      tabsPreview.SizeMode = TabSizeMode.Fixed;
47
48      validators = new Action[] {
49        ()=>validateDeleteColumnsInfo(),
50        ()=>validateDeleteColumnsVariance(),
51        ()=>validateDeleteRowsInfo(),
52        ()=>validateReplaceWith(),
53        ()=>{btnApply.Enabled = true; lblPreviewShuffle.Text = "Data will be shuffled randomly - preview not possible";} //shuffle
54      };
55
56      manipulations = new Action[] {
57        ()=>Content.ManipulationLogic.DeleteColumnsWithMissingValuesGreater(getDeleteColumnsInfo()),
58        ()=>Content.ManipulationLogic.DeleteColumnsWithVarianceSmaller(getDeleteColumnsVariance()),
59        ()=>Content.ManipulationLogic.DeleteRowsWithMissingValuesGreater(getRowsColumnsInfo()),
60        ()=>replaceMissingValues(),
61        ()=>Content.ManipulationLogic.ShuffleWithRanges()
62      };
63
64    }
65
66    protected override void OnContentChanged() {
67      base.OnContentChanged();
68      if (Content != null) {
69        cmbVariableNames.Items.Clear();
70        foreach (var name in Content.ManipulationLogic.VariableNames) {
71          cmbVariableNames.Items.Add(name);
72        }
73        cmbVariableNames.SelectedIndex = 0;
74        CheckFilters();
75      }
76    }
77
78    protected override void RegisterContentEvents() {
79      base.RegisterContentEvents();
80      Content.ManipulationLogic.Changed += Content_Changed;
81      Content.FilterLogic.FilterChanged += FilterLogic_FilterChanged;
82    }
83
84    protected override void DeregisterContentEvents() {
85      base.DeregisterContentEvents();
86      Content.ManipulationLogic.Changed -= Content_Changed;
87      Content.FilterLogic.FilterChanged -= FilterLogic_FilterChanged;
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        int count = Content.ManipulationLogic.ColumnsWithMissingValuesGreater(getDeleteColumnsInfo()).Count;
155        int rowCount = Content.FilterLogic.PreprocessingData.Rows;
156        lblPreviewColumnsInfo.Text = count + " column" + (count > 1 || count == 0 ? "s" : "") + " of " + rowCount + " (" + string.Format("{0:F2}%", 100d / rowCount * count) + ") were detected with more than " + txtDeleteColumnsInfo.Text + "% missing values.";
157        if (count > 0) {
158          lblPreviewColumnsInfo.Text += Environment.NewLine + Environment.NewLine + "Please press the button \"Apply Manipulation\" if you wish to delete those columns.";
159        } else {
160          btnApply.Enabled = false;
161        }
162      } else {
163        lblPreviewColumnsInfo.Text = "Preview not possible yet - please input the limit above.";
164      }
165    }
166
167    private void validateDeleteColumnsVariance() {
168      validateDoubleTextBox(txtDeleteColumnsVariance.Text);
169      if (btnApply.Enabled) {
170        int count = Content.ManipulationLogic.ColumnsWithVarianceSmaller(getDeleteColumnsVariance()).Count;
171        int rowCount = Content.FilterLogic.PreprocessingData.Rows;
172        lblPreviewColumnsVariance.Text = count + " column" + (count > 1 || count == 0 ? "s" : "") + " of " + rowCount + " (" + string.Format("{0:F2}%", 100d / rowCount * count) + ") were detected with a variance smaller than " + txtDeleteColumnsVariance.Text + ".";
173        if (count > 0) {
174          lblPreviewColumnsVariance.Text += Environment.NewLine + Environment.NewLine + "Please press the button \"Apply Manipulation\" if you wish to delete those columns.";
175        } else {
176          btnApply.Enabled = false;
177        }
178      } else {
179        lblPreviewColumnsVariance.Text = "Preview not possible yet - please input the limit for the variance above.";
180      }
181    }
182
183    private void validateDeleteRowsInfo() {
184      validateDoubleTextBox(txtDeleteRowsInfo.Text);
185      if (btnApply.Enabled) {
186        int count = Content.ManipulationLogic.RowsWithMissingValuesGreater(getRowsColumnsInfo()).Count;
187        int rowCount = Content.FilterLogic.PreprocessingData.Rows;
188        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.";
189        if (count > 0) {
190          lblPreviewRowsInfo.Text += Environment.NewLine + Environment.NewLine + "Please press the button \"Apply Manipulation\" if you wish to delete those rows.";
191        } else {
192          btnApply.Enabled = false;
193        }
194      } else {
195        lblPreviewRowsInfo.Text = "Preview not possible yet - please input the limit above.";
196      }
197    }
198
199    private void validateReplaceWith() {
200      btnApply.Enabled = false;
201      string replaceWith = (string)cmbReplaceWith.SelectedItem;
202      int columnIndex = cmbVariableNames.SelectedIndex;
203
204      if (cmbReplaceWith.SelectedIndex == 0) {
205        string errorMessage;
206        string replaceValue = txtReplaceValue.Text;
207        if (string.IsNullOrEmpty(replaceValue)) {
208          lblPreviewReplaceMissingValues.Text = "Preview not possible yet - please input the text which will be used as replacement.";
209        } else if (!Content.ManipulationLogic.PreProcessingData.Validate(txtReplaceValue.Text, out errorMessage, columnIndex)) {
210          lblPreviewReplaceMissingValues.Text = "Preview not possible yet - " + errorMessage;
211        } else {
212          btnApply.Enabled = true;
213        }
214        replaceWith = "\"" + replaceValue + "\"";
215      } else {
216        btnApply.Enabled = true;
217      }
218      if (btnApply.Enabled) {
219        var allIndices = Content.SearchLogic.GetMissingValueIndices();
220        int count = allIndices[columnIndex].Count;
221        int cellCount = Content.FilterLogic.PreprocessingData.Rows*Content.FilterLogic.PreprocessingData.Columns;
222        lblPreviewReplaceMissingValues.Text = count + " cell" + (count > 1 || count == 0 ? "s" : "")
223          + " of " + cellCount + " (" + string.Format("{0:F2}%", 100d / cellCount * count) + ") were detected with missing values which would be replaced with " + replaceWith;
224        if (count > 0) {
225          lblPreviewReplaceMissingValues.Text += Environment.NewLine + Environment.NewLine + "Please press the button \"Apply Manipulation\" if you wish to perform the replacement.";
226        } else {
227          btnApply.Enabled = false;
228        }
229      }
230    }
231
232    public new ManipulationContent Content {
233      get { return (ManipulationContent)base.Content; }
234      set { base.Content = value; }
235    }
236
237    private void lstMethods_SelectedIndexChanged(object sender, System.EventArgs e) {
238      int index = lstMethods.SelectedIndex;
239      tabsData.SelectedIndex = index + 1;
240      tabsPreview.SelectedIndex = index + 1;
241      btnApply.Enabled = false;
242
243      //in order that button is enabled if necessary input was already entered
244      if (index >= 0) {
245        validators[index]();
246      }
247    }
248
249    private void btnApply_Click(object sender, System.EventArgs e) {
250      manipulations[lstMethods.SelectedIndex]();
251      switch (lstMethods.SelectedIndex) {
252        case 0:
253          lblPreviewColumnsInfo.Text = "columns successfully deleted.";
254          break;
255        case 1:
256          lblPreviewColumnsVariance.Text = "columns successfully deleted.";
257          break;
258        case 2:
259          lblPreviewRowsInfo.Text = "rows successfully deleted.";
260          break;
261        case 3:
262          lblPreviewReplaceMissingValues.Text = "missing values successfully replaced.";
263          btnApply.Enabled = false;
264          break;
265        case 4:
266          lblPreviewShuffle.Text = "dataset shuffled successfully.";
267          btnApply.Enabled = false;
268          break;
269      }
270    }
271
272    private void validateDoubleTextBox(String text) {
273      btnApply.Enabled = false;
274      if (!string.IsNullOrEmpty(text)) {
275        double percent;
276        if (Double.TryParse(text, out percent)) {
277          btnApply.Enabled = true;
278        }
279      }
280    }
281
282    private void txtDeleteColumnsInfo_TextChanged(object sender, EventArgs e) {
283      validateDeleteColumnsInfo();
284    }
285
286    private void txtDeleteColumnsVariance_TextChanged(object sender, EventArgs e) {
287      validateDeleteColumnsVariance();
288    }
289
290    private void txtDeleteRowsInfo_TextChanged(object sender, EventArgs e) {
291      validateDeleteRowsInfo();
292    }
293
294    void Content_Changed(object sender, DataPreprocessingChangedEventArgs e) {
295      switch (lstMethods.SelectedIndex) {
296        case 0:
297          validateDeleteColumnsInfo();
298          break;
299        case 1:
300          validateDeleteColumnsVariance();
301          break;
302        case 2:
303          validateDeleteRowsInfo();
304          break;
305      }
306    }
307
308    private void cmbReplaceWith_SelectedIndexChanged(object sender, EventArgs e) {
309      bool isReplaceWithValueSelected = cmbReplaceWith.SelectedIndex == 0;
310      lblValueColon.Visible = isReplaceWithValueSelected;
311      txtReplaceValue.Visible = isReplaceWithValueSelected;
312      validateReplaceWith();
313    }
314
315    private void txtReplaceValue_TextChanged(object sender, EventArgs e) {
316      validateReplaceWith();
317    }
318  }
319}
Note: See TracBrowser for help on using the repository browser.