Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/DataAnalysisProblemData.cs @ 8174

Last change on this file since 8174 was 8139, checked in by mkommend, 12 years ago

#1722: Renamed indizes to indices in the whole trunk solution.

File size: 6.8 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
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 properties
56    protected bool isEmpty = false;
57    public bool IsEmpty {
58      get { return isEmpty; }
59    }
60    public Dataset Dataset {
61      get { return DatasetParameter.Value; }
62    }
63    public ICheckedItemList<StringValue> InputVariables {
64      get { return InputVariablesParameter.Value; }
65    }
66    public IEnumerable<string> AllowedInputVariables {
67      get { return InputVariables.CheckedItems.Select(x => x.Value.Value); }
68    }
69
70    public IntRange TrainingPartition {
71      get { return TrainingPartitionParameter.Value; }
72    }
73    public IntRange TestPartition {
74      get { return TestPartitionParameter.Value; }
75    }
76
77    public virtual IEnumerable<int> TrainingIndices {
78      get {
79        return Enumerable.Range(TrainingPartition.Start, Math.Max(0, TrainingPartition.End - TrainingPartition.Start))
80                         .Where(IsTrainingSample);
81      }
82    }
83    public virtual IEnumerable<int> TestIndices {
84      get {
85        return Enumerable.Range(TestPartition.Start, Math.Max(0, TestPartition.End - TestPartition.Start))
86           .Where(IsTestSample);
87      }
88    }
89
90    public virtual bool IsTrainingSample(int index) {
91      return index >= 0 && index < Dataset.Rows &&
92        TrainingPartition.Start <= index && index < TrainingPartition.End &&
93        (index < TestPartition.Start || TestPartition.End <= index);
94    }
95
96    public virtual bool IsTestSample(int index) {
97      return index >= 0 && index < Dataset.Rows &&
98             TestPartition.Start <= index && index < TestPartition.End;
99    }
100    #endregion
101
102    protected DataAnalysisProblemData(DataAnalysisProblemData original, Cloner cloner)
103      : base(original, cloner) {
104      isEmpty = original.isEmpty;
105      RegisterEventHandlers();
106    }
107    [StorableConstructor]
108    protected DataAnalysisProblemData(bool deserializing) : base(deserializing) { }
109    [StorableHook(HookType.AfterDeserialization)]
110    private void AfterDeserialization() {
111      RegisterEventHandlers();
112    }
113
114    protected DataAnalysisProblemData(Dataset dataset, IEnumerable<string> allowedInputVariables) {
115      if (dataset == null) throw new ArgumentNullException("The dataset must not be null.");
116      if (allowedInputVariables == null) throw new ArgumentNullException("The allowedInputVariables must not be null.");
117
118      if (allowedInputVariables.Except(dataset.DoubleVariables).Any())
119        throw new ArgumentException("All allowed input variables must be present in the dataset and of type double.");
120
121      var inputVariables = new CheckedItemList<StringValue>(dataset.DoubleVariables.Select(x => new StringValue(x)));
122      foreach (StringValue x in inputVariables)
123        inputVariables.SetItemCheckedState(x, allowedInputVariables.Contains(x.Value));
124
125      int trainingPartitionStart = 0;
126      int trainingPartitionEnd = dataset.Rows / 2;
127      int testPartitionStart = dataset.Rows / 2;
128      int testPartitionEnd = dataset.Rows;
129
130      Parameters.Add(new FixedValueParameter<Dataset>(DatasetParameterName, "", dataset));
131      Parameters.Add(new FixedValueParameter<ReadOnlyCheckedItemList<StringValue>>(InputVariablesParameterName, "", inputVariables.AsReadOnly()));
132      Parameters.Add(new FixedValueParameter<IntRange>(TrainingPartitionParameterName, "", new IntRange(trainingPartitionStart, trainingPartitionEnd)));
133      Parameters.Add(new FixedValueParameter<IntRange>(TestPartitionParameterName, "", new IntRange(testPartitionStart, testPartitionEnd)));
134
135      ((ValueParameter<Dataset>)DatasetParameter).ReactOnValueToStringChangedAndValueItemImageChanged = false;
136      RegisterEventHandlers();
137    }
138
139    private void RegisterEventHandlers() {
140      DatasetParameter.ValueChanged += new EventHandler(Parameter_ValueChanged);
141      InputVariables.CheckedItemsChanged += new CollectionItemsChangedEventHandler<IndexedItem<StringValue>>(InputVariables_CheckedItemsChanged);
142      TrainingPartition.ValueChanged += new EventHandler(Parameter_ValueChanged);
143      TestPartition.ValueChanged += new EventHandler(Parameter_ValueChanged);
144    }
145
146    private void InputVariables_CheckedItemsChanged(object sender, CollectionItemsChangedEventArgs<IndexedItem<StringValue>> e) {
147      OnChanged();
148    }
149
150    private void Parameter_ValueChanged(object sender, EventArgs e) {
151      OnChanged();
152    }
153
154    public event EventHandler Changed;
155    protected virtual void OnChanged() {
156      var listeners = Changed;
157      if (listeners != null) listeners(this, EventArgs.Empty);
158    }
159  }
160}
Note: See TracBrowser for help on using the repository browser.