Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Problems.Instances.DataAnalysis.Views/3.3/DataAnalysisImportDialog.cs @ 17991

Last change on this file since 17991 was 17181, checked in by swagner, 5 years ago

#2875: Merged r17180 from trunk to stable

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