Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.Instances.DataAnalysis/3.3/TimeSeries/TimeSeriesPrognosisInstanceProvider.cs @ 8798

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

#1081: Intermediate commit of trunk updates - interpreter changes must be redone.

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