Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 10635 was 10540, checked in by mkommend, 11 years ago

#1758: Reimplemented functionality to load new problem data to data analysis solution and redesigned the according views.

  • Added setter for the target variable of regression and classification problem data.
  • Added functionality to check the compatibility of problem data.
  • Added functionality to adjust the properties of a problem data.
  • Added flowLayoutPanel with according buttons for loading a new problem data, simplifying and exporting data analysis solutions.
  • TradingProblemData currently throws a NotSupportedException when the properties should be adjusted.
File size: 8.6 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";
[5586]40
41    #region parameter properites
[5601]42    public IFixedValueParameter<Dataset> DatasetParameter {
43      get { return (IFixedValueParameter<Dataset>)Parameters[DatasetParameterName]; }
[5586]44    }
[5847]45    public IFixedValueParameter<ReadOnlyCheckedItemList<StringValue>> InputVariablesParameter {
46      get { return (IFixedValueParameter<ReadOnlyCheckedItemList<StringValue>>)Parameters[InputVariablesParameterName]; }
[5586]47    }
[5759]48    public IFixedValueParameter<IntRange> TrainingPartitionParameter {
49      get { return (IFixedValueParameter<IntRange>)Parameters[TrainingPartitionParameterName]; }
[5586]50    }
[5759]51    public IFixedValueParameter<IntRange> TestPartitionParameter {
52      get { return (IFixedValueParameter<IntRange>)Parameters[TestPartitionParameterName]; }
[5586]53    }
54    #endregion
55
[6666]56    #region properties
57    protected bool isEmpty = false;
58    public bool IsEmpty {
59      get { return isEmpty; }
60    }
[5540]61    public Dataset Dataset {
[5586]62      get { return DatasetParameter.Value; }
[5540]63    }
[5649]64    public ICheckedItemList<StringValue> InputVariables {
[5586]65      get { return InputVariablesParameter.Value; }
66    }
[5554]67    public IEnumerable<string> AllowedInputVariables {
[5649]68      get { return InputVariables.CheckedItems.Select(x => x.Value.Value); }
[5540]69    }
70
[5759]71    public IntRange TrainingPartition {
72      get { return TrainingPartitionParameter.Value; }
[5540]73    }
[5759]74    public IntRange TestPartition {
75      get { return TestPartitionParameter.Value; }
[5540]76    }
[5554]77
[8139]78    public virtual IEnumerable<int> TrainingIndices {
[5554]79      get {
[7265]80        return Enumerable.Range(TrainingPartition.Start, Math.Max(0, TrainingPartition.End - TrainingPartition.Start))
[6672]81                         .Where(IsTrainingSample);
[5540]82      }
83    }
[8139]84    public virtual IEnumerable<int> TestIndices {
[5554]85      get {
[7265]86        return Enumerable.Range(TestPartition.Start, Math.Max(0, TestPartition.End - TestPartition.Start))
[6672]87           .Where(IsTestSample);
[5554]88      }
89    }
[6672]90
91    public virtual bool IsTrainingSample(int index) {
92      return index >= 0 && index < Dataset.Rows &&
93        TrainingPartition.Start <= index && index < TrainingPartition.End &&
94        (index < TestPartition.Start || TestPartition.End <= index);
95    }
96
97    public virtual bool IsTestSample(int index) {
98      return index >= 0 && index < Dataset.Rows &&
99             TestPartition.Start <= index && index < TestPartition.End;
100    }
[5540]101    #endregion
102
[6236]103    protected DataAnalysisProblemData(DataAnalysisProblemData original, Cloner cloner)
104      : base(original, cloner) {
[6666]105      isEmpty = original.isEmpty;
[6236]106      RegisterEventHandlers();
107    }
[5540]108    [StorableConstructor]
109    protected DataAnalysisProblemData(bool deserializing) : base(deserializing) { }
[8542]110
[6581]111    [StorableHook(HookType.AfterDeserialization)]
112    private void AfterDeserialization() {
113      RegisterEventHandlers();
114    }
[5559]115
[5554]116    protected DataAnalysisProblemData(Dataset dataset, IEnumerable<string> allowedInputVariables) {
[5559]117      if (dataset == null) throw new ArgumentNullException("The dataset must not be null.");
118      if (allowedInputVariables == null) throw new ArgumentNullException("The allowedInputVariables must not be null.");
119
[6740]120      if (allowedInputVariables.Except(dataset.DoubleVariables).Any())
121        throw new ArgumentException("All allowed input variables must be present in the dataset and of type double.");
[5554]122
[6740]123      var inputVariables = new CheckedItemList<StringValue>(dataset.DoubleVariables.Select(x => new StringValue(x)));
[5586]124      foreach (StringValue x in inputVariables)
125        inputVariables.SetItemCheckedState(x, allowedInputVariables.Contains(x.Value));
[5540]126
[5586]127      int trainingPartitionStart = 0;
128      int trainingPartitionEnd = dataset.Rows / 2;
129      int testPartitionStart = dataset.Rows / 2;
130      int testPartitionEnd = dataset.Rows;
[5540]131
[5601]132      Parameters.Add(new FixedValueParameter<Dataset>(DatasetParameterName, "", dataset));
[5847]133      Parameters.Add(new FixedValueParameter<ReadOnlyCheckedItemList<StringValue>>(InputVariablesParameterName, "", inputVariables.AsReadOnly()));
[5759]134      Parameters.Add(new FixedValueParameter<IntRange>(TrainingPartitionParameterName, "", new IntRange(trainingPartitionStart, trainingPartitionEnd)));
135      Parameters.Add(new FixedValueParameter<IntRange>(TestPartitionParameterName, "", new IntRange(testPartitionStart, testPartitionEnd)));
[5586]136
[5601]137      ((ValueParameter<Dataset>)DatasetParameter).ReactOnValueToStringChangedAndValueItemImageChanged = false;
[5586]138      RegisterEventHandlers();
[5540]139    }
[5542]140
[5586]141    private void RegisterEventHandlers() {
142      DatasetParameter.ValueChanged += new EventHandler(Parameter_ValueChanged);
[5649]143      InputVariables.CheckedItemsChanged += new CollectionItemsChangedEventHandler<IndexedItem<StringValue>>(InputVariables_CheckedItemsChanged);
[5759]144      TrainingPartition.ValueChanged += new EventHandler(Parameter_ValueChanged);
145      TestPartition.ValueChanged += new EventHandler(Parameter_ValueChanged);
[5540]146    }
147
[5649]148    private void InputVariables_CheckedItemsChanged(object sender, CollectionItemsChangedEventArgs<IndexedItem<StringValue>> e) {
[5586]149      OnChanged();
150    }
[5649]151
[5586]152    private void Parameter_ValueChanged(object sender, EventArgs e) {
153      OnChanged();
154    }
155
[5554]156    public event EventHandler Changed;
157    protected virtual void OnChanged() {
[5540]158      var listeners = Changed;
159      if (listeners != null) listeners(this, EventArgs.Empty);
160    }
[10540]161
162    protected virtual bool IsProblemDataCompatible(IDataAnalysisProblemData problemData, out string errorMessage) {
163      errorMessage = string.Empty;
164      if (problemData == null) throw new ArgumentNullException("problemData", "The provided problemData is null.");
165
166      //check allowed input variables
167      StringBuilder message = new StringBuilder();
168      var variables = new HashSet<string>(problemData.InputVariables.Select(x => x.Value));
169      foreach (var item in AllowedInputVariables) {
170        if (!variables.Contains(item))
171          message.AppendLine("Input variable '" + item + "' is not present in the new problem data.");
172      }
173
174      if (message.Length != 0) {
175        errorMessage = message.ToString();
176        return false;
177      }
178      return true;
179
180    }
181
182    public virtual void AdjustProblemDataProperties(IDataAnalysisProblemData problemData) {
183      DataAnalysisProblemData data = problemData as DataAnalysisProblemData;
184      if (data == null) throw new ArgumentException("The problem data is not a data analysis problem data. Instead a " + problemData.GetType().GetPrettyName() + " was provided.", "problemData");
185
186      string errorMessage;
187      if (!data.IsProblemDataCompatible(this, out errorMessage)) {
188        throw new InvalidOperationException(errorMessage);
189      }
190
191      foreach (var inputVariable in InputVariables) {
192        var variable = data.InputVariables.FirstOrDefault(i => i.Value == inputVariable.Value);
193        InputVariables.SetItemCheckedState(inputVariable, variable != null && data.InputVariables.ItemChecked(variable));
194      }
195
196      TrainingPartition.Start = TrainingPartition.End = 0;
197      TestPartition.Start = 0;
198      TestPartition.End = Dataset.Rows;
199    }
[5540]200  }
201}
Note: See TracBrowser for help on using the repository browser.