Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1081: Corrected time series interpreter and updated visualizations.

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