Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.DataPreprocessing/3.4/ProblemDataCreator.cs @ 17460

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

#2875: Merged r17180 from trunk to stable

File size: 5.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 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.Common;
26using HeuristicLab.Problems.DataAnalysis;
27
28namespace HeuristicLab.DataPreprocessing {
29  public class ProblemDataCreator {
30    private readonly PreprocessingContext context;
31
32    private Dataset ExportedDataset {
33      get { return context.Data.ExportToDataset(); }
34    }
35
36    private IList<ITransformation> Transformations {
37      get { return context.Data.Transformations; }
38    }
39
40    public ProblemDataCreator(PreprocessingContext context) {
41      this.context = context;
42    }
43
44    public IDataAnalysisProblemData CreateProblemData(IDataAnalysisProblemData oldProblemData) {
45      if (context.Data.Rows == 0 || context.Data.Columns == 0) return null;
46
47      IDataAnalysisProblemData problemData;
48
49      if (oldProblemData is TimeSeriesPrognosisProblemData) {
50        problemData = CreateTimeSeriesPrognosisData((TimeSeriesPrognosisProblemData)oldProblemData);
51      } else if (oldProblemData is RegressionProblemData) {
52        problemData = CreateRegressionData((RegressionProblemData)oldProblemData);
53      } else if (oldProblemData is ClassificationProblemData) {
54        problemData = CreateClassificationData((ClassificationProblemData)oldProblemData);
55      } else if (oldProblemData is ClusteringProblemData) {
56        problemData = CreateClusteringData((ClusteringProblemData)oldProblemData);
57      } else {
58        throw new NotImplementedException("The type of the DataAnalysisProblemData is not supported.");
59      }
60
61      SetTrainingAndTestPartition(problemData);
62      // set the input variables to the correct checked state
63      var inputVariables = oldProblemData.InputVariables.ToDictionary(x => x.Value, x => x);
64      foreach (var variable in problemData.InputVariables) {
65        bool isChecked = inputVariables.ContainsKey(variable.Value) && oldProblemData.InputVariables.ItemChecked(inputVariables[variable.Value]);
66        problemData.InputVariables.SetItemCheckedState(variable, isChecked);
67      }
68
69      return problemData;
70    }
71
72    private IDataAnalysisProblemData CreateTimeSeriesPrognosisData(TimeSeriesPrognosisProblemData oldProblemData) {
73      var targetVariable = oldProblemData.TargetVariable;
74      if (!context.Data.VariableNames.Contains(targetVariable))
75        targetVariable = context.Data.VariableNames.First();
76      var inputVariables = GetDoubleInputVariables(targetVariable);
77      var newProblemData = new TimeSeriesPrognosisProblemData(ExportedDataset, inputVariables, targetVariable, Transformations) {
78        TrainingHorizon = oldProblemData.TrainingHorizon,
79        TestHorizon = oldProblemData.TestHorizon
80      };
81      return newProblemData;
82    }
83
84    private IDataAnalysisProblemData CreateRegressionData(RegressionProblemData oldProblemData) {
85      var targetVariable = oldProblemData.TargetVariable;
86      if (!context.Data.VariableNames.Contains(targetVariable))
87        targetVariable = context.Data.VariableNames.First();
88      var inputVariables = GetDoubleInputVariables(targetVariable);
89      var newProblemData = new RegressionProblemData(ExportedDataset, inputVariables, targetVariable, Transformations);
90      return newProblemData;
91    }
92
93    private IDataAnalysisProblemData CreateClassificationData(ClassificationProblemData oldProblemData) {
94      var targetVariable = oldProblemData.TargetVariable;
95      if (!context.Data.VariableNames.Contains(targetVariable))
96        targetVariable = context.Data.VariableNames.First();
97      var inputVariables = GetDoubleInputVariables(targetVariable);
98      var newProblemData = new ClassificationProblemData(ExportedDataset, inputVariables, targetVariable, Transformations) {
99        PositiveClass = oldProblemData.PositiveClass
100      };
101      return newProblemData;
102    }
103
104    private IDataAnalysisProblemData CreateClusteringData(ClusteringProblemData oldProblemData) {
105      return new ClusteringProblemData(ExportedDataset, GetDoubleInputVariables(String.Empty), Transformations);
106    }
107
108    private void SetTrainingAndTestPartition(IDataAnalysisProblemData problemData) {
109      var ppData = context.Data;
110
111      problemData.TrainingPartition.Start = ppData.TrainingPartition.Start;
112      problemData.TrainingPartition.End = ppData.TrainingPartition.End;
113      problemData.TestPartition.Start = ppData.TestPartition.Start;
114      problemData.TestPartition.End = ppData.TestPartition.End;
115    }
116
117    private IEnumerable<string> GetDoubleInputVariables(string targetVariable) {
118      var variableNames = new List<string>();
119      for (int i = 0; i < context.Data.Columns; ++i) {
120        var variableName = context.Data.GetVariableName(i);
121        if (context.Data.VariableHasType<double>(i)
122          && variableName != targetVariable
123          && IsNotConstantInputVariable(context.Data.GetValues<double>(i))) {
124
125          variableNames.Add(variableName);
126        }
127      }
128      return variableNames;
129    }
130
131    private bool IsNotConstantInputVariable(IList<double> list) {
132      return context.Data.TrainingPartition.End - context.Data.TrainingPartition.Start > 1 || list.Range() > 0;
133    }
134  }
135}
Note: See TracBrowser for help on using the repository browser.