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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.ComponentModel;
|
---|
25 | using System.Globalization;
|
---|
26 | using System.Linq;
|
---|
27 | using System.Windows.Forms;
|
---|
28 | using HeuristicLab.Common.Resources;
|
---|
29 | using HeuristicLab.Problems.DataAnalysis;
|
---|
30 |
|
---|
31 | namespace 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 | OpenFileButton.Text = String.Empty;
|
---|
78 | OpenFileButton.Image = VSImageLibrary.Open;
|
---|
79 | SeparatorInfoButton.Text = String.Empty;
|
---|
80 | SeparatorInfoButton.Image = VSImageLibrary.Information;
|
---|
81 | DecimalSeparatorInfoButton.Text = String.Empty;
|
---|
82 | DecimalSeparatorInfoButton.Image = VSImageLibrary.Information;
|
---|
83 | DateTimeFormatInfoButton.Text = String.Empty;
|
---|
84 | DateTimeFormatInfoButton.Image = VSImageLibrary.Information;
|
---|
85 | ShuffleDataInfoButton.Text = String.Empty;
|
---|
86 | ShuffleDataInfoButton.Image = VSImageLibrary.Information;
|
---|
87 |
|
---|
88 | SeparatorComboBox.DataSource = POSSIBLE_SEPARATORS;
|
---|
89 | SeparatorComboBox.ValueMember = "Key";
|
---|
90 | SeparatorComboBox.DisplayMember = "Value";
|
---|
91 | DecimalSeparatorComboBox.DataSource = POSSIBLE_DECIMAL_SEPARATORS;
|
---|
92 | DecimalSeparatorComboBox.ValueMember = "Key";
|
---|
93 | DecimalSeparatorComboBox.DisplayMember = "Value";
|
---|
94 | DateTimeFormatComboBox.DataSource = dateTimeFormats;
|
---|
95 | DateTimeFormatComboBox.ValueMember = "Key";
|
---|
96 | DateTimeFormatComboBox.DisplayMember = "Value";
|
---|
97 | }
|
---|
98 |
|
---|
99 | private void TrainingTestTrackBar_ValueChanged(object sender, System.EventArgs e) {
|
---|
100 | TrainingLabel.Text = "Training: " + TrainingTestTrackBar.Value + " %";
|
---|
101 | TestLabel.Text = "Test: " + (TrainingTestTrackBar.Maximum - TrainingTestTrackBar.Value) + " %";
|
---|
102 | }
|
---|
103 |
|
---|
104 | private void openFileButton_Click(object sender, System.EventArgs e) {
|
---|
105 | if (openFileDialog.ShowDialog(this) == DialogResult.OK) {
|
---|
106 | ProblemTextBox.Text = openFileDialog.FileName;
|
---|
107 | ParseCSVFile();
|
---|
108 | }
|
---|
109 | }
|
---|
110 |
|
---|
111 | protected void CSVFormatComboBoxSelectionChangeCommitted(object sender, EventArgs e) {
|
---|
112 | if (!ProblemTextBox.Text.Equals(String.Empty)) {
|
---|
113 | ParseCSVFile();
|
---|
114 | }
|
---|
115 | }
|
---|
116 |
|
---|
117 | protected void ParseCSVFile() {
|
---|
118 | PreviewDatasetMatrix.Content = null;
|
---|
119 | try {
|
---|
120 | TableFileParser csvParser = new TableFileParser();
|
---|
121 | csvParser.Parse(ProblemTextBox.Text,
|
---|
122 | (NumberFormatInfo)DecimalSeparatorComboBox.SelectedValue,
|
---|
123 | (DateTimeFormatInfo)DateTimeFormatComboBox.SelectedValue,
|
---|
124 | (char)SeparatorComboBox.SelectedValue);
|
---|
125 | OkButton.Enabled = true;
|
---|
126 | IEnumerable<string> variableNamesWithType = GetVariableNamesWithType(csvParser);
|
---|
127 | PreviewDatasetMatrix.Content = new Dataset(variableNamesWithType, csvParser.Values);
|
---|
128 | var bla = new Dataset(csvParser.VariableNames, csvParser.Values);
|
---|
129 |
|
---|
130 | if (!csvParser.Values.Any(x => x is List<double>)) {
|
---|
131 | throw new ArgumentException("No double column could be found!");
|
---|
132 | }
|
---|
133 | ErrorTextBox.Text = String.Empty;
|
---|
134 | ErrorTextBox.Visible = false;
|
---|
135 | }
|
---|
136 | catch (Exception ex) {
|
---|
137 | OkButton.Enabled = false;
|
---|
138 | ErrorTextBox.Text = ex.Message;
|
---|
139 | ErrorTextBox.Visible = true;
|
---|
140 | }
|
---|
141 | }
|
---|
142 |
|
---|
143 | private IEnumerable<string> GetVariableNamesWithType(TableFileParser csvParser) {
|
---|
144 | IList<string> variableNamesWithType = csvParser.VariableNames.ToList();
|
---|
145 | for (int i = 0; i < csvParser.Values.Count; i++) {
|
---|
146 | if (csvParser.Values[i] is List<double>) {
|
---|
147 | variableNamesWithType[i] += " (Double)";
|
---|
148 | } else if (csvParser.Values[i] is List<string>) {
|
---|
149 | variableNamesWithType[i] += " (String)";
|
---|
150 | } else if (csvParser.Values[i] is List<DateTime>) {
|
---|
151 | variableNamesWithType[i] += " (DateTime)";
|
---|
152 | } else {
|
---|
153 | throw new ArgumentException("The variable values must be of type List<double>, List<string> or List<DateTime>");
|
---|
154 | }
|
---|
155 | }
|
---|
156 | return variableNamesWithType;
|
---|
157 | }
|
---|
158 | }
|
---|
159 | }
|
---|