Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Problems.DataAnalysis.Trading/HeuristicLab.Problems.DataAnalysis.Trading/3.4/CSVInstanceProvider.cs @ 9176

Last change on this file since 9176 was 9176, checked in by gkronber, 11 years ago

#1508 updated trading plugin to work with current trunk version

File size: 5.0 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.IO;
26using System.Linq;
27using HeuristicLab.Common;
28using HeuristicLab.Problems.DataAnalysis;
29using HeuristicLab.Problems.Instances;
30using HeuristicLab.Problems.Instances.DataAnalysis;
31
32namespace HeuristicLab.Problems.DataAnalysis.Trading {
33  public class CSVInstanceProvider : IProblemInstanceProvider<ITradingProblemData> {
34    public string Name {
35      get { return "CSV File"; }
36    }
37    public string Description {
38      get {
39        return "";
40      }
41    }
42    public Uri WebLink {
43      get { return new Uri("http://dev.heuristiclab.com/trac/hl/core/wiki/UsersFAQ#DataAnalysisImportFileFormat"); }
44    }
45    public string ReferencePublication {
46      get { return ""; }
47    }
48
49    public IEnumerable<IDataDescriptor> GetDataDescriptors() {
50      return new List<IDataDescriptor>();
51    }
52    public ITradingProblemData LoadData(IDataDescriptor descriptor) {
53      throw new NotImplementedException();
54    }
55
56    public bool CanImportData {
57      get { return true; }
58    }
59    public ITradingProblemData 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      if (trainingIndizes.Count() >= 2) {
70        foreach (var variableName in dataset.DoubleVariables) {
71          if (dataset.GetDoubleValues(variableName, trainingIndizes).Range() > 0 &&
72            variableName != targetVar)
73            allowedInputVars.Add(variableName);
74        }
75      } else {
76        allowedInputVars.AddRange(dataset.DoubleVariables.Where(x => !x.Equals(targetVar)));
77      }
78
79      ITradingProblemData regressionData = new TradingProblemData(dataset, allowedInputVars, targetVar);
80
81      var trainingPartEnd = trainingIndizes.Last();
82      regressionData.TrainingPartition.Start = trainingIndizes.First();
83      regressionData.TrainingPartition.End = trainingPartEnd;
84      regressionData.TestPartition.Start = trainingPartEnd;
85      regressionData.TestPartition.End = csvFileParser.Rows;
86
87      regressionData.Name = Path.GetFileName(path);
88
89      return regressionData;
90    }
91
92    protected ITradingProblemData ImportData(string path, RegressionImportType type, TableFileParser csvFileParser) {
93      List<IList> values = csvFileParser.Values;
94      Dataset dataset = new Dataset(csvFileParser.VariableNames, values);
95
96      // turn of input variables that are constant in the training partition
97      var allowedInputVars = new List<string>();
98      int trainingPartEnd = (csvFileParser.Rows * type.TrainingPercentage) / 100;
99      trainingPartEnd = trainingPartEnd > 0 ? trainingPartEnd : 1;
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            variableName != type.TargetVariable)
105            allowedInputVars.Add(variableName);
106        }
107      } else {
108        allowedInputVars.AddRange(dataset.DoubleVariables.Where(x => !x.Equals(type.TargetVariable)));
109      }
110
111      ITradingProblemData regressionData = new TradingProblemData(dataset, allowedInputVars, type.TargetVariable);
112
113      regressionData.TrainingPartition.Start = 0;
114      regressionData.TrainingPartition.End = trainingPartEnd;
115      regressionData.TestPartition.Start = trainingPartEnd;
116      regressionData.TestPartition.End = csvFileParser.Rows;
117
118      regressionData.Name = Path.GetFileName(path);
119
120      return regressionData;
121    }
122    public bool CanExportData {
123      get { return false; }
124    }
125
126    public void ExportData(ITradingProblemData instance, string path) {
127      throw new NotImplementedException();
128    }
129  }
130}
Note: See TracBrowser for help on using the repository browser.