[8598] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[15584] | 3 | * Copyright (C) 2002-2018 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;
|
---|
[13584] | 27 | using System.Text;
|
---|
[8598] | 28 | using System.Windows.Forms;
|
---|
[8877] | 29 | using HeuristicLab.Core.Views;
|
---|
| 30 | using HeuristicLab.Problems.DataAnalysis;
|
---|
[8598] | 31 |
|
---|
| 32 | namespace HeuristicLab.Problems.Instances.DataAnalysis.Views {
|
---|
[15185] | 33 | public partial class DataAnalysisImportDialog : Form {
|
---|
[8598] | 34 |
|
---|
[13584] | 35 | private static readonly List<KeyValuePair<DateTimeFormatInfo, string>> dateTimeFormats =
|
---|
| 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 =
|
---|
| 44 | new List<KeyValuePair<char, string>>{
|
---|
| 45 | new KeyValuePair<char, string>(';', "; (Semicolon)" ),
|
---|
| 46 | new KeyValuePair<char, string>(',', ", (Comma)" ),
|
---|
| 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)"),
|
---|
| 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"),
|
---|
| 61 | new KeyValuePair<Encoding, string>(Encoding.Unicode, "Unicode"),
|
---|
| 62 | new KeyValuePair<Encoding, string>(Encoding.UTF8, "UTF8")
|
---|
| 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";
|
---|
| 99 | DateTimeFormatComboBox.DataSource = dateTimeFormats;
|
---|
| 100 | DateTimeFormatComboBox.ValueMember = "Key";
|
---|
| 101 | DateTimeFormatComboBox.DisplayMember = "Value";
|
---|
[13584] | 102 | EncodingComboBox.DataSource = POSSIBLE_ENCODINGS;
|
---|
| 103 | EncodingComboBox.ValueMember = "Key";
|
---|
| 104 | EncodingComboBox.DisplayMember = "Value";
|
---|
| 105 |
|
---|
[8598] | 106 | }
|
---|
[8599] | 107 |
|
---|
| 108 | private void TrainingTestTrackBar_ValueChanged(object sender, System.EventArgs e) {
|
---|
| 109 | TrainingLabel.Text = "Training: " + TrainingTestTrackBar.Value + " %";
|
---|
| 110 | TestLabel.Text = "Test: " + (TrainingTestTrackBar.Maximum - TrainingTestTrackBar.Value) + " %";
|
---|
| 111 | }
|
---|
[8693] | 112 |
|
---|
[8877] | 113 | protected virtual void OpenFileButtonClick(object sender, System.EventArgs e) {
|
---|
| 114 | if (openFileDialog.ShowDialog(this) != DialogResult.OK) return;
|
---|
| 115 |
|
---|
[9449] | 116 | SeparatorComboBox.Enabled = true;
|
---|
| 117 | DecimalSeparatorComboBox.Enabled = true;
|
---|
| 118 | DateTimeFormatComboBox.Enabled = true;
|
---|
[13584] | 119 | EncodingComboBox.Enabled = true;
|
---|
[8877] | 120 | ProblemTextBox.Text = openFileDialog.FileName;
|
---|
[9608] | 121 | TableFileParser csvParser = new TableFileParser();
|
---|
| 122 | CheckboxColumnNames.Checked = csvParser.AreColumnNamesInFirstLine(ProblemTextBox.Text,
|
---|
| 123 | (NumberFormatInfo)DecimalSeparatorComboBox.SelectedValue,
|
---|
| 124 | (DateTimeFormatInfo)DateTimeFormatComboBox.SelectedValue,
|
---|
| 125 | (char)SeparatorComboBox.SelectedValue);
|
---|
[8877] | 126 | ParseCSVFile();
|
---|
| 127 | }
|
---|
| 128 |
|
---|
| 129 | protected virtual void CSVFormatComboBoxSelectionChangeCommitted(object sender, EventArgs e) {
|
---|
| 130 | if (string.IsNullOrEmpty(ProblemTextBox.Text)) return;
|
---|
| 131 |
|
---|
| 132 | ParseCSVFile();
|
---|
| 133 | }
|
---|
| 134 |
|
---|
[9608] | 135 | protected virtual void CheckboxColumnNames_CheckedChanged(object sender, EventArgs e) {
|
---|
| 136 | if (string.IsNullOrEmpty(ProblemTextBox.Text)) return;
|
---|
| 137 |
|
---|
| 138 | ParseCSVFile();
|
---|
| 139 | }
|
---|
| 140 |
|
---|
[8877] | 141 | protected void ParseCSVFile() {
|
---|
| 142 | PreviewDatasetMatrix.Content = null;
|
---|
| 143 | try {
|
---|
| 144 | TableFileParser csvParser = new TableFileParser();
|
---|
[13584] | 145 | csvParser.Encoding = (Encoding)EncodingComboBox.SelectedValue;
|
---|
[8877] | 146 | csvParser.Parse(ProblemTextBox.Text,
|
---|
| 147 | (NumberFormatInfo)DecimalSeparatorComboBox.SelectedValue,
|
---|
| 148 | (DateTimeFormatInfo)DateTimeFormatComboBox.SelectedValue,
|
---|
[9608] | 149 | (char)SeparatorComboBox.SelectedValue,
|
---|
[13413] | 150 | CheckboxColumnNames.Checked, lineLimit: 500);
|
---|
[8877] | 151 | IEnumerable<string> variableNamesWithType = GetVariableNamesWithType(csvParser);
|
---|
| 152 | PreviewDatasetMatrix.Content = new Dataset(variableNamesWithType, csvParser.Values);
|
---|
| 153 |
|
---|
| 154 | CheckAdditionalConstraints(csvParser);
|
---|
| 155 |
|
---|
| 156 | ErrorTextBox.Text = String.Empty;
|
---|
| 157 | ErrorTextBox.Visible = false;
|
---|
[8693] | 158 | OkButton.Enabled = true;
|
---|
| 159 | }
|
---|
[8877] | 160 | catch (Exception ex) {
|
---|
[14285] | 161 | if (ex is IOException || ex is InvalidOperationException || ex is ArgumentException) {
|
---|
[9449] | 162 | OkButton.Enabled = false;
|
---|
| 163 | ErrorTextBox.Text = ex.Message;
|
---|
| 164 | ErrorTextBox.Visible = true;
|
---|
| 165 | } else {
|
---|
| 166 | throw;
|
---|
| 167 | }
|
---|
[8877] | 168 | }
|
---|
[8693] | 169 | }
|
---|
[8877] | 170 |
|
---|
| 171 | protected virtual void CheckAdditionalConstraints(TableFileParser csvParser) {
|
---|
| 172 | if (!csvParser.Values.Any(x => x is List<double>)) {
|
---|
| 173 | throw new ArgumentException("No double column could be found!");
|
---|
| 174 | }
|
---|
| 175 | }
|
---|
| 176 |
|
---|
| 177 | private IEnumerable<string> GetVariableNamesWithType(TableFileParser csvParser) {
|
---|
| 178 | IList<string> variableNamesWithType = csvParser.VariableNames.ToList();
|
---|
| 179 | for (int i = 0; i < csvParser.Values.Count; i++) {
|
---|
| 180 | if (csvParser.Values[i] is List<double>) {
|
---|
| 181 | variableNamesWithType[i] += " (Double)";
|
---|
| 182 | } else if (csvParser.Values[i] is List<string>) {
|
---|
| 183 | variableNamesWithType[i] += " (String)";
|
---|
| 184 | } else if (csvParser.Values[i] is List<DateTime>) {
|
---|
| 185 | variableNamesWithType[i] += " (DateTime)";
|
---|
| 186 | } else {
|
---|
| 187 | throw new ArgumentException("The variable values must be of type List<double>, List<string> or List<DateTime>");
|
---|
| 188 | }
|
---|
| 189 | }
|
---|
| 190 | return variableNamesWithType;
|
---|
| 191 | }
|
---|
| 192 |
|
---|
| 193 | protected void ControlToolTip_DoubleClick(object sender, EventArgs e) {
|
---|
| 194 | Control control = sender as Control;
|
---|
| 195 | if (control != null) {
|
---|
| 196 | using (TextDialog dialog = new TextDialog(control.Name, (string)control.Tag, true)) {
|
---|
| 197 | dialog.ShowDialog(this);
|
---|
| 198 | }
|
---|
| 199 | }
|
---|
| 200 | }
|
---|
[8598] | 201 | }
|
---|
| 202 | }
|
---|