Free cookie consent management tool by TermsFeed Policy Generator

source: branches/SimulationCore/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/DataAnalysisProblemData.cs @ 6619

Last change on this file since 6619 was 6581, checked in by mkommend, 13 years ago

#1596: Added AfterDeserializationHook in DataAnalysisProblemData.

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