Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DataPreprocessing/HeuristicLab.DataPreprocessing/3.3/ProblemDataCreator.cs @ 10383

Last change on this file since 10383 was 10383, checked in by pfleck, 10 years ago
  • Added ProblemDataCreator for instancing a new DataAnalysisProblemData with changed Dataset etc.
  • Added export functionality to PreprocessingContext. (cloned Algorithm or Problem)
  • Commented out code in StatisticsLogic which breaks the build. :(
File size: 4.0 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.Linq;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Data;
27using HeuristicLab.Problems.DataAnalysis;
28
29namespace HeuristicLab.DataPreprocessing {
30  internal class ProblemDataCreator {
31
32    private readonly IPreprocessingContext context;
33
34    public ProblemDataCreator(IPreprocessingContext context) {
35      this.context = context;
36    }
37
38    public IDataAnalysisProblemData CreateProblemData() {
39
40      IDataAnalysisProblemData problemData = CloneProblemDataWithDataset();
41
42      SetTrainingAndTestPartition(problemData);
43
44      if (problemData is RegressionProblemData) {
45        //SetRegressionData((RegressionProblemData)problemData);
46      } else if (problemData is ClassificationProblemData) {
47        //SetClassificationData((ClassificationProblemData)problemData);
48      } else if (problemData is ClusteringProblemData) {
49        throw new NotImplementedException();
50      }
51
52      return problemData;
53    }
54
55    private IDataAnalysisProblemData CloneProblemDataWithDataset() {
56      var cloner = new Cloner();
57
58      var problemData = context.Problem.ProblemData;
59
60      Dataset dataset = context.Data.ExportToDataset();
61
62      cloner.RegisterClonedObject(problemData.Dataset, dataset);
63
64      return cloner.Clone(problemData);
65    }
66
67    private void SetTrainingAndTestPartition(IDataAnalysisProblemData problemData) {
68      var ppData = context.Data;
69
70      problemData.TrainingPartition.Start = ppData.TrainingPartition.Start;
71      problemData.TrainingPartition.End = ppData.TrainingPartition.End;
72      problemData.TestPartition.Start = ppData.TestPartition.Start;
73      problemData.TestPartition.End = ppData.TestPartition.End;
74    }
75
76    private void SetRegressionData(RegressionProblemData regressionProblemData) {
77      SetInputVariables(regressionProblemData, regressionProblemData.TargetVariable);
78      SetTargetVariable(regressionProblemData.TargetVariableParameter);
79    }
80
81    private void SetClassificationData(ClassificationProblemData classificationProblemData) {
82      SetInputVariables(classificationProblemData, classificationProblemData.TargetVariable);
83      SetTargetVariable(classificationProblemData.TargetVariableParameter);
84    }
85
86    private void SetInputVariables(DataAnalysisProblemData problemData, string targetVariable) {
87      //TODO: InputVariables Set is Readonly
88
89      problemData.InputVariables.Clear();
90
91      foreach (string variable in context.Data.VariableNames) {
92        problemData.InputVariables.Add(new StringValue(variable), variable != targetVariable);
93      }
94    }
95
96    private void SetTargetVariable(IConstrainedValueParameter<StringValue> targetVariableParameter) {
97      string oldTarget = targetVariableParameter.Value.Value;
98
99      var validValues = targetVariableParameter.ValidValues;
100      validValues.Clear();
101
102      foreach (string variable in context.Data.VariableNames.Where(x => context.Data.IsType<double>(x))) {
103        validValues.Add(new StringValue(variable));
104      }
105
106      targetVariableParameter.ActualValue = validValues.FirstOrDefault(v => v.Value == oldTarget);
107    }
108  }
109}
Note: See TracBrowser for help on using the repository browser.