Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Problems.Instances.DataAnalysis.Views/3.3/DataAnalysisImportTypeDialog.cs @ 9753

Last change on this file since 9753 was 9753, checked in by mkommend, 11 years ago

#2047: Integrated r9652 in the stable branch.

File size: 7.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.Globalization;
25using System.IO;
26using System.Linq;
27using System.Windows.Forms;
28using HeuristicLab.Core.Views;
29using HeuristicLab.Problems.DataAnalysis;
30
31namespace HeuristicLab.Problems.Instances.DataAnalysis.Views {
32  public partial class DataAnalysisImportTypeDialog : Form {
33
34    public static readonly List<KeyValuePair<DateTimeFormatInfo, string>> dateTimeFormats = new List<KeyValuePair<DateTimeFormatInfo, string>>{
35      new KeyValuePair<DateTimeFormatInfo, string>(DateTimeFormatInfo.GetInstance(new CultureInfo("de-DE")), "dd/mm/yyyy hh:MM:ss" ),
36      new KeyValuePair<DateTimeFormatInfo, string>(DateTimeFormatInfo.InvariantInfo, "mm/dd/yyyy hh:MM:ss" ),
37      new KeyValuePair<DateTimeFormatInfo, string>(DateTimeFormatInfo.InvariantInfo, "yyyy/mm/dd hh:MM:ss" ),
38      new KeyValuePair<DateTimeFormatInfo, string>(DateTimeFormatInfo.InvariantInfo, "mm/yyyy/dd hh:MM:ss" )
39    };
40
41    public static readonly List<KeyValuePair<char, string>> POSSIBLE_SEPARATORS = new List<KeyValuePair<char, string>>{ 
42      new KeyValuePair<char, string>(';', "; (Semicolon)" ),
43      new KeyValuePair<char, string>(',', ", (Comma)" ),   
44      new KeyValuePair<char, string>('\t', "\\t (Tab)"),
45      new KeyValuePair<char, string>((char)0, "all whitespaces (including tabs and spaces)")
46    };
47
48    public static readonly List<KeyValuePair<NumberFormatInfo, string>> POSSIBLE_DECIMAL_SEPARATORS = new List<KeyValuePair<NumberFormatInfo, string>>{
49      new KeyValuePair<NumberFormatInfo, string>(NumberFormatInfo.GetInstance(new CultureInfo("de-DE")), ", (Comma)"),
50      new KeyValuePair<NumberFormatInfo, string>(NumberFormatInfo.InvariantInfo, ". (Period)" )   
51    };
52
53    public string Path {
54      get { return ProblemTextBox.Text; }
55    }
56
57    public DataAnalysisImportType ImportType {
58      get {
59        return new DataAnalysisImportType() {
60          Shuffle = ShuffleDataCheckbox.Checked,
61          TrainingPercentage = TrainingTestTrackBar.Value
62        };
63      }
64    }
65
66    public DataAnalysisCSVFormat CSVFormat {
67      get {
68        return new DataAnalysisCSVFormat() {
69          Separator = (char)SeparatorComboBox.SelectedValue,
70          NumberFormatInfo = (NumberFormatInfo)DecimalSeparatorComboBox.SelectedValue,
71          DateTimeFormatInfo = (DateTimeFormatInfo)DateTimeFormatComboBox.SelectedValue,
72          VariableNamesAvailable = CheckboxColumnNames.Checked
73        };
74      }
75    }
76
77    public DataAnalysisImportTypeDialog() {
78      InitializeComponent();
79
80      SeparatorComboBox.DataSource = POSSIBLE_SEPARATORS;
81      SeparatorComboBox.ValueMember = "Key";
82      SeparatorComboBox.DisplayMember = "Value";
83      DecimalSeparatorComboBox.DataSource = POSSIBLE_DECIMAL_SEPARATORS;
84      DecimalSeparatorComboBox.ValueMember = "Key";
85      DecimalSeparatorComboBox.DisplayMember = "Value";
86      DateTimeFormatComboBox.DataSource = dateTimeFormats;
87      DateTimeFormatComboBox.ValueMember = "Key";
88      DateTimeFormatComboBox.DisplayMember = "Value";
89    }
90
91    private void TrainingTestTrackBar_ValueChanged(object sender, System.EventArgs e) {
92      TrainingLabel.Text = "Training: " + TrainingTestTrackBar.Value + " %";
93      TestLabel.Text = "Test: " + (TrainingTestTrackBar.Maximum - TrainingTestTrackBar.Value) + " %";
94    }
95
96    protected virtual void OpenFileButtonClick(object sender, System.EventArgs e) {
97      if (openFileDialog.ShowDialog(this) != DialogResult.OK) return;
98
99      SeparatorComboBox.Enabled = true;
100      DecimalSeparatorComboBox.Enabled = true;
101      DateTimeFormatComboBox.Enabled = true;
102      ProblemTextBox.Text = openFileDialog.FileName;
103      TableFileParser csvParser = new TableFileParser();
104      CheckboxColumnNames.Checked = csvParser.AreColumnNamesInFirstLine(ProblemTextBox.Text,
105                                                                      (NumberFormatInfo)DecimalSeparatorComboBox.SelectedValue,
106                                                                      (DateTimeFormatInfo)DateTimeFormatComboBox.SelectedValue,
107                                                                      (char)SeparatorComboBox.SelectedValue);
108      ParseCSVFile();
109    }
110
111    protected virtual void CSVFormatComboBoxSelectionChangeCommitted(object sender, EventArgs e) {
112      if (string.IsNullOrEmpty(ProblemTextBox.Text)) return;
113
114      ParseCSVFile();
115    }
116
117    protected virtual void CheckboxColumnNames_CheckedChanged(object sender, EventArgs e) {
118      if (string.IsNullOrEmpty(ProblemTextBox.Text)) return;
119
120      ParseCSVFile();
121    }
122
123    protected void ParseCSVFile() {
124      PreviewDatasetMatrix.Content = null;
125      try {
126        TableFileParser csvParser = new TableFileParser();
127        csvParser.Parse(ProblemTextBox.Text,
128                        (NumberFormatInfo)DecimalSeparatorComboBox.SelectedValue,
129                        (DateTimeFormatInfo)DateTimeFormatComboBox.SelectedValue,
130                        (char)SeparatorComboBox.SelectedValue,
131                        CheckboxColumnNames.Checked);
132        IEnumerable<string> variableNamesWithType = GetVariableNamesWithType(csvParser);
133        PreviewDatasetMatrix.Content = new Dataset(variableNamesWithType, csvParser.Values);
134
135        CheckAdditionalConstraints(csvParser);
136
137        ErrorTextBox.Text = String.Empty;
138        ErrorTextBox.Visible = false;
139        OkButton.Enabled = true;
140      }
141      catch (Exception ex) {
142        if (ex is IOException || ex is InvalidOperationException || ex is ArgumentException || ex is TableFileParser.DataFormatException) {
143          OkButton.Enabled = false;
144          ErrorTextBox.Text = ex.Message;
145          ErrorTextBox.Visible = true;
146        } else {
147          throw;
148        }
149      }
150    }
151
152    protected virtual void CheckAdditionalConstraints(TableFileParser csvParser) {
153      if (!csvParser.Values.Any(x => x is List<double>)) {
154        throw new ArgumentException("No double column could be found!");
155      }
156    }
157
158    private IEnumerable<string> GetVariableNamesWithType(TableFileParser csvParser) {
159      IList<string> variableNamesWithType = csvParser.VariableNames.ToList();
160      for (int i = 0; i < csvParser.Values.Count; i++) {
161        if (csvParser.Values[i] is List<double>) {
162          variableNamesWithType[i] += " (Double)";
163        } else if (csvParser.Values[i] is List<string>) {
164          variableNamesWithType[i] += " (String)";
165        } else if (csvParser.Values[i] is List<DateTime>) {
166          variableNamesWithType[i] += " (DateTime)";
167        } else {
168          throw new ArgumentException("The variable values must be of type List<double>, List<string> or List<DateTime>");
169        }
170      }
171      return variableNamesWithType;
172    }
173
174    protected void ControlToolTip_DoubleClick(object sender, EventArgs e) {
175      Control control = sender as Control;
176      if (control != null) {
177        using (TextDialog dialog = new TextDialog(control.Name, (string)control.Tag, true)) {
178          dialog.ShowDialog(this);
179        }
180      }
181    }
182  }
183}
Note: See TracBrowser for help on using the repository browser.