source: trunk/sources/HeuristicLab.Problems.Instances.DataAnalysis/3.3/Clustering/CSV/ClusteringCSVInstanceProvider.cs @ 11540

Last change on this file since 11540 was 11540, checked in by mkommend, 8 years ago

#2273: Changed behavior of CSV import for clustering problem data to enable all input variables per default.

File size: 4.6 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2014 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.IO;
26using System.Linq;
27using HeuristicLab.Common;
28using HeuristicLab.Problems.DataAnalysis;
29
30namespace HeuristicLab.Problems.Instances.DataAnalysis {
31  public class ClusteringCSVInstanceProvider : ClusteringInstanceProvider {
32    public override string Name {
33      get { return "CSV File"; }
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.fcgi/wiki/Documentation/FAQ#DataAnalysisImportFileFormat"); }
42    }
43    public override string ReferencePublication {
44      get { return ""; }
45    }
46
47    public override IEnumerable<IDataDescriptor> GetDataDescriptors() {
48      return new List<IDataDescriptor>();
49    }
50
51    public override IClusteringProblemData LoadData(IDataDescriptor descriptor) {
52      throw new NotImplementedException();
53    }
54
55    public override bool CanImportData {
56      get { return true; }
57    }
58    public override IClusteringProblemData ImportData(string path) {
59      var csvFileParser = new TableFileParser();
60      csvFileParser.Parse(path, csvFileParser.AreColumnNamesInFirstLine(path));
61
62      Dataset dataset = new Dataset(csvFileParser.VariableNames, csvFileParser.Values);
63
64      // turn of input variables that are constant in the training partition
65      var allowedInputVars = new List<string>();
66      var trainingIndizes = Enumerable.Range(0, (csvFileParser.Rows * 2) / 3);
67      if (trainingIndizes.Count() >= 2) {
68        foreach (var variableName in dataset.DoubleVariables) {
69          if (dataset.GetDoubleValues(variableName, trainingIndizes).Range() > 0)
70            allowedInputVars.Add(variableName);
71        }
72      } else {
73        allowedInputVars.AddRange(dataset.DoubleVariables);
74      }
75
76      ClusteringProblemData clusteringData = new ClusteringProblemData(dataset, allowedInputVars);
77
78      int trainingPartEnd = trainingIndizes.Last();
79      clusteringData.TrainingPartition.Start = trainingIndizes.First();
80      clusteringData.TrainingPartition.End = trainingPartEnd;
81      clusteringData.TestPartition.Start = trainingPartEnd;
82      clusteringData.TestPartition.End = csvFileParser.Rows;
83
84      clusteringData.Name = Path.GetFileName(path);
85
86      return clusteringData;
87    }
88
89    protected override IClusteringProblemData ImportData(string path, DataAnalysisImportType type, TableFileParser csvFileParser) {
90      List<IList> values = csvFileParser.Values;
91      if (type.Shuffle) {
92        values = Shuffle(values);
93      }
94
95      Dataset dataset = new Dataset(csvFileParser.VariableNames, values);
96
97      // turn of input variables that are constant in the training partition
98      var allowedInputVars = new List<string>();
99      int trainingPartEnd = (csvFileParser.Rows * type.TrainingPercentage) / 100;
100      var trainingIndizes = Enumerable.Range(0, trainingPartEnd);
101      if (trainingIndizes.Count() >= 2) {
102        foreach (var variableName in dataset.DoubleVariables) {
103          if (dataset.GetDoubleValues(variableName, trainingIndizes).Range() > 0)
104            allowedInputVars.Add(variableName);
105        }
106      } else {
107        allowedInputVars.AddRange(dataset.DoubleVariables);
108      }
109
110      ClusteringProblemData clusteringData = new ClusteringProblemData(dataset, allowedInputVars);
111
112      clusteringData.TrainingPartition.Start = 0;
113      clusteringData.TrainingPartition.End = trainingPartEnd;
114      clusteringData.TestPartition.Start = trainingPartEnd;
115      clusteringData.TestPartition.End = csvFileParser.Rows;
116
117      clusteringData.Name = Path.GetFileName(path);
118
119      return clusteringData;
120    }
121  }
122}
Note: See TracBrowser for help on using the repository browser.