Free cookie consent management tool by TermsFeed Policy Generator

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