Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.DataPreprocessing/3.4/ProblemDataCreator.cs @ 13508

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

#2559

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