Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.Instances.DataAnalysis/3.3/Regression/CSV/RegressionCSVInstanceProvider.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: 3.9 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 RegressionCSVInstanceProvider : RegressionInstanceProvider {
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    public override IRegressionProblemData LoadData(IDataDescriptor descriptor) {
51      throw new NotImplementedException();
52    }
53
54    public override bool CanImportData {
[8180]55      get { return true; }
56    }
[8192]57    public override IRegressionProblemData ImportData(string path) {
58      TableFileParser csvFileParser = new TableFileParser();
59      csvFileParser.Parse(path);
[8180]60
[8192]61      Dataset dataset = new Dataset(csvFileParser.VariableNames, csvFileParser.Values);
62      string targetVar = csvFileParser.VariableNames.Where(x => dataset.DoubleVariables.Contains(x)).Last();
63
64      IEnumerable<string> allowedInputVars = dataset.DoubleVariables.Where(x => !x.Equals(targetVar));
65
66      IRegressionProblemData regData = new RegressionProblemData(dataset, allowedInputVars, targetVar);
67
68      int trainingPartEnd = csvFileParser.Rows * 2 / 3;
69      regData.TrainingPartition.Start = 0;
70      regData.TrainingPartition.End = trainingPartEnd;
71      regData.TestPartition.Start = trainingPartEnd;
72      regData.TestPartition.End = csvFileParser.Rows;
73
74      int pos = path.LastIndexOf('\\');
75      if (pos < 0)
76        regData.Name = path;
77      else {
78        pos++;
79        regData.Name = path.Substring(pos, path.Length - pos);
80      }
81      return regData;
[7860]82    }
83
[8192]84    public override bool CanExportData {
85      get { return true; }
86    }
87    public override void ExportData(IRegressionProblemData instance, string path) {
[8530]88      var strBuilder = new StringBuilder();
[8180]89
90      foreach (var variable in instance.InputVariables) {
[8530]91        strBuilder.Append(variable + CultureInfo.CurrentCulture.TextInfo.ListSeparator);
[8180]92      }
[8530]93      strBuilder.Remove(strBuilder.Length - CultureInfo.CurrentCulture.TextInfo.ListSeparator.Length, CultureInfo.CurrentCulture.TextInfo.ListSeparator.Length);
[8180]94      strBuilder.AppendLine();
95
[8530]96      var dataset = instance.Dataset;
[8180]97
98      for (int i = 0; i < dataset.Rows; i++) {
99        for (int j = 0; j < dataset.Columns; j++) {
[8530]100          if (j > 0) strBuilder.Append(CultureInfo.CurrentCulture.TextInfo.ListSeparator);
101          strBuilder.Append(dataset.GetValue(i, j));
[8180]102        }
103        strBuilder.AppendLine();
104      }
105
[8530]106      using (var writer = new StreamWriter(path)) {
[8180]107        writer.Write(strBuilder);
108      }
109    }
[7860]110  }
111}
Note: See TracBrowser for help on using the repository browser.