[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 |
|
---|
[13502] | 31 | private readonly PreprocessingContext context;
|
---|
[10310] | 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 |
|
---|
[13502] | 41 | public ProblemDataCreator(PreprocessingContext context) {
|
---|
[10310] | 42 | this.context = context;
|
---|
| 43 | }
|
---|
| 44 |
|
---|
[13502] | 45 | public IDataAnalysisProblemData CreateProblemData(IDataAnalysisProblemData oldProblemData) {
|
---|
[11098] | 46 | if (context.Data.Rows == 0 || context.Data.Columns == 0) return null;
|
---|
| 47 |
|
---|
[10990] | 48 | IDataAnalysisProblemData problemData;
|
---|
[10310] | 49 |
|
---|
[13508] | 50 | if (oldProblemData is TimeSeriesPrognosisProblemData) {
|
---|
| 51 | problemData = CreateTimeSeriesPrognosisData((TimeSeriesPrognosisProblemData)oldProblemData);
|
---|
| 52 | } else if (oldProblemData is RegressionProblemData) {
|
---|
[10695] | 53 | problemData = CreateRegressionData((RegressionProblemData)oldProblemData);
|
---|
[10536] | 54 | } else if (oldProblemData is ClassificationProblemData) {
|
---|
[10695] | 55 | problemData = CreateClassificationData((ClassificationProblemData)oldProblemData);
|
---|
[10536] | 56 | } else if (oldProblemData is ClusteringProblemData) {
|
---|
[10695] | 57 | problemData = CreateClusteringData((ClusteringProblemData)oldProblemData);
|
---|
[10536] | 58 | } else {
|
---|
| 59 | throw new NotImplementedException("The type of the DataAnalysisProblemData is not supported.");
|
---|
[10383] | 60 | }
|
---|
| 61 |
|
---|
[10536] | 62 | SetTrainingAndTestPartition(problemData);
|
---|
[11382] | 63 | // set the input variables to the correct checked state
|
---|
[12676] | 64 | var inputVariables = oldProblemData.InputVariables.ToDictionary(x => x.Value, x => x);
|
---|
| 65 | foreach (var variable in problemData.InputVariables) {
|
---|
[13252] | 66 | bool isChecked = inputVariables.ContainsKey(variable.Value) && oldProblemData.InputVariables.ItemChecked(inputVariables[variable.Value]);
|
---|
[12676] | 67 | problemData.InputVariables.SetItemCheckedState(variable, isChecked);
|
---|
[11382] | 68 | }
|
---|
[10536] | 69 |
|
---|
[10383] | 70 | return problemData;
|
---|
| 71 | }
|
---|
| 72 |
|
---|
[13508] | 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 |
|
---|
[10695] | 85 | private IDataAnalysisProblemData CreateRegressionData(RegressionProblemData oldProblemData) {
|
---|
[10536] | 86 | var targetVariable = oldProblemData.TargetVariable;
|
---|
[13252] | 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;
|
---|
[10536] | 92 | }
|
---|
[10310] | 93 |
|
---|
[10695] | 94 | private IDataAnalysisProblemData CreateClassificationData(ClassificationProblemData oldProblemData) {
|
---|
[10536] | 95 | var targetVariable = oldProblemData.TargetVariable;
|
---|
[13252] | 96 | if (!context.Data.VariableNames.Contains(targetVariable))
|
---|
| 97 | targetVariable = context.Data.VariableNames.First();
|
---|
| 98 | var inputVariables = GetDoubleInputVariables(targetVariable);
|
---|
[13508] | 99 | var newProblemData = new ClassificationProblemData(ExportedDataset, inputVariables, targetVariable, Transformations) {
|
---|
| 100 | PositiveClass = oldProblemData.PositiveClass
|
---|
| 101 | };
|
---|
[12676] | 102 | return newProblemData;
|
---|
[10536] | 103 | }
|
---|
[10383] | 104 |
|
---|
[10695] | 105 | private IDataAnalysisProblemData CreateClusteringData(ClusteringProblemData oldProblemData) {
|
---|
[10982] | 106 | return new ClusteringProblemData(ExportedDataset, GetDoubleInputVariables(String.Empty), Transformations);
|
---|
[10383] | 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 | }
|
---|
[10982] | 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);
|
---|
[11156] | 122 | if (context.Data.VariableHasType<double>(i)
|
---|
[10982] | 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 | }
|
---|
[10310] | 135 | }
|
---|
| 136 | }
|
---|