Free cookie consent management tool by TermsFeed Policy Generator

source: branches/OptimizationNetworks/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/DataAnalysisProblemData.cs @ 11062

Last change on this file since 11062 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
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2013 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;
32
33namespace HeuristicLab.Problems.DataAnalysis {
34  [StorableClass]
35  public abstract class DataAnalysisProblemData : ParameterizedNamedItem, IDataAnalysisProblemData {
36    protected const string DatasetParameterName = "Dataset";
37    protected const string InputVariablesParameterName = "InputVariables";
38    protected const string TrainingPartitionParameterName = "TrainingPartition";
39    protected const string TestPartitionParameterName = "TestPartition";
40
41    #region parameter properites
42    public IFixedValueParameter<Dataset> DatasetParameter {
43      get { return (IFixedValueParameter<Dataset>)Parameters[DatasetParameterName]; }
44    }
45    public IFixedValueParameter<ReadOnlyCheckedItemList<StringValue>> InputVariablesParameter {
46      get { return (IFixedValueParameter<ReadOnlyCheckedItemList<StringValue>>)Parameters[InputVariablesParameterName]; }
47    }
48    public IFixedValueParameter<IntRange> TrainingPartitionParameter {
49      get { return (IFixedValueParameter<IntRange>)Parameters[TrainingPartitionParameterName]; }
50    }
51    public IFixedValueParameter<IntRange> TestPartitionParameter {
52      get { return (IFixedValueParameter<IntRange>)Parameters[TestPartitionParameterName]; }
53    }
54    #endregion
55
56    #region properties
57    protected bool isEmpty = false;
58    public bool IsEmpty {
59      get { return isEmpty; }
60    }
61    public Dataset Dataset {
62      get { return DatasetParameter.Value; }
63    }
64    public ICheckedItemList<StringValue> InputVariables {
65      get { return InputVariablesParameter.Value; }
66    }
67    public IEnumerable<string> AllowedInputVariables {
68      get { return InputVariables.CheckedItems.Select(x => x.Value.Value); }
69    }
70
71    public IntRange TrainingPartition {
72      get { return TrainingPartitionParameter.Value; }
73    }
74    public IntRange TestPartition {
75      get { return TestPartitionParameter.Value; }
76    }
77
78    public virtual IEnumerable<int> TrainingIndices {
79      get {
80        return Enumerable.Range(TrainingPartition.Start, Math.Max(0, TrainingPartition.End - TrainingPartition.Start))
81                         .Where(IsTrainingSample);
82      }
83    }
84    public virtual IEnumerable<int> TestIndices {
85      get {
86        return Enumerable.Range(TestPartition.Start, Math.Max(0, TestPartition.End - TestPartition.Start))
87           .Where(IsTestSample);
88      }
89    }
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    }
101    #endregion
102
103    protected DataAnalysisProblemData(DataAnalysisProblemData original, Cloner cloner)
104      : base(original, cloner) {
105      isEmpty = original.isEmpty;
106      RegisterEventHandlers();
107    }
108    [StorableConstructor]
109    protected DataAnalysisProblemData(bool deserializing) : base(deserializing) { }
110
111    [StorableHook(HookType.AfterDeserialization)]
112    private void AfterDeserialization() {
113      RegisterEventHandlers();
114    }
115
116    protected DataAnalysisProblemData(Dataset dataset, IEnumerable<string> allowedInputVariables) {
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
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.");
122
123      var inputVariables = new CheckedItemList<StringValue>(dataset.DoubleVariables.Select(x => new StringValue(x)));
124      foreach (StringValue x in inputVariables)
125        inputVariables.SetItemCheckedState(x, allowedInputVariables.Contains(x.Value));
126
127      int trainingPartitionStart = 0;
128      int trainingPartitionEnd = dataset.Rows / 2;
129      int testPartitionStart = dataset.Rows / 2;
130      int testPartitionEnd = dataset.Rows;
131
132      Parameters.Add(new FixedValueParameter<Dataset>(DatasetParameterName, "", dataset));
133      Parameters.Add(new FixedValueParameter<ReadOnlyCheckedItemList<StringValue>>(InputVariablesParameterName, "", inputVariables.AsReadOnly()));
134      Parameters.Add(new FixedValueParameter<IntRange>(TrainingPartitionParameterName, "", new IntRange(trainingPartitionStart, trainingPartitionEnd)));
135      Parameters.Add(new FixedValueParameter<IntRange>(TestPartitionParameterName, "", new IntRange(testPartitionStart, testPartitionEnd)));
136
137      ((ValueParameter<Dataset>)DatasetParameter).ReactOnValueToStringChangedAndValueItemImageChanged = false;
138      RegisterEventHandlers();
139    }
140
141    private void RegisterEventHandlers() {
142      DatasetParameter.ValueChanged += new EventHandler(Parameter_ValueChanged);
143      InputVariables.CheckedItemsChanged += new CollectionItemsChangedEventHandler<IndexedItem<StringValue>>(InputVariables_CheckedItemsChanged);
144      TrainingPartition.ValueChanged += new EventHandler(Parameter_ValueChanged);
145      TestPartition.ValueChanged += new EventHandler(Parameter_ValueChanged);
146    }
147
148    private void InputVariables_CheckedItemsChanged(object sender, CollectionItemsChangedEventArgs<IndexedItem<StringValue>> e) {
149      OnChanged();
150    }
151
152    private void Parameter_ValueChanged(object sender, EventArgs e) {
153      OnChanged();
154    }
155
156    public event EventHandler Changed;
157    protected virtual void OnChanged() {
158      var listeners = Changed;
159      if (listeners != null) listeners(this, EventArgs.Empty);
160    }
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    }
200  }
201}
Note: See TracBrowser for help on using the repository browser.