Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 16565 was 16565, checked in by gkronber, 6 years ago

#2520: merged changes from PersistenceOverhaul branch (r16451:16564) into trunk

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