Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 12329 was 12012, checked in by ascheibe, 10 years ago

#2212 merged r12008, r12009, r12010 back into trunk

File size: 12.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 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        ()=>validateShuffle()
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.Shuffle(shuffleSeparatelyCheckbox.Checked)
62      };
63    }
64
65    protected override void OnContentChanged() {
66      base.OnContentChanged();
67      if (Content != null) {
68        cmbVariableNames.Items.Clear();
69        foreach (var name in Content.ManipulationLogic.VariableNames) {
70          cmbVariableNames.Items.Add(name);
71        }
72        cmbVariableNames.SelectedIndex = 0;
73        CheckFilters();
74      }
75    }
76
77    protected override void RegisterContentEvents() {
78      base.RegisterContentEvents();
79      Content.FilterLogic.FilterChanged += FilterLogic_FilterChanged;
80    }
81
82    protected override void DeregisterContentEvents() {
83      Content.FilterLogic.FilterChanged -= FilterLogic_FilterChanged;
84      base.DeregisterContentEvents();
85    }
86
87    private void FilterLogic_FilterChanged(object sender, EventArgs e) {
88      if (Content != null) {
89        CheckFilters();
90      }
91    }
92
93    private void CheckFilters() {
94      if (Content.FilterLogic.IsFiltered) {
95        tabsPreview.SelectedIndex = 0;
96        lstMethods.Enabled = false;
97        tabsData.Enabled = false;
98        tabsPreview.Enabled = false;
99        lblPreviewInActive.Visible = true;
100        btnApply.Enabled = false;
101      } else {
102        lblPreviewInActive.Visible = false;
103        tabsData.Enabled = true;
104        tabsPreview.Enabled = true;
105        lstMethods.Enabled = true;
106        lstMethods_SelectedIndexChanged(null, null);
107      }
108    }
109
110    private double getDeleteColumnsInfo() {
111      return double.Parse(txtDeleteColumnsInfo.Text);
112    }
113
114    private double getDeleteColumnsVariance() {
115      return double.Parse(txtDeleteColumnsVariance.Text);
116    }
117
118    private double getRowsColumnsInfo() {
119      return double.Parse(txtDeleteRowsInfo.Text);
120    }
121
122    private void replaceMissingValues() {
123      var allIndices = Content.SearchLogic.GetMissingValueIndices();
124      var columnIndex = cmbVariableNames.SelectedIndex;
125      var columnIndices = new Dictionary<int, IList<int>>{
126          {columnIndex,   allIndices[columnIndex]}
127      };
128
129      switch (cmbReplaceWith.SelectedIndex) {
130        case 0: //Value
131          Content.ManipulationLogic.ReplaceIndicesByValue(columnIndices, txtReplaceValue.Text);
132          break;
133        case 1: //Average
134          Content.ManipulationLogic.ReplaceIndicesByAverageValue(columnIndices);
135          break;
136        case 2: //Median
137          Content.ManipulationLogic.ReplaceIndicesByMedianValue(columnIndices);
138          break;
139        case 3: //Most Common
140          Content.ManipulationLogic.ReplaceIndicesByMostCommonValue(columnIndices);
141          break;
142        case 4: //Random
143          Content.ManipulationLogic.ReplaceIndicesByRandomValue(columnIndices);
144          break;
145      }
146    }
147
148    private void validateDeleteColumnsInfo() {
149      validateDoubleTextBox(txtDeleteColumnsInfo.Text);
150      if (btnApply.Enabled) {
151        int count = Content.ManipulationLogic.ColumnsWithMissingValuesGreater(getDeleteColumnsInfo()).Count;
152        int rowCount = Content.FilterLogic.PreprocessingData.Rows;
153        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.";
154        if (count > 0) {
155          lblPreviewColumnsInfo.Text += Environment.NewLine + Environment.NewLine + "Please press the button \"Apply Manipulation\" if you wish to delete those columns.";
156        } else {
157          btnApply.Enabled = false;
158        }
159      } else {
160        lblPreviewColumnsInfo.Text = "Preview not possible yet - please input the limit above.";
161      }
162    }
163
164    private void validateDeleteColumnsVariance() {
165      validateDoubleTextBox(txtDeleteColumnsVariance.Text);
166      if (btnApply.Enabled) {
167        int count = Content.ManipulationLogic.ColumnsWithVarianceSmaller(getDeleteColumnsVariance()).Count;
168        int rowCount = Content.FilterLogic.PreprocessingData.Rows;
169        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 + ".";
170        if (count > 0) {
171          lblPreviewColumnsVariance.Text += Environment.NewLine + Environment.NewLine + "Please press the button \"Apply Manipulation\" if you wish to delete those columns.";
172        } else {
173          btnApply.Enabled = false;
174        }
175      } else {
176        lblPreviewColumnsVariance.Text = "Preview not possible yet - please input the limit for the variance above.";
177      }
178    }
179
180    private void validateDeleteRowsInfo() {
181      validateDoubleTextBox(txtDeleteRowsInfo.Text);
182      if (btnApply.Enabled) {
183        int count = Content.ManipulationLogic.RowsWithMissingValuesGreater(getRowsColumnsInfo()).Count;
184        int rowCount = Content.FilterLogic.PreprocessingData.Rows;
185        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.";
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        int cellCount = Content.FilterLogic.PreprocessingData.Rows * Content.FilterLogic.PreprocessingData.Columns;
219        lblPreviewReplaceMissingValues.Text = count + " cell" + (count > 1 || count == 0 ? "s" : "")
220          + " of " + cellCount + " (" + string.Format("{0:F2}%", 100d / cellCount * count) + ") were detected with missing values which would be replaced with " + replaceWith;
221        if (count > 0) {
222          lblPreviewReplaceMissingValues.Text += Environment.NewLine + Environment.NewLine + "Please press the button \"Apply Manipulation\" if you wish to perform the replacement.";
223        } else {
224          btnApply.Enabled = false;
225        }
226      }
227    }
228
229    private void validateShuffle() {
230      btnApply.Enabled = true;
231      lblShuffleProperties.Enabled = false;
232      lblShuffleProperties.Visible = false;
233      shuffleSeparatelyCheckbox.Enabled = true;
234      shuffleSeparatelyCheckbox.Visible = true;
235    }
236
237    public new ManipulationContent Content {
238      get { return (ManipulationContent)base.Content; }
239      set { base.Content = value; }
240    }
241
242    private void lstMethods_SelectedIndexChanged(object sender, System.EventArgs e) {
243      int index = lstMethods.SelectedIndex;
244      tabsData.SelectedIndex = index + 1;
245      tabsPreview.SelectedIndex = index + 1;
246      btnApply.Enabled = false;
247
248      //in order that button is enabled if necessary input was already entered
249      if (index >= 0) {
250        validators[index]();
251      }
252    }
253
254    private void btnApply_Click(object sender, System.EventArgs e) {
255      manipulations[lstMethods.SelectedIndex]();
256      switch (lstMethods.SelectedIndex) {
257        case 0:
258          lblPreviewColumnsInfo.Text = "columns successfully deleted.";
259          break;
260        case 1:
261          lblPreviewColumnsVariance.Text = "columns successfully deleted.";
262          break;
263        case 2:
264          lblPreviewRowsInfo.Text = "rows successfully deleted.";
265          break;
266        case 3:
267          lblPreviewReplaceMissingValues.Text = "missing values successfully replaced.";
268          btnApply.Enabled = false;
269          break;
270        case 4:
271          lblPreviewShuffle.Text = "dataset shuffled successfully.";
272          btnApply.Enabled = false;
273          break;
274      }
275    }
276
277    private void validateDoubleTextBox(String text) {
278      btnApply.Enabled = false;
279      if (!string.IsNullOrEmpty(text)) {
280        double percent;
281        if (Double.TryParse(text, out percent)) {
282          btnApply.Enabled = true;
283        }
284      }
285    }
286
287    private void txtDeleteColumnsInfo_TextChanged(object sender, EventArgs e) {
288      validateDeleteColumnsInfo();
289    }
290
291    private void txtDeleteColumnsVariance_TextChanged(object sender, EventArgs e) {
292      validateDeleteColumnsVariance();
293    }
294
295    private void txtDeleteRowsInfo_TextChanged(object sender, EventArgs e) {
296      validateDeleteRowsInfo();
297    }
298
299    private void cmbReplaceWith_SelectedIndexChanged(object sender, EventArgs e) {
300      bool isReplaceWithValueSelected = cmbReplaceWith.SelectedIndex == 0;
301      lblValueColon.Visible = isReplaceWithValueSelected;
302      txtReplaceValue.Visible = isReplaceWithValueSelected;
303      validateReplaceWith();
304    }
305
306    private void txtReplaceValue_TextChanged(object sender, EventArgs e) {
307      validateReplaceWith();
308    }
309  }
310}
Note: See TracBrowser for help on using the repository browser.