source: trunk/sources/HeuristicLab.Problems.Instances.DataAnalysis.Views/3.3/DataAnalysisImportTypeDialog.cs @ 9456

Last change on this file since 9456 was 9456, checked in by swagner, 8 years ago

Updated copyright year and added some missing license headers (#1889)

File size: 7.0 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>(' ', "' ' (Space)" )
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        };
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      ParseCSVFile();
103    }
104
105    protected virtual void CSVFormatComboBoxSelectionChangeCommitted(object sender, EventArgs e) {
106      if (string.IsNullOrEmpty(ProblemTextBox.Text)) return;
107
108      ParseCSVFile();
109    }
110
111    protected void ParseCSVFile() {
112      PreviewDatasetMatrix.Content = null;
113      try {
114        TableFileParser csvParser = new TableFileParser();
115        csvParser.Parse(ProblemTextBox.Text,
116                        (NumberFormatInfo)DecimalSeparatorComboBox.SelectedValue,
117                        (DateTimeFormatInfo)DateTimeFormatComboBox.SelectedValue,
118                        (char)SeparatorComboBox.SelectedValue);
119        IEnumerable<string> variableNamesWithType = GetVariableNamesWithType(csvParser);
120        PreviewDatasetMatrix.Content = new Dataset(variableNamesWithType, csvParser.Values);
121
122        CheckAdditionalConstraints(csvParser);
123
124        ErrorTextBox.Text = String.Empty;
125        ErrorTextBox.Visible = false;
126        OkButton.Enabled = true;
127      }
128      catch (Exception ex) {
129        if (ex is IOException || ex is InvalidOperationException || ex is ArgumentException || ex is TableFileParser.DataFormatException) {
130          OkButton.Enabled = false;
131          ErrorTextBox.Text = ex.Message;
132          ErrorTextBox.Visible = true;
133        } else {
134          throw;
135        }
136      }
137    }
138
139    protected virtual void CheckAdditionalConstraints(TableFileParser csvParser) {
140      if (!csvParser.Values.Any(x => x is List<double>)) {
141        throw new ArgumentException("No double column could be found!");
142      }
143    }
144
145    private IEnumerable<string> GetVariableNamesWithType(TableFileParser csvParser) {
146      IList<string> variableNamesWithType = csvParser.VariableNames.ToList();
147      for (int i = 0; i < csvParser.Values.Count; i++) {
148        if (csvParser.Values[i] is List<double>) {
149          variableNamesWithType[i] += " (Double)";
150        } else if (csvParser.Values[i] is List<string>) {
151          variableNamesWithType[i] += " (String)";
152        } else if (csvParser.Values[i] is List<DateTime>) {
153          variableNamesWithType[i] += " (DateTime)";
154        } else {
155          throw new ArgumentException("The variable values must be of type List<double>, List<string> or List<DateTime>");
156        }
157      }
158      return variableNamesWithType;
159    }
160
161    protected void ControlToolTip_DoubleClick(object sender, EventArgs e) {
162      Control control = sender as Control;
163      if (control != null) {
164        using (TextDialog dialog = new TextDialog(control.Name, (string)control.Tag, true)) {
165          dialog.ShowDialog(this);
166        }
167      }
168    }
169  }
170}
Note: See TracBrowser for help on using the repository browser.