[8084] | 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;
|
---|
[8530] | 24 | using System.Globalization;
|
---|
[8180] | 25 | using System.IO;
|
---|
[8566] | 26 | using System.Linq;
|
---|
[8180] | 27 | using System.Text;
|
---|
[8566] | 28 | using HeuristicLab.Common;
|
---|
[8084] | 29 | using HeuristicLab.Problems.DataAnalysis;
|
---|
| 30 |
|
---|
| 31 | namespace HeuristicLab.Problems.Instances.DataAnalysis {
|
---|
| 32 | public class ClusteringCSVInstanceProvider : ClusteringInstanceProvider {
|
---|
| 33 | public override string Name {
|
---|
[8211] | 34 | get { return "CSV File"; }
|
---|
[8084] | 35 | }
|
---|
| 36 | public override string Description {
|
---|
| 37 | get {
|
---|
| 38 | return "";
|
---|
| 39 | }
|
---|
| 40 | }
|
---|
| 41 | public override Uri WebLink {
|
---|
| 42 | get { return new Uri("http://dev.heuristiclab.com/trac/hl/core/wiki/UsersFAQ#DataAnalysisImportFileFormat"); }
|
---|
| 43 | }
|
---|
| 44 | public override string ReferencePublication {
|
---|
| 45 | get { return ""; }
|
---|
| 46 | }
|
---|
| 47 |
|
---|
| 48 | public override IEnumerable<IDataDescriptor> GetDataDescriptors() {
|
---|
| 49 | return new List<IDataDescriptor>();
|
---|
| 50 | }
|
---|
| 51 |
|
---|
[8192] | 52 | public override IClusteringProblemData LoadData(IDataDescriptor descriptor) {
|
---|
| 53 | throw new NotImplementedException();
|
---|
| 54 | }
|
---|
| 55 |
|
---|
| 56 | public override bool CanImportData {
|
---|
[8180] | 57 | get { return true; }
|
---|
| 58 | }
|
---|
[8192] | 59 | public override IClusteringProblemData ImportData(string path) {
|
---|
| 60 | var csvFileParser = new TableFileParser();
|
---|
[8180] | 61 |
|
---|
[8192] | 62 | csvFileParser.Parse(path);
|
---|
| 63 |
|
---|
| 64 | var dataset = new Dataset(csvFileParser.VariableNames, csvFileParser.Values);
|
---|
| 65 |
|
---|
[8566] | 66 | // turn of input variables that are constant in the training partition
|
---|
| 67 | var allowedInputVars = new List<string>();
|
---|
| 68 | var trainingIndizes = Enumerable.Range(0, (csvFileParser.Rows * 2) / 3);
|
---|
| 69 | foreach (var variableName in dataset.DoubleVariables) {
|
---|
| 70 | if (dataset.GetDoubleValues(variableName, trainingIndizes).Range() > 0)
|
---|
| 71 | allowedInputVars.Add(variableName);
|
---|
[8192] | 72 | }
|
---|
| 73 |
|
---|
[8566] | 74 | var clusteringData = new ClusteringProblemData(dataset, allowedInputVars);
|
---|
| 75 |
|
---|
| 76 | int trainingPartEnd = trainingIndizes.Last();
|
---|
| 77 | clusteringData.TrainingPartition.Start = trainingIndizes.First();
|
---|
| 78 | clusteringData.TrainingPartition.End = trainingPartEnd;
|
---|
| 79 | clusteringData.TestPartition.Start = trainingPartEnd;
|
---|
| 80 | clusteringData.TestPartition.End = csvFileParser.Rows;
|
---|
| 81 |
|
---|
| 82 | clusteringData.Name = Path.GetFileName(path);
|
---|
| 83 |
|
---|
| 84 | return clusteringData;
|
---|
[8192] | 85 | }
|
---|
| 86 |
|
---|
| 87 | public override bool CanExportData {
|
---|
| 88 | get { return true; }
|
---|
| 89 | }
|
---|
| 90 | public override void ExportData(IClusteringProblemData instance, string path) {
|
---|
[8180] | 91 | var strBuilder = new StringBuilder();
|
---|
| 92 |
|
---|
| 93 | foreach (var variable in instance.InputVariables) {
|
---|
[8530] | 94 | strBuilder.Append(variable + CultureInfo.CurrentCulture.TextInfo.ListSeparator);
|
---|
[8180] | 95 | }
|
---|
[8530] | 96 | strBuilder.Remove(strBuilder.Length - CultureInfo.CurrentCulture.TextInfo.ListSeparator.Length, CultureInfo.CurrentCulture.TextInfo.ListSeparator.Length);
|
---|
[8180] | 97 | strBuilder.AppendLine();
|
---|
| 98 |
|
---|
| 99 | var dataset = instance.Dataset;
|
---|
| 100 |
|
---|
| 101 | for (int i = 0; i < dataset.Rows; i++) {
|
---|
| 102 | for (int j = 0; j < dataset.Columns; j++) {
|
---|
[8530] | 103 | if (j > 0) strBuilder.Append(CultureInfo.CurrentCulture.TextInfo.ListSeparator);
|
---|
| 104 | strBuilder.Append(dataset.GetValue(i, j));
|
---|
[8180] | 105 | }
|
---|
| 106 | strBuilder.AppendLine();
|
---|
| 107 | }
|
---|
| 108 |
|
---|
| 109 | using (var writer = new StreamWriter(path)) {
|
---|
| 110 | writer.Write(strBuilder);
|
---|
| 111 | }
|
---|
| 112 | }
|
---|
[8084] | 113 | }
|
---|
| 114 | }
|
---|