Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DataPreprocessing/HeuristicLab.DataPreprocessing/3.4/ProblemDataCreator.cs @ 11068

Last change on this file since 11068 was 11068, checked in by mkommend, 10 years ago

#2206: Removed seperate plugin for transformations and integrated them in HeuristicLab.Problems.DataAnalysis.

File size: 4.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2013 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 HeuristicLab.Common;
25using HeuristicLab.Problems.DataAnalysis;
26
27namespace HeuristicLab.DataPreprocessing {
28  public class ProblemDataCreator {
29
30    private readonly IPreprocessingContext context;
31
32    private Dataset ExportedDataset {
33      get { return exporteDataset ?? (exporteDataset = context.Data.ExportToDataset()); }
34    }
35    private Dataset exporteDataset;
36
37    private IList<ITransformation> Transformations { get { return context.Data.Transformations; } }
38
39    public ProblemDataCreator(IPreprocessingContext context) {
40      this.context = context;
41    }
42
43    public IDataAnalysisProblemData CreateProblemData() {
44      var oldProblemData = context.ProblemData;
45
46      IDataAnalysisProblemData problemData;
47
48      if (oldProblemData is RegressionProblemData) {
49        problemData = CreateRegressionData((RegressionProblemData)oldProblemData);
50      } else if (oldProblemData is ClassificationProblemData) {
51        problemData = CreateClassificationData((ClassificationProblemData)oldProblemData);
52      } else if (oldProblemData is ClusteringProblemData) {
53        problemData = CreateClusteringData((ClusteringProblemData)oldProblemData);
54      } else {
55        throw new NotImplementedException("The type of the DataAnalysisProblemData is not supported.");
56      }
57
58      SetTrainingAndTestPartition(problemData);
59
60      return problemData;
61    }
62
63    private IDataAnalysisProblemData CreateRegressionData(RegressionProblemData oldProblemData) {
64      var targetVariable = oldProblemData.TargetVariable;
65      // target variable must be double and must exist in the new dataset
66      return new RegressionProblemData(ExportedDataset, GetDoubleInputVariables(targetVariable), targetVariable, Transformations);
67    }
68
69    private IDataAnalysisProblemData CreateClassificationData(ClassificationProblemData oldProblemData) {
70      var targetVariable = oldProblemData.TargetVariable;
71      // target variable must be double and must exist in the new dataset
72      return new ClassificationProblemData(ExportedDataset, GetDoubleInputVariables(targetVariable), targetVariable, Transformations);
73    }
74
75    private IDataAnalysisProblemData CreateClusteringData(ClusteringProblemData oldProblemData) {
76      return new ClusteringProblemData(ExportedDataset, GetDoubleInputVariables(String.Empty), Transformations);
77    }
78
79    private void SetTrainingAndTestPartition(IDataAnalysisProblemData problemData) {
80      var ppData = context.Data;
81
82      problemData.TrainingPartition.Start = ppData.TrainingPartition.Start;
83      problemData.TrainingPartition.End = ppData.TrainingPartition.End;
84      problemData.TestPartition.Start = ppData.TestPartition.Start;
85      problemData.TestPartition.End = ppData.TestPartition.End;
86    }
87
88    private IEnumerable<string> GetDoubleInputVariables(string targetVariable) {
89      var variableNames = new List<string>();
90      for (int i = 0; i < context.Data.Columns; ++i) {
91        var variableName = context.Data.GetVariableName(i);
92        if (context.Data.IsType<double>(i)
93          && variableName != targetVariable
94          && IsNotConstantInputVariable(context.Data.GetValues<double>(i))) {
95
96          variableNames.Add(variableName);
97        }
98      }
99      return variableNames;
100    }
101
102    private bool IsNotConstantInputVariable(IList<double> list) {
103      return context.Data.TrainingPartition.End - context.Data.TrainingPartition.Start > 1 || list.Range() > 0;
104    }
105  }
106}
Note: See TracBrowser for help on using the repository browser.