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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Globalization;
|
---|
25 | using System.IO;
|
---|
26 | using System.Linq;
|
---|
27 | using System.Text;
|
---|
28 | using HeuristicLab.Common;
|
---|
29 | using HeuristicLab.Problems.DataAnalysis;
|
---|
30 |
|
---|
31 | namespace HeuristicLab.Problems.Instances.DataAnalysis {
|
---|
32 | public class ClassificationCSVInstanceProvider : ClassificationInstanceProvider {
|
---|
33 | public override string Name {
|
---|
34 | get { return "CSV File"; }
|
---|
35 | }
|
---|
36 | public override string Description {
|
---|
37 | get {
|
---|
38 | return "";
|
---|
39 | }
|
---|
40 | }
|
---|
41 | public override Uri WebLink {
|
---|
42 | get { return new Uri("http://dev.heuristiclab.com/trac/hl/core/wiki/UsersFAQ#DataAnalysisImportFileFormat"); }
|
---|
43 | }
|
---|
44 | public override string ReferencePublication {
|
---|
45 | get { return ""; }
|
---|
46 | }
|
---|
47 |
|
---|
48 | public override IEnumerable<IDataDescriptor> GetDataDescriptors() {
|
---|
49 | return new List<IDataDescriptor>();
|
---|
50 | }
|
---|
51 |
|
---|
52 | public override IClassificationProblemData LoadData(IDataDescriptor descriptor) {
|
---|
53 | throw new NotImplementedException();
|
---|
54 | }
|
---|
55 |
|
---|
56 | public override bool CanImportData {
|
---|
57 | get { return true; }
|
---|
58 | }
|
---|
59 | public override IClassificationProblemData ImportData(string path) {
|
---|
60 | TableFileParser csvFileParser = new TableFileParser();
|
---|
61 |
|
---|
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 | foreach (var variableName in dataset.DoubleVariables) {
|
---|
71 | if (dataset.GetDoubleValues(variableName, trainingIndizes).Range() > 0 &&
|
---|
72 | variableName != targetVar)
|
---|
73 | allowedInputVars.Add(variableName);
|
---|
74 | }
|
---|
75 |
|
---|
76 | ClassificationProblemData classificationData = new ClassificationProblemData(dataset, allowedInputVars, targetVar);
|
---|
77 |
|
---|
78 | int trainingPartEnd = trainingIndizes.Last();
|
---|
79 | classificationData.TrainingPartition.Start = trainingIndizes.First();
|
---|
80 | classificationData.TrainingPartition.End = trainingPartEnd;
|
---|
81 | classificationData.TestPartition.Start = trainingPartEnd;
|
---|
82 | classificationData.TestPartition.End = csvFileParser.Rows;
|
---|
83 |
|
---|
84 | classificationData.Name = Path.GetFileName(path);
|
---|
85 |
|
---|
86 | return classificationData;
|
---|
87 | }
|
---|
88 |
|
---|
89 | public override bool CanExportData {
|
---|
90 | get { return true; }
|
---|
91 | }
|
---|
92 | public override void ExportData(IClassificationProblemData instance, string path) {
|
---|
93 | var strBuilder = new StringBuilder();
|
---|
94 |
|
---|
95 | foreach (var variable in instance.InputVariables) {
|
---|
96 | strBuilder.Append(variable + CultureInfo.CurrentCulture.TextInfo.ListSeparator);
|
---|
97 | }
|
---|
98 | strBuilder.Remove(strBuilder.Length - CultureInfo.CurrentCulture.TextInfo.ListSeparator.Length, CultureInfo.CurrentCulture.TextInfo.ListSeparator.Length);
|
---|
99 | strBuilder.AppendLine();
|
---|
100 |
|
---|
101 | var dataset = instance.Dataset;
|
---|
102 |
|
---|
103 | for (int i = 0; i < dataset.Rows; i++) {
|
---|
104 | for (int j = 0; j < dataset.Columns; j++) {
|
---|
105 | if (j > 0) strBuilder.Append(CultureInfo.CurrentCulture.TextInfo.ListSeparator);
|
---|
106 | strBuilder.Append(dataset.GetValue(i, j));
|
---|
107 | }
|
---|
108 | strBuilder.AppendLine();
|
---|
109 | }
|
---|
110 |
|
---|
111 | using (var writer = new StreamWriter(path)) {
|
---|
112 | writer.Write(strBuilder);
|
---|
113 | }
|
---|
114 | }
|
---|
115 | }
|
---|
116 | }
|
---|