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

Last change on this file since 4022 was 4022, checked in by gkronber, 11 years ago

Worked on symbolic regression classes to prepare for time series prognosis plugin. #1081

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