Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 11037 was 11002, checked in by mleitner, 10 years ago

Refactoring

File size: 11.9 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        lblPreviewColumnsInfo.Text = count + " column" + (count > 1 || count == 0 ? "s" : "") + " were detected with more than " + txtDeleteColumnsInfo.Text + "% missing values.";
156        if (count > 0) {
157          lblPreviewColumnsInfo.Text += Environment.NewLine + Environment.NewLine + "Please press the button \"Apply Manipulation\" if you wish to delete those columns.";
158        } else {
159          btnApply.Enabled = false;
160        }
161      } else {
162        lblPreviewColumnsInfo.Text = "Preview not possible yet - please input the limit above.";
163      }
164    }
165
166    private void validateDeleteColumnsVariance() {
167      validateDoubleTextBox(txtDeleteColumnsVariance.Text);
168      if (btnApply.Enabled) {
169        int count = Content.ManipulationLogic.ColumnsWithVarianceSmaller(getDeleteColumnsVariance()).Count;
170        lblPreviewColumnsVariance.Text = count + " column" + (count > 1 || count == 0 ? "s" : "") + " were detected with a variance smaller than " + txtDeleteColumnsVariance.Text + ".";
171        if (count > 0) {
172          lblPreviewColumnsVariance.Text += Environment.NewLine + Environment.NewLine + "Please press the button \"Apply Manipulation\" if you wish to delete those columns.";
173        } else {
174          btnApply.Enabled = false;
175        }
176      } else {
177        lblPreviewColumnsVariance.Text = "Preview not possible yet - please input the limit for the variance above.";
178      }
179    }
180
181    private void validateDeleteRowsInfo() {
182      validateDoubleTextBox(txtDeleteRowsInfo.Text);
183      if (btnApply.Enabled) {
184        int count = Content.ManipulationLogic.RowsWithMissingValuesGreater(getRowsColumnsInfo()).Count;
185        lblPreviewRowsInfo.Text = count + " row" + (count > 1 || count == 0 ? "s" : "") + " were detected with more than " + txtDeleteRowsInfo.Text + "% missing values.";
186        if (count > 0) {
187          lblPreviewRowsInfo.Text += Environment.NewLine + Environment.NewLine + "Please press the button \"Apply Manipulation\" if you wish to delete those rows.";
188        } else {
189          btnApply.Enabled = false;
190        }
191      } else {
192        lblPreviewRowsInfo.Text = "Preview not possible yet - please input the limit above.";
193      }
194    }
195
196    private void validateReplaceWith() {
197      btnApply.Enabled = false;
198      string replaceWith = (string)cmbReplaceWith.SelectedItem;
199      int columnIndex = cmbVariableNames.SelectedIndex;
200
201      if (cmbReplaceWith.SelectedIndex == 0) {
202        string errorMessage;
203        string replaceValue = txtReplaceValue.Text;
204        if (string.IsNullOrEmpty(replaceValue)) {
205          lblPreviewReplaceMissingValues.Text = "Preview not possible yet - please input the text which will be used as replacement.";
206        } else if (!Content.ManipulationLogic.PreProcessingData.Validate(txtReplaceValue.Text, out errorMessage, columnIndex)) {
207          lblPreviewReplaceMissingValues.Text = "Preview not possible yet - " + errorMessage;
208        } else {
209          btnApply.Enabled = true;
210        }
211        replaceWith = "\"" + replaceValue + "\"";
212      } else {
213        btnApply.Enabled = true;
214      }
215      if (btnApply.Enabled) {
216        var allIndices = Content.SearchLogic.GetMissingValueIndices();
217        int count = allIndices[columnIndex].Count;
218        lblPreviewReplaceMissingValues.Text = count + " cell" + (count > 1 || count == 0 ? "s" : "")
219          + " were detected with missing values which would be replaced with " + replaceWith;
220        if (count > 0) {
221          lblPreviewReplaceMissingValues.Text += Environment.NewLine + Environment.NewLine + "Please press the button \"Apply Manipulation\" if you wish to perform the replacement.";
222        } else {
223          btnApply.Enabled = false;
224        }
225      }
226    }
227
228    public new ManipulationContent Content {
229      get { return (ManipulationContent)base.Content; }
230      set { base.Content = value; }
231    }
232
233    private void lstMethods_SelectedIndexChanged(object sender, System.EventArgs e) {
234      int index = lstMethods.SelectedIndex;
235      tabsData.SelectedIndex = index + 1;
236      tabsPreview.SelectedIndex = index + 1;
237      btnApply.Enabled = false;
238
239      //in order that button is enabled if necessary input was already entered
240      if (index >= 0) {
241        validators[index]();
242      }
243    }
244
245    private void btnApply_Click(object sender, System.EventArgs e) {
246      manipulations[lstMethods.SelectedIndex]();
247      switch (lstMethods.SelectedIndex) {
248        case 0:
249          lblPreviewColumnsInfo.Text = "columns successfully deleted.";
250          break;
251        case 1:
252          lblPreviewColumnsVariance.Text = "columns successfully deleted.";
253          break;
254        case 2:
255          lblPreviewRowsInfo.Text = "rows successfully deleted.";
256          break;
257        case 3:
258          lblPreviewReplaceMissingValues.Text = "missing values successfully replaced.";
259          btnApply.Enabled = false;
260          break;
261        case 4:
262          lblPreviewShuffle.Text = "dataset shuffled successfully.";
263          btnApply.Enabled = false;
264          break;
265      }
266    }
267
268    private void validateDoubleTextBox(String text) {
269      btnApply.Enabled = false;
270      if (!string.IsNullOrEmpty(text)) {
271        double percent;
272        if (Double.TryParse(text, out percent)) {
273          btnApply.Enabled = true;
274        }
275      }
276    }
277
278    private void txtDeleteColumnsInfo_TextChanged(object sender, EventArgs e) {
279      validateDeleteColumnsInfo();
280    }
281
282    private void txtDeleteColumnsVariance_TextChanged(object sender, EventArgs e) {
283      validateDeleteColumnsVariance();
284    }
285
286    private void txtDeleteRowsInfo_TextChanged(object sender, EventArgs e) {
287      validateDeleteRowsInfo();
288    }
289
290    void Content_Changed(object sender, DataPreprocessingChangedEventArgs e) {
291      switch (lstMethods.SelectedIndex) {
292        case 0:
293          validateDeleteColumnsInfo();
294          break;
295        case 1:
296          validateDeleteColumnsVariance();
297          break;
298        case 2:
299          validateDeleteRowsInfo();
300          break;
301      }
302    }
303
304    private void cmbReplaceWith_SelectedIndexChanged(object sender, EventArgs e) {
305      bool isReplaceWithValueSelected = cmbReplaceWith.SelectedIndex == 0;
306      lblValueColon.Visible = isReplaceWithValueSelected;
307      txtReplaceValue.Visible = isReplaceWithValueSelected;
308      validateReplaceWith();
309    }
310
311    private void txtReplaceValue_TextChanged(object sender, EventArgs e) {
312      validateReplaceWith();
313    }
314  }
315}
Note: See TracBrowser for help on using the repository browser.