[7890] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[16565] | 3 | * Copyright (C) 2002-2019 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[7890] | 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 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.Collections.Generic;
|
---|
[8885] | 24 | using System.IO;
|
---|
| 25 | using System.Linq;
|
---|
| 26 | using HeuristicLab.Common;
|
---|
[7890] | 27 | using HeuristicLab.Problems.DataAnalysis;
|
---|
[8885] | 28 |
|
---|
[7890] | 29 | namespace HeuristicLab.Problems.Instances.DataAnalysis {
|
---|
| 30 | public class TimeSeriesPrognosisCSVInstanceProvider : TimeSeriesPrognosisInstanceProvider {
|
---|
| 31 | public override string Name {
|
---|
| 32 | get { return "CSV Problem Provider"; }
|
---|
| 33 | }
|
---|
| 34 | public override string Description {
|
---|
| 35 | get {
|
---|
| 36 | return "";
|
---|
| 37 | }
|
---|
| 38 | }
|
---|
| 39 | public override Uri WebLink {
|
---|
[11283] | 40 | get { return new Uri("http://dev.heuristiclab.com/trac.fcgi/wiki/Documentation/FAQ#DataAnalysisImportFileFormat"); }
|
---|
[7890] | 41 | }
|
---|
| 42 | public override string ReferencePublication {
|
---|
| 43 | get { return ""; }
|
---|
| 44 | }
|
---|
| 45 |
|
---|
| 46 | public override IEnumerable<IDataDescriptor> GetDataDescriptors() {
|
---|
| 47 | return new List<IDataDescriptor>();
|
---|
| 48 | }
|
---|
| 49 |
|
---|
| 50 | public override ITimeSeriesPrognosisProblemData LoadData(IDataDescriptor descriptor) {
|
---|
| 51 | throw new NotImplementedException();
|
---|
| 52 | }
|
---|
[8885] | 53 |
|
---|
| 54 | public override bool CanImportData { get { return true; } }
|
---|
| 55 |
|
---|
| 56 | public override ITimeSeriesPrognosisProblemData ImportData(string path) {
|
---|
| 57 | TableFileParser csvFileParser = new TableFileParser();
|
---|
[9608] | 58 | csvFileParser.Parse(path, csvFileParser.AreColumnNamesInFirstLine(path));
|
---|
[8885] | 59 |
|
---|
| 60 | Dataset dataset = new Dataset(csvFileParser.VariableNames, csvFileParser.Values);
|
---|
| 61 | string targetVar = csvFileParser.VariableNames.Last();
|
---|
| 62 |
|
---|
| 63 | IEnumerable<string> allowedInputVars = dataset.DoubleVariables.Where(x => !x.Equals(targetVar));
|
---|
| 64 |
|
---|
| 65 | ITimeSeriesPrognosisProblemData timeSeriesPrognosisData = new TimeSeriesPrognosisProblemData(dataset, allowedInputVars, targetVar);
|
---|
| 66 |
|
---|
| 67 | int trainingPartEnd = csvFileParser.Rows * 2 / 3;
|
---|
| 68 | timeSeriesPrognosisData.TrainingPartition.Start = 0;
|
---|
| 69 | timeSeriesPrognosisData.TrainingPartition.End = trainingPartEnd;
|
---|
| 70 | timeSeriesPrognosisData.TestPartition.Start = trainingPartEnd;
|
---|
| 71 | timeSeriesPrognosisData.TestPartition.End = csvFileParser.Rows;
|
---|
| 72 |
|
---|
| 73 | int pos = path.LastIndexOf('\\');
|
---|
| 74 | if (pos < 0)
|
---|
| 75 | timeSeriesPrognosisData.Name = path;
|
---|
| 76 | else {
|
---|
| 77 | pos++;
|
---|
| 78 | timeSeriesPrognosisData.Name = path.Substring(pos, path.Length - pos);
|
---|
| 79 | }
|
---|
| 80 | return timeSeriesPrognosisData;
|
---|
| 81 | }
|
---|
| 82 |
|
---|
| 83 | protected override ITimeSeriesPrognosisProblemData ImportData(string path, TimeSeriesPrognosisImportType type, TableFileParser csvFileParser) {
|
---|
| 84 | Dataset dataset = new Dataset(csvFileParser.VariableNames, csvFileParser.Values);
|
---|
| 85 |
|
---|
| 86 | // turn of input variables that are constant in the training partition
|
---|
| 87 | var allowedInputVars = new List<string>();
|
---|
[9021] | 88 | int trainingPartEnd = (csvFileParser.Rows * type.TrainingPercentage) / 100;
|
---|
[8885] | 89 | trainingPartEnd = trainingPartEnd > 0 ? trainingPartEnd : 1;
|
---|
| 90 | var trainingIndizes = Enumerable.Range(0, trainingPartEnd);
|
---|
| 91 | if (trainingIndizes.Count() >= 2) {
|
---|
| 92 | foreach (var variableName in dataset.DoubleVariables) {
|
---|
| 93 | if (dataset.GetDoubleValues(variableName, trainingIndizes).Range() > 0 &&
|
---|
| 94 | variableName != type.TargetVariable)
|
---|
| 95 | allowedInputVars.Add(variableName);
|
---|
| 96 | }
|
---|
| 97 | } else {
|
---|
| 98 | allowedInputVars.AddRange(dataset.DoubleVariables.Where(x => !x.Equals(type.TargetVariable)));
|
---|
| 99 | }
|
---|
| 100 |
|
---|
| 101 | TimeSeriesPrognosisProblemData timeSeriesPrognosisData = new TimeSeriesPrognosisProblemData(dataset, allowedInputVars, type.TargetVariable);
|
---|
| 102 |
|
---|
| 103 | timeSeriesPrognosisData.TrainingPartition.Start = 0;
|
---|
| 104 | timeSeriesPrognosisData.TrainingPartition.End = trainingPartEnd;
|
---|
| 105 | timeSeriesPrognosisData.TestPartition.Start = trainingPartEnd;
|
---|
| 106 | timeSeriesPrognosisData.TestPartition.End = csvFileParser.Rows;
|
---|
| 107 |
|
---|
| 108 | timeSeriesPrognosisData.Name = Path.GetFileName(path);
|
---|
| 109 |
|
---|
| 110 | return timeSeriesPrognosisData;
|
---|
| 111 | }
|
---|
[7890] | 112 | }
|
---|
| 113 | }
|
---|