Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2070: Merged r9608, r9610, r9611, r9623, and r9624 in the stable branch.

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