Free cookie consent management tool by TermsFeed Policy Generator

source: branches/symbreg-factors-2650/HeuristicLab.Problems.Instances.DataAnalysis.Views/3.3/DataAnalysisImportTypeDialog.cs @ 14330

Last change on this file since 14330 was 14330, checked in by gkronber, 7 years ago

#2650 Merged r14282:14322 from trunk to branch (fixing conflicts)

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