[7860] | 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;
|
---|
[8598] | 23 | using System.Collections;
|
---|
[7860] | 24 | using System.Collections.Generic;
|
---|
[8530] | 25 | using System.Globalization;
|
---|
[8180] | 26 | using System.IO;
|
---|
[8192] | 27 | using System.Linq;
|
---|
[8180] | 28 | using System.Text;
|
---|
[8566] | 29 | using HeuristicLab.Common;
|
---|
[7860] | 30 | using HeuristicLab.Problems.DataAnalysis;
|
---|
[8192] | 31 |
|
---|
[7860] | 32 | namespace HeuristicLab.Problems.Instances.DataAnalysis {
|
---|
| 33 | public class ClassificationCSVInstanceProvider : ClassificationInstanceProvider {
|
---|
| 34 | public override string Name {
|
---|
[8211] | 35 | get { return "CSV File"; }
|
---|
[7860] | 36 | }
|
---|
| 37 | public override string Description {
|
---|
| 38 | get {
|
---|
| 39 | return "";
|
---|
| 40 | }
|
---|
| 41 | }
|
---|
| 42 | public override Uri WebLink {
|
---|
| 43 | get { return new Uri("http://dev.heuristiclab.com/trac/hl/core/wiki/UsersFAQ#DataAnalysisImportFileFormat"); }
|
---|
| 44 | }
|
---|
| 45 | public override string ReferencePublication {
|
---|
| 46 | get { return ""; }
|
---|
| 47 | }
|
---|
| 48 |
|
---|
[8192] | 49 | public override IEnumerable<IDataDescriptor> GetDataDescriptors() {
|
---|
| 50 | return new List<IDataDescriptor>();
|
---|
| 51 | }
|
---|
| 52 |
|
---|
| 53 | public override IClassificationProblemData LoadData(IDataDescriptor descriptor) {
|
---|
| 54 | throw new NotImplementedException();
|
---|
| 55 | }
|
---|
| 56 |
|
---|
| 57 | public override bool CanImportData {
|
---|
[8180] | 58 | get { return true; }
|
---|
| 59 | }
|
---|
[8192] | 60 | public override IClassificationProblemData ImportData(string path) {
|
---|
| 61 | TableFileParser csvFileParser = new TableFileParser();
|
---|
[8180] | 62 |
|
---|
[8192] | 63 | csvFileParser.Parse(path);
|
---|
| 64 |
|
---|
| 65 | Dataset dataset = new Dataset(csvFileParser.VariableNames, csvFileParser.Values);
|
---|
[8566] | 66 | string targetVar = dataset.DoubleVariables.Last();
|
---|
[8192] | 67 |
|
---|
[8566] | 68 | // turn of input variables that are constant in the training partition
|
---|
| 69 | var allowedInputVars = new List<string>();
|
---|
| 70 | var trainingIndizes = Enumerable.Range(0, (csvFileParser.Rows * 2) / 3);
|
---|
| 71 | foreach (var variableName in dataset.DoubleVariables) {
|
---|
| 72 | if (dataset.GetDoubleValues(variableName, trainingIndizes).Range() > 0 &&
|
---|
| 73 | variableName != targetVar)
|
---|
| 74 | allowedInputVars.Add(variableName);
|
---|
[8192] | 75 | }
|
---|
| 76 |
|
---|
[8566] | 77 | ClassificationProblemData classificationData = new ClassificationProblemData(dataset, allowedInputVars, targetVar);
|
---|
| 78 |
|
---|
| 79 | int trainingPartEnd = trainingIndizes.Last();
|
---|
| 80 | classificationData.TrainingPartition.Start = trainingIndizes.First();
|
---|
| 81 | classificationData.TrainingPartition.End = trainingPartEnd;
|
---|
| 82 | classificationData.TestPartition.Start = trainingPartEnd;
|
---|
| 83 | classificationData.TestPartition.End = csvFileParser.Rows;
|
---|
| 84 |
|
---|
| 85 | classificationData.Name = Path.GetFileName(path);
|
---|
| 86 |
|
---|
| 87 | return classificationData;
|
---|
[7860] | 88 | }
|
---|
| 89 |
|
---|
[8598] | 90 | public override IClassificationProblemData ImportData(string path, DataAnalysisImportType type) {
|
---|
| 91 | TableFileParser csvFileParser = new TableFileParser();
|
---|
| 92 | csvFileParser.Parse(path);
|
---|
| 93 |
|
---|
| 94 | List<IList> values = csvFileParser.Values;
|
---|
| 95 | if (type.Shuffle) {
|
---|
| 96 | values = Shuffle(values);
|
---|
| 97 | }
|
---|
| 98 |
|
---|
| 99 | Dataset dataset = new Dataset(csvFileParser.VariableNames, values);
|
---|
| 100 | string targetVar = dataset.DoubleVariables.Last();
|
---|
| 101 |
|
---|
| 102 | // turn of input variables that are constant in the training partition
|
---|
| 103 | var allowedInputVars = new List<string>();
|
---|
| 104 | var trainingIndizes = Enumerable.Range(0, (csvFileParser.Rows * 2) / 3);
|
---|
| 105 | foreach (var variableName in dataset.DoubleVariables) {
|
---|
| 106 | if (dataset.GetDoubleValues(variableName, trainingIndizes).Range() > 0 &&
|
---|
| 107 | variableName != targetVar)
|
---|
| 108 | allowedInputVars.Add(variableName);
|
---|
| 109 | }
|
---|
| 110 |
|
---|
| 111 | ClassificationProblemData classificationData = new ClassificationProblemData(dataset, allowedInputVars, targetVar);
|
---|
| 112 |
|
---|
| 113 | int trainingPartEnd = trainingIndizes.Last();
|
---|
| 114 | classificationData.TrainingPartition.Start = trainingIndizes.First();
|
---|
| 115 | classificationData.TrainingPartition.End = trainingPartEnd;
|
---|
| 116 | classificationData.TestPartition.Start = trainingPartEnd;
|
---|
| 117 | classificationData.TestPartition.End = csvFileParser.Rows;
|
---|
| 118 |
|
---|
| 119 | classificationData.Name = Path.GetFileName(path);
|
---|
| 120 |
|
---|
| 121 | return classificationData;
|
---|
| 122 | }
|
---|
| 123 |
|
---|
[8192] | 124 | public override bool CanExportData {
|
---|
| 125 | get { return true; }
|
---|
| 126 | }
|
---|
| 127 | public override void ExportData(IClassificationProblemData instance, string path) {
|
---|
[8530] | 128 | var strBuilder = new StringBuilder();
|
---|
[8180] | 129 |
|
---|
| 130 | foreach (var variable in instance.InputVariables) {
|
---|
[8530] | 131 | strBuilder.Append(variable + CultureInfo.CurrentCulture.TextInfo.ListSeparator);
|
---|
[8180] | 132 | }
|
---|
[8530] | 133 | strBuilder.Remove(strBuilder.Length - CultureInfo.CurrentCulture.TextInfo.ListSeparator.Length, CultureInfo.CurrentCulture.TextInfo.ListSeparator.Length);
|
---|
[8180] | 134 | strBuilder.AppendLine();
|
---|
| 135 |
|
---|
[8530] | 136 | var dataset = instance.Dataset;
|
---|
[8180] | 137 |
|
---|
| 138 | for (int i = 0; i < dataset.Rows; i++) {
|
---|
| 139 | for (int j = 0; j < dataset.Columns; j++) {
|
---|
[8530] | 140 | if (j > 0) strBuilder.Append(CultureInfo.CurrentCulture.TextInfo.ListSeparator);
|
---|
| 141 | strBuilder.Append(dataset.GetValue(i, j));
|
---|
[8180] | 142 | }
|
---|
| 143 | strBuilder.AppendLine();
|
---|
| 144 | }
|
---|
| 145 |
|
---|
[8530] | 146 | using (var writer = new StreamWriter(path)) {
|
---|
[8180] | 147 | writer.Write(strBuilder);
|
---|
| 148 | }
|
---|
| 149 | }
|
---|
[7860] | 150 | }
|
---|
| 151 | }
|
---|