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.IO;
|
---|
25 | using System.Linq;
|
---|
26 | using System.Text;
|
---|
27 | using HeuristicLab.Problems.DataAnalysis;
|
---|
28 |
|
---|
29 | namespace HeuristicLab.Problems.Instances.DataAnalysis {
|
---|
30 | public class RegressionCSVInstanceProvider : RegressionInstanceProvider {
|
---|
31 | public override string Name {
|
---|
32 | get { return "CSV File"; }
|
---|
33 | }
|
---|
34 | public override string Description {
|
---|
35 | get {
|
---|
36 | return "";
|
---|
37 | }
|
---|
38 | }
|
---|
39 | public override Uri WebLink {
|
---|
40 | get { return new Uri("http://dev.heuristiclab.com/trac/hl/core/wiki/UsersFAQ#DataAnalysisImportFileFormat"); }
|
---|
41 | }
|
---|
42 | public override string ReferencePublication {
|
---|
43 | get { return ""; }
|
---|
44 | }
|
---|
45 |
|
---|
46 | public override IEnumerable<IDataDescriptor> GetDataDescriptors() {
|
---|
47 | return new List<IDataDescriptor>();
|
---|
48 | }
|
---|
49 | public override IRegressionProblemData LoadData(IDataDescriptor descriptor) {
|
---|
50 | throw new NotImplementedException();
|
---|
51 | }
|
---|
52 |
|
---|
53 | public override bool CanImportData {
|
---|
54 | get { return true; }
|
---|
55 | }
|
---|
56 | public override IRegressionProblemData ImportData(string path) {
|
---|
57 | TableFileParser csvFileParser = new TableFileParser();
|
---|
58 | csvFileParser.Parse(path);
|
---|
59 |
|
---|
60 | Dataset dataset = new Dataset(csvFileParser.VariableNames, csvFileParser.Values);
|
---|
61 | string targetVar = csvFileParser.VariableNames.Where(x => dataset.DoubleVariables.Contains(x)).Last();
|
---|
62 |
|
---|
63 | IEnumerable<string> allowedInputVars = dataset.DoubleVariables.Where(x => !x.Equals(targetVar));
|
---|
64 |
|
---|
65 | IRegressionProblemData regData = new RegressionProblemData(dataset, allowedInputVars, targetVar);
|
---|
66 |
|
---|
67 | int trainingPartEnd = csvFileParser.Rows * 2 / 3;
|
---|
68 | regData.TrainingPartition.Start = 0;
|
---|
69 | regData.TrainingPartition.End = trainingPartEnd;
|
---|
70 | regData.TestPartition.Start = trainingPartEnd;
|
---|
71 | regData.TestPartition.End = csvFileParser.Rows;
|
---|
72 |
|
---|
73 | int pos = path.LastIndexOf('\\');
|
---|
74 | if (pos < 0)
|
---|
75 | regData.Name = path;
|
---|
76 | else {
|
---|
77 | pos++;
|
---|
78 | regData.Name = path.Substring(pos, path.Length - pos);
|
---|
79 | }
|
---|
80 | return regData;
|
---|
81 | }
|
---|
82 |
|
---|
83 | public override bool CanExportData {
|
---|
84 | get { return true; }
|
---|
85 | }
|
---|
86 | public override void ExportData(IRegressionProblemData instance, string path) {
|
---|
87 | StringBuilder strBuilder = new StringBuilder();
|
---|
88 |
|
---|
89 | foreach (var variable in instance.InputVariables) {
|
---|
90 | strBuilder.Append(variable + ";");
|
---|
91 | }
|
---|
92 | strBuilder.Remove(strBuilder.Length - 1, 1);
|
---|
93 | strBuilder.AppendLine();
|
---|
94 |
|
---|
95 | Dataset dataset = instance.Dataset;
|
---|
96 |
|
---|
97 | for (int i = 0; i < dataset.Rows; i++) {
|
---|
98 | for (int j = 0; j < dataset.Columns; j++) {
|
---|
99 | strBuilder.Append(dataset.GetValue(i, j) + ";");
|
---|
100 | }
|
---|
101 | strBuilder.Remove(strBuilder.Length - 1, 1);
|
---|
102 | strBuilder.AppendLine();
|
---|
103 | }
|
---|
104 |
|
---|
105 | using (StreamWriter writer = new StreamWriter(path)) {
|
---|
106 | writer.Write(strBuilder);
|
---|
107 | }
|
---|
108 | }
|
---|
109 | }
|
---|
110 | }
|
---|