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

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

Added null check before event registration (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      if (TargetVariable != null) {
250        RegisterStringValueEventHandlers(TargetVariable);
251        OnProblemDataChanged(EventArgs.Empty);
252      }
253    }
254    private void TrainingSamplesStartParameter_ValueChanged(object sender, EventArgs e) {
255      RegisterValueTypeEventHandlers(TrainingSamplesStart);
256      OnProblemDataChanged(EventArgs.Empty);
257    }
258    private void TrainingSamplesEndParameter_ValueChanged(object sender, EventArgs e) {
259      RegisterValueTypeEventHandlers(TrainingSamplesEnd);
260      OnProblemDataChanged(EventArgs.Empty);
261    }
262    private void TestSamplesStartParameter_ValueChanged(object sender, EventArgs e) {
263      RegisterValueTypeEventHandlers(TestSamplesStart);
264      OnProblemDataChanged(EventArgs.Empty);
265    }
266    private void TestSamplesEndParameter_ValueChanged(object sender, EventArgs e) {
267      RegisterValueTypeEventHandlers(TestSamplesEnd);
268      OnProblemDataChanged(EventArgs.Empty);
269    }
270    #endregion
271
272    private void RegisterInputVariablesEventHandlers() {
273      InputVariables.CollectionReset += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<HeuristicLab.Collections.IndexedItem<StringValue>>(InputVariables_CollectionReset);
274      InputVariables.ItemsAdded += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<HeuristicLab.Collections.IndexedItem<StringValue>>(InputVariables_ItemsAdded);
275      InputVariables.ItemsRemoved += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<HeuristicLab.Collections.IndexedItem<StringValue>>(InputVariables_ItemsRemoved);
276      InputVariables.CheckedItemsChanged += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<HeuristicLab.Collections.IndexedItem<StringValue>>(InputVariables_CheckedItemsChanged);
277      foreach (var item in InputVariables) {
278        item.ValueChanged += new EventHandler(InputVariable_ValueChanged);
279      }
280    }
281
282    private void DeregisterInputVariablesEventHandlers() {
283      InputVariables.CollectionReset -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<HeuristicLab.Collections.IndexedItem<StringValue>>(InputVariables_CollectionReset);
284      InputVariables.ItemsAdded -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<HeuristicLab.Collections.IndexedItem<StringValue>>(InputVariables_ItemsAdded);
285      InputVariables.ItemsRemoved -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<HeuristicLab.Collections.IndexedItem<StringValue>>(InputVariables_ItemsRemoved);
286      InputVariables.CheckedItemsChanged -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<HeuristicLab.Collections.IndexedItem<StringValue>>(InputVariables_CheckedItemsChanged);
287      foreach (var item in InputVariables) {
288        item.ValueChanged -= new EventHandler(InputVariable_ValueChanged);
289      }
290    }
291
292    private void InputVariables_CheckedItemsChanged(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<HeuristicLab.Collections.IndexedItem<StringValue>> e) {
293      OnProblemDataChanged(e);
294    }
295    private void InputVariables_ItemsRemoved(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<HeuristicLab.Collections.IndexedItem<StringValue>> e) {
296      foreach (var indexedItem in e.Items)
297        indexedItem.Value.ValueChanged -= new EventHandler(InputVariable_ValueChanged);
298      OnProblemDataChanged(e);
299    }
300    private void InputVariables_ItemsAdded(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<HeuristicLab.Collections.IndexedItem<StringValue>> e) {
301      foreach (var indexedItem in e.Items)
302        indexedItem.Value.ValueChanged += new EventHandler(InputVariable_ValueChanged);
303      OnProblemDataChanged(e);
304    }
305    private void InputVariables_CollectionReset(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<HeuristicLab.Collections.IndexedItem<StringValue>> e) {
306      foreach (var indexedItem in e.OldItems)
307        indexedItem.Value.ValueChanged -= new EventHandler(InputVariable_ValueChanged);
308      OnProblemDataChanged(e);
309    }
310    private void InputVariable_ValueChanged(object sender, EventArgs e) {
311      OnProblemDataChanged(e);
312    }
313
314    #region helper
315    private void RegisterValueTypeEventHandlers<T>(ValueTypeValue<T> value) where T : struct {
316      value.ValueChanged += new EventHandler(value_ValueChanged);
317    }
318    private void DeregisterValueTypeEventHandlers<T>(ValueTypeValue<T> value) where T : struct {
319      value.ValueChanged -= new EventHandler(value_ValueChanged);
320    }
321    private void RegisterStringValueEventHandlers(StringValue value) {
322      value.ValueChanged += new EventHandler(value_ValueChanged);
323    }
324    private void DeregisterStringValueEventHandlers(StringValue value) {
325      value.ValueChanged -= new EventHandler(value_ValueChanged);
326    }
327
328    private void value_ValueChanged(object sender, EventArgs e) {
329      OnProblemDataChanged(e);
330    }
331    #endregion
332    #endregion
333
334    public virtual void ImportFromFile(string fileName) {
335      var csvFileParser = new CsvFileParser();
336      csvFileParser.Parse(fileName);
337      suppressEvents = true;
338      Name = "Data imported from " + Path.GetFileName(fileName);
339      Dataset = new Dataset(csvFileParser.VariableNames, csvFileParser.Values);
340      Dataset.Name = Path.GetFileName(fileName);
341      var variableNames = Dataset.VariableNames.Select(x => new StringValue(x).AsReadOnly()).ToList();
342      ((ConstrainedValueParameter<StringValue>)TargetVariableParameter).ValidValues.Clear();
343      foreach (var variableName in variableNames)
344        ((ConstrainedValueParameter<StringValue>)TargetVariableParameter).ValidValues.Add(variableName);
345      TargetVariable = variableNames.First();
346      InputVariables = new CheckedItemList<StringValue>(variableNames).AsReadOnly();
347      InputVariables.SetItemCheckedState(variableNames.First(), false);
348      int middle = (int)(csvFileParser.Rows * 0.5);
349      TrainingSamplesStart = new IntValue(0);
350      TrainingSamplesEnd = new IntValue(middle);
351      TestSamplesStart = new IntValue(middle);
352      TestSamplesEnd = new IntValue(csvFileParser.Rows);
353      suppressEvents = false;
354      OnProblemDataChanged(EventArgs.Empty);
355    }
356
357    public override IDeepCloneable Clone(Cloner cloner) {
358      DataAnalysisProblemData clone = (DataAnalysisProblemData)base.Clone(cloner);
359      clone.RegisterParameterEventHandlers();
360      clone.RegisterParameterValueEventHandlers();
361      return clone;
362    }
363  }
364}
Note: See TracBrowser for help on using the repository browser.