Free cookie consent management tool by TermsFeed Policy Generator

source: branches/GP-MoveOperators/HeuristicLab.Problems.Instances.DataAnalysis/3.3/Clustering/CSV/ClusteringCSVInstanceProvider.cs @ 8660

Last change on this file since 8660 was 8660, checked in by gkronber, 12 years ago

#1847 merged r8205:8635 from trunk into branch

File size: 5.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;
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 ClusteringCSVInstanceProvider : ClusteringInstanceProvider {
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 IClusteringProblemData LoadData(IDataDescriptor descriptor) {
54      throw new NotImplementedException();
55    }
56
57    public override bool CanImportData {
58      get { return true; }
59    }
60    public override IClusteringProblemData ImportData(string path) {
61      var csvFileParser = new TableFileParser();
62      csvFileParser.Parse(path);
63
64      Dataset dataset = new Dataset(csvFileParser.VariableNames, csvFileParser.Values);
65      string targetVar = dataset.DoubleVariables.Last();
66
67      // turn of input variables that are constant in the training partition
68      var allowedInputVars = new List<string>();
69      var trainingIndizes = Enumerable.Range(0, (csvFileParser.Rows * 2) / 3);
70      if (trainingIndizes.Count() >= 2) {
71        foreach (var variableName in dataset.DoubleVariables) {
72          if (dataset.GetDoubleValues(variableName, trainingIndizes).Range() > 0 &&
73            variableName != targetVar)
74            allowedInputVars.Add(variableName);
75        }
76      } else {
77        allowedInputVars.AddRange(dataset.DoubleVariables.Where(x => x.Equals(targetVar)));
78      }
79
80      ClusteringProblemData clusteringData = new ClusteringProblemData(dataset, allowedInputVars);
81
82      int trainingPartEnd = trainingIndizes.Last();
83      clusteringData.TrainingPartition.Start = trainingIndizes.First();
84      clusteringData.TrainingPartition.End = trainingPartEnd;
85      clusteringData.TestPartition.Start = trainingPartEnd;
86      clusteringData.TestPartition.End = csvFileParser.Rows;
87
88      clusteringData.Name = Path.GetFileName(path);
89
90      return clusteringData;
91    }
92
93    public override IClusteringProblemData ImportData(string path, DataAnalysisImportType type) {
94      TableFileParser csvFileParser = new TableFileParser();
95      csvFileParser.Parse(path);
96
97      List<IList> values = csvFileParser.Values;
98      if (type.Shuffle) {
99        values = Shuffle(values);
100      }
101
102      Dataset dataset = new Dataset(csvFileParser.VariableNames, values);
103      string targetVar = dataset.DoubleVariables.Last();
104
105      // turn of input variables that are constant in the training partition
106      var allowedInputVars = new List<string>();
107      int trainingPartEnd = (csvFileParser.Rows * type.Training) / 100;
108      var trainingIndizes = Enumerable.Range(0, trainingPartEnd);
109      foreach (var variableName in dataset.DoubleVariables) {
110        if (trainingIndizes.Count() >= 2 && dataset.GetDoubleValues(variableName, trainingIndizes).Range() > 0 &&
111          variableName != targetVar)
112          allowedInputVars.Add(variableName);
113      }
114
115      ClusteringProblemData clusteringData = new ClusteringProblemData(dataset, allowedInputVars);
116
117      clusteringData.TrainingPartition.Start = 0;
118      clusteringData.TrainingPartition.End = trainingPartEnd;
119      clusteringData.TestPartition.Start = trainingPartEnd;
120      clusteringData.TestPartition.End = csvFileParser.Rows;
121
122      clusteringData.Name = Path.GetFileName(path);
123
124      return clusteringData;
125    }
126
127    public override bool CanExportData {
128      get { return true; }
129    }
130    public override void ExportData(IClusteringProblemData instance, string path) {
131      var strBuilder = new StringBuilder();
132
133      foreach (var variable in instance.InputVariables) {
134        strBuilder.Append(variable + CultureInfo.CurrentCulture.TextInfo.ListSeparator);
135      }
136      strBuilder.Remove(strBuilder.Length - CultureInfo.CurrentCulture.TextInfo.ListSeparator.Length, CultureInfo.CurrentCulture.TextInfo.ListSeparator.Length);
137      strBuilder.AppendLine();
138
139      var dataset = instance.Dataset;
140
141      for (int i = 0; i < dataset.Rows; i++) {
142        for (int j = 0; j < dataset.Columns; j++) {
143          if (j > 0) strBuilder.Append(CultureInfo.CurrentCulture.TextInfo.ListSeparator);
144          strBuilder.Append(dataset.GetValue(i, j));
145        }
146        strBuilder.AppendLine();
147      }
148
149      using (var writer = new StreamWriter(path)) {
150        writer.Write(strBuilder);
151      }
152    }
153  }
154}
Note: See TracBrowser for help on using the repository browser.