Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 8693 was 8693, checked in by sforsten, 12 years ago

#1942:

  • fixed type "Shuffel"
  • removed icon and control box
File size: 5.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;
24using System.Collections.Generic;
25using System.Globalization;
26using System.IO;
27using System.Linq;
28using System.Text;
29using HeuristicLab.Common;
30using HeuristicLab.Problems.DataAnalysis;
31
32namespace HeuristicLab.Problems.Instances.DataAnalysis {
33  public class ClassificationCSVInstanceProvider : ClassificationInstanceProvider {
34    public override string Name {
35      get { return "CSV File"; }
36    }
37    public override string Description {
38      get {
39        return "";
40      }
41    }
42    public override Uri WebLink {
43      get { return new Uri("http://dev.heuristiclab.com/trac/hl/core/wiki/UsersFAQ#DataAnalysisImportFileFormat"); }
44    }
45    public override string ReferencePublication {
46      get { return ""; }
47    }
48
49    public override IEnumerable<IDataDescriptor> GetDataDescriptors() {
50      return new List<IDataDescriptor>();
51    }
52
53    public override IClassificationProblemData LoadData(IDataDescriptor descriptor) {
54      throw new NotImplementedException();
55    }
56
57    public override bool CanImportData {
58      get { return true; }
59    }
60    public override IClassificationProblemData ImportData(string path) {
61      TableFileParser csvFileParser = new TableFileParser();
62
63      csvFileParser.Parse(path);
64
65      Dataset dataset = new Dataset(csvFileParser.VariableNames, csvFileParser.Values);
66      string targetVar = dataset.DoubleVariables.Last();
67
68      // turn of input variables that are constant in the training partition
69      var allowedInputVars = new List<string>();
70      var trainingIndizes = Enumerable.Range(0, (csvFileParser.Rows * 2) / 3);
71      if (trainingIndizes.Count() >= 2) {
72        foreach (var variableName in dataset.DoubleVariables) {
73          if (dataset.GetDoubleValues(variableName, trainingIndizes).Range() > 0 &&
74            variableName != targetVar)
75            allowedInputVars.Add(variableName);
76        }
77      } else {
78        allowedInputVars.AddRange(dataset.DoubleVariables.Where(x => x.Equals(targetVar)));
79      }
80
81      ClassificationProblemData classificationData = new ClassificationProblemData(dataset, allowedInputVars, targetVar);
82
83      int trainingPartEnd = trainingIndizes.Last();
84      classificationData.TrainingPartition.Start = trainingIndizes.First();
85      classificationData.TrainingPartition.End = trainingPartEnd;
86      classificationData.TestPartition.Start = trainingPartEnd;
87      classificationData.TestPartition.End = csvFileParser.Rows;
88
89      classificationData.Name = Path.GetFileName(path);
90
91      return classificationData;
92    }
93
94    public override IClassificationProblemData ImportData(string path, DataAnalysisImportType type) {
95      TableFileParser csvFileParser = new TableFileParser();
96      csvFileParser.Parse(path);
97
98      int trainingPartEnd = (csvFileParser.Rows * type.Training) / 100;
99      List<IList> values = csvFileParser.Values;
100      if (type.Shuffle) {
101        values = Shuffle(values);
102      }
103
104      Dataset dataset = new Dataset(csvFileParser.VariableNames, values);
105      string targetVar = dataset.DoubleVariables.Last();
106
107      // turn of input variables that are constant in the training partition
108      var allowedInputVars = new List<string>();
109      var trainingIndizes = Enumerable.Range(0, trainingPartEnd);
110      foreach (var variableName in dataset.DoubleVariables) {
111        if (trainingIndizes.Count() >= 2 && dataset.GetDoubleValues(variableName, trainingIndizes).Range() > 0 &&
112          variableName != targetVar)
113          allowedInputVars.Add(variableName);
114      }
115
116      ClassificationProblemData classificationData = new ClassificationProblemData(dataset, allowedInputVars, targetVar);
117
118      classificationData.TrainingPartition.Start = 0;
119      classificationData.TrainingPartition.End = trainingPartEnd;
120      classificationData.TestPartition.Start = trainingPartEnd;
121      classificationData.TestPartition.End = csvFileParser.Rows;
122
123      classificationData.Name = Path.GetFileName(path);
124
125      return classificationData;
126    }
127
128    public override bool CanExportData {
129      get { return true; }
130    }
131    public override void ExportData(IClassificationProblemData instance, string path) {
132      var strBuilder = new StringBuilder();
133      var colSep = CultureInfo.CurrentCulture.TextInfo.ListSeparator;
134      foreach (var variable in instance.Dataset.VariableNames) {
135        strBuilder.Append(variable.Replace(colSep, String.Empty) + colSep);
136      }
137      strBuilder.Remove(strBuilder.Length - colSep.Length, colSep.Length);
138      strBuilder.AppendLine();
139
140      var dataset = instance.Dataset;
141
142      for (int i = 0; i < dataset.Rows; i++) {
143        for (int j = 0; j < dataset.Columns; j++) {
144          if (j > 0) strBuilder.Append(colSep);
145          strBuilder.Append(dataset.GetValue(i, j));
146        }
147        strBuilder.AppendLine();
148      }
149
150      using (var writer = new StreamWriter(path)) {
151        writer.Write(strBuilder);
152      }
153    }
154  }
155}
Note: See TracBrowser for help on using the repository browser.