Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3933 was 3933, checked in by mkommend, 14 years ago

removed cloning of dataset and made it readonly (ticket #938)

File size: 15.9 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 readonly 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
188    [StorableConstructor]
189    private DataAnalysisProblemData(bool deserializing) : base() { }
190
191    [StorableHook(HookType.AfterDeserialization)]
192    private void AfterDeserializationHook() {
193      RegisterParameterEventHandlers();
194      RegisterParameterValueEventHandlers();
195    }
196
197    #region events
198    public event EventHandler ProblemDataChanged;
199    protected virtual void OnProblemDataChanged(EventArgs e) {
200      if (!suppressEvents) {
201        var listeners = ProblemDataChanged;
202        if (listeners != null) listeners(this, e);
203      }
204    }
205
206    private void RegisterParameterEventHandlers() {
207      DatasetParameter.ValueChanged += new EventHandler(DatasetParameter_ValueChanged);
208      InputVariablesParameter.ValueChanged += new EventHandler(InputVariablesParameter_ValueChanged);
209      TargetVariableParameter.ValueChanged += new EventHandler(TargetVariableParameter_ValueChanged);
210      TrainingSamplesStartParameter.ValueChanged += new EventHandler(TrainingSamplesStartParameter_ValueChanged);
211      TrainingSamplesEndParameter.ValueChanged += new EventHandler(TrainingSamplesEndParameter_ValueChanged);
212      TestSamplesStartParameter.ValueChanged += new EventHandler(TestSamplesStartParameter_ValueChanged);
213      TestSamplesEndParameter.ValueChanged += new EventHandler(TestSamplesEndParameter_ValueChanged);
214    }
215
216    private void RegisterParameterValueEventHandlers() {
217      RegisterInputVariablesEventHandlers();
218      if (TargetVariable != null) RegisterStringValueEventHandlers(TargetVariable);
219      RegisterValueTypeEventHandlers(TrainingSamplesStart);
220      RegisterValueTypeEventHandlers(TrainingSamplesEnd);
221      RegisterValueTypeEventHandlers(TestSamplesStart);
222      RegisterValueTypeEventHandlers(TestSamplesEnd);
223    }
224
225
226    #region parameter value changed event handlers
227    void DatasetParameter_ValueChanged(object sender, EventArgs e) {
228      OnProblemDataChanged(EventArgs.Empty);
229    }
230    void InputVariablesParameter_ValueChanged(object sender, EventArgs e) {
231      RegisterInputVariablesEventHandlers();
232      OnProblemDataChanged(EventArgs.Empty);
233    }
234    void TargetVariableParameter_ValueChanged(object sender, EventArgs e) {
235      if (TargetVariable != null) {
236        RegisterStringValueEventHandlers(TargetVariable);
237        OnProblemDataChanged(EventArgs.Empty);
238      }
239    }
240    void TrainingSamplesStartParameter_ValueChanged(object sender, EventArgs e) {
241      RegisterValueTypeEventHandlers(TrainingSamplesStart);
242      OnProblemDataChanged(EventArgs.Empty);
243    }
244    void TrainingSamplesEndParameter_ValueChanged(object sender, EventArgs e) {
245      RegisterValueTypeEventHandlers(TrainingSamplesEnd);
246      OnProblemDataChanged(EventArgs.Empty);
247    }
248    void TestSamplesStartParameter_ValueChanged(object sender, EventArgs e) {
249      RegisterValueTypeEventHandlers(TestSamplesStart);
250      OnProblemDataChanged(EventArgs.Empty);
251    }
252    void TestSamplesEndParameter_ValueChanged(object sender, EventArgs e) {
253      RegisterValueTypeEventHandlers(TestSamplesEnd);
254      OnProblemDataChanged(EventArgs.Empty);
255    }
256    #endregion
257
258    private void RegisterInputVariablesEventHandlers() {
259      InputVariables.CollectionReset += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<HeuristicLab.Collections.IndexedItem<StringValue>>(InputVariables_CollectionReset);
260      InputVariables.ItemsAdded += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<HeuristicLab.Collections.IndexedItem<StringValue>>(InputVariables_ItemsAdded);
261      InputVariables.ItemsRemoved += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<HeuristicLab.Collections.IndexedItem<StringValue>>(InputVariables_ItemsRemoved);
262      InputVariables.CheckedItemsChanged += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<HeuristicLab.Collections.IndexedItem<StringValue>>(InputVariables_CheckedItemsChanged);
263      foreach (var item in InputVariables)
264        item.ValueChanged += new EventHandler(InputVariable_ValueChanged);
265    }
266
267    private void DeregisterInputVariablesEventHandlers() {
268      InputVariables.CollectionReset -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<HeuristicLab.Collections.IndexedItem<StringValue>>(InputVariables_CollectionReset);
269      InputVariables.ItemsAdded -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<HeuristicLab.Collections.IndexedItem<StringValue>>(InputVariables_ItemsAdded);
270      InputVariables.ItemsRemoved -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<HeuristicLab.Collections.IndexedItem<StringValue>>(InputVariables_ItemsRemoved);
271      InputVariables.CheckedItemsChanged -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<HeuristicLab.Collections.IndexedItem<StringValue>>(InputVariables_CheckedItemsChanged);
272      foreach (var item in InputVariables) {
273        item.ValueChanged -= new EventHandler(InputVariable_ValueChanged);
274      }
275    }
276
277    private void InputVariables_CheckedItemsChanged(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<HeuristicLab.Collections.IndexedItem<StringValue>> e) {
278      OnProblemDataChanged(e);
279    }
280
281    private void InputVariables_ItemsRemoved(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<HeuristicLab.Collections.IndexedItem<StringValue>> e) {
282      foreach (var indexedItem in e.Items)
283        indexedItem.Value.ValueChanged -= new EventHandler(InputVariable_ValueChanged);
284      OnProblemDataChanged(e);
285    }
286
287    private void InputVariables_ItemsAdded(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<HeuristicLab.Collections.IndexedItem<StringValue>> e) {
288      foreach (var indexedItem in e.Items)
289        indexedItem.Value.ValueChanged += new EventHandler(InputVariable_ValueChanged);
290      OnProblemDataChanged(e);
291    }
292
293    private void InputVariables_CollectionReset(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<HeuristicLab.Collections.IndexedItem<StringValue>> e) {
294      foreach (var indexedItem in e.OldItems)
295        indexedItem.Value.ValueChanged -= new EventHandler(InputVariable_ValueChanged);
296      OnProblemDataChanged(e);
297    }
298
299    void InputVariable_ValueChanged(object sender, EventArgs e) {
300      OnProblemDataChanged(e);
301    }
302    #region helper
303
304    private void RegisterValueTypeEventHandlers<T>(ValueTypeValue<T> value) where T : struct {
305      value.ValueChanged += new EventHandler(value_ValueChanged);
306    }
307
308    private void DeregisterValueTypeEventHandlers<T>(ValueTypeValue<T> value) where T : struct {
309      value.ValueChanged -= new EventHandler(value_ValueChanged);
310    }
311
312    void value_ValueChanged(object sender, EventArgs e) {
313      OnProblemDataChanged(e);
314    }
315
316    private void RegisterStringValueEventHandlers(StringValue value) {
317      value.ValueChanged += new EventHandler(value_ValueChanged);
318    }
319
320    private void DeregisterStringValueEventHandlers(StringValue value) {
321      value.ValueChanged -= new EventHandler(value_ValueChanged);
322    }
323
324    #endregion
325    #endregion
326
327    public virtual void ImportFromFile(string fileName) {
328      var csvFileParser = new CsvFileParser();
329      csvFileParser.Parse(fileName);
330      suppressEvents = true;
331      Name = "Data imported from " + Path.GetFileName(fileName);
332      Dataset = new Dataset(csvFileParser.VariableNames, csvFileParser.Values);
333      Dataset.Name = Path.GetFileName(fileName);
334      var variableNames = Dataset.VariableNames.Select(x => new StringValue(x).AsReadOnly()).ToList();
335      ((ConstrainedValueParameter<StringValue>)TargetVariableParameter).ValidValues.Clear();
336      foreach (var variableName in variableNames)
337        ((ConstrainedValueParameter<StringValue>)TargetVariableParameter).ValidValues.Add(variableName);
338      TargetVariable = variableNames.First();
339      InputVariables = new CheckedItemList<StringValue>(variableNames).AsReadOnly();
340      InputVariables.SetItemCheckedState(variableNames.First(), false);
341      int middle = (int)(csvFileParser.Rows * 0.5);
342      TrainingSamplesStart = new IntValue(0);
343      TrainingSamplesEnd = new IntValue(middle);
344      TestSamplesStart = new IntValue(middle);
345      TestSamplesEnd = new IntValue(csvFileParser.Rows);
346      suppressEvents = false;
347      OnProblemDataChanged(EventArgs.Empty);
348    }
349
350    public override IDeepCloneable Clone(Cloner cloner) {
351      DataAnalysisProblemData clone = (DataAnalysisProblemData)base.Clone(cloner);
352      clone.RegisterParameterEventHandlers();
353      clone.RegisterParameterValueEventHandlers();
354      return clone;
355    }
356  }
357}
Note: See TracBrowser for help on using the repository browser.