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

Last change on this file since 8530 was 8530, checked in by abeham, 10 years ago

#1922: Used the culture's defined list separator in CSV export

File size: 3.7 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.Globalization;
25using System.IO;
26using System.Text;
27using HeuristicLab.Problems.DataAnalysis;
28
29namespace HeuristicLab.Problems.Instances.DataAnalysis {
30  public class ClusteringCSVInstanceProvider : ClusteringInstanceProvider {
31    public override string Name {
32      get { return "CSV File"; }
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
50    public override IClusteringProblemData LoadData(IDataDescriptor descriptor) {
51      throw new NotImplementedException();
52    }
53
54    public override bool CanImportData {
55      get { return true; }
56    }
57    public override IClusteringProblemData ImportData(string path) {
58      var csvFileParser = new TableFileParser();
59
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) {
85      var strBuilder = new StringBuilder();
86
87      foreach (var variable in instance.InputVariables) {
88        strBuilder.Append(variable + CultureInfo.CurrentCulture.TextInfo.ListSeparator);
89      }
90      strBuilder.Remove(strBuilder.Length - CultureInfo.CurrentCulture.TextInfo.ListSeparator.Length, CultureInfo.CurrentCulture.TextInfo.ListSeparator.Length);
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++) {
97          if (j > 0) strBuilder.Append(CultureInfo.CurrentCulture.TextInfo.ListSeparator);
98          strBuilder.Append(dataset.GetValue(i, j));
99        }
100        strBuilder.AppendLine();
101      }
102
103      using (var writer = new StreamWriter(path)) {
104        writer.Write(strBuilder);
105      }
106    }
107  }
108}
Note: See TracBrowser for help on using the repository browser.