Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.TimeSeries/HeuristicLab.Problems.Instances.DataAnalysis/3.3/TimeSeries/TimeSeriesPrognosisInstanceProvider.cs @ 7890

Last change on this file since 7890 was 7890, checked in by mkommend, 12 years ago

#1081: Implemented CSV problems instances provider for time series prognosis problems.

File size: 3.3 KB
RevLine 
[7849]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 System.Text;
28using HeuristicLab.Problems.DataAnalysis;
29
30namespace HeuristicLab.Problems.Instances.DataAnalysis {
[7890]31  public abstract class TimeSeriesPrognosisInstanceProvider : IProblemInstanceProvider<ITimeSeriesPrognosisProblemData> {
[7849]32
[7890]33    public ITimeSeriesPrognosisProblemData LoadData(string path) {
[7849]34      TableFileParser csvFileParser = new TableFileParser();
[7851]35      csvFileParser.Parse(path);
[7849]36
37      Dataset dataset = new Dataset(csvFileParser.VariableNames, csvFileParser.Values);
38      string targetVar = csvFileParser.VariableNames.Last();
[7860]39
[7849]40      IEnumerable<string> allowedInputVars = csvFileParser.VariableNames.Where(x => !x.Equals(targetVar));
41
[7890]42      ITimeSeriesPrognosisProblemData regData = new TimeSeriesPrognosisProblemData(dataset, allowedInputVars, new List<string> { targetVar });
[7849]43
44      int trainingPartEnd = csvFileParser.Rows * 2 / 3;
45      regData.TrainingPartition.Start = 0;
46      regData.TrainingPartition.End = trainingPartEnd;
47      regData.TestPartition.Start = trainingPartEnd;
48      regData.TestPartition.End = csvFileParser.Rows;
49
[7851]50      int pos = path.LastIndexOf('\\');
51      if (pos < 0)
52        regData.Name = path;
53      else {
54        pos++;
55        regData.Name = path.Substring(pos, path.Length - pos);
56      }
[7849]57      return regData;
58    }
59
[7890]60    public void SaveData(ITimeSeriesPrognosisProblemData instance, string path) {
[7849]61      StringBuilder strBuilder = new StringBuilder();
62
63      foreach (var variable in instance.InputVariables) {
64        strBuilder.Append(variable + ";");
65      }
66      strBuilder.Remove(strBuilder.Length - 1, 1);
67      strBuilder.AppendLine();
68
69      Dataset dataset = instance.Dataset;
70
71      for (int i = 0; i < dataset.Rows; i++) {
72        for (int j = 0; j < dataset.Columns; j++) {
73          strBuilder.Append(dataset.GetValue(i, j) + ";");
74        }
75        strBuilder.Remove(strBuilder.Length - 1, 1);
76        strBuilder.AppendLine();
77      }
78
79      using (StreamWriter writer = new StreamWriter(path)) {
80        writer.Write(strBuilder);
81      }
82    }
83
84    public abstract IEnumerable<IDataDescriptor> GetDataDescriptors();
[7890]85    public abstract ITimeSeriesPrognosisProblemData LoadData(IDataDescriptor descriptor);
[7849]86
87    public abstract string Name { get; }
88    public abstract string Description { get; }
89    public abstract Uri WebLink { get; }
90    public abstract string ReferencePublication { get; }
91  }
92}
Note: See TracBrowser for help on using the repository browser.