Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.Instances.DataAnalysis/3.3/Clustering/CSV/ClusteringCSVInstanceProvider.cs @ 8199

Last change on this file since 8199 was 8199, checked in by mkommend, 12 years ago

#1784: Renamed CSV instance providers and corrected tooltips.

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