[5540] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[17180] | 3 | * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[5540] | 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 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.Collections.Generic;
|
---|
| 24 | using System.Linq;
|
---|
[5586] | 25 | using HeuristicLab.Collections;
|
---|
[5540] | 26 | using HeuristicLab.Common;
|
---|
| 27 | using HeuristicLab.Core;
|
---|
[5586] | 28 | using HeuristicLab.Data;
|
---|
| 29 | using HeuristicLab.Parameters;
|
---|
[16565] | 30 | using HEAL.Attic;
|
---|
[5540] | 31 |
|
---|
| 32 | namespace HeuristicLab.Problems.DataAnalysis {
|
---|
[16565] | 33 | [StorableType("85AE1542-D563-434F-A760-1D181EFC2101")]
|
---|
[5586] | 34 | public abstract class DataAnalysisProblemData : ParameterizedNamedItem, IDataAnalysisProblemData {
|
---|
[6666] | 35 | protected const string DatasetParameterName = "Dataset";
|
---|
| 36 | protected const string InputVariablesParameterName = "InputVariables";
|
---|
| 37 | protected const string TrainingPartitionParameterName = "TrainingPartition";
|
---|
| 38 | protected const string TestPartitionParameterName = "TestPartition";
|
---|
[11114] | 39 | protected const string TransformationsParameterName = "Transformations";
|
---|
[5586] | 40 |
|
---|
| 41 | #region parameter properites
|
---|
[14507] | 42 | //mkommend: inserted parameter caching due to performance reasons
|
---|
| 43 | private IFixedValueParameter<Dataset> datasetParameter;
|
---|
[5601] | 44 | public IFixedValueParameter<Dataset> DatasetParameter {
|
---|
[14507] | 45 | get {
|
---|
| 46 | if (datasetParameter == null) datasetParameter = (IFixedValueParameter<Dataset>)Parameters[DatasetParameterName];
|
---|
| 47 | return datasetParameter;
|
---|
| 48 | }
|
---|
[5586] | 49 | }
|
---|
[14507] | 50 |
|
---|
| 51 | private IFixedValueParameter<ReadOnlyCheckedItemList<StringValue>> inputVariablesParameter;
|
---|
[5847] | 52 | public IFixedValueParameter<ReadOnlyCheckedItemList<StringValue>> InputVariablesParameter {
|
---|
[14507] | 53 | get {
|
---|
| 54 | if (inputVariablesParameter == null) inputVariablesParameter = (IFixedValueParameter<ReadOnlyCheckedItemList<StringValue>>)Parameters[InputVariablesParameterName];
|
---|
| 55 | return inputVariablesParameter;
|
---|
| 56 | }
|
---|
[5586] | 57 | }
|
---|
[14507] | 58 |
|
---|
| 59 | private IFixedValueParameter<IntRange> trainingPartitionParameter;
|
---|
[5759] | 60 | public IFixedValueParameter<IntRange> TrainingPartitionParameter {
|
---|
[14507] | 61 | get {
|
---|
| 62 | if (trainingPartitionParameter == null) trainingPartitionParameter = (IFixedValueParameter<IntRange>)Parameters[TrainingPartitionParameterName];
|
---|
| 63 | return trainingPartitionParameter;
|
---|
| 64 | }
|
---|
[5586] | 65 | }
|
---|
[14507] | 66 |
|
---|
| 67 | private IFixedValueParameter<IntRange> testPartitionParameter;
|
---|
[5759] | 68 | public IFixedValueParameter<IntRange> TestPartitionParameter {
|
---|
[14507] | 69 | get {
|
---|
| 70 | if (testPartitionParameter == null) testPartitionParameter = (IFixedValueParameter<IntRange>)Parameters[TestPartitionParameterName];
|
---|
| 71 | return testPartitionParameter;
|
---|
| 72 | }
|
---|
[5586] | 73 | }
|
---|
[14507] | 74 |
|
---|
[11114] | 75 | public IFixedValueParameter<ReadOnlyItemList<ITransformation>> TransformationsParameter {
|
---|
| 76 | get { return (IFixedValueParameter<ReadOnlyItemList<ITransformation>>)Parameters[TransformationsParameterName]; }
|
---|
| 77 | }
|
---|
[5586] | 78 | #endregion
|
---|
| 79 |
|
---|
[6666] | 80 | #region properties
|
---|
| 81 | protected bool isEmpty = false;
|
---|
| 82 | public bool IsEmpty {
|
---|
| 83 | get { return isEmpty; }
|
---|
| 84 | }
|
---|
[12509] | 85 | public IDataset Dataset {
|
---|
[5586] | 86 | get { return DatasetParameter.Value; }
|
---|
[5540] | 87 | }
|
---|
[5649] | 88 | public ICheckedItemList<StringValue> InputVariables {
|
---|
[5586] | 89 | get { return InputVariablesParameter.Value; }
|
---|
| 90 | }
|
---|
[5554] | 91 | public IEnumerable<string> AllowedInputVariables {
|
---|
[5649] | 92 | get { return InputVariables.CheckedItems.Select(x => x.Value.Value); }
|
---|
[5540] | 93 | }
|
---|
| 94 |
|
---|
[14843] | 95 | public double[,] AllowedInputsTrainingValues {
|
---|
| 96 | get { return Dataset.ToArray(AllowedInputVariables, TrainingIndices); }
|
---|
| 97 | }
|
---|
| 98 |
|
---|
| 99 | public double[,] AllowedInputsTestValues { get { return Dataset.ToArray(AllowedInputVariables, TestIndices); } }
|
---|
[5759] | 100 | public IntRange TrainingPartition {
|
---|
| 101 | get { return TrainingPartitionParameter.Value; }
|
---|
[5540] | 102 | }
|
---|
[5759] | 103 | public IntRange TestPartition {
|
---|
| 104 | get { return TestPartitionParameter.Value; }
|
---|
[5540] | 105 | }
|
---|
[5554] | 106 |
|
---|
[13766] | 107 | public virtual IEnumerable<int> AllIndices {
|
---|
| 108 | get { return Enumerable.Range(0, Dataset.Rows); }
|
---|
| 109 | }
|
---|
[8139] | 110 | public virtual IEnumerable<int> TrainingIndices {
|
---|
[5554] | 111 | get {
|
---|
[7265] | 112 | return Enumerable.Range(TrainingPartition.Start, Math.Max(0, TrainingPartition.End - TrainingPartition.Start))
|
---|
[6672] | 113 | .Where(IsTrainingSample);
|
---|
[5540] | 114 | }
|
---|
| 115 | }
|
---|
[8139] | 116 | public virtual IEnumerable<int> TestIndices {
|
---|
[5554] | 117 | get {
|
---|
[7265] | 118 | return Enumerable.Range(TestPartition.Start, Math.Max(0, TestPartition.End - TestPartition.Start))
|
---|
[6672] | 119 | .Where(IsTestSample);
|
---|
[5554] | 120 | }
|
---|
| 121 | }
|
---|
[6672] | 122 |
|
---|
[11114] | 123 | public IEnumerable<ITransformation> Transformations {
|
---|
| 124 | get { return TransformationsParameter.Value; }
|
---|
| 125 | }
|
---|
| 126 |
|
---|
[6672] | 127 | public virtual bool IsTrainingSample(int index) {
|
---|
| 128 | return index >= 0 && index < Dataset.Rows &&
|
---|
[14507] | 129 | TrainingPartition.Start <= index && index < TrainingPartition.End &&
|
---|
| 130 | (index < TestPartition.Start || TestPartition.End <= index);
|
---|
[6672] | 131 | }
|
---|
| 132 |
|
---|
| 133 | public virtual bool IsTestSample(int index) {
|
---|
| 134 | return index >= 0 && index < Dataset.Rows &&
|
---|
| 135 | TestPartition.Start <= index && index < TestPartition.End;
|
---|
| 136 | }
|
---|
[5540] | 137 | #endregion
|
---|
| 138 |
|
---|
[6236] | 139 | protected DataAnalysisProblemData(DataAnalysisProblemData original, Cloner cloner)
|
---|
| 140 | : base(original, cloner) {
|
---|
[6666] | 141 | isEmpty = original.isEmpty;
|
---|
[6236] | 142 | RegisterEventHandlers();
|
---|
| 143 | }
|
---|
[5540] | 144 | [StorableConstructor]
|
---|
[16565] | 145 | protected DataAnalysisProblemData(StorableConstructorFlag _) : base(_) { }
|
---|
[8542] | 146 |
|
---|
[6581] | 147 | [StorableHook(HookType.AfterDeserialization)]
|
---|
| 148 | private void AfterDeserialization() {
|
---|
[11114] | 149 | if (!Parameters.ContainsKey(TransformationsParameterName)) {
|
---|
| 150 | Parameters.Add(new FixedValueParameter<ReadOnlyItemList<ITransformation>>(TransformationsParameterName, "", new ItemList<ITransformation>().AsReadOnly()));
|
---|
| 151 | TransformationsParameter.Hidden = true;
|
---|
| 152 | }
|
---|
[6581] | 153 | RegisterEventHandlers();
|
---|
| 154 | }
|
---|
[5559] | 155 |
|
---|
[12509] | 156 | protected DataAnalysisProblemData(IDataset dataset, IEnumerable<string> allowedInputVariables, IEnumerable<ITransformation> transformations = null) {
|
---|
[5559] | 157 | if (dataset == null) throw new ArgumentNullException("The dataset must not be null.");
|
---|
[14826] | 158 | if (allowedInputVariables == null) throw new ArgumentNullException("The allowed input variables must not be null.");
|
---|
[5559] | 159 |
|
---|
[14826] | 160 | if (allowedInputVariables.Except(dataset.DoubleVariables).Except(dataset.StringVariables).Any())
|
---|
| 161 | throw new ArgumentException("All allowed input variables must be present in the dataset and of type double or string.");
|
---|
[5554] | 162 |
|
---|
[14826] | 163 | var variables = dataset.VariableNames.Where(variable => dataset.VariableHasType<double>(variable) || dataset.VariableHasType<string>(variable));
|
---|
[16059] | 164 | var inputVariables = new CheckedItemList<StringValue>(variables.Select(x => new StringValue(x).AsReadOnly()));
|
---|
[5586] | 165 | foreach (StringValue x in inputVariables)
|
---|
| 166 | inputVariables.SetItemCheckedState(x, allowedInputVariables.Contains(x.Value));
|
---|
[5540] | 167 |
|
---|
[5586] | 168 | int trainingPartitionStart = 0;
|
---|
| 169 | int trainingPartitionEnd = dataset.Rows / 2;
|
---|
| 170 | int testPartitionStart = dataset.Rows / 2;
|
---|
| 171 | int testPartitionEnd = dataset.Rows;
|
---|
[5540] | 172 |
|
---|
[11156] | 173 | var transformationsList = new ItemList<ITransformation>(transformations ?? Enumerable.Empty<ITransformation>());
|
---|
[11114] | 174 |
|
---|
[12509] | 175 | Parameters.Add(new FixedValueParameter<Dataset>(DatasetParameterName, "", (Dataset)dataset));
|
---|
[5847] | 176 | Parameters.Add(new FixedValueParameter<ReadOnlyCheckedItemList<StringValue>>(InputVariablesParameterName, "", inputVariables.AsReadOnly()));
|
---|
[5759] | 177 | Parameters.Add(new FixedValueParameter<IntRange>(TrainingPartitionParameterName, "", new IntRange(trainingPartitionStart, trainingPartitionEnd)));
|
---|
| 178 | Parameters.Add(new FixedValueParameter<IntRange>(TestPartitionParameterName, "", new IntRange(testPartitionStart, testPartitionEnd)));
|
---|
[11114] | 179 | Parameters.Add(new FixedValueParameter<ReadOnlyItemList<ITransformation>>(TransformationsParameterName, "", transformationsList.AsReadOnly()));
|
---|
[5586] | 180 |
|
---|
[11114] | 181 | TransformationsParameter.Hidden = true;
|
---|
| 182 |
|
---|
[5601] | 183 | ((ValueParameter<Dataset>)DatasetParameter).ReactOnValueToStringChangedAndValueItemImageChanged = false;
|
---|
[5586] | 184 | RegisterEventHandlers();
|
---|
[5540] | 185 | }
|
---|
[5542] | 186 |
|
---|
[5586] | 187 | private void RegisterEventHandlers() {
|
---|
| 188 | DatasetParameter.ValueChanged += new EventHandler(Parameter_ValueChanged);
|
---|
[5649] | 189 | InputVariables.CheckedItemsChanged += new CollectionItemsChangedEventHandler<IndexedItem<StringValue>>(InputVariables_CheckedItemsChanged);
|
---|
[5759] | 190 | TrainingPartition.ValueChanged += new EventHandler(Parameter_ValueChanged);
|
---|
| 191 | TestPartition.ValueChanged += new EventHandler(Parameter_ValueChanged);
|
---|
[11114] | 192 | TransformationsParameter.ValueChanged += new EventHandler(Parameter_ValueChanged);
|
---|
[5540] | 193 | }
|
---|
| 194 |
|
---|
[5649] | 195 | private void InputVariables_CheckedItemsChanged(object sender, CollectionItemsChangedEventArgs<IndexedItem<StringValue>> e) {
|
---|
[5586] | 196 | OnChanged();
|
---|
| 197 | }
|
---|
[5649] | 198 |
|
---|
[5586] | 199 | private void Parameter_ValueChanged(object sender, EventArgs e) {
|
---|
| 200 | OnChanged();
|
---|
| 201 | }
|
---|
| 202 |
|
---|
[5554] | 203 | public event EventHandler Changed;
|
---|
| 204 | protected virtual void OnChanged() {
|
---|
[5540] | 205 | var listeners = Changed;
|
---|
| 206 | if (listeners != null) listeners(this, EventArgs.Empty);
|
---|
| 207 | }
|
---|
| 208 | }
|
---|
| 209 | }
|
---|