[5540] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
| 3 | * Copyright (C) 2002-2011 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 |
|
---|
| 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;
|
---|
[5540] | 30 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 31 |
|
---|
| 32 | namespace HeuristicLab.Problems.DataAnalysis {
|
---|
| 33 | [StorableClass]
|
---|
[5586] | 34 | public abstract class DataAnalysisProblemData : ParameterizedNamedItem, IDataAnalysisProblemData {
|
---|
| 35 | private const string DatasetParameterName = "Dataset";
|
---|
| 36 | private const string InputVariablesParameterName = "InputVariables";
|
---|
[5759] | 37 | private const string TrainingPartitionParameterName = "TrainingPartition";
|
---|
| 38 | private const string TestPartitionParameterName = "TestPartition";
|
---|
[5586] | 39 |
|
---|
| 40 | #region parameter properites
|
---|
[5601] | 41 | public IFixedValueParameter<Dataset> DatasetParameter {
|
---|
| 42 | get { return (IFixedValueParameter<Dataset>)Parameters[DatasetParameterName]; }
|
---|
[5586] | 43 | }
|
---|
[5847] | 44 | public IFixedValueParameter<ReadOnlyCheckedItemList<StringValue>> InputVariablesParameter {
|
---|
| 45 | get { return (IFixedValueParameter<ReadOnlyCheckedItemList<StringValue>>)Parameters[InputVariablesParameterName]; }
|
---|
[5586] | 46 | }
|
---|
[5759] | 47 | public IFixedValueParameter<IntRange> TrainingPartitionParameter {
|
---|
| 48 | get { return (IFixedValueParameter<IntRange>)Parameters[TrainingPartitionParameterName]; }
|
---|
[5586] | 49 | }
|
---|
[5759] | 50 | public IFixedValueParameter<IntRange> TestPartitionParameter {
|
---|
| 51 | get { return (IFixedValueParameter<IntRange>)Parameters[TestPartitionParameterName]; }
|
---|
[5586] | 52 | }
|
---|
| 53 | #endregion
|
---|
| 54 |
|
---|
[5540] | 55 | #region propeties
|
---|
| 56 | public Dataset Dataset {
|
---|
[5586] | 57 | get { return DatasetParameter.Value; }
|
---|
[5540] | 58 | }
|
---|
[5649] | 59 | public ICheckedItemList<StringValue> InputVariables {
|
---|
[5586] | 60 | get { return InputVariablesParameter.Value; }
|
---|
| 61 | }
|
---|
[5554] | 62 | public IEnumerable<string> AllowedInputVariables {
|
---|
[5649] | 63 | get { return InputVariables.CheckedItems.Select(x => x.Value.Value); }
|
---|
[5540] | 64 | }
|
---|
| 65 |
|
---|
[5759] | 66 | public IntRange TrainingPartition {
|
---|
| 67 | get { return TrainingPartitionParameter.Value; }
|
---|
[5540] | 68 | }
|
---|
[5759] | 69 | public IntRange TestPartition {
|
---|
| 70 | get { return TestPartitionParameter.Value; }
|
---|
[5540] | 71 | }
|
---|
[5554] | 72 |
|
---|
| 73 | public IEnumerable<int> TrainingIndizes {
|
---|
| 74 | get {
|
---|
[5759] | 75 | return Enumerable.Range(TrainingPartition.Start, TrainingPartition.End - TrainingPartition.Start)
|
---|
| 76 | .Where(i => i >= 0 && i < Dataset.Rows && (i < TestPartition.Start || TestPartition.End <= i));
|
---|
[5540] | 77 | }
|
---|
| 78 | }
|
---|
[5554] | 79 | public IEnumerable<int> TestIndizes {
|
---|
| 80 | get {
|
---|
[5759] | 81 | return Enumerable.Range(TestPartition.Start, TestPartition.End - TestPartition.Start)
|
---|
[5554] | 82 | .Where(i => i >= 0 && i < Dataset.Rows);
|
---|
| 83 | }
|
---|
| 84 | }
|
---|
[5540] | 85 | #endregion
|
---|
| 86 |
|
---|
[5559] | 87 | protected DataAnalysisProblemData(DataAnalysisProblemData original, Cloner cloner) : base(original, cloner) { }
|
---|
[5540] | 88 | [StorableConstructor]
|
---|
| 89 | protected DataAnalysisProblemData(bool deserializing) : base(deserializing) { }
|
---|
[5559] | 90 |
|
---|
[5554] | 91 | protected DataAnalysisProblemData(Dataset dataset, IEnumerable<string> allowedInputVariables) {
|
---|
[5559] | 92 | if (dataset == null) throw new ArgumentNullException("The dataset must not be null.");
|
---|
| 93 | if (allowedInputVariables == null) throw new ArgumentNullException("The allowedInputVariables must not be null.");
|
---|
| 94 |
|
---|
[5554] | 95 | if (allowedInputVariables.Except(dataset.VariableNames).Any())
|
---|
| 96 | throw new ArgumentException("All allowed input variables must be present in the dataset.");
|
---|
| 97 |
|
---|
[5649] | 98 | var inputVariables = new CheckedItemList<StringValue>(dataset.VariableNames.Select(x => new StringValue(x)));
|
---|
[5586] | 99 | foreach (StringValue x in inputVariables)
|
---|
| 100 | inputVariables.SetItemCheckedState(x, allowedInputVariables.Contains(x.Value));
|
---|
[5540] | 101 |
|
---|
[5586] | 102 | int trainingPartitionStart = 0;
|
---|
| 103 | int trainingPartitionEnd = dataset.Rows / 2;
|
---|
| 104 | int testPartitionStart = dataset.Rows / 2;
|
---|
| 105 | int testPartitionEnd = dataset.Rows;
|
---|
[5540] | 106 |
|
---|
[5601] | 107 | Parameters.Add(new FixedValueParameter<Dataset>(DatasetParameterName, "", dataset));
|
---|
[5847] | 108 | Parameters.Add(new FixedValueParameter<ReadOnlyCheckedItemList<StringValue>>(InputVariablesParameterName, "", inputVariables.AsReadOnly()));
|
---|
[5759] | 109 | Parameters.Add(new FixedValueParameter<IntRange>(TrainingPartitionParameterName, "", new IntRange(trainingPartitionStart, trainingPartitionEnd)));
|
---|
| 110 | Parameters.Add(new FixedValueParameter<IntRange>(TestPartitionParameterName, "", new IntRange(testPartitionStart, testPartitionEnd)));
|
---|
[5586] | 111 |
|
---|
[5601] | 112 | ((ValueParameter<Dataset>)DatasetParameter).ReactOnValueToStringChangedAndValueItemImageChanged = false;
|
---|
[5586] | 113 | RegisterEventHandlers();
|
---|
[5540] | 114 | }
|
---|
[5542] | 115 |
|
---|
[5586] | 116 | private void RegisterEventHandlers() {
|
---|
| 117 | DatasetParameter.ValueChanged += new EventHandler(Parameter_ValueChanged);
|
---|
[5649] | 118 | InputVariables.CheckedItemsChanged += new CollectionItemsChangedEventHandler<IndexedItem<StringValue>>(InputVariables_CheckedItemsChanged);
|
---|
[5759] | 119 | TrainingPartition.ValueChanged += new EventHandler(Parameter_ValueChanged);
|
---|
| 120 | TestPartition.ValueChanged += new EventHandler(Parameter_ValueChanged);
|
---|
[5540] | 121 | }
|
---|
| 122 |
|
---|
[5649] | 123 | private void InputVariables_CheckedItemsChanged(object sender, CollectionItemsChangedEventArgs<IndexedItem<StringValue>> e) {
|
---|
[5586] | 124 | OnChanged();
|
---|
| 125 | }
|
---|
[5649] | 126 |
|
---|
[5586] | 127 | private void Parameter_ValueChanged(object sender, EventArgs e) {
|
---|
| 128 | OnChanged();
|
---|
| 129 | }
|
---|
| 130 |
|
---|
[5554] | 131 | public event EventHandler Changed;
|
---|
| 132 | protected virtual void OnChanged() {
|
---|
[5540] | 133 | var listeners = Changed;
|
---|
| 134 | if (listeners != null) listeners(this, EventArgs.Empty);
|
---|
| 135 | }
|
---|
| 136 | }
|
---|
| 137 | }
|
---|