[10310] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[12012] | 3 | * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[10310] | 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 |
|
---|
[10383] | 22 | using System;
|
---|
[10536] | 23 | using System.Collections.Generic;
|
---|
[11382] | 24 | using System.Linq;
|
---|
[10982] | 25 | using HeuristicLab.Common;
|
---|
[10310] | 26 | using HeuristicLab.Problems.DataAnalysis;
|
---|
| 27 |
|
---|
| 28 | namespace HeuristicLab.DataPreprocessing {
|
---|
[10908] | 29 | public class ProblemDataCreator {
|
---|
[10310] | 30 |
|
---|
| 31 | private readonly IPreprocessingContext context;
|
---|
| 32 |
|
---|
[10695] | 33 | private Dataset ExportedDataset {
|
---|
[11418] | 34 | get {
|
---|
| 35 | return context.Data.ExportToDataset();
|
---|
| 36 | }
|
---|
[10695] | 37 | }
|
---|
| 38 |
|
---|
[10922] | 39 | private IList<ITransformation> Transformations { get { return context.Data.Transformations; } }
|
---|
[10695] | 40 |
|
---|
[10383] | 41 | public ProblemDataCreator(IPreprocessingContext context) {
|
---|
[10310] | 42 | this.context = context;
|
---|
| 43 | }
|
---|
| 44 |
|
---|
[10383] | 45 | public IDataAnalysisProblemData CreateProblemData() {
|
---|
[11098] | 46 | if (context.Data.Rows == 0 || context.Data.Columns == 0) return null;
|
---|
| 47 |
|
---|
[10990] | 48 | var oldProblemData = context.ProblemData;
|
---|
| 49 | IDataAnalysisProblemData problemData;
|
---|
[10310] | 50 |
|
---|
[10536] | 51 | if (oldProblemData is RegressionProblemData) {
|
---|
[10695] | 52 | problemData = CreateRegressionData((RegressionProblemData)oldProblemData);
|
---|
[10536] | 53 | } else if (oldProblemData is ClassificationProblemData) {
|
---|
[10695] | 54 | problemData = CreateClassificationData((ClassificationProblemData)oldProblemData);
|
---|
[10536] | 55 | } else if (oldProblemData is ClusteringProblemData) {
|
---|
[10695] | 56 | problemData = CreateClusteringData((ClusteringProblemData)oldProblemData);
|
---|
[10536] | 57 | } else {
|
---|
| 58 | throw new NotImplementedException("The type of the DataAnalysisProblemData is not supported.");
|
---|
[10383] | 59 | }
|
---|
| 60 |
|
---|
[10536] | 61 | SetTrainingAndTestPartition(problemData);
|
---|
[11382] | 62 | // set the input variables to the correct checked state
|
---|
[12676] | 63 | var inputVariables = oldProblemData.InputVariables.ToDictionary(x => x.Value, x => x);
|
---|
| 64 | foreach (var variable in problemData.InputVariables) {
|
---|
[13252] | 65 | bool isChecked = inputVariables.ContainsKey(variable.Value) && oldProblemData.InputVariables.ItemChecked(inputVariables[variable.Value]);
|
---|
[12676] | 66 | problemData.InputVariables.SetItemCheckedState(variable, isChecked);
|
---|
[11382] | 67 | }
|
---|
[10536] | 68 |
|
---|
[10383] | 69 | return problemData;
|
---|
| 70 | }
|
---|
| 71 |
|
---|
[10695] | 72 | private IDataAnalysisProblemData CreateRegressionData(RegressionProblemData oldProblemData) {
|
---|
[10536] | 73 | var targetVariable = oldProblemData.TargetVariable;
|
---|
[13252] | 74 | if (!context.Data.VariableNames.Contains(targetVariable))
|
---|
| 75 | targetVariable = context.Data.VariableNames.First();
|
---|
| 76 | var inputVariables = GetDoubleInputVariables(targetVariable);
|
---|
| 77 | var newProblemData = new RegressionProblemData(ExportedDataset, inputVariables, targetVariable, Transformations);
|
---|
| 78 | return newProblemData;
|
---|
[10536] | 79 | }
|
---|
[10310] | 80 |
|
---|
[10695] | 81 | private IDataAnalysisProblemData CreateClassificationData(ClassificationProblemData oldProblemData) {
|
---|
[10536] | 82 | var targetVariable = oldProblemData.TargetVariable;
|
---|
[13252] | 83 | if (!context.Data.VariableNames.Contains(targetVariable))
|
---|
| 84 | targetVariable = context.Data.VariableNames.First();
|
---|
| 85 | var inputVariables = GetDoubleInputVariables(targetVariable);
|
---|
| 86 | var newProblemData = new ClassificationProblemData(ExportedDataset, inputVariables, targetVariable, Transformations);
|
---|
[12676] | 87 | newProblemData.PositiveClass = oldProblemData.PositiveClass;
|
---|
| 88 | return newProblemData;
|
---|
[10536] | 89 | }
|
---|
[10383] | 90 |
|
---|
[10695] | 91 | private IDataAnalysisProblemData CreateClusteringData(ClusteringProblemData oldProblemData) {
|
---|
[10982] | 92 | return new ClusteringProblemData(ExportedDataset, GetDoubleInputVariables(String.Empty), Transformations);
|
---|
[10383] | 93 | }
|
---|
| 94 |
|
---|
| 95 | private void SetTrainingAndTestPartition(IDataAnalysisProblemData problemData) {
|
---|
| 96 | var ppData = context.Data;
|
---|
| 97 |
|
---|
| 98 | problemData.TrainingPartition.Start = ppData.TrainingPartition.Start;
|
---|
| 99 | problemData.TrainingPartition.End = ppData.TrainingPartition.End;
|
---|
| 100 | problemData.TestPartition.Start = ppData.TestPartition.Start;
|
---|
| 101 | problemData.TestPartition.End = ppData.TestPartition.End;
|
---|
| 102 | }
|
---|
[10982] | 103 |
|
---|
| 104 | private IEnumerable<string> GetDoubleInputVariables(string targetVariable) {
|
---|
| 105 | var variableNames = new List<string>();
|
---|
| 106 | for (int i = 0; i < context.Data.Columns; ++i) {
|
---|
| 107 | var variableName = context.Data.GetVariableName(i);
|
---|
[11156] | 108 | if (context.Data.VariableHasType<double>(i)
|
---|
[10982] | 109 | && variableName != targetVariable
|
---|
| 110 | && IsNotConstantInputVariable(context.Data.GetValues<double>(i))) {
|
---|
| 111 |
|
---|
| 112 | variableNames.Add(variableName);
|
---|
| 113 | }
|
---|
| 114 | }
|
---|
| 115 | return variableNames;
|
---|
| 116 | }
|
---|
| 117 |
|
---|
| 118 | private bool IsNotConstantInputVariable(IList<double> list) {
|
---|
| 119 | return context.Data.TrainingPartition.End - context.Data.TrainingPartition.Start > 1 || list.Range() > 0;
|
---|
| 120 | }
|
---|
[10310] | 121 | }
|
---|
| 122 | }
|
---|