Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 11114 was 11114, checked in by mkommend, 10 years ago

#2206: Merged data preprocessing into the trunk.

File size: 9.8 KB
RevLine 
[5540]1#region License Information
2/* HeuristicLab
[9456]3 * Copyright (C) 2002-2013 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[5540]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;
[10540]25using System.Text;
[5586]26using HeuristicLab.Collections;
[5540]27using HeuristicLab.Common;
28using HeuristicLab.Core;
[5586]29using HeuristicLab.Data;
30using HeuristicLab.Parameters;
[5540]31using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
32
33namespace HeuristicLab.Problems.DataAnalysis {
34  [StorableClass]
[5586]35  public abstract class DataAnalysisProblemData : ParameterizedNamedItem, IDataAnalysisProblemData {
[6666]36    protected const string DatasetParameterName = "Dataset";
37    protected const string InputVariablesParameterName = "InputVariables";
38    protected const string TrainingPartitionParameterName = "TrainingPartition";
39    protected const string TestPartitionParameterName = "TestPartition";
[11114]40    protected const string TransformationsParameterName = "Transformations";
[5586]41
42    #region parameter properites
[5601]43    public IFixedValueParameter<Dataset> DatasetParameter {
44      get { return (IFixedValueParameter<Dataset>)Parameters[DatasetParameterName]; }
[5586]45    }
[5847]46    public IFixedValueParameter<ReadOnlyCheckedItemList<StringValue>> InputVariablesParameter {
47      get { return (IFixedValueParameter<ReadOnlyCheckedItemList<StringValue>>)Parameters[InputVariablesParameterName]; }
[5586]48    }
[5759]49    public IFixedValueParameter<IntRange> TrainingPartitionParameter {
50      get { return (IFixedValueParameter<IntRange>)Parameters[TrainingPartitionParameterName]; }
[5586]51    }
[5759]52    public IFixedValueParameter<IntRange> TestPartitionParameter {
53      get { return (IFixedValueParameter<IntRange>)Parameters[TestPartitionParameterName]; }
[5586]54    }
[11114]55    public IFixedValueParameter<ReadOnlyItemList<ITransformation>> TransformationsParameter {
56      get { return (IFixedValueParameter<ReadOnlyItemList<ITransformation>>)Parameters[TransformationsParameterName]; }
57    }
[5586]58    #endregion
59
[6666]60    #region properties
61    protected bool isEmpty = false;
62    public bool IsEmpty {
63      get { return isEmpty; }
64    }
[5540]65    public Dataset Dataset {
[5586]66      get { return DatasetParameter.Value; }
[5540]67    }
[5649]68    public ICheckedItemList<StringValue> InputVariables {
[5586]69      get { return InputVariablesParameter.Value; }
70    }
[5554]71    public IEnumerable<string> AllowedInputVariables {
[5649]72      get { return InputVariables.CheckedItems.Select(x => x.Value.Value); }
[5540]73    }
74
[5759]75    public IntRange TrainingPartition {
76      get { return TrainingPartitionParameter.Value; }
[5540]77    }
[5759]78    public IntRange TestPartition {
79      get { return TestPartitionParameter.Value; }
[5540]80    }
[5554]81
[8139]82    public virtual IEnumerable<int> TrainingIndices {
[5554]83      get {
[7265]84        return Enumerable.Range(TrainingPartition.Start, Math.Max(0, TrainingPartition.End - TrainingPartition.Start))
[6672]85                         .Where(IsTrainingSample);
[5540]86      }
87    }
[8139]88    public virtual IEnumerable<int> TestIndices {
[5554]89      get {
[7265]90        return Enumerable.Range(TestPartition.Start, Math.Max(0, TestPartition.End - TestPartition.Start))
[6672]91           .Where(IsTestSample);
[5554]92      }
93    }
[6672]94
[11114]95    public IEnumerable<ITransformation> Transformations {
96      get { return TransformationsParameter.Value; }
97    }
98
[6672]99    public virtual bool IsTrainingSample(int index) {
100      return index >= 0 && index < Dataset.Rows &&
101        TrainingPartition.Start <= index && index < TrainingPartition.End &&
102        (index < TestPartition.Start || TestPartition.End <= index);
103    }
104
105    public virtual bool IsTestSample(int index) {
106      return index >= 0 && index < Dataset.Rows &&
107             TestPartition.Start <= index && index < TestPartition.End;
108    }
[5540]109    #endregion
110
[6236]111    protected DataAnalysisProblemData(DataAnalysisProblemData original, Cloner cloner)
112      : base(original, cloner) {
[6666]113      isEmpty = original.isEmpty;
[6236]114      RegisterEventHandlers();
115    }
[5540]116    [StorableConstructor]
117    protected DataAnalysisProblemData(bool deserializing) : base(deserializing) { }
[8542]118
[6581]119    [StorableHook(HookType.AfterDeserialization)]
120    private void AfterDeserialization() {
[11114]121      if (!Parameters.ContainsKey(TransformationsParameterName)) {
122        Parameters.Add(new FixedValueParameter<ReadOnlyItemList<ITransformation>>(TransformationsParameterName, "", new ItemList<ITransformation>().AsReadOnly()));
123        TransformationsParameter.Hidden = true;
124      }
[6581]125      RegisterEventHandlers();
126    }
[5559]127
[11114]128    protected DataAnalysisProblemData(Dataset dataset, IEnumerable<string> allowedInputVariables, IEnumerable<ITransformation> transformations) {
[5559]129      if (dataset == null) throw new ArgumentNullException("The dataset must not be null.");
130      if (allowedInputVariables == null) throw new ArgumentNullException("The allowedInputVariables must not be null.");
131
[6740]132      if (allowedInputVariables.Except(dataset.DoubleVariables).Any())
133        throw new ArgumentException("All allowed input variables must be present in the dataset and of type double.");
[5554]134
[11114]135      if (transformations == null) throw new ArgumentNullException("The transformations must not be null.");
136
[6740]137      var inputVariables = new CheckedItemList<StringValue>(dataset.DoubleVariables.Select(x => new StringValue(x)));
[5586]138      foreach (StringValue x in inputVariables)
139        inputVariables.SetItemCheckedState(x, allowedInputVariables.Contains(x.Value));
[5540]140
[5586]141      int trainingPartitionStart = 0;
142      int trainingPartitionEnd = dataset.Rows / 2;
143      int testPartitionStart = dataset.Rows / 2;
144      int testPartitionEnd = dataset.Rows;
[5540]145
[11114]146      var transformationsList = new ItemList<ITransformation>(transformations);
147
[5601]148      Parameters.Add(new FixedValueParameter<Dataset>(DatasetParameterName, "", dataset));
[5847]149      Parameters.Add(new FixedValueParameter<ReadOnlyCheckedItemList<StringValue>>(InputVariablesParameterName, "", inputVariables.AsReadOnly()));
[5759]150      Parameters.Add(new FixedValueParameter<IntRange>(TrainingPartitionParameterName, "", new IntRange(trainingPartitionStart, trainingPartitionEnd)));
151      Parameters.Add(new FixedValueParameter<IntRange>(TestPartitionParameterName, "", new IntRange(testPartitionStart, testPartitionEnd)));
[11114]152      Parameters.Add(new FixedValueParameter<ReadOnlyItemList<ITransformation>>(TransformationsParameterName, "", transformationsList.AsReadOnly()));
[5586]153
[11114]154      TransformationsParameter.Hidden = true;
155
[5601]156      ((ValueParameter<Dataset>)DatasetParameter).ReactOnValueToStringChangedAndValueItemImageChanged = false;
[5586]157      RegisterEventHandlers();
[5540]158    }
[5542]159
[5586]160    private void RegisterEventHandlers() {
161      DatasetParameter.ValueChanged += new EventHandler(Parameter_ValueChanged);
[5649]162      InputVariables.CheckedItemsChanged += new CollectionItemsChangedEventHandler<IndexedItem<StringValue>>(InputVariables_CheckedItemsChanged);
[5759]163      TrainingPartition.ValueChanged += new EventHandler(Parameter_ValueChanged);
164      TestPartition.ValueChanged += new EventHandler(Parameter_ValueChanged);
[11114]165      TransformationsParameter.ValueChanged += new EventHandler(Parameter_ValueChanged);
[5540]166    }
167
[5649]168    private void InputVariables_CheckedItemsChanged(object sender, CollectionItemsChangedEventArgs<IndexedItem<StringValue>> e) {
[5586]169      OnChanged();
170    }
[5649]171
[5586]172    private void Parameter_ValueChanged(object sender, EventArgs e) {
173      OnChanged();
174    }
175
[5554]176    public event EventHandler Changed;
177    protected virtual void OnChanged() {
[5540]178      var listeners = Changed;
179      if (listeners != null) listeners(this, EventArgs.Empty);
180    }
[10540]181
182    protected virtual bool IsProblemDataCompatible(IDataAnalysisProblemData problemData, out string errorMessage) {
183      errorMessage = string.Empty;
184      if (problemData == null) throw new ArgumentNullException("problemData", "The provided problemData is null.");
185
186      //check allowed input variables
187      StringBuilder message = new StringBuilder();
188      var variables = new HashSet<string>(problemData.InputVariables.Select(x => x.Value));
189      foreach (var item in AllowedInputVariables) {
190        if (!variables.Contains(item))
191          message.AppendLine("Input variable '" + item + "' is not present in the new problem data.");
192      }
193
194      if (message.Length != 0) {
195        errorMessage = message.ToString();
196        return false;
197      }
198      return true;
199
200    }
201
202    public virtual void AdjustProblemDataProperties(IDataAnalysisProblemData problemData) {
203      DataAnalysisProblemData data = problemData as DataAnalysisProblemData;
204      if (data == null) throw new ArgumentException("The problem data is not a data analysis problem data. Instead a " + problemData.GetType().GetPrettyName() + " was provided.", "problemData");
205
206      string errorMessage;
207      if (!data.IsProblemDataCompatible(this, out errorMessage)) {
208        throw new InvalidOperationException(errorMessage);
209      }
210
211      foreach (var inputVariable in InputVariables) {
212        var variable = data.InputVariables.FirstOrDefault(i => i.Value == inputVariable.Value);
213        InputVariables.SetItemCheckedState(inputVariable, variable != null && data.InputVariables.ItemChecked(variable));
214      }
215
216      TrainingPartition.Start = TrainingPartition.End = 0;
217      TestPartition.Start = 0;
218      TestPartition.End = Dataset.Rows;
219    }
[5540]220  }
221}
Note: See TracBrowser for help on using the repository browser.