Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/DataAnalysisProblemData.cs @ 14927

Last change on this file since 14927 was 14186, checked in by swagner, 8 years ago

#2526: Updated year of copyrights in license headers

File size: 9.9 KB
RevLine 
[5540]1#region License Information
2/* HeuristicLab
[14186]3 * Copyright (C) 2002-2016 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;
[11144]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";
[11159]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    }
[11159]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    }
[12702]65    public IDataset 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
[14022]82    public virtual IEnumerable<int> AllIndices {
83      get { return Enumerable.Range(0, Dataset.Rows); }
84    }
[8139]85    public virtual IEnumerable<int> TrainingIndices {
[5554]86      get {
[7265]87        return Enumerable.Range(TrainingPartition.Start, Math.Max(0, TrainingPartition.End - TrainingPartition.Start))
[6672]88                         .Where(IsTrainingSample);
[5540]89      }
90    }
[8139]91    public virtual IEnumerable<int> TestIndices {
[5554]92      get {
[7265]93        return Enumerable.Range(TestPartition.Start, Math.Max(0, TestPartition.End - TestPartition.Start))
[6672]94           .Where(IsTestSample);
[5554]95      }
96    }
[6672]97
[11159]98    public IEnumerable<ITransformation> Transformations {
99      get { return TransformationsParameter.Value; }
100    }
101
[6672]102    public virtual bool IsTrainingSample(int index) {
103      return index >= 0 && index < Dataset.Rows &&
104        TrainingPartition.Start <= index && index < TrainingPartition.End &&
105        (index < TestPartition.Start || TestPartition.End <= index);
106    }
107
108    public virtual bool IsTestSample(int index) {
109      return index >= 0 && index < Dataset.Rows &&
110             TestPartition.Start <= index && index < TestPartition.End;
111    }
[5540]112    #endregion
113
[6236]114    protected DataAnalysisProblemData(DataAnalysisProblemData original, Cloner cloner)
115      : base(original, cloner) {
[6666]116      isEmpty = original.isEmpty;
[6236]117      RegisterEventHandlers();
118    }
[5540]119    [StorableConstructor]
120    protected DataAnalysisProblemData(bool deserializing) : base(deserializing) { }
[8542]121
[6581]122    [StorableHook(HookType.AfterDeserialization)]
123    private void AfterDeserialization() {
[11159]124      if (!Parameters.ContainsKey(TransformationsParameterName)) {
125        Parameters.Add(new FixedValueParameter<ReadOnlyItemList<ITransformation>>(TransformationsParameterName, "", new ItemList<ITransformation>().AsReadOnly()));
126        TransformationsParameter.Hidden = true;
127      }
[6581]128      RegisterEventHandlers();
129    }
[5559]130
[12702]131    protected DataAnalysisProblemData(IDataset dataset, IEnumerable<string> allowedInputVariables, IEnumerable<ITransformation> transformations = null) {
[5559]132      if (dataset == null) throw new ArgumentNullException("The dataset must not be null.");
133      if (allowedInputVariables == null) throw new ArgumentNullException("The allowedInputVariables must not be null.");
134
[6740]135      if (allowedInputVariables.Except(dataset.DoubleVariables).Any())
136        throw new ArgumentException("All allowed input variables must be present in the dataset and of type double.");
[5554]137
[6740]138      var inputVariables = new CheckedItemList<StringValue>(dataset.DoubleVariables.Select(x => new StringValue(x)));
[5586]139      foreach (StringValue x in inputVariables)
140        inputVariables.SetItemCheckedState(x, allowedInputVariables.Contains(x.Value));
[5540]141
[5586]142      int trainingPartitionStart = 0;
143      int trainingPartitionEnd = dataset.Rows / 2;
144      int testPartitionStart = dataset.Rows / 2;
145      int testPartitionEnd = dataset.Rows;
[5540]146
[11159]147      var transformationsList = new ItemList<ITransformation>(transformations ?? Enumerable.Empty<ITransformation>());
148
[12702]149      Parameters.Add(new FixedValueParameter<Dataset>(DatasetParameterName, "", (Dataset)dataset));
[5847]150      Parameters.Add(new FixedValueParameter<ReadOnlyCheckedItemList<StringValue>>(InputVariablesParameterName, "", inputVariables.AsReadOnly()));
[5759]151      Parameters.Add(new FixedValueParameter<IntRange>(TrainingPartitionParameterName, "", new IntRange(trainingPartitionStart, trainingPartitionEnd)));
152      Parameters.Add(new FixedValueParameter<IntRange>(TestPartitionParameterName, "", new IntRange(testPartitionStart, testPartitionEnd)));
[11159]153      Parameters.Add(new FixedValueParameter<ReadOnlyItemList<ITransformation>>(TransformationsParameterName, "", transformationsList.AsReadOnly()));
[5586]154
[11159]155      TransformationsParameter.Hidden = true;
156
[5601]157      ((ValueParameter<Dataset>)DatasetParameter).ReactOnValueToStringChangedAndValueItemImageChanged = false;
[5586]158      RegisterEventHandlers();
[5540]159    }
[5542]160
[5586]161    private void RegisterEventHandlers() {
162      DatasetParameter.ValueChanged += new EventHandler(Parameter_ValueChanged);
[5649]163      InputVariables.CheckedItemsChanged += new CollectionItemsChangedEventHandler<IndexedItem<StringValue>>(InputVariables_CheckedItemsChanged);
[5759]164      TrainingPartition.ValueChanged += new EventHandler(Parameter_ValueChanged);
165      TestPartition.ValueChanged += new EventHandler(Parameter_ValueChanged);
[11159]166      TransformationsParameter.ValueChanged += new EventHandler(Parameter_ValueChanged);
[5540]167    }
168
[5649]169    private void InputVariables_CheckedItemsChanged(object sender, CollectionItemsChangedEventArgs<IndexedItem<StringValue>> e) {
[5586]170      OnChanged();
171    }
[5649]172
[5586]173    private void Parameter_ValueChanged(object sender, EventArgs e) {
174      OnChanged();
175    }
176
[5554]177    public event EventHandler Changed;
178    protected virtual void OnChanged() {
[5540]179      var listeners = Changed;
180      if (listeners != null) listeners(this, EventArgs.Empty);
181    }
[11144]182
183    protected virtual bool IsProblemDataCompatible(IDataAnalysisProblemData problemData, out string errorMessage) {
184      errorMessage = string.Empty;
185      if (problemData == null) throw new ArgumentNullException("problemData", "The provided problemData is null.");
186
187      //check allowed input variables
188      StringBuilder message = new StringBuilder();
189      var variables = new HashSet<string>(problemData.InputVariables.Select(x => x.Value));
190      foreach (var item in AllowedInputVariables) {
191        if (!variables.Contains(item))
192          message.AppendLine("Input variable '" + item + "' is not present in the new problem data.");
193      }
194
195      if (message.Length != 0) {
196        errorMessage = message.ToString();
197        return false;
198      }
199      return true;
200
201    }
202
203    public virtual void AdjustProblemDataProperties(IDataAnalysisProblemData problemData) {
204      DataAnalysisProblemData data = problemData as DataAnalysisProblemData;
205      if (data == null) throw new ArgumentException("The problem data is not a data analysis problem data. Instead a " + problemData.GetType().GetPrettyName() + " was provided.", "problemData");
206
207      string errorMessage;
208      if (!data.IsProblemDataCompatible(this, out errorMessage)) {
209        throw new InvalidOperationException(errorMessage);
210      }
211
212      foreach (var inputVariable in InputVariables) {
213        var variable = data.InputVariables.FirstOrDefault(i => i.Value == inputVariable.Value);
214        InputVariables.SetItemCheckedState(inputVariable, variable != null && data.InputVariables.ItemChecked(variable));
215      }
216
217      TrainingPartition.Start = TrainingPartition.End = 0;
218      TestPartition.Start = 0;
219      TestPartition.End = Dataset.Rows;
220    }
[5540]221  }
222}
Note: See TracBrowser for help on using the repository browser.