Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DataPreprocessing/HeuristicLab.Problems.Instances.DataAnalysis/3.3/TimeSeries/CSV/TimeSeriesPrognosisCSVInstanceProvider.cs @ 10187

Last change on this file since 10187 was 9608, checked in by sforsten, 11 years ago

#2070:

  • changed parse methods in TableFileParser to accept a bool which defines, if the first line contains variable names
  • added methods in TableFileParser to check if the first line contains variable names
  • adapted unit tests
  • adapted DataAnalysisImportTypeDialog so that a checkbox can be set to define if the first line contains variable names
  • added the flag NumberStyles.AllowTrailingSign for parsing doubles
File size: 4.6 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2013 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;
29
30namespace HeuristicLab.Problems.Instances.DataAnalysis {
31  public class TimeSeriesPrognosisCSVInstanceProvider : TimeSeriesPrognosisInstanceProvider {
32    public override string Name {
33      get { return "CSV Problem Provider"; }
34    }
35    public override string Description {
36      get {
37        return "";
38      }
39    }
40    public override Uri WebLink {
41      get { return new Uri("http://dev.heuristiclab.com/trac/hl/core/wiki/UsersFAQ#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
51    public override ITimeSeriesPrognosisProblemData LoadData(IDataDescriptor descriptor) {
52      throw new NotImplementedException();
53    }
54
55    public override bool CanImportData { get { return true; } }
56
57    public override ITimeSeriesPrognosisProblemData 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 = csvFileParser.VariableNames.Last();
63
64      IEnumerable<string> allowedInputVars = dataset.DoubleVariables.Where(x => !x.Equals(targetVar));
65
66      ITimeSeriesPrognosisProblemData timeSeriesPrognosisData = new TimeSeriesPrognosisProblemData(dataset, allowedInputVars, targetVar);
67
68      int trainingPartEnd = csvFileParser.Rows * 2 / 3;
69      timeSeriesPrognosisData.TrainingPartition.Start = 0;
70      timeSeriesPrognosisData.TrainingPartition.End = trainingPartEnd;
71      timeSeriesPrognosisData.TestPartition.Start = trainingPartEnd;
72      timeSeriesPrognosisData.TestPartition.End = csvFileParser.Rows;
73
74      int pos = path.LastIndexOf('\\');
75      if (pos < 0)
76        timeSeriesPrognosisData.Name = path;
77      else {
78        pos++;
79        timeSeriesPrognosisData.Name = path.Substring(pos, path.Length - pos);
80      }
81      return timeSeriesPrognosisData;
82    }
83
84    protected override ITimeSeriesPrognosisProblemData ImportData(string path, TimeSeriesPrognosisImportType type, TableFileParser csvFileParser) {
85      Dataset dataset = new Dataset(csvFileParser.VariableNames, csvFileParser.Values);
86
87      // turn of input variables that are constant in the training partition
88      var allowedInputVars = new List<string>();
89      int trainingPartEnd = (csvFileParser.Rows * type.TrainingPercentage) / 100;
90      trainingPartEnd = trainingPartEnd > 0 ? trainingPartEnd : 1;
91      var trainingIndizes = Enumerable.Range(0, trainingPartEnd);
92      if (trainingIndizes.Count() >= 2) {
93        foreach (var variableName in dataset.DoubleVariables) {
94          if (dataset.GetDoubleValues(variableName, trainingIndizes).Range() > 0 &&
95            variableName != type.TargetVariable)
96            allowedInputVars.Add(variableName);
97        }
98      } else {
99        allowedInputVars.AddRange(dataset.DoubleVariables.Where(x => !x.Equals(type.TargetVariable)));
100      }
101
102      TimeSeriesPrognosisProblemData timeSeriesPrognosisData = new TimeSeriesPrognosisProblemData(dataset, allowedInputVars, type.TargetVariable);
103
104      timeSeriesPrognosisData.TrainingPartition.Start = 0;
105      timeSeriesPrognosisData.TrainingPartition.End = trainingPartEnd;
106      timeSeriesPrognosisData.TestPartition.Start = trainingPartEnd;
107      timeSeriesPrognosisData.TestPartition.End = csvFileParser.Rows;
108
109      timeSeriesPrognosisData.Name = Path.GetFileName(path);
110
111      return timeSeriesPrognosisData;
112    }
113  }
114}
Note: See TracBrowser for help on using the repository browser.