Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2906_Transformations/HeuristicLab.DataPreprocessing/3.4/ProblemDataCreator.cs @ 18066

Last change on this file since 18066 was 15885, checked in by pfleck, 7 years ago

#2906 Updated project references + small refactoring

File size: 6.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2018 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.Linq;
25using HeuristicLab.Problems.DataAnalysis;
26
27namespace HeuristicLab.DataPreprocessing {
28  public class ProblemDataCreator {
29    private readonly PreprocessingContext context;
30
31    private Dataset ExportedDataset {
32      get { return context.Data.ExportToDataset(); }
33    }
34
35    private IList<PreprocessingTransformation> Transformations {
36      get { return context.Data.Transformations; }
37    }
38
39    public ProblemDataCreator(PreprocessingContext context) {
40      this.context = context;
41    }
42
43    public IDataAnalysisProblemData CreateProblemData(IDataAnalysisProblemData oldProblemData) {
44      if (context.Data.Rows == 0 || context.Data.Columns == 0) return null;
45
46      IDataAnalysisProblemData problemData;
47      if (oldProblemData is TimeSeriesPrognosisProblemData) {
48        problemData = CreateTimeSeriesPrognosisData((TimeSeriesPrognosisProblemData)oldProblemData);
49      } else if (oldProblemData is RegressionProblemData) {
50        problemData = CreateRegressionData((RegressionProblemData)oldProblemData);
51      } else if (oldProblemData is ClassificationProblemData) {
52        problemData = CreateClassificationData((ClassificationProblemData)oldProblemData);
53      } else if (oldProblemData is ClusteringProblemData) {
54        problemData = CreateClusteringData((ClusteringProblemData)oldProblemData);
55      } else {
56        throw new NotImplementedException("The type of the DataAnalysisProblemData is not supported.");
57      }
58
59      SetTrainingAndTestPartition(problemData, context.Data);
60      SetAllowedInputVariables(problemData, oldProblemData);
61
62      return problemData;
63    }
64
65    private IDataAnalysisProblemData CreateTimeSeriesPrognosisData(TimeSeriesPrognosisProblemData oldProblemData) {
66      var targetVariable = oldProblemData.TargetVariable;
67      if (!context.Data.VariableNames.Contains(targetVariable))
68        targetVariable = context.Data.VariableNames.First();
69      var newProblemData = new TimeSeriesPrognosisProblemData(ExportedDataset, Enumerable.Empty<string>(), targetVariable, CreateDataAnalysisTransformation()) {
70        TrainingHorizon = oldProblemData.TrainingHorizon,
71        TestHorizon = oldProblemData.TestHorizon
72      };
73      return newProblemData;
74    }
75
76    private IDataAnalysisProblemData CreateRegressionData(RegressionProblemData oldProblemData) {
77      var targetVariable = DataAnalysisTransformation.GetStrictTransitiveVariables(oldProblemData.TargetVariable, CreateDataAnalysisTransformation(), false).Last();
78      if (!context.Data.VariableNames.Contains(targetVariable))
79        targetVariable = context.Data.VariableNames.First();
80      var newProblemData = new RegressionProblemData(ExportedDataset, Enumerable.Empty<string>(), targetVariable, CreateDataAnalysisTransformation());
81      return newProblemData;
82    }
83
84    private IDataAnalysisProblemData CreateClassificationData(ClassificationProblemData oldProblemData) {
85      var targetVariable = oldProblemData.TargetVariable;
86      if (!context.Data.VariableNames.Contains(targetVariable))
87        targetVariable = context.Data.VariableNames.First();
88      var newProblemData = new ClassificationProblemData(ExportedDataset, Enumerable.Empty<string>(), targetVariable, CreateDataAnalysisTransformation()) {
89        PositiveClass = oldProblemData.PositiveClass
90      };
91      return newProblemData;
92    }
93
94    private IDataAnalysisProblemData CreateClusteringData(ClusteringProblemData oldProblemData) {
95      return new ClusteringProblemData(ExportedDataset, Enumerable.Empty<string>(), CreateDataAnalysisTransformation());
96    }
97
98    private static void SetTrainingAndTestPartition(IDataAnalysisProblemData problemData, IPreprocessingData ppData) {
99      problemData.TrainingPartition.Start = ppData.TrainingPartition.Start;
100      problemData.TrainingPartition.End = ppData.TrainingPartition.End;
101      problemData.TestPartition.Start = ppData.TestPartition.Start;
102      problemData.TestPartition.End = ppData.TestPartition.End;
103    }
104
105    private static void SetAllowedInputVariables(IDataAnalysisProblemData problemData, IDataAnalysisProblemData oldProblemData) {
106      // original inputs + extended(transitive) inputs
107      var inputs = DataAnalysisTransformation.ExtendVariables(oldProblemData.AllowedInputVariables, problemData.Transformations).ToList();
108      foreach (var input in problemData.InputVariables) {
109        problemData.InputVariables.SetItemCheckedState(input, inputs.Contains(input.Value));
110      }
111
112      // new variables that were not created via transformations
113      var originalAndVirtualVariables = DataAnalysisTransformation.ExtendVariables(oldProblemData.Dataset.VariableNames, problemData.Transformations);
114      var newVariables = problemData.Dataset.VariableNames.Except(originalAndVirtualVariables).ToList();
115      foreach (var input in problemData.InputVariables) {
116        if (newVariables.Contains(input.Value))
117          problemData.InputVariables.SetItemCheckedState(input, true);
118      }
119    }
120
121    private IEnumerable<IDataAnalysisTransformation> CreateDataAnalysisTransformation() {
122      return Transformations.Select(x => new DataAnalysisTransformation(x.OriginalVariable, x.TransformedVariable, (ITransformation)x.Transformation.Clone()));
123    }
124  }
125}
Note: See TracBrowser for help on using the repository browser.