Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 9458 was 9458, checked in by sforsten, 11 years ago

#2045: removed the option to select spaces as separators in DataAnalysisImportTypeDialog

File size: 7.0 KB
RevLine 
[8598]1#region License Information
2/* HeuristicLab
[9456]3 * Copyright (C) 2002-2013 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[8598]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
[8877]22using System;
23using System.Collections.Generic;
24using System.Globalization;
[9449]25using System.IO;
[8877]26using System.Linq;
[8598]27using System.Windows.Forms;
[8877]28using HeuristicLab.Core.Views;
29using HeuristicLab.Problems.DataAnalysis;
[8598]30
31namespace HeuristicLab.Problems.Instances.DataAnalysis.Views {
32  public partial class DataAnalysisImportTypeDialog : Form {
33
[8885]34    public static readonly List<KeyValuePair<DateTimeFormatInfo, string>> dateTimeFormats = new List<KeyValuePair<DateTimeFormatInfo, string>>{
[8877]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
[9449]41    public static readonly List<KeyValuePair<char, string>> POSSIBLE_SEPARATORS = new List<KeyValuePair<char, string>>{ 
[8877]42      new KeyValuePair<char, string>(';', "; (Semicolon)" ),
[9449]43      new KeyValuePair<char, string>(',', ", (Comma)" ),   
[9458]44      new KeyValuePair<char, string>('\t', "\\t (Tab)")
[8877]45    };
46
[8885]47    public static readonly List<KeyValuePair<NumberFormatInfo, string>> POSSIBLE_DECIMAL_SEPARATORS = new List<KeyValuePair<NumberFormatInfo, string>>{
[9449]48      new KeyValuePair<NumberFormatInfo, string>(NumberFormatInfo.GetInstance(new CultureInfo("de-DE")), ", (Comma)"),
49      new KeyValuePair<NumberFormatInfo, string>(NumberFormatInfo.InvariantInfo, ". (Period)" )   
[8877]50    };
51
[8693]52    public string Path {
53      get { return ProblemTextBox.Text; }
54    }
55
[8599]56    public DataAnalysisImportType ImportType {
57      get {
58        return new DataAnalysisImportType() {
[8693]59          Shuffle = ShuffleDataCheckbox.Checked,
[9021]60          TrainingPercentage = TrainingTestTrackBar.Value
[8599]61        };
62      }
63    }
[8598]64
[8877]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
[8598]75    public DataAnalysisImportTypeDialog() {
76      InitializeComponent();
[8877]77
78      SeparatorComboBox.DataSource = POSSIBLE_SEPARATORS;
79      SeparatorComboBox.ValueMember = "Key";
80      SeparatorComboBox.DisplayMember = "Value";
81      DecimalSeparatorComboBox.DataSource = POSSIBLE_DECIMAL_SEPARATORS;
82      DecimalSeparatorComboBox.ValueMember = "Key";
83      DecimalSeparatorComboBox.DisplayMember = "Value";
84      DateTimeFormatComboBox.DataSource = dateTimeFormats;
85      DateTimeFormatComboBox.ValueMember = "Key";
86      DateTimeFormatComboBox.DisplayMember = "Value";
[8598]87    }
[8599]88
89    private void TrainingTestTrackBar_ValueChanged(object sender, System.EventArgs e) {
90      TrainingLabel.Text = "Training: " + TrainingTestTrackBar.Value + " %";
91      TestLabel.Text = "Test: " + (TrainingTestTrackBar.Maximum - TrainingTestTrackBar.Value) + " %";
92    }
[8693]93
[8877]94    protected virtual void OpenFileButtonClick(object sender, System.EventArgs e) {
95      if (openFileDialog.ShowDialog(this) != DialogResult.OK) return;
96
[9449]97      SeparatorComboBox.Enabled = true;
98      DecimalSeparatorComboBox.Enabled = true;
99      DateTimeFormatComboBox.Enabled = true;
[8877]100      ProblemTextBox.Text = openFileDialog.FileName;
101      ParseCSVFile();
102    }
103
104    protected virtual void CSVFormatComboBoxSelectionChangeCommitted(object sender, EventArgs e) {
105      if (string.IsNullOrEmpty(ProblemTextBox.Text)) return;
106
107      ParseCSVFile();
108    }
109
110    protected void ParseCSVFile() {
111      PreviewDatasetMatrix.Content = null;
112      try {
113        TableFileParser csvParser = new TableFileParser();
114        csvParser.Parse(ProblemTextBox.Text,
115                        (NumberFormatInfo)DecimalSeparatorComboBox.SelectedValue,
116                        (DateTimeFormatInfo)DateTimeFormatComboBox.SelectedValue,
117                        (char)SeparatorComboBox.SelectedValue);
118        IEnumerable<string> variableNamesWithType = GetVariableNamesWithType(csvParser);
119        PreviewDatasetMatrix.Content = new Dataset(variableNamesWithType, csvParser.Values);
120
121        CheckAdditionalConstraints(csvParser);
122
123        ErrorTextBox.Text = String.Empty;
124        ErrorTextBox.Visible = false;
[8693]125        OkButton.Enabled = true;
126      }
[8877]127      catch (Exception ex) {
[9449]128        if (ex is IOException || ex is InvalidOperationException || ex is ArgumentException || ex is TableFileParser.DataFormatException) {
129          OkButton.Enabled = false;
130          ErrorTextBox.Text = ex.Message;
131          ErrorTextBox.Visible = true;
132        } else {
133          throw;
134        }
[8877]135      }
[8693]136    }
[8877]137
138    protected virtual void CheckAdditionalConstraints(TableFileParser csvParser) {
139      if (!csvParser.Values.Any(x => x is List<double>)) {
140        throw new ArgumentException("No double column could be found!");
141      }
142    }
143
144    private IEnumerable<string> GetVariableNamesWithType(TableFileParser csvParser) {
145      IList<string> variableNamesWithType = csvParser.VariableNames.ToList();
146      for (int i = 0; i < csvParser.Values.Count; i++) {
147        if (csvParser.Values[i] is List<double>) {
148          variableNamesWithType[i] += " (Double)";
149        } else if (csvParser.Values[i] is List<string>) {
150          variableNamesWithType[i] += " (String)";
151        } else if (csvParser.Values[i] is List<DateTime>) {
152          variableNamesWithType[i] += " (DateTime)";
153        } else {
154          throw new ArgumentException("The variable values must be of type List<double>, List<string> or List<DateTime>");
155        }
156      }
157      return variableNamesWithType;
158    }
159
160    protected void ControlToolTip_DoubleClick(object sender, EventArgs e) {
161      Control control = sender as Control;
162      if (control != null) {
163        using (TextDialog dialog = new TextDialog(control.Name, (string)control.Tag, true)) {
164          dialog.ShowDialog(this);
165        }
166      }
167    }
[8598]168  }
169}
Note: See TracBrowser for help on using the repository browser.