Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Problems.Instances.DataAnalysis/3.3/Regression/CSV/RegressionCSVInstanceProvider.cs @ 17578

Last change on this file since 17578 was 17181, checked in by swagner, 5 years ago

#2875: Merged r17180 from trunk to stable

File size: 4.9 KB
RevLine 
[7860]1#region License Information
2/* HeuristicLab
[17181]3 * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[7860]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;
[8598]23using System.Collections;
[7860]24using System.Collections.Generic;
[8180]25using System.IO;
[8192]26using System.Linq;
[8566]27using HeuristicLab.Common;
[7860]28using HeuristicLab.Problems.DataAnalysis;
[8192]29
[7860]30namespace HeuristicLab.Problems.Instances.DataAnalysis {
31  public class RegressionCSVInstanceProvider : RegressionInstanceProvider {
32    public override string Name {
[8211]33      get { return "CSV File"; }
[7860]34    }
35    public override string Description {
36      get {
37        return "";
38      }
39    }
40    public override Uri WebLink {
[11335]41      get { return new Uri("http://dev.heuristiclab.com/trac.fcgi/wiki/Documentation/FAQ#DataAnalysisImportFileFormat"); }
[7860]42    }
43    public override string ReferencePublication {
44      get { return ""; }
45    }
46
[8192]47    public override IEnumerable<IDataDescriptor> GetDataDescriptors() {
48      return new List<IDataDescriptor>();
49    }
50    public override IRegressionProblemData LoadData(IDataDescriptor descriptor) {
51      throw new NotImplementedException();
52    }
53
54    public override bool CanImportData {
[8180]55      get { return true; }
56    }
[8192]57    public override IRegressionProblemData ImportData(string path) {
58      TableFileParser csvFileParser = new TableFileParser();
[9651]59      csvFileParser.Parse(path, csvFileParser.AreColumnNamesInFirstLine(path));
[8180]60
[8192]61      Dataset dataset = new Dataset(csvFileParser.VariableNames, csvFileParser.Values);
[8566]62      string targetVar = dataset.DoubleVariables.Last();
[8192]63
[10020]64      // turn off input variables that are constant in the training partition
[8566]65      var allowedInputVars = new List<string>();
66      var trainingIndizes = Enumerable.Range(0, (csvFileParser.Rows * 2) / 3);
[8877]67      if (trainingIndizes.Count() >= 2) {
68        foreach (var variableName in dataset.DoubleVariables) {
69          if (dataset.GetDoubleValues(variableName, trainingIndizes).Range() > 0 &&
70            variableName != targetVar)
71            allowedInputVars.Add(variableName);
72        }
73      } else {
74        allowedInputVars.AddRange(dataset.DoubleVariables.Where(x => !x.Equals(targetVar)));
[8566]75      }
[8192]76
[8566]77      IRegressionProblemData regressionData = new RegressionProblemData(dataset, allowedInputVars, targetVar);
[8192]78
[8566]79      var trainingPartEnd = trainingIndizes.Last();
80      regressionData.TrainingPartition.Start = trainingIndizes.First();
81      regressionData.TrainingPartition.End = trainingPartEnd;
82      regressionData.TestPartition.Start = trainingPartEnd;
83      regressionData.TestPartition.End = csvFileParser.Rows;
[8192]84
[8566]85      regressionData.Name = Path.GetFileName(path);
86
87      return regressionData;
[7860]88    }
89
[8877]90    protected override IRegressionProblemData ImportData(string path, RegressionImportType type, TableFileParser csvFileParser) {
[8598]91      List<IList> values = csvFileParser.Values;
92      if (type.Shuffle) {
93        values = Shuffle(values);
94      }
95      Dataset dataset = new Dataset(csvFileParser.VariableNames, values);
96
97      // turn of input variables that are constant in the training partition
98      var allowedInputVars = new List<string>();
[9021]99      int trainingPartEnd = (csvFileParser.Rows * type.TrainingPercentage) / 100;
[8601]100      trainingPartEnd = trainingPartEnd > 0 ? trainingPartEnd : 1;
[8599]101      var trainingIndizes = Enumerable.Range(0, trainingPartEnd);
[8601]102      if (trainingIndizes.Count() >= 2) {
103        foreach (var variableName in dataset.DoubleVariables) {
104          if (dataset.GetDoubleValues(variableName, trainingIndizes).Range() > 0 &&
[8877]105            variableName != type.TargetVariable)
[8601]106            allowedInputVars.Add(variableName);
107        }
108      } else {
[8877]109        allowedInputVars.AddRange(dataset.DoubleVariables.Where(x => !x.Equals(type.TargetVariable)));
[8598]110      }
111
[8877]112      RegressionProblemData regressionData = new RegressionProblemData(dataset, allowedInputVars, type.TargetVariable);
[8598]113
[8599]114      regressionData.TrainingPartition.Start = 0;
[8598]115      regressionData.TrainingPartition.End = trainingPartEnd;
116      regressionData.TestPartition.Start = trainingPartEnd;
117      regressionData.TestPartition.End = csvFileParser.Rows;
118
119      regressionData.Name = Path.GetFileName(path);
120
121      return regressionData;
122    }
[7860]123  }
124}
Note: See TracBrowser for help on using the repository browser.