[8084] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[11171] | 3 | * Copyright (C) 2002-2014 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[8084] | 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;
|
---|
[8084] | 24 | using System.Collections.Generic;
|
---|
[8180] | 25 | using System.IO;
|
---|
[8566] | 26 | using System.Linq;
|
---|
| 27 | using HeuristicLab.Common;
|
---|
[8084] | 28 | using HeuristicLab.Problems.DataAnalysis;
|
---|
| 29 |
|
---|
| 30 | namespace HeuristicLab.Problems.Instances.DataAnalysis {
|
---|
| 31 | public class ClusteringCSVInstanceProvider : ClusteringInstanceProvider {
|
---|
| 32 | public override string Name {
|
---|
[8211] | 33 | get { return "CSV File"; }
|
---|
[8084] | 34 | }
|
---|
| 35 | public override string Description {
|
---|
| 36 | get {
|
---|
| 37 | return "";
|
---|
| 38 | }
|
---|
| 39 | }
|
---|
| 40 | public override Uri WebLink {
|
---|
[11283] | 41 | get { return new Uri("http://dev.heuristiclab.com/trac.fcgi/wiki/Documentation/FAQ#DataAnalysisImportFileFormat"); }
|
---|
[8084] | 42 | }
|
---|
| 43 | public override string ReferencePublication {
|
---|
| 44 | get { return ""; }
|
---|
| 45 | }
|
---|
| 46 |
|
---|
| 47 | public override IEnumerable<IDataDescriptor> GetDataDescriptors() {
|
---|
| 48 | return new List<IDataDescriptor>();
|
---|
| 49 | }
|
---|
| 50 |
|
---|
[8192] | 51 | public override IClusteringProblemData LoadData(IDataDescriptor descriptor) {
|
---|
| 52 | throw new NotImplementedException();
|
---|
| 53 | }
|
---|
| 54 |
|
---|
| 55 | public override bool CanImportData {
|
---|
[8180] | 56 | get { return true; }
|
---|
| 57 | }
|
---|
[8192] | 58 | public override IClusteringProblemData ImportData(string path) {
|
---|
| 59 | var csvFileParser = new TableFileParser();
|
---|
[9608] | 60 | csvFileParser.Parse(path, csvFileParser.AreColumnNamesInFirstLine(path));
|
---|
[8180] | 61 |
|
---|
[8598] | 62 | Dataset dataset = new Dataset(csvFileParser.VariableNames, csvFileParser.Values);
|
---|
| 63 | string targetVar = dataset.DoubleVariables.Last();
|
---|
| 64 |
|
---|
| 65 | // turn of input variables that are constant in the training partition
|
---|
| 66 | var allowedInputVars = new List<string>();
|
---|
| 67 | var trainingIndizes = Enumerable.Range(0, (csvFileParser.Rows * 2) / 3);
|
---|
[8601] | 68 | if (trainingIndizes.Count() >= 2) {
|
---|
| 69 | foreach (var variableName in dataset.DoubleVariables) {
|
---|
| 70 | if (dataset.GetDoubleValues(variableName, trainingIndizes).Range() > 0 &&
|
---|
| 71 | variableName != targetVar)
|
---|
| 72 | allowedInputVars.Add(variableName);
|
---|
| 73 | }
|
---|
| 74 | } else {
|
---|
[8877] | 75 | allowedInputVars.AddRange(dataset.DoubleVariables.Where(x => !x.Equals(targetVar)));
|
---|
[8598] | 76 | }
|
---|
| 77 |
|
---|
| 78 | ClusteringProblemData clusteringData = new ClusteringProblemData(dataset, allowedInputVars);
|
---|
| 79 |
|
---|
| 80 | int trainingPartEnd = trainingIndizes.Last();
|
---|
| 81 | clusteringData.TrainingPartition.Start = trainingIndizes.First();
|
---|
| 82 | clusteringData.TrainingPartition.End = trainingPartEnd;
|
---|
| 83 | clusteringData.TestPartition.Start = trainingPartEnd;
|
---|
| 84 | clusteringData.TestPartition.End = csvFileParser.Rows;
|
---|
| 85 |
|
---|
| 86 | clusteringData.Name = Path.GetFileName(path);
|
---|
| 87 |
|
---|
| 88 | return clusteringData;
|
---|
| 89 | }
|
---|
| 90 |
|
---|
[8877] | 91 | protected override IClusteringProblemData ImportData(string path, DataAnalysisImportType type, TableFileParser csvFileParser) {
|
---|
[8598] | 92 | List<IList> values = csvFileParser.Values;
|
---|
| 93 | if (type.Shuffle) {
|
---|
| 94 | values = Shuffle(values);
|
---|
| 95 | }
|
---|
[8192] | 96 |
|
---|
[8598] | 97 | Dataset dataset = new Dataset(csvFileParser.VariableNames, values);
|
---|
| 98 | string targetVar = dataset.DoubleVariables.Last();
|
---|
| 99 |
|
---|
[8566] | 100 | // turn of input variables that are constant in the training partition
|
---|
| 101 | var allowedInputVars = new List<string>();
|
---|
[9021] | 102 | int trainingPartEnd = (csvFileParser.Rows * type.TrainingPercentage) / 100;
|
---|
[8599] | 103 | var trainingIndizes = Enumerable.Range(0, trainingPartEnd);
|
---|
[8877] | 104 | if (trainingIndizes.Count() >= 2) {
|
---|
| 105 | foreach (var variableName in dataset.DoubleVariables) {
|
---|
| 106 | if (dataset.GetDoubleValues(variableName, trainingIndizes).Range() > 0 &&
|
---|
| 107 | variableName != targetVar)
|
---|
| 108 | allowedInputVars.Add(variableName);
|
---|
| 109 | }
|
---|
| 110 | } else {
|
---|
| 111 | allowedInputVars.AddRange(dataset.DoubleVariables.Where(x => !x.Equals(targetVar)));
|
---|
[8192] | 112 | }
|
---|
| 113 |
|
---|
[8598] | 114 | ClusteringProblemData clusteringData = new ClusteringProblemData(dataset, allowedInputVars);
|
---|
[8566] | 115 |
|
---|
[8599] | 116 | clusteringData.TrainingPartition.Start = 0;
|
---|
[8566] | 117 | clusteringData.TrainingPartition.End = trainingPartEnd;
|
---|
| 118 | clusteringData.TestPartition.Start = trainingPartEnd;
|
---|
| 119 | clusteringData.TestPartition.End = csvFileParser.Rows;
|
---|
| 120 |
|
---|
| 121 | clusteringData.Name = Path.GetFileName(path);
|
---|
| 122 |
|
---|
| 123 | return clusteringData;
|
---|
[8192] | 124 | }
|
---|
[8084] | 125 | }
|
---|
| 126 | }
|
---|