Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DatasetFeatureCorrelation/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/DataAnalysisProblemData.cs @ 8276

Last change on this file since 8276 was 8276, checked in by sforsten, 12 years ago

#1292:

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