Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 11414 was 11414, checked in by mkommend, 8 years ago

#2246: Merged r11382 into stable.

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