Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 4144 was 4068, checked in by swagner, 14 years ago

Sorted usings and removed unused usings in entire solution (#1094)

File size: 17.3 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
72    public IValueParameter<StringValue> TargetVariableParameter {
73      get { return (IValueParameter<StringValue>)Parameters["TargetVariable"]; }
74    }
75
76    public IValueParameter<ICheckedItemList<StringValue>> InputVariablesParameter {
77      get { return (IValueParameter<ICheckedItemList<StringValue>>)Parameters["InputVariables"]; }
78    }
79
80    public IValueParameter<IntValue> TrainingSamplesStartParameter {
81      get { return (IValueParameter<IntValue>)Parameters["TrainingSamplesStart"]; }
82    }
83    public IValueParameter<IntValue> TrainingSamplesEndParameter {
84      get { return (IValueParameter<IntValue>)Parameters["TrainingSamplesEnd"]; }
85    }
86    public IValueParameter<IntValue> TestSamplesStartParameter {
87      get { return (IValueParameter<IntValue>)Parameters["TestSamplesStart"]; }
88    }
89    public IValueParameter<IntValue> TestSamplesEndParameter {
90      get { return (IValueParameter<IntValue>)Parameters["TestSamplesEnd"]; }
91    }
92    #endregion
93
94    #region properties
95    public Dataset Dataset {
96      get { return (Dataset)DatasetParameter.Value; }
97      set {
98        if (value != Dataset) {
99          if (value == null) throw new ArgumentNullException();
100          DatasetParameter.Value = value;
101        }
102      }
103    }
104    public StringValue TargetVariable {
105      get { return (StringValue)TargetVariableParameter.Value; }
106      set {
107        if (value != TargetVariableParameter.Value) {
108          if (value == null) throw new ArgumentNullException();
109          if (TargetVariable != null) DeregisterStringValueEventHandlers(TargetVariable);
110          TargetVariableParameter.Value = value;
111        }
112      }
113    }
114    public ICheckedItemList<StringValue> InputVariables {
115      get { return (ICheckedItemList<StringValue>)InputVariablesParameter.Value; }
116      set {
117        if (value != InputVariables) {
118          if (value == null) throw new ArgumentNullException();
119          if (InputVariables != null) DeregisterInputVariablesEventHandlers();
120          InputVariablesParameter.Value = value;
121        }
122      }
123    }
124    public IntValue TrainingSamplesStart {
125      get { return (IntValue)TrainingSamplesStartParameter.Value; }
126      set {
127        if (value != TrainingSamplesStart) {
128          if (value == null) throw new ArgumentNullException();
129          if (TrainingSamplesStart != null) DeregisterValueTypeEventHandlers(TrainingSamplesStart);
130          TrainingSamplesStartParameter.Value = value;
131        }
132      }
133    }
134    public IntValue TrainingSamplesEnd {
135      get { return (IntValue)TrainingSamplesEndParameter.Value; }
136      set {
137        if (value != TrainingSamplesEnd) {
138          if (value == null) throw new ArgumentNullException();
139          if (TrainingSamplesEnd != null) DeregisterValueTypeEventHandlers(TrainingSamplesEnd);
140          TrainingSamplesEndParameter.Value = value;
141        }
142      }
143    }
144    public IntValue TestSamplesStart {
145      get { return (IntValue)TestSamplesStartParameter.Value; }
146      set {
147        if (value != TestSamplesStart) {
148          if (value == null) throw new ArgumentNullException();
149          if (TestSamplesStart != null) DeregisterValueTypeEventHandlers(TestSamplesStart);
150          TestSamplesStartParameter.Value = value;
151        }
152      }
153    }
154    public IntValue TestSamplesEnd {
155      get { return (IntValue)TestSamplesEndParameter.Value; }
156      set {
157        if (value != TestSamplesEnd) {
158          if (value == null) throw new ArgumentNullException();
159          if (TestSamplesEnd != null) DeregisterValueTypeEventHandlers(TestSamplesEnd);
160          TestSamplesEndParameter.Value = value;
161        }
162      }
163    }
164    #endregion
165
166    public DataAnalysisProblemData()
167      : base() {
168      var inputVariables = new CheckedItemList<StringValue>();
169      StringValue inputVariable = new StringValue("x");
170      inputVariables.Add(inputVariable);
171      StringValue targetVariable = new StringValue("y");
172      var validTargetVariables = new ItemSet<StringValue>();
173      validTargetVariables.Add(targetVariable);
174      Parameters.Add(new ValueParameter<Dataset>("Dataset", new Dataset(new string[] { "y", "x" }, kozaF1)));
175      Parameters.Add(new ValueParameter<ICheckedItemList<StringValue>>("InputVariables", inputVariables.AsReadOnly()));
176      Parameters.Add(new ConstrainedValueParameter<StringValue>("TargetVariable", validTargetVariables, targetVariable));
177      Parameters.Add(new ValueParameter<IntValue>("TrainingSamplesStart", new IntValue(0)));
178      Parameters.Add(new ValueParameter<IntValue>("TrainingSamplesEnd", new IntValue(15)));
179      Parameters.Add(new ValueParameter<IntValue>("TestSamplesStart", new IntValue(15)));
180      Parameters.Add(new ValueParameter<IntValue>("TestSamplesEnd", new IntValue(25)));
181      RegisterParameterEventHandlers();
182      RegisterParameterValueEventHandlers();
183    }
184
185    public DataAnalysisProblemData(Dataset dataset, IEnumerable<string> inputVariables, string targetVariable,
186      int trainingSamplesStart, int trainingSamplesEnd, int testSamplesStart, int testSamplesEnd) {
187      var inputVariablesList = new CheckedItemList<StringValue>(inputVariables.Select(x => new StringValue(x)));
188      StringValue targetVariableValue = new StringValue(targetVariable);
189      var validTargetVariables = new ItemSet<StringValue>();
190      foreach (var variable in dataset.VariableNames)
191        if (variable != targetVariable)
192          validTargetVariables.Add(new StringValue(variable));
193      validTargetVariables.Add(targetVariableValue);
194      Parameters.Add(new ValueParameter<Dataset>("Dataset", dataset));
195      Parameters.Add(new ValueParameter<ICheckedItemList<StringValue>>("InputVariables", inputVariablesList.AsReadOnly()));
196      Parameters.Add(new ConstrainedValueParameter<StringValue>("TargetVariable", validTargetVariables, targetVariableValue));
197      Parameters.Add(new ValueParameter<IntValue>("TrainingSamplesStart", new IntValue(trainingSamplesStart)));
198      Parameters.Add(new ValueParameter<IntValue>("TrainingSamplesEnd", new IntValue(trainingSamplesEnd)));
199      Parameters.Add(new ValueParameter<IntValue>("TestSamplesStart", new IntValue(testSamplesStart)));
200      Parameters.Add(new ValueParameter<IntValue>("TestSamplesEnd", new IntValue(testSamplesEnd)));
201      RegisterParameterEventHandlers();
202      RegisterParameterValueEventHandlers();
203    }
204
205    [StorableConstructor]
206    private DataAnalysisProblemData(bool deserializing) : base() { }
207
208    [StorableHook(HookType.AfterDeserialization)]
209    private void AfterDeserializationHook() {
210      RegisterParameterEventHandlers();
211      RegisterParameterValueEventHandlers();
212    }
213
214    #region events
215    public event EventHandler ProblemDataChanged;
216    protected virtual void OnProblemDataChanged(EventArgs e) {
217      if (!suppressEvents) {
218        var listeners = ProblemDataChanged;
219        if (listeners != null) listeners(this, e);
220      }
221    }
222
223    private void RegisterParameterEventHandlers() {
224      DatasetParameter.ValueChanged += new EventHandler(DatasetParameter_ValueChanged);
225      InputVariablesParameter.ValueChanged += new EventHandler(InputVariablesParameter_ValueChanged);
226      TargetVariableParameter.ValueChanged += new EventHandler(TargetVariableParameter_ValueChanged);
227      TrainingSamplesStartParameter.ValueChanged += new EventHandler(TrainingSamplesStartParameter_ValueChanged);
228      TrainingSamplesEndParameter.ValueChanged += new EventHandler(TrainingSamplesEndParameter_ValueChanged);
229      TestSamplesStartParameter.ValueChanged += new EventHandler(TestSamplesStartParameter_ValueChanged);
230      TestSamplesEndParameter.ValueChanged += new EventHandler(TestSamplesEndParameter_ValueChanged);
231    }
232
233    private void RegisterParameterValueEventHandlers() {
234      RegisterInputVariablesEventHandlers();
235      if (TargetVariable != null) RegisterStringValueEventHandlers(TargetVariable);
236      RegisterValueTypeEventHandlers(TrainingSamplesStart);
237      RegisterValueTypeEventHandlers(TrainingSamplesEnd);
238      RegisterValueTypeEventHandlers(TestSamplesStart);
239      RegisterValueTypeEventHandlers(TestSamplesEnd);
240    }
241
242
243    #region parameter value changed event handlers
244    void DatasetParameter_ValueChanged(object sender, EventArgs e) {
245      OnProblemDataChanged(EventArgs.Empty);
246    }
247    void InputVariablesParameter_ValueChanged(object sender, EventArgs e) {
248      RegisterInputVariablesEventHandlers();
249      OnProblemDataChanged(EventArgs.Empty);
250    }
251    void TargetVariableParameter_ValueChanged(object sender, EventArgs e) {
252      if (TargetVariable != null) {
253        RegisterStringValueEventHandlers(TargetVariable);
254        OnProblemDataChanged(EventArgs.Empty);
255      }
256    }
257    void TrainingSamplesStartParameter_ValueChanged(object sender, EventArgs e) {
258      RegisterValueTypeEventHandlers(TrainingSamplesStart);
259      OnProblemDataChanged(EventArgs.Empty);
260    }
261    void TrainingSamplesEndParameter_ValueChanged(object sender, EventArgs e) {
262      RegisterValueTypeEventHandlers(TrainingSamplesEnd);
263      OnProblemDataChanged(EventArgs.Empty);
264    }
265    void TestSamplesStartParameter_ValueChanged(object sender, EventArgs e) {
266      RegisterValueTypeEventHandlers(TestSamplesStart);
267      OnProblemDataChanged(EventArgs.Empty);
268    }
269    void TestSamplesEndParameter_ValueChanged(object sender, EventArgs e) {
270      RegisterValueTypeEventHandlers(TestSamplesEnd);
271      OnProblemDataChanged(EventArgs.Empty);
272    }
273    #endregion
274
275    private void RegisterInputVariablesEventHandlers() {
276      InputVariables.CollectionReset += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<HeuristicLab.Collections.IndexedItem<StringValue>>(InputVariables_CollectionReset);
277      InputVariables.ItemsAdded += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<HeuristicLab.Collections.IndexedItem<StringValue>>(InputVariables_ItemsAdded);
278      InputVariables.ItemsRemoved += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<HeuristicLab.Collections.IndexedItem<StringValue>>(InputVariables_ItemsRemoved);
279      InputVariables.CheckedItemsChanged += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<HeuristicLab.Collections.IndexedItem<StringValue>>(InputVariables_CheckedItemsChanged);
280      foreach (var item in InputVariables)
281        item.ValueChanged += new EventHandler(InputVariable_ValueChanged);
282    }
283
284    private void DeregisterInputVariablesEventHandlers() {
285      InputVariables.CollectionReset -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<HeuristicLab.Collections.IndexedItem<StringValue>>(InputVariables_CollectionReset);
286      InputVariables.ItemsAdded -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<HeuristicLab.Collections.IndexedItem<StringValue>>(InputVariables_ItemsAdded);
287      InputVariables.ItemsRemoved -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<HeuristicLab.Collections.IndexedItem<StringValue>>(InputVariables_ItemsRemoved);
288      InputVariables.CheckedItemsChanged -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<HeuristicLab.Collections.IndexedItem<StringValue>>(InputVariables_CheckedItemsChanged);
289      foreach (var item in InputVariables) {
290        item.ValueChanged -= new EventHandler(InputVariable_ValueChanged);
291      }
292    }
293
294    private void InputVariables_CheckedItemsChanged(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<HeuristicLab.Collections.IndexedItem<StringValue>> e) {
295      OnProblemDataChanged(e);
296    }
297
298    private void InputVariables_ItemsRemoved(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
304    private void InputVariables_ItemsAdded(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<HeuristicLab.Collections.IndexedItem<StringValue>> e) {
305      foreach (var indexedItem in e.Items)
306        indexedItem.Value.ValueChanged += new EventHandler(InputVariable_ValueChanged);
307      OnProblemDataChanged(e);
308    }
309
310    private void InputVariables_CollectionReset(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<HeuristicLab.Collections.IndexedItem<StringValue>> e) {
311      foreach (var indexedItem in e.OldItems)
312        indexedItem.Value.ValueChanged -= new EventHandler(InputVariable_ValueChanged);
313      OnProblemDataChanged(e);
314    }
315
316    void InputVariable_ValueChanged(object sender, EventArgs e) {
317      OnProblemDataChanged(e);
318    }
319    #region helper
320
321    private void RegisterValueTypeEventHandlers<T>(ValueTypeValue<T> value) where T : struct {
322      value.ValueChanged += new EventHandler(value_ValueChanged);
323    }
324
325    private void DeregisterValueTypeEventHandlers<T>(ValueTypeValue<T> value) where T : struct {
326      value.ValueChanged -= new EventHandler(value_ValueChanged);
327    }
328
329    void value_ValueChanged(object sender, EventArgs e) {
330      OnProblemDataChanged(e);
331    }
332
333    private void RegisterStringValueEventHandlers(StringValue value) {
334      value.ValueChanged += new EventHandler(value_ValueChanged);
335    }
336
337    private void DeregisterStringValueEventHandlers(StringValue value) {
338      value.ValueChanged -= new EventHandler(value_ValueChanged);
339    }
340
341    #endregion
342    #endregion
343
344    public virtual void ImportFromFile(string fileName) {
345      var csvFileParser = new CsvFileParser();
346      csvFileParser.Parse(fileName);
347      suppressEvents = true;
348      Name = "Data imported from " + Path.GetFileName(fileName);
349      Dataset = new Dataset(csvFileParser.VariableNames, csvFileParser.Values);
350      Dataset.Name = Path.GetFileName(fileName);
351      var variableNames = Dataset.VariableNames.Select(x => new StringValue(x).AsReadOnly()).ToList();
352      ((ConstrainedValueParameter<StringValue>)TargetVariableParameter).ValidValues.Clear();
353      foreach (var variableName in variableNames)
354        ((ConstrainedValueParameter<StringValue>)TargetVariableParameter).ValidValues.Add(variableName);
355      TargetVariable = variableNames.First();
356      InputVariables = new CheckedItemList<StringValue>(variableNames).AsReadOnly();
357      InputVariables.SetItemCheckedState(variableNames.First(), false);
358      int middle = (int)(csvFileParser.Rows * 0.5);
359      TrainingSamplesStart = new IntValue(0);
360      TrainingSamplesEnd = new IntValue(middle);
361      TestSamplesStart = new IntValue(middle);
362      TestSamplesEnd = new IntValue(csvFileParser.Rows);
363      suppressEvents = false;
364      OnProblemDataChanged(EventArgs.Empty);
365    }
366
367    public override IDeepCloneable Clone(Cloner cloner) {
368      DataAnalysisProblemData clone = (DataAnalysisProblemData)base.Clone(cloner);
369      clone.RegisterParameterEventHandlers();
370      clone.RegisterParameterValueEventHandlers();
371      return clone;
372    }
373  }
374}
Note: See TracBrowser for help on using the repository browser.