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