Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DataPreprocessing/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/DataAnalysisProblemData.cs @ 10772

Last change on this file since 10772 was 10772, checked in by pfleck, 10 years ago
  • Current Transformations from GUI stored in TransformationContent.
  • Corrected namespace from Transformation interfaces.
File size: 8.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2013 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.Collections;
26using HeuristicLab.Common;
27using HeuristicLab.Core;
28using HeuristicLab.Data;
29using HeuristicLab.Parameters;
30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
31using HeuristicLab.Problems.DataAnalysis.Transformations;
32
33namespace HeuristicLab.Problems.DataAnalysis {
34  [StorableClass]
35  public abstract class DataAnalysisProblemData : ParameterizedNamedItem, IDataAnalysisProblemData {
36    protected const string DatasetParameterName = "Dataset";
37    protected const string InputVariablesParameterName = "InputVariables";
38    protected const string TrainingPartitionParameterName = "TrainingPartition";
39    protected const string TestPartitionParameterName = "TestPartition";
40    protected const string TransformationsParameterName = "Transformations";
41
42    #region parameter properites
43    public IFixedValueParameter<Dataset> DatasetParameter {
44      get { return (IFixedValueParameter<Dataset>)Parameters[DatasetParameterName]; }
45    }
46    public IFixedValueParameter<ReadOnlyCheckedItemList<StringValue>> InputVariablesParameter {
47      get { return (IFixedValueParameter<ReadOnlyCheckedItemList<StringValue>>)Parameters[InputVariablesParameterName]; }
48    }
49    public IFixedValueParameter<IntRange> TrainingPartitionParameter {
50      get { return (IFixedValueParameter<IntRange>)Parameters[TrainingPartitionParameterName]; }
51    }
52    public IFixedValueParameter<IntRange> TestPartitionParameter {
53      get { return (IFixedValueParameter<IntRange>)Parameters[TestPartitionParameterName]; }
54    }
55    public IFixedValueParameter<ReadOnlyItemCollection<ITransformation>> TransformationsParameter {
56      get { return (IFixedValueParameter<ReadOnlyItemCollection<ITransformation>>)Parameters[TransformationsParameterName]; }
57    }
58    #endregion
59
60    #region properties
61    protected bool isEmpty = false;
62    public bool IsEmpty {
63      get { return isEmpty; }
64    }
65    public Dataset Dataset {
66      get { return DatasetParameter.Value; }
67    }
68    public ICheckedItemList<StringValue> InputVariables {
69      get { return InputVariablesParameter.Value; }
70    }
71    public IEnumerable<string> AllowedInputVariables {
72      get { return InputVariables.CheckedItems.Select(x => x.Value.Value); }
73    }
74
75    public IntRange TrainingPartition {
76      get { return TrainingPartitionParameter.Value; }
77    }
78    public IntRange TestPartition {
79      get { return TestPartitionParameter.Value; }
80    }
81
82    public virtual IEnumerable<int> TrainingIndices {
83      get {
84        return Enumerable.Range(TrainingPartition.Start, Math.Max(0, TrainingPartition.End - TrainingPartition.Start))
85                         .Where(IsTrainingSample);
86      }
87    }
88    public virtual IEnumerable<int> TestIndices {
89      get {
90        return Enumerable.Range(TestPartition.Start, Math.Max(0, TestPartition.End - TestPartition.Start))
91           .Where(IsTestSample);
92      }
93    }
94
95    public IEnumerable<ITransformation> Transformations {
96      get { return TransformationsParameter.Value; }
97    }
98
99    public virtual bool IsTrainingSample(int index) {
100      return index >= 0 && index < Dataset.Rows &&
101        TrainingPartition.Start <= index && index < TrainingPartition.End &&
102        (index < TestPartition.Start || TestPartition.End <= index);
103    }
104
105    public virtual bool IsTestSample(int index) {
106      return index >= 0 && index < Dataset.Rows &&
107             TestPartition.Start <= index && index < TestPartition.End;
108    }
109    #endregion
110
111    protected DataAnalysisProblemData(DataAnalysisProblemData original, Cloner cloner)
112      : base(original, cloner) {
113      isEmpty = original.isEmpty;
114      RegisterEventHandlers();
115    }
116    [StorableConstructor]
117    protected DataAnalysisProblemData(bool deserializing) : base(deserializing) { }
118
119    [StorableHook(HookType.AfterDeserialization)]
120    private void AfterDeserialization() {
121      if (!Parameters.ContainsKey(TransformationsParameterName)) {
122        Parameters.Add(new FixedValueParameter<ReadOnlyItemCollection<ITransformation>>(TransformationsParameterName, "", new ItemCollection<ITransformation>().AsReadOnly()));
123      }
124      RegisterEventHandlers();
125    }
126
127    protected DataAnalysisProblemData(Dataset dataset, IEnumerable<string> allowedInputVariables, IEnumerable<ITransformation> transformations) {
128      if (dataset == null) throw new ArgumentNullException("The dataset must not be null.");
129      if (allowedInputVariables == null) throw new ArgumentNullException("The allowedInputVariables must not be null.");
130
131      if (allowedInputVariables.Except(dataset.DoubleVariables).Any())
132        throw new ArgumentException("All allowed input variables must be present in the dataset and of type double.");
133
134      if (transformations == null) throw new ArgumentNullException("The transformations must not be null.");
135
136      var inputVariables = new CheckedItemList<StringValue>(dataset.DoubleVariables.Select(x => new StringValue(x)));
137      foreach (StringValue x in inputVariables)
138        inputVariables.SetItemCheckedState(x, allowedInputVariables.Contains(x.Value));
139
140      int trainingPartitionStart = 0;
141      int trainingPartitionEnd = dataset.Rows / 2;
142      int testPartitionStart = dataset.Rows / 2;
143      int testPartitionEnd = dataset.Rows;
144
145      var transformationsCollection = new ItemCollection<ITransformation>(transformations);
146
147      Parameters.Add(new FixedValueParameter<Dataset>(DatasetParameterName, "", dataset));
148      Parameters.Add(new FixedValueParameter<ReadOnlyCheckedItemList<StringValue>>(InputVariablesParameterName, "", inputVariables.AsReadOnly()));
149      Parameters.Add(new FixedValueParameter<IntRange>(TrainingPartitionParameterName, "", new IntRange(trainingPartitionStart, trainingPartitionEnd)));
150      Parameters.Add(new FixedValueParameter<IntRange>(TestPartitionParameterName, "", new IntRange(testPartitionStart, testPartitionEnd)));
151      Parameters.Add(new FixedValueParameter<ReadOnlyItemCollection<ITransformation>>(TransformationsParameterName, "", transformationsCollection.AsReadOnly()));
152
153      ((ValueParameter<Dataset>)DatasetParameter).ReactOnValueToStringChangedAndValueItemImageChanged = false;
154      RegisterEventHandlers();
155    }
156
157    private void RegisterEventHandlers() {
158      DatasetParameter.ValueChanged += new EventHandler(Parameter_ValueChanged);
159      InputVariables.CheckedItemsChanged += new CollectionItemsChangedEventHandler<IndexedItem<StringValue>>(InputVariables_CheckedItemsChanged);
160      TrainingPartition.ValueChanged += new EventHandler(Parameter_ValueChanged);
161      TestPartition.ValueChanged += new EventHandler(Parameter_ValueChanged);
162      TransformationsParameter.ValueChanged += new EventHandler(Parameter_ValueChanged);
163    }
164
165    private void InputVariables_CheckedItemsChanged(object sender, CollectionItemsChangedEventArgs<IndexedItem<StringValue>> e) {
166      OnChanged();
167    }
168
169    private void Parameter_ValueChanged(object sender, EventArgs e) {
170      OnChanged();
171    }
172
173    public event EventHandler Changed;
174    protected virtual void OnChanged() {
175      var listeners = Changed;
176      if (listeners != null) listeners(this, EventArgs.Empty);
177    }
178  }
179}
Note: See TracBrowser for help on using the repository browser.