Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.Instances.DataAnalysis/3.3/Classification/CSV/ClassifiactionCSVInstanceProvider.cs @ 8530

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

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

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