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