Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2971_named_intervals/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/DataAnalysisProblemData.cs @ 16628

Last change on this file since 16628 was 16628, checked in by gkronber, 5 years ago

#2971: made branch compile with current version of trunk

File size: 9.4 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2018 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 System.Text;
26using HeuristicLab.Collections;
27using HeuristicLab.Common;
28using HeuristicLab.Core;
29using HeuristicLab.Data;
30using HeuristicLab.Parameters;
31using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
32using HEAL.Attic;
33
34namespace HeuristicLab.Problems.DataAnalysis {
35  [StorableType("7446B783-B2F0-4225-825B-23BD22592783")]
36  public abstract class DataAnalysisProblemData : ParameterizedNamedItem, IDataAnalysisProblemData {
37    protected const string DatasetParameterName = "Dataset";
38    protected const string InputVariablesParameterName = "InputVariables";
39    protected const string TrainingPartitionParameterName = "TrainingPartition";
40    protected const string TestPartitionParameterName = "TestPartition";
41    protected const string TransformationsParameterName = "Transformations";
42
43    #region parameter properites
44    //mkommend: inserted parameter caching due to performance reasons
45    private IFixedValueParameter<Dataset> datasetParameter;
46    public IFixedValueParameter<Dataset> DatasetParameter {
47      get {
48        if (datasetParameter == null) datasetParameter = (IFixedValueParameter<Dataset>)Parameters[DatasetParameterName];
49        return datasetParameter;
50      }
51    }
52
53    private IFixedValueParameter<ReadOnlyCheckedItemList<StringValue>> inputVariablesParameter;
54    public IFixedValueParameter<ReadOnlyCheckedItemList<StringValue>> InputVariablesParameter {
55      get {
56        if (inputVariablesParameter == null) inputVariablesParameter = (IFixedValueParameter<ReadOnlyCheckedItemList<StringValue>>)Parameters[InputVariablesParameterName];
57        return inputVariablesParameter;
58      }
59    }
60
61    private IFixedValueParameter<IntRange> trainingPartitionParameter;
62    public IFixedValueParameter<IntRange> TrainingPartitionParameter {
63      get {
64        if (trainingPartitionParameter == null) trainingPartitionParameter = (IFixedValueParameter<IntRange>)Parameters[TrainingPartitionParameterName];
65        return trainingPartitionParameter;
66      }
67    }
68
69    private IFixedValueParameter<IntRange> testPartitionParameter;
70    public IFixedValueParameter<IntRange> TestPartitionParameter {
71      get {
72        if (testPartitionParameter == null) testPartitionParameter = (IFixedValueParameter<IntRange>)Parameters[TestPartitionParameterName];
73        return testPartitionParameter;
74      }
75    }
76
77    public IFixedValueParameter<ReadOnlyItemList<ITransformation>> TransformationsParameter {
78      get { return (IFixedValueParameter<ReadOnlyItemList<ITransformation>>)Parameters[TransformationsParameterName]; }
79    }
80    #endregion
81
82    #region properties
83    protected bool isEmpty = false;
84    public bool IsEmpty {
85      get { return isEmpty; }
86    }
87    public IDataset Dataset {
88      get { return DatasetParameter.Value; }
89    }
90    public ICheckedItemList<StringValue> InputVariables {
91      get { return InputVariablesParameter.Value; }
92    }
93    public IEnumerable<string> AllowedInputVariables {
94      get { return InputVariables.CheckedItems.Select(x => x.Value.Value); }
95    }
96
97    public double[,] AllowedInputsTrainingValues {
98      get { return Dataset.ToArray(AllowedInputVariables, TrainingIndices); }
99    }
100
101    public double[,] AllowedInputsTestValues { get { return Dataset.ToArray(AllowedInputVariables, TestIndices); } }
102    public IntRange TrainingPartition {
103      get { return TrainingPartitionParameter.Value; }
104    }
105    public IntRange TestPartition {
106      get { return TestPartitionParameter.Value; }
107    }
108
109    public virtual IEnumerable<int> AllIndices {
110      get { return Enumerable.Range(0, Dataset.Rows); }
111    }
112    public virtual IEnumerable<int> TrainingIndices {
113      get {
114        return Enumerable.Range(TrainingPartition.Start, Math.Max(0, TrainingPartition.End - TrainingPartition.Start))
115                         .Where(IsTrainingSample);
116      }
117    }
118    public virtual IEnumerable<int> TestIndices {
119      get {
120        return Enumerable.Range(TestPartition.Start, Math.Max(0, TestPartition.End - TestPartition.Start))
121           .Where(IsTestSample);
122      }
123    }
124
125    public IEnumerable<ITransformation> Transformations {
126      get { return TransformationsParameter.Value; }
127    }
128
129    public virtual bool IsTrainingSample(int index) {
130      return index >= 0 && index < Dataset.Rows &&
131             TrainingPartition.Start <= index && index < TrainingPartition.End &&
132             (index < TestPartition.Start || TestPartition.End <= index);
133    }
134
135    public virtual bool IsTestSample(int index) {
136      return index >= 0 && index < Dataset.Rows &&
137             TestPartition.Start <= index && index < TestPartition.End;
138    }
139    #endregion
140
141    protected DataAnalysisProblemData(DataAnalysisProblemData original, Cloner cloner)
142      : base(original, cloner) {
143      isEmpty = original.isEmpty;
144      RegisterEventHandlers();
145    }
146    [StorableConstructor]
147    protected DataAnalysisProblemData(StorableConstructorFlag _) : base(_) { }
148
149    [StorableHook(HookType.AfterDeserialization)]
150    private void AfterDeserialization() {
151      if (!Parameters.ContainsKey(TransformationsParameterName)) {
152        Parameters.Add(new FixedValueParameter<ReadOnlyItemList<ITransformation>>(TransformationsParameterName, "", new ItemList<ITransformation>().AsReadOnly()));
153        TransformationsParameter.Hidden = true;
154      }
155      RegisterEventHandlers();
156    }
157
158    protected DataAnalysisProblemData(IDataset dataset, IEnumerable<string> allowedInputVariables, IEnumerable<ITransformation> transformations = null) {
159      if (dataset == null) throw new ArgumentNullException("The dataset must not be null.");
160      if (allowedInputVariables == null) throw new ArgumentNullException("The allowed input variables must not be null.");
161
162      if (allowedInputVariables.Except(dataset.DoubleVariables).Except(dataset.StringVariables).Any())
163        throw new ArgumentException("All allowed input variables must be present in the dataset and of type double or string.");
164
165      var variables = dataset.VariableNames.Where(variable => dataset.VariableHasType<double>(variable) || dataset.VariableHasType<string>(variable));
166      var inputVariables = new CheckedItemList<StringValue>(variables.Select(x => new StringValue(x).AsReadOnly()));
167      foreach (StringValue x in inputVariables)
168        inputVariables.SetItemCheckedState(x, allowedInputVariables.Contains(x.Value));
169
170      int trainingPartitionStart = 0;
171      int trainingPartitionEnd = dataset.Rows / 2;
172      int testPartitionStart = dataset.Rows / 2;
173      int testPartitionEnd = dataset.Rows;
174
175      var transformationsList = new ItemList<ITransformation>(transformations ?? Enumerable.Empty<ITransformation>());
176
177      Parameters.Add(new FixedValueParameter<Dataset>(DatasetParameterName, "", (Dataset)dataset));
178      Parameters.Add(new FixedValueParameter<ReadOnlyCheckedItemList<StringValue>>(InputVariablesParameterName, "", inputVariables.AsReadOnly()));
179      Parameters.Add(new FixedValueParameter<IntRange>(TrainingPartitionParameterName, "", new IntRange(trainingPartitionStart, trainingPartitionEnd)));
180      Parameters.Add(new FixedValueParameter<IntRange>(TestPartitionParameterName, "", new IntRange(testPartitionStart, testPartitionEnd)));
181      Parameters.Add(new FixedValueParameter<ReadOnlyItemList<ITransformation>>(TransformationsParameterName, "", transformationsList.AsReadOnly()));
182
183      TransformationsParameter.Hidden = true;
184
185      ((ValueParameter<Dataset>)DatasetParameter).ReactOnValueToStringChangedAndValueItemImageChanged = false;
186      RegisterEventHandlers();
187    }
188
189    private void RegisterEventHandlers() {
190      DatasetParameter.ValueChanged += new EventHandler(Parameter_ValueChanged);
191      InputVariables.CheckedItemsChanged += new CollectionItemsChangedEventHandler<IndexedItem<StringValue>>(InputVariables_CheckedItemsChanged);
192      TrainingPartition.ValueChanged += new EventHandler(Parameter_ValueChanged);
193      TestPartition.ValueChanged += new EventHandler(Parameter_ValueChanged);
194      TransformationsParameter.ValueChanged += new EventHandler(Parameter_ValueChanged);
195    }
196
197    private void InputVariables_CheckedItemsChanged(object sender, CollectionItemsChangedEventArgs<IndexedItem<StringValue>> e) {
198      OnChanged();
199    }
200
201    private void Parameter_ValueChanged(object sender, EventArgs e) {
202      OnChanged();
203    }
204
205    public event EventHandler Changed;
206    protected virtual void OnChanged() {
207      var listeners = Changed;
208      if (listeners != null) listeners(this, EventArgs.Empty);
209    }
210  }
211}
Note: See TracBrowser for help on using the repository browser.