Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1942: Reintegrated branch for CSV import.

File size: 7.0 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 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.ComponentModel;
25using System.Globalization;
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 BindingList<KeyValuePair<DateTimeFormatInfo, string>> dateTimeFormats = new BindingList<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 BindingList<KeyValuePair<char, string>> POSSIBLE_SEPARATORS = new BindingList<KeyValuePair<char, string>>{
42      new KeyValuePair<char, string>(',', ", (Comma)" ),
43      new KeyValuePair<char, string>(';', "; (Semicolon)" ),
44      new KeyValuePair<char, string>('\t', "\\t (Tab)")
45    };
46
47    public static readonly BindingList<KeyValuePair<NumberFormatInfo, string>> POSSIBLE_DECIMAL_SEPARATORS = new BindingList<KeyValuePair<NumberFormatInfo, string>>{
48      new KeyValuePair<NumberFormatInfo, string>(NumberFormatInfo.InvariantInfo, ". (Period)" ),
49      new KeyValuePair<NumberFormatInfo, string>(NumberFormatInfo.GetInstance(new CultureInfo("de-DE")), ", (Comma)" )     
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          Training = 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        };
72      }
73    }
74
75    public DataAnalysisImportTypeDialog() {
76      InitializeComponent();
77      ToolTip.SetToolTip(SeparatorInfoLabel, (string)SeparatorInfoLabel.Tag);
78      ToolTip.SetToolTip(DecimalSeparatorInfoLabel, (string)DecimalSeparatorInfoLabel.Tag);
79      ToolTip.SetToolTip(DateTimeFormatInfoLabel, (string)DateTimeFormatInfoLabel.Tag);
80      ToolTip.SetToolTip(ShuffelInfoLabel, (string)ShuffelInfoLabel.Tag);
81
82      SeparatorComboBox.DataSource = POSSIBLE_SEPARATORS;
83      SeparatorComboBox.ValueMember = "Key";
84      SeparatorComboBox.DisplayMember = "Value";
85      DecimalSeparatorComboBox.DataSource = POSSIBLE_DECIMAL_SEPARATORS;
86      DecimalSeparatorComboBox.ValueMember = "Key";
87      DecimalSeparatorComboBox.DisplayMember = "Value";
88      DateTimeFormatComboBox.DataSource = dateTimeFormats;
89      DateTimeFormatComboBox.ValueMember = "Key";
90      DateTimeFormatComboBox.DisplayMember = "Value";
91    }
92
93    private void TrainingTestTrackBar_ValueChanged(object sender, System.EventArgs e) {
94      TrainingLabel.Text = "Training: " + TrainingTestTrackBar.Value + " %";
95      TestLabel.Text = "Test: " + (TrainingTestTrackBar.Maximum - TrainingTestTrackBar.Value) + " %";
96    }
97
98    protected virtual void OpenFileButtonClick(object sender, System.EventArgs e) {
99      if (openFileDialog.ShowDialog(this) != DialogResult.OK) return;
100
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        OkButton.Enabled = false;
130        ErrorTextBox.Text = ex.Message;
131        ErrorTextBox.Visible = true;
132      }
133    }
134
135    protected virtual void CheckAdditionalConstraints(TableFileParser csvParser) {
136      if (!csvParser.Values.Any(x => x is List<double>)) {
137        throw new ArgumentException("No double column could be found!");
138      }
139    }
140
141    private IEnumerable<string> GetVariableNamesWithType(TableFileParser csvParser) {
142      IList<string> variableNamesWithType = csvParser.VariableNames.ToList();
143      for (int i = 0; i < csvParser.Values.Count; i++) {
144        if (csvParser.Values[i] is List<double>) {
145          variableNamesWithType[i] += " (Double)";
146        } else if (csvParser.Values[i] is List<string>) {
147          variableNamesWithType[i] += " (String)";
148        } else if (csvParser.Values[i] is List<DateTime>) {
149          variableNamesWithType[i] += " (DateTime)";
150        } else {
151          throw new ArgumentException("The variable values must be of type List<double>, List<string> or List<DateTime>");
152        }
153      }
154      return variableNamesWithType;
155    }
156
157    protected void ControlToolTip_DoubleClick(object sender, EventArgs e) {
158      Control control = sender as Control;
159      if (control != null) {
160        using (TextDialog dialog = new TextDialog(control.Name, (string)control.Tag, true)) {
161          dialog.ShowDialog(this);
162        }
163      }
164    }
165  }
166}
Note: See TracBrowser for help on using the repository browser.