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

Last change on this file since 8885 was 8885, checked in by sforsten, 10 years ago

#1942:

  • implemented changes suggested by mkommend in comment:15:ticket:1942 except the first remark
  • TimeSeriesPrognosisInstanceProvider has been adapted to work similar to other DataAnalysisInstanceProvider, also views have been created for it
File size: 6.6 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.Globalization;
25using System.Linq;
26using System.Windows.Forms;
27using HeuristicLab.Core.Views;
28using HeuristicLab.Problems.DataAnalysis;
29
30namespace HeuristicLab.Problems.Instances.DataAnalysis.Views {
31  public partial class DataAnalysisImportTypeDialog : Form {
32
33    public static readonly List<KeyValuePair<DateTimeFormatInfo, string>> dateTimeFormats = new List<KeyValuePair<DateTimeFormatInfo, string>>{
34      new KeyValuePair<DateTimeFormatInfo, string>(DateTimeFormatInfo.GetInstance(new CultureInfo("de-DE")), "dd/mm/yyyy hh:MM:ss" ),
35      new KeyValuePair<DateTimeFormatInfo, string>(DateTimeFormatInfo.InvariantInfo, "mm/dd/yyyy hh:MM:ss" ),
36      new KeyValuePair<DateTimeFormatInfo, string>(DateTimeFormatInfo.InvariantInfo, "yyyy/mm/dd hh:MM:ss" ),
37      new KeyValuePair<DateTimeFormatInfo, string>(DateTimeFormatInfo.InvariantInfo, "mm/yyyy/dd hh:MM:ss" )
38    };
39
40    public static readonly List<KeyValuePair<char, string>> POSSIBLE_SEPARATORS = new List<KeyValuePair<char, string>>{
41      new KeyValuePair<char, string>(',', ", (Comma)" ),
42      new KeyValuePair<char, string>(';', "; (Semicolon)" ),
43      new KeyValuePair<char, string>('\t', "\\t (Tab)")
44    };
45
46    public static readonly List<KeyValuePair<NumberFormatInfo, string>> POSSIBLE_DECIMAL_SEPARATORS = new List<KeyValuePair<NumberFormatInfo, string>>{
47      new KeyValuePair<NumberFormatInfo, string>(NumberFormatInfo.InvariantInfo, ". (Period)" ),
48      new KeyValuePair<NumberFormatInfo, string>(NumberFormatInfo.GetInstance(new CultureInfo("de-DE")), ", (Comma)" )     
49    };
50
51    public string Path {
52      get { return ProblemTextBox.Text; }
53    }
54
55    public DataAnalysisImportType ImportType {
56      get {
57        return new DataAnalysisImportType() {
58          Shuffle = ShuffleDataCheckbox.Checked,
59          Training = TrainingTestTrackBar.Value
60        };
61      }
62    }
63
64    public DataAnalysisCSVFormat CSVFormat {
65      get {
66        return new DataAnalysisCSVFormat() {
67          Separator = (char)SeparatorComboBox.SelectedValue,
68          NumberFormatInfo = (NumberFormatInfo)DecimalSeparatorComboBox.SelectedValue,
69          DateTimeFormatInfo = (DateTimeFormatInfo)DateTimeFormatComboBox.SelectedValue
70        };
71      }
72    }
73
74    public DataAnalysisImportTypeDialog() {
75      InitializeComponent();
76
77      SeparatorComboBox.DataSource = POSSIBLE_SEPARATORS;
78      SeparatorComboBox.ValueMember = "Key";
79      SeparatorComboBox.DisplayMember = "Value";
80      DecimalSeparatorComboBox.DataSource = POSSIBLE_DECIMAL_SEPARATORS;
81      DecimalSeparatorComboBox.ValueMember = "Key";
82      DecimalSeparatorComboBox.DisplayMember = "Value";
83      DateTimeFormatComboBox.DataSource = dateTimeFormats;
84      DateTimeFormatComboBox.ValueMember = "Key";
85      DateTimeFormatComboBox.DisplayMember = "Value";
86    }
87
88    private void TrainingTestTrackBar_ValueChanged(object sender, System.EventArgs e) {
89      TrainingLabel.Text = "Training: " + TrainingTestTrackBar.Value + " %";
90      TestLabel.Text = "Test: " + (TrainingTestTrackBar.Maximum - TrainingTestTrackBar.Value) + " %";
91    }
92
93    protected virtual void OpenFileButtonClick(object sender, System.EventArgs e) {
94      if (openFileDialog.ShowDialog(this) != DialogResult.OK) return;
95
96      ProblemTextBox.Text = openFileDialog.FileName;
97      ParseCSVFile();
98    }
99
100    protected virtual void CSVFormatComboBoxSelectionChangeCommitted(object sender, EventArgs e) {
101      if (string.IsNullOrEmpty(ProblemTextBox.Text)) return;
102
103      ParseCSVFile();
104    }
105
106    protected void ParseCSVFile() {
107      PreviewDatasetMatrix.Content = null;
108      try {
109        TableFileParser csvParser = new TableFileParser();
110        csvParser.Parse(ProblemTextBox.Text,
111                        (NumberFormatInfo)DecimalSeparatorComboBox.SelectedValue,
112                        (DateTimeFormatInfo)DateTimeFormatComboBox.SelectedValue,
113                        (char)SeparatorComboBox.SelectedValue);
114        IEnumerable<string> variableNamesWithType = GetVariableNamesWithType(csvParser);
115        PreviewDatasetMatrix.Content = new Dataset(variableNamesWithType, csvParser.Values);
116
117        CheckAdditionalConstraints(csvParser);
118
119        ErrorTextBox.Text = String.Empty;
120        ErrorTextBox.Visible = false;
121        OkButton.Enabled = true;
122      }
123      catch (Exception ex) {
124        OkButton.Enabled = false;
125        ErrorTextBox.Text = ex.Message;
126        ErrorTextBox.Visible = true;
127      }
128    }
129
130    protected virtual void CheckAdditionalConstraints(TableFileParser csvParser) {
131      if (!csvParser.Values.Any(x => x is List<double>)) {
132        throw new ArgumentException("No double column could be found!");
133      }
134    }
135
136    private IEnumerable<string> GetVariableNamesWithType(TableFileParser csvParser) {
137      IList<string> variableNamesWithType = csvParser.VariableNames.ToList();
138      for (int i = 0; i < csvParser.Values.Count; i++) {
139        if (csvParser.Values[i] is List<double>) {
140          variableNamesWithType[i] += " (Double)";
141        } else if (csvParser.Values[i] is List<string>) {
142          variableNamesWithType[i] += " (String)";
143        } else if (csvParser.Values[i] is List<DateTime>) {
144          variableNamesWithType[i] += " (DateTime)";
145        } else {
146          throw new ArgumentException("The variable values must be of type List<double>, List<string> or List<DateTime>");
147        }
148      }
149      return variableNamesWithType;
150    }
151
152    protected void ControlToolTip_DoubleClick(object sender, EventArgs e) {
153      Control control = sender as Control;
154      if (control != null) {
155        using (TextDialog dialog = new TextDialog(control.Name, (string)control.Tag, true)) {
156          dialog.ShowDialog(this);
157        }
158      }
159    }
160  }
161}
Note: See TracBrowser for help on using the repository browser.