source: trunk/sources/HeuristicLab.Problems.DataAnalysis/3.3/DataAnalysisProblemData.cs @ 4291

Last change on this file since 4291 was 4291, checked in by mkommend, 12 years ago

Reverted incorrect commit (r4250) regarding the registration of parameter events and chained deserialization ctor correctly (ticket #1028).

File size: 17.4 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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.IO;
25using System.Linq;
26using HeuristicLab.Common;
27using HeuristicLab.Core;
28using HeuristicLab.Data;
29using HeuristicLab.Parameters;
30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
31
32namespace HeuristicLab.Problems.DataAnalysis {
33  [Item("DataAnalysisProblemData", "Represents an item containing all data defining a data analysis problem.")]
34  [StorableClass]
35  public class DataAnalysisProblemData : ParameterizedNamedItem {
36    private bool suppressEvents = false;
37    #region default data
38    // y = x^4 + x^3 + x^2 + x
39    private static double[,] kozaF1 = new double[,] {
40{2.017885919, -1.449165046},
41{1.30060506,  -1.344523885},
42{1.147134798, -1.317989331},
43{0.877182504, -1.266142284},
44{0.852562452, -1.261020794},
45{0.431095788, -1.158793317},
46{0.112586002, -1.050908405},
47{0.04594507,  -1.021989402},
48{0.042572879, -1.020438113},
49{-0.074027291,  -0.959859562},
50{-0.109178553,  -0.938094706},
51{-0.259721109,  -0.803635355},
52{-0.272991057,  -0.387519561},
53{-0.161978191,  -0.193611001},
54{-0.102489983,  -0.114215349},
55{-0.01469968, -0.014918985},
56{-0.008863365,  -0.008942626},
57{0.026751057, 0.026054094},
58{0.166922436, 0.14309643},
59{0.176953808, 0.1504144},
60{0.190233418, 0.159916534},
61{0.199800708, 0.166635331},
62{0.261502822, 0.207600348},
63{0.30182879,  0.232370249},
64{0.83763905,  0.468046718}
65    };
66    #endregion
67    #region parameter properties
68    public IValueParameter<Dataset> DatasetParameter {
69      get { return (IValueParameter<Dataset>)Parameters["Dataset"]; }
70    }
71    public IValueParameter<StringValue> TargetVariableParameter {
72      get { return (IValueParameter<StringValue>)Parameters["TargetVariable"]; }
73    }
74    public IValueParameter<ICheckedItemList<StringValue>> InputVariablesParameter {
75      get { return (IValueParameter<ICheckedItemList<StringValue>>)Parameters["InputVariables"]; }
76    }
77    public IValueParameter<IntValue> TrainingSamplesStartParameter {
78      get { return (IValueParameter<IntValue>)Parameters["TrainingSamplesStart"]; }
79    }
80    public IValueParameter<IntValue> TrainingSamplesEndParameter {
81      get { return (IValueParameter<IntValue>)Parameters["TrainingSamplesEnd"]; }
82    }
83    public IValueParameter<IntValue> TestSamplesStartParameter {
84      get { return (IValueParameter<IntValue>)Parameters["TestSamplesStart"]; }
85    }
86    public IValueParameter<IntValue> TestSamplesEndParameter {
87      get { return (IValueParameter<IntValue>)Parameters["TestSamplesEnd"]; }
88    }
89    #endregion
90
91    #region properties
92    public Dataset Dataset {
93      get { return (Dataset)DatasetParameter.Value; }
94      set {
95        if (value != Dataset) {
96          if (value == null) throw new ArgumentNullException();
97          DatasetParameter.Value = value;
98        }
99      }
100    }
101    public StringValue TargetVariable {
102      get { return (StringValue)TargetVariableParameter.Value; }
103      set {
104        if (value != TargetVariableParameter.Value) {
105          if (value == null) throw new ArgumentNullException();
106          if (TargetVariable != null) DeregisterStringValueEventHandlers(TargetVariable);
107          TargetVariableParameter.Value = value;
108        }
109      }
110    }
111    public ICheckedItemList<StringValue> InputVariables {
112      get { return (ICheckedItemList<StringValue>)InputVariablesParameter.Value; }
113      set {
114        if (value != InputVariables) {
115          if (value == null) throw new ArgumentNullException();
116          if (InputVariables != null) DeregisterInputVariablesEventHandlers();
117          InputVariablesParameter.Value = value;
118        }
119      }
120    }
121    public IntValue TrainingSamplesStart {
122      get { return (IntValue)TrainingSamplesStartParameter.Value; }
123      set {
124        if (value != TrainingSamplesStart) {
125          if (value == null) throw new ArgumentNullException();
126          if (TrainingSamplesStart != null) DeregisterValueTypeEventHandlers(TrainingSamplesStart);
127          TrainingSamplesStartParameter.Value = value;
128        }
129      }
130    }
131    public IntValue TrainingSamplesEnd {
132      get { return (IntValue)TrainingSamplesEndParameter.Value; }
133      set {
134        if (value != TrainingSamplesEnd) {
135          if (value == null) throw new ArgumentNullException();
136          if (TrainingSamplesEnd != null) DeregisterValueTypeEventHandlers(TrainingSamplesEnd);
137          TrainingSamplesEndParameter.Value = value;
138        }
139      }
140    }
141    public IntValue TestSamplesStart {
142      get { return (IntValue)TestSamplesStartParameter.Value; }
143      set {
144        if (value != TestSamplesStart) {
145          if (value == null) throw new ArgumentNullException();
146          if (TestSamplesStart != null) DeregisterValueTypeEventHandlers(TestSamplesStart);
147          TestSamplesStartParameter.Value = value;
148        }
149      }
150    }
151    public IntValue TestSamplesEnd {
152      get { return (IntValue)TestSamplesEndParameter.Value; }
153      set {
154        if (value != TestSamplesEnd) {
155          if (value == null) throw new ArgumentNullException();
156          if (TestSamplesEnd != null) DeregisterValueTypeEventHandlers(TestSamplesEnd);
157          TestSamplesEndParameter.Value = value;
158        }
159      }
160    }
161    #endregion
162
163    public DataAnalysisProblemData()
164      : base() {
165      var inputVariables = new CheckedItemList<StringValue>();
166      StringValue inputVariable = new StringValue("x");
167      inputVariables.Add(inputVariable);
168      StringValue targetVariable = new StringValue("y");
169      var validTargetVariables = new ItemSet<StringValue>();
170      validTargetVariables.Add(targetVariable);
171      Parameters.Add(new ValueParameter<Dataset>("Dataset", new Dataset(new string[] { "y", "x" }, kozaF1)));
172      Parameters.Add(new ValueParameter<ICheckedItemList<StringValue>>("InputVariables", inputVariables.AsReadOnly()));
173      Parameters.Add(new ConstrainedValueParameter<StringValue>("TargetVariable", validTargetVariables, targetVariable));
174      Parameters.Add(new ValueParameter<IntValue>("TrainingSamplesStart", new IntValue(0)));
175      Parameters.Add(new ValueParameter<IntValue>("TrainingSamplesEnd", new IntValue(15)));
176      Parameters.Add(new ValueParameter<IntValue>("TestSamplesStart", new IntValue(15)));
177      Parameters.Add(new ValueParameter<IntValue>("TestSamplesEnd", new IntValue(25)));
178      RegisterParameterEventHandlers();
179      RegisterParameterValueEventHandlers();
180    }
181
182    public DataAnalysisProblemData(Dataset dataset, IEnumerable<string> inputVariables, string targetVariable,
183      int trainingSamplesStart, int trainingSamplesEnd, int testSamplesStart, int testSamplesEnd) {
184      var inputVariablesList = new CheckedItemList<StringValue>(inputVariables.Select(x => new StringValue(x)));
185      StringValue targetVariableValue = new StringValue(targetVariable);
186      var validTargetVariables = new ItemSet<StringValue>();
187      foreach (var variable in dataset.VariableNames)
188        if (variable != targetVariable)
189          validTargetVariables.Add(new StringValue(variable));
190      validTargetVariables.Add(targetVariableValue);
191      Parameters.Add(new ValueParameter<Dataset>("Dataset", dataset));
192      Parameters.Add(new ValueParameter<ICheckedItemList<StringValue>>("InputVariables", inputVariablesList.AsReadOnly()));
193      Parameters.Add(new ConstrainedValueParameter<StringValue>("TargetVariable", validTargetVariables, targetVariableValue));
194      Parameters.Add(new ValueParameter<IntValue>("TrainingSamplesStart", new IntValue(trainingSamplesStart)));
195      Parameters.Add(new ValueParameter<IntValue>("TrainingSamplesEnd", new IntValue(trainingSamplesEnd)));
196      Parameters.Add(new ValueParameter<IntValue>("TestSamplesStart", new IntValue(testSamplesStart)));
197      Parameters.Add(new ValueParameter<IntValue>("TestSamplesEnd", new IntValue(testSamplesEnd)));
198      RegisterParameterEventHandlers();
199      RegisterParameterValueEventHandlers();
200    }
201
202    [StorableConstructor]
203    private DataAnalysisProblemData(bool deserializing) : base(deserializing) { }
204
205    [StorableHook(HookType.AfterDeserialization)]
206    private void AfterDeserializationHook() {
207      RegisterParameterEventHandlers();
208      RegisterParameterValueEventHandlers();
209    }
210
211    #region events
212    public event EventHandler ProblemDataChanged;
213    protected virtual void OnProblemDataChanged(EventArgs e) {
214      if (!suppressEvents) {
215        var listeners = ProblemDataChanged;
216        if (listeners != null) listeners(this, e);
217      }
218    }
219
220    private void RegisterParameterEventHandlers() {
221      DatasetParameter.ValueChanged += new EventHandler(DatasetParameter_ValueChanged);
222      InputVariablesParameter.ValueChanged += new EventHandler(InputVariablesParameter_ValueChanged);
223      TargetVariableParameter.ValueChanged += new EventHandler(TargetVariableParameter_ValueChanged);
224      TrainingSamplesStartParameter.ValueChanged += new EventHandler(TrainingSamplesStartParameter_ValueChanged);
225      TrainingSamplesEndParameter.ValueChanged += new EventHandler(TrainingSamplesEndParameter_ValueChanged);
226      TestSamplesStartParameter.ValueChanged += new EventHandler(TestSamplesStartParameter_ValueChanged);
227      TestSamplesEndParameter.ValueChanged += new EventHandler(TestSamplesEndParameter_ValueChanged);
228    }
229
230    private void RegisterParameterValueEventHandlers() {
231      RegisterInputVariablesEventHandlers();
232      if (TargetVariable != null) RegisterStringValueEventHandlers(TargetVariable);
233      RegisterValueTypeEventHandlers(TrainingSamplesStart);
234      RegisterValueTypeEventHandlers(TrainingSamplesEnd);
235      RegisterValueTypeEventHandlers(TestSamplesStart);
236      RegisterValueTypeEventHandlers(TestSamplesEnd);
237    }
238
239
240    #region parameter value changed event handlers
241    private void DatasetParameter_ValueChanged(object sender, EventArgs e) {
242      OnProblemDataChanged(EventArgs.Empty);
243    }
244    private void InputVariablesParameter_ValueChanged(object sender, EventArgs e) {
245      RegisterInputVariablesEventHandlers();
246      OnProblemDataChanged(EventArgs.Empty);
247    }
248    private void TargetVariableParameter_ValueChanged(object sender, EventArgs e) {
249      RegisterStringValueEventHandlers(TargetVariable);
250      if (TargetVariable != null) OnProblemDataChanged(EventArgs.Empty);
251    }
252    private void TrainingSamplesStartParameter_ValueChanged(object sender, EventArgs e) {
253      RegisterValueTypeEventHandlers(TrainingSamplesStart);
254      OnProblemDataChanged(EventArgs.Empty);
255    }
256    private void TrainingSamplesEndParameter_ValueChanged(object sender, EventArgs e) {
257      RegisterValueTypeEventHandlers(TrainingSamplesEnd);
258      OnProblemDataChanged(EventArgs.Empty);
259    }
260    private void TestSamplesStartParameter_ValueChanged(object sender, EventArgs e) {
261      RegisterValueTypeEventHandlers(TestSamplesStart);
262      OnProblemDataChanged(EventArgs.Empty);
263    }
264    private void TestSamplesEndParameter_ValueChanged(object sender, EventArgs e) {
265      RegisterValueTypeEventHandlers(TestSamplesEnd);
266      OnProblemDataChanged(EventArgs.Empty);
267    }
268    #endregion
269
270    private void RegisterInputVariablesEventHandlers() {
271      InputVariables.CollectionReset += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<HeuristicLab.Collections.IndexedItem<StringValue>>(InputVariables_CollectionReset);
272      InputVariables.ItemsAdded += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<HeuristicLab.Collections.IndexedItem<StringValue>>(InputVariables_ItemsAdded);
273      InputVariables.ItemsRemoved += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<HeuristicLab.Collections.IndexedItem<StringValue>>(InputVariables_ItemsRemoved);
274      InputVariables.CheckedItemsChanged += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<HeuristicLab.Collections.IndexedItem<StringValue>>(InputVariables_CheckedItemsChanged);
275      foreach (var item in InputVariables) {
276        item.ValueChanged += new EventHandler(InputVariable_ValueChanged);
277      }
278    }
279
280    private void DeregisterInputVariablesEventHandlers() {
281      InputVariables.CollectionReset -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<HeuristicLab.Collections.IndexedItem<StringValue>>(InputVariables_CollectionReset);
282      InputVariables.ItemsAdded -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<HeuristicLab.Collections.IndexedItem<StringValue>>(InputVariables_ItemsAdded);
283      InputVariables.ItemsRemoved -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<HeuristicLab.Collections.IndexedItem<StringValue>>(InputVariables_ItemsRemoved);
284      InputVariables.CheckedItemsChanged -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<HeuristicLab.Collections.IndexedItem<StringValue>>(InputVariables_CheckedItemsChanged);
285      foreach (var item in InputVariables) {
286        item.ValueChanged -= new EventHandler(InputVariable_ValueChanged);
287      }
288    }
289
290    private void InputVariables_CheckedItemsChanged(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<HeuristicLab.Collections.IndexedItem<StringValue>> e) {
291      OnProblemDataChanged(e);
292    }
293    private void InputVariables_ItemsRemoved(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<HeuristicLab.Collections.IndexedItem<StringValue>> e) {
294      foreach (var indexedItem in e.Items)
295        indexedItem.Value.ValueChanged -= new EventHandler(InputVariable_ValueChanged);
296      OnProblemDataChanged(e);
297    }
298    private void InputVariables_ItemsAdded(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<HeuristicLab.Collections.IndexedItem<StringValue>> e) {
299      foreach (var indexedItem in e.Items)
300        indexedItem.Value.ValueChanged += new EventHandler(InputVariable_ValueChanged);
301      OnProblemDataChanged(e);
302    }
303    private void InputVariables_CollectionReset(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<HeuristicLab.Collections.IndexedItem<StringValue>> e) {
304      foreach (var indexedItem in e.OldItems)
305        indexedItem.Value.ValueChanged -= new EventHandler(InputVariable_ValueChanged);
306      OnProblemDataChanged(e);
307    }
308    private void InputVariable_ValueChanged(object sender, EventArgs e) {
309      OnProblemDataChanged(e);
310    }
311
312    #region helper
313    private void RegisterValueTypeEventHandlers<T>(ValueTypeValue<T> value) where T : struct {
314      value.ValueChanged += new EventHandler(value_ValueChanged);
315    }
316    private void DeregisterValueTypeEventHandlers<T>(ValueTypeValue<T> value) where T : struct {
317      value.ValueChanged -= new EventHandler(value_ValueChanged);
318    }
319    private void RegisterStringValueEventHandlers(StringValue value) {
320      value.ValueChanged += new EventHandler(value_ValueChanged);
321    }
322    private void DeregisterStringValueEventHandlers(StringValue value) {
323      value.ValueChanged -= new EventHandler(value_ValueChanged);
324    }
325
326    private void value_ValueChanged(object sender, EventArgs e) {
327      OnProblemDataChanged(e);
328    }
329    #endregion
330    #endregion
331
332    public virtual void ImportFromFile(string fileName) {
333      var csvFileParser = new CsvFileParser();
334      csvFileParser.Parse(fileName);
335      suppressEvents = true;
336      Name = "Data imported from " + Path.GetFileName(fileName);
337      Dataset = new Dataset(csvFileParser.VariableNames, csvFileParser.Values);
338      Dataset.Name = Path.GetFileName(fileName);
339      var variableNames = Dataset.VariableNames.Select(x => new StringValue(x).AsReadOnly()).ToList();
340      ((ConstrainedValueParameter<StringValue>)TargetVariableParameter).ValidValues.Clear();
341      foreach (var variableName in variableNames)
342        ((ConstrainedValueParameter<StringValue>)TargetVariableParameter).ValidValues.Add(variableName);
343      TargetVariable = variableNames.First();
344      InputVariables = new CheckedItemList<StringValue>(variableNames).AsReadOnly();
345      InputVariables.SetItemCheckedState(variableNames.First(), false);
346      int middle = (int)(csvFileParser.Rows * 0.5);
347      TrainingSamplesStart = new IntValue(0);
348      TrainingSamplesEnd = new IntValue(middle);
349      TestSamplesStart = new IntValue(middle);
350      TestSamplesEnd = new IntValue(csvFileParser.Rows);
351      suppressEvents = false;
352      OnProblemDataChanged(EventArgs.Empty);
353    }
354
355    public override IDeepCloneable Clone(Cloner cloner) {
356      DataAnalysisProblemData clone = (DataAnalysisProblemData)base.Clone(cloner);
357      clone.RegisterParameterEventHandlers();
358      clone.RegisterParameterValueEventHandlers();
359      return clone;
360    }
361  }
362}
Note: See TracBrowser for help on using the repository browser.