Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2708_ScopedAlgorithms/HeuristicLab.Problems.DataAnalysis.Trading/3.4/InstanceProviders/CsvProblemInstanceProvider.cs @ 17603

Last change on this file since 17603 was 12012, checked in by ascheibe, 10 years ago

#2212 merged r12008, r12009, r12010 back into trunk

File size: 3.6 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 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.Generic;
24using System.IO;
25using System.Linq;
26using HeuristicLab.Common;
27using HeuristicLab.Problems.Instances;
28using HeuristicLab.Problems.Instances.DataAnalysis;
29
30namespace HeuristicLab.Problems.DataAnalysis.Trading {
31  public class CsvProblemInstanceProvider : ProblemInstanceProvider<IProblemData> {
32    public override string Name {
33      get { return "CSV File"; }
34    }
35    public override string Description {
36      get {
37        return "Comma separated values file importer";
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    public override IProblemData LoadData(IDataDescriptor descriptor) {
51      throw new NotImplementedException();
52    }
53
54    public override bool CanImportData {
55      get { return true; }
56    }
57    public override IProblemData ImportData(string path) {
58      TableFileParser csvFileParser = new TableFileParser();
59      csvFileParser.Parse(path, csvFileParser.AreColumnNamesInFirstLine(path));
60
61      Dataset dataset = new Dataset(csvFileParser.VariableNames, csvFileParser.Values);
62      string targetVar = (from v in dataset.DoubleVariables
63                          where dataset.GetReadOnlyDoubleValues(v).Min() <= 0
64                          where dataset.GetReadOnlyDoubleValues(v).Max() >= 0
65                          select v).LastOrDefault();
66      if (targetVar == null) throw new ArgumentException("The target variable must contain changes (deltas) of the asset price over time.");
67
68      // turn off input variables that are constant in the training partition
69      var allowedInputVars = new List<string>();
70      var trainingIndizes = Enumerable.Range(0, (csvFileParser.Rows * 2) / 3);
71      if (trainingIndizes.Count() >= 2) {
72        foreach (var variableName in dataset.DoubleVariables) {
73          if (dataset.GetDoubleValues(variableName, trainingIndizes).Range() > 0)
74            allowedInputVars.Add(variableName);
75        }
76      } else {
77        allowedInputVars.AddRange(dataset.DoubleVariables);
78      }
79
80      IProblemData problemData = new ProblemData(dataset, allowedInputVars, targetVar);
81
82      var trainingPartEnd = trainingIndizes.Last();
83      problemData.TrainingPartition.Start = trainingIndizes.First();
84      problemData.TrainingPartition.End = trainingPartEnd;
85      problemData.TestPartition.Start = trainingPartEnd;
86      problemData.TestPartition.End = csvFileParser.Rows;
87
88      problemData.Name = Path.GetFileName(path);
89
90      return problemData;
91    }
92  }
93}
Note: See TracBrowser for help on using the repository browser.