Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Problems.Orienteering/HeuristicLab.DataPreprocessing.Views/3.4/ManipulationView.cs @ 11185

Last change on this file since 11185 was 11185, checked in by pfleck, 10 years ago

#2208 merged trunk and updated version info

File size: 12.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2014 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.FilterLogic.FilterChanged += FilterLogic_FilterChanged;
81    }
82
83    protected override void DeregisterContentEvents() {
84      Content.FilterLogic.FilterChanged -= FilterLogic_FilterChanged;
85      base.DeregisterContentEvents();
86    }
87
88    private void FilterLogic_FilterChanged(object sender, EventArgs e) {
89      if (Content != null) {
90        CheckFilters();
91      }
92    }
93
94    private void CheckFilters() {
95      if (Content.FilterLogic.IsFiltered) {
96        tabsPreview.SelectedIndex = 0;
97        lstMethods.Enabled = false;
98        tabsData.Enabled = false;
99        tabsPreview.Enabled = false;
100        lblPreviewInActive.Visible = true;
101        btnApply.Enabled = false;
102      } else {
103        lblPreviewInActive.Visible = false;
104        tabsData.Enabled = true;
105        tabsPreview.Enabled = true;
106        lstMethods.Enabled = true;
107        lstMethods_SelectedIndexChanged(null, null);
108      }
109    }
110
111    private double getDeleteColumnsInfo() {
112      return double.Parse(txtDeleteColumnsInfo.Text);
113    }
114
115    private double getDeleteColumnsVariance() {
116      return double.Parse(txtDeleteColumnsVariance.Text);
117    }
118
119    private double getRowsColumnsInfo() {
120      return double.Parse(txtDeleteRowsInfo.Text);
121    }
122
123    private void replaceMissingValues() {
124      var allIndices = Content.SearchLogic.GetMissingValueIndices();
125      var columnIndex = cmbVariableNames.SelectedIndex;
126      var columnIndices = new Dictionary<int, IList<int>>{
127          {columnIndex,   allIndices[columnIndex]}
128      };
129
130      switch (cmbReplaceWith.SelectedIndex) {
131        case 0: //Value
132          Content.ManipulationLogic.ReplaceIndicesByValue(columnIndices, txtReplaceValue.Text);
133          break;
134        case 1: //Average
135          Content.ManipulationLogic.ReplaceIndicesByAverageValue(columnIndices);
136          break;
137        case 2: //Median
138          Content.ManipulationLogic.ReplaceIndicesByMedianValue(columnIndices);
139          break;
140        case 3: //Most Common
141          Content.ManipulationLogic.ReplaceIndicesByMostCommonValue(columnIndices);
142          break;
143        case 4: //Random
144          Content.ManipulationLogic.ReplaceIndicesByRandomValue(columnIndices);
145          break;
146      }
147    }
148
149    private void validateDeleteColumnsInfo() {
150      validateDoubleTextBox(txtDeleteColumnsInfo.Text);
151      if (btnApply.Enabled) {
152        int count = Content.ManipulationLogic.ColumnsWithMissingValuesGreater(getDeleteColumnsInfo()).Count;
153        int rowCount = Content.FilterLogic.PreprocessingData.Rows;
154        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.";
155        if (count > 0) {
156          lblPreviewColumnsInfo.Text += Environment.NewLine + Environment.NewLine + "Please press the button \"Apply Manipulation\" if you wish to delete those columns.";
157        } else {
158          btnApply.Enabled = false;
159        }
160      } else {
161        lblPreviewColumnsInfo.Text = "Preview not possible yet - please input the limit above.";
162      }
163    }
164
165    private void validateDeleteColumnsVariance() {
166      validateDoubleTextBox(txtDeleteColumnsVariance.Text);
167      if (btnApply.Enabled) {
168        int count = Content.ManipulationLogic.ColumnsWithVarianceSmaller(getDeleteColumnsVariance()).Count;
169        int rowCount = Content.FilterLogic.PreprocessingData.Rows;
170        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 + ".";
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        int rowCount = Content.FilterLogic.PreprocessingData.Rows;
186        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.";
187        if (count > 0) {
188          lblPreviewRowsInfo.Text += Environment.NewLine + Environment.NewLine + "Please press the button \"Apply Manipulation\" if you wish to delete those rows.";
189        } else {
190          btnApply.Enabled = false;
191        }
192      } else {
193        lblPreviewRowsInfo.Text = "Preview not possible yet - please input the limit above.";
194      }
195    }
196
197    private void validateReplaceWith() {
198      btnApply.Enabled = false;
199      string replaceWith = (string)cmbReplaceWith.SelectedItem;
200      int columnIndex = cmbVariableNames.SelectedIndex;
201
202      if (cmbReplaceWith.SelectedIndex == 0) {
203        string errorMessage;
204        string replaceValue = txtReplaceValue.Text;
205        if (string.IsNullOrEmpty(replaceValue)) {
206          lblPreviewReplaceMissingValues.Text = "Preview not possible yet - please input the text which will be used as replacement.";
207        } else if (!Content.ManipulationLogic.PreProcessingData.Validate(txtReplaceValue.Text, out errorMessage, columnIndex)) {
208          lblPreviewReplaceMissingValues.Text = "Preview not possible yet - " + errorMessage;
209        } else {
210          btnApply.Enabled = true;
211        }
212        replaceWith = "\"" + replaceValue + "\"";
213      } else {
214        btnApply.Enabled = true;
215      }
216      if (btnApply.Enabled) {
217        var allIndices = Content.SearchLogic.GetMissingValueIndices();
218        int count = allIndices[columnIndex].Count;
219        int cellCount = Content.FilterLogic.PreprocessingData.Rows * Content.FilterLogic.PreprocessingData.Columns;
220        lblPreviewReplaceMissingValues.Text = count + " cell" + (count > 1 || count == 0 ? "s" : "")
221          + " of " + cellCount + " (" + string.Format("{0:F2}%", 100d / cellCount * count) + ") were detected with missing values which would be replaced with " + replaceWith;
222        if (count > 0) {
223          lblPreviewReplaceMissingValues.Text += Environment.NewLine + Environment.NewLine + "Please press the button \"Apply Manipulation\" if you wish to perform the replacement.";
224        } else {
225          btnApply.Enabled = false;
226        }
227      }
228    }
229
230    public new ManipulationContent Content {
231      get { return (ManipulationContent)base.Content; }
232      set { base.Content = value; }
233    }
234
235    private void lstMethods_SelectedIndexChanged(object sender, System.EventArgs e) {
236      int index = lstMethods.SelectedIndex;
237      tabsData.SelectedIndex = index + 1;
238      tabsPreview.SelectedIndex = index + 1;
239      btnApply.Enabled = false;
240
241      //in order that button is enabled if necessary input was already entered
242      if (index >= 0) {
243        validators[index]();
244      }
245    }
246
247    private void btnApply_Click(object sender, System.EventArgs e) {
248      manipulations[lstMethods.SelectedIndex]();
249      switch (lstMethods.SelectedIndex) {
250        case 0:
251          lblPreviewColumnsInfo.Text = "columns successfully deleted.";
252          break;
253        case 1:
254          lblPreviewColumnsVariance.Text = "columns successfully deleted.";
255          break;
256        case 2:
257          lblPreviewRowsInfo.Text = "rows successfully deleted.";
258          break;
259        case 3:
260          lblPreviewReplaceMissingValues.Text = "missing values successfully replaced.";
261          btnApply.Enabled = false;
262          break;
263        case 4:
264          lblPreviewShuffle.Text = "dataset shuffled successfully.";
265          btnApply.Enabled = false;
266          break;
267      }
268    }
269
270    private void validateDoubleTextBox(String text) {
271      btnApply.Enabled = false;
272      if (!string.IsNullOrEmpty(text)) {
273        double percent;
274        if (Double.TryParse(text, out percent)) {
275          btnApply.Enabled = true;
276        }
277      }
278    }
279
280    private void txtDeleteColumnsInfo_TextChanged(object sender, EventArgs e) {
281      validateDeleteColumnsInfo();
282    }
283
284    private void txtDeleteColumnsVariance_TextChanged(object sender, EventArgs e) {
285      validateDeleteColumnsVariance();
286    }
287
288    private void txtDeleteRowsInfo_TextChanged(object sender, EventArgs e) {
289      validateDeleteRowsInfo();
290    }
291
292    private void cmbReplaceWith_SelectedIndexChanged(object sender, EventArgs e) {
293      bool isReplaceWithValueSelected = cmbReplaceWith.SelectedIndex == 0;
294      lblValueColon.Visible = isReplaceWithValueSelected;
295      txtReplaceValue.Visible = isReplaceWithValueSelected;
296      validateReplaceWith();
297    }
298
299    private void txtReplaceValue_TextChanged(object sender, EventArgs e) {
300      validateReplaceWith();
301    }
302  }
303}
Note: See TracBrowser for help on using the repository browser.