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