Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Problems.DataAnalysis.Trading/3.4/InstanceProviders/CsvProblemInstanceProvider.cs @ 17514

Last change on this file since 17514 was 17181, checked in by swagner, 5 years ago

#2875: Merged r17180 from trunk to stable

File size: 3.6 KB
RevLine 
[7860]1#region License Information
2/* HeuristicLab
[17181]3 * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[7860]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;
[8180]24using System.IO;
[8192]25using System.Linq;
[8566]26using HeuristicLab.Common;
[9989]27using HeuristicLab.Problems.Instances;
28using HeuristicLab.Problems.Instances.DataAnalysis;
[8192]29
[9989]30namespace HeuristicLab.Problems.DataAnalysis.Trading {
31  public class CsvProblemInstanceProvider : ProblemInstanceProvider<IProblemData> {
[7860]32    public override string Name {
[8211]33      get { return "CSV File"; }
[7860]34    }
35    public override string Description {
36      get {
[9989]37        return "Comma separated values file importer";
[7860]38      }
39    }
40    public override Uri WebLink {
[11335]41      get { return new Uri("http://dev.heuristiclab.com/trac.fcgi/wiki/Documentation/FAQ#DataAnalysisImportFileFormat"); }
[7860]42    }
43    public override string ReferencePublication {
44      get { return ""; }
45    }
46
[8192]47    public override IEnumerable<IDataDescriptor> GetDataDescriptors() {
48      return new List<IDataDescriptor>();
49    }
[9989]50    public override IProblemData LoadData(IDataDescriptor descriptor) {
[8192]51      throw new NotImplementedException();
52    }
53
54    public override bool CanImportData {
[8180]55      get { return true; }
56    }
[9989]57    public override IProblemData ImportData(string path) {
[8192]58      TableFileParser csvFileParser = new TableFileParser();
[9608]59      csvFileParser.Parse(path, csvFileParser.AreColumnNamesInFirstLine(path));
[8180]60
[8192]61      Dataset dataset = new Dataset(csvFileParser.VariableNames, csvFileParser.Values);
[9989]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.");
[8192]67
[9989]68      // turn off input variables that are constant in the training partition
[8566]69      var allowedInputVars = new List<string>();
70      var trainingIndizes = Enumerable.Range(0, (csvFileParser.Rows * 2) / 3);
[8877]71      if (trainingIndizes.Count() >= 2) {
72        foreach (var variableName in dataset.DoubleVariables) {
[9995]73          if (dataset.GetDoubleValues(variableName, trainingIndizes).Range() > 0)
[8877]74            allowedInputVars.Add(variableName);
75        }
76      } else {
[9995]77        allowedInputVars.AddRange(dataset.DoubleVariables);
[8566]78      }
[8192]79
[9989]80      IProblemData problemData = new ProblemData(dataset, allowedInputVars, targetVar);
[8192]81
[8566]82      var trainingPartEnd = trainingIndizes.Last();
[9989]83      problemData.TrainingPartition.Start = trainingIndizes.First();
84      problemData.TrainingPartition.End = trainingPartEnd;
85      problemData.TestPartition.Start = trainingPartEnd;
86      problemData.TestPartition.End = csvFileParser.Rows;
[8192]87
[9989]88      problemData.Name = Path.GetFileName(path);
[8566]89
[9989]90      return problemData;
[7860]91    }
92  }
93}
Note: See TracBrowser for help on using the repository browser.