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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Linq;
|
---|
25 | using HeuristicLab.Common;
|
---|
26 | using HeuristicLab.Core;
|
---|
27 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
28 | using HeuristicLab.Parameters;
|
---|
29 | using HeuristicLab.Data;
|
---|
30 | using HeuristicLab.Problems.DataAnalysis;
|
---|
31 | using System.Drawing;
|
---|
32 | using System.IO;
|
---|
33 |
|
---|
34 | namespace HeuristicLab.Problems.DataAnalysis {
|
---|
35 | [Item("DataAnalysisProblemData", "Represents an item containing all data defining a data analysis problem.")]
|
---|
36 | [StorableClass]
|
---|
37 | public class DataAnalysisProblemData : NamedItem {
|
---|
38 | [Storable]
|
---|
39 | private VariableCollection variables;
|
---|
40 | public VariableCollection Variables {
|
---|
41 | get { return variables; }
|
---|
42 | }
|
---|
43 |
|
---|
44 | #region variable properties
|
---|
45 | public IVariable DatasetVariable {
|
---|
46 | get { return variables["Dataset"]; }
|
---|
47 | }
|
---|
48 |
|
---|
49 | public IVariable TargetVariableVariable {
|
---|
50 | get { return variables["TargetVariable"]; }
|
---|
51 | }
|
---|
52 |
|
---|
53 | public IVariable InputVariablesVariable {
|
---|
54 | get { return variables["InputVariables"]; }
|
---|
55 | }
|
---|
56 |
|
---|
57 | public IVariable TrainingSamplesStartVariable {
|
---|
58 | get { return variables["TrainingSamplesStart"]; }
|
---|
59 | }
|
---|
60 | public IVariable TrainingSamplesEndVariable {
|
---|
61 | get { return variables["TrainingSamplesEnd"]; }
|
---|
62 | }
|
---|
63 |
|
---|
64 | public IVariable TestSamplesStartVariable {
|
---|
65 | get { return variables["TestSamplesStart"]; }
|
---|
66 | }
|
---|
67 | public IVariable TestSamplesEndVariable {
|
---|
68 | get { return variables["TestSamplesEnd"]; }
|
---|
69 | }
|
---|
70 | #endregion
|
---|
71 |
|
---|
72 | #region properties
|
---|
73 | public Dataset Dataset {
|
---|
74 | get { return (Dataset)DatasetVariable.Value; }
|
---|
75 | set {
|
---|
76 | if (value != Dataset) {
|
---|
77 | if (value == null) throw new ArgumentNullException();
|
---|
78 | if (Dataset != null) DeregisterDatasetEventHandlers();
|
---|
79 | DatasetVariable.Value = value;
|
---|
80 | RegisterDatasetEventHandlers();
|
---|
81 | OnProblemDataChanged(EventArgs.Empty);
|
---|
82 | }
|
---|
83 | }
|
---|
84 | }
|
---|
85 | public StringValue TargetVariable {
|
---|
86 | get { return (StringValue)TargetVariableVariable.Value; }
|
---|
87 | set {
|
---|
88 | if (value != TargetVariableVariable) {
|
---|
89 | if (value == null) throw new ArgumentNullException();
|
---|
90 | if (TargetVariable != null) DeregisterStringValueEventHandlers(TargetVariable);
|
---|
91 | TargetVariableVariable.Value = value;
|
---|
92 | RegisterStringValueEventHandlers(TargetVariable);
|
---|
93 | OnProblemDataChanged(EventArgs.Empty);
|
---|
94 | }
|
---|
95 | }
|
---|
96 | }
|
---|
97 | public ItemList<StringValue> InputVariables {
|
---|
98 | get { return (ItemList<StringValue>)InputVariablesVariable.Value; }
|
---|
99 | set {
|
---|
100 | if (value != InputVariables) {
|
---|
101 | if (value == null) throw new ArgumentNullException();
|
---|
102 | if (InputVariables != null) DeregisterInputVariablesEventHandlers();
|
---|
103 | InputVariablesVariable.Value = value;
|
---|
104 | RegisterInputVariablesEventHandlers();
|
---|
105 | OnProblemDataChanged(EventArgs.Empty);
|
---|
106 | }
|
---|
107 | }
|
---|
108 | }
|
---|
109 | public IntValue TrainingSamplesStart {
|
---|
110 | get { return (IntValue)TrainingSamplesStartVariable.Value; }
|
---|
111 | set {
|
---|
112 | if (value != TrainingSamplesStart) {
|
---|
113 | if (value == null) throw new ArgumentNullException();
|
---|
114 | if (TrainingSamplesStart != null) DeregisterValueTypeEventHandlers(TrainingSamplesStart);
|
---|
115 | TrainingSamplesStartVariable.Value = value;
|
---|
116 | RegisterValueTypeEventHandlers(TrainingSamplesStart);
|
---|
117 | OnProblemDataChanged(EventArgs.Empty);
|
---|
118 | }
|
---|
119 | }
|
---|
120 | }
|
---|
121 | public IntValue TrainingSamplesEnd {
|
---|
122 | get { return (IntValue)TrainingSamplesEndVariable.Value; }
|
---|
123 | set {
|
---|
124 | if (value != TrainingSamplesEnd) {
|
---|
125 | if (value == null) throw new ArgumentNullException();
|
---|
126 | if (TrainingSamplesEnd != null) DeregisterValueTypeEventHandlers(TrainingSamplesEnd);
|
---|
127 | TrainingSamplesEndVariable.Value = value;
|
---|
128 | RegisterValueTypeEventHandlers(TrainingSamplesEnd);
|
---|
129 | OnProblemDataChanged(EventArgs.Empty);
|
---|
130 | }
|
---|
131 | }
|
---|
132 | }
|
---|
133 | public IntValue TestSamplesStart {
|
---|
134 | get { return (IntValue)TestSamplesStartVariable.Value; }
|
---|
135 | set {
|
---|
136 | if (value != TestSamplesStart) {
|
---|
137 | if (value == null) throw new ArgumentNullException();
|
---|
138 | if (TestSamplesStart != null) DeregisterValueTypeEventHandlers(TestSamplesStart);
|
---|
139 | TestSamplesStartVariable.Value = value;
|
---|
140 | RegisterValueTypeEventHandlers(TestSamplesStart);
|
---|
141 | OnProblemDataChanged(EventArgs.Empty);
|
---|
142 | }
|
---|
143 | }
|
---|
144 | }
|
---|
145 | public IntValue TestSamplesEnd {
|
---|
146 | get { return (IntValue)TestSamplesEndVariable.Value; }
|
---|
147 | set {
|
---|
148 | if (value != TestSamplesEnd) {
|
---|
149 | if (value == null) throw new ArgumentNullException();
|
---|
150 | if (TestSamplesEnd != null) DeregisterValueTypeEventHandlers(TestSamplesEnd);
|
---|
151 | TestSamplesEndVariable.Value = value;
|
---|
152 | RegisterValueTypeEventHandlers(TestSamplesEnd);
|
---|
153 | OnProblemDataChanged(EventArgs.Empty);
|
---|
154 | }
|
---|
155 | }
|
---|
156 | }
|
---|
157 | #endregion
|
---|
158 |
|
---|
159 | public DataAnalysisProblemData()
|
---|
160 | : base() {
|
---|
161 | variables = new VariableCollection();
|
---|
162 | variables.Add(new Variable("Dataset", new Dataset()));
|
---|
163 | variables.Add(new Variable("InputVariables", new ItemList<StringValue>()));
|
---|
164 | variables.Add(new Variable("TargetVariable", new StringValue()));
|
---|
165 | variables.Add(new Variable("TrainingSamplesStart", new IntValue()));
|
---|
166 | variables.Add(new Variable("TrainingSamplesEnd", new IntValue()));
|
---|
167 | variables.Add(new Variable("TestSamplesStart", new IntValue()));
|
---|
168 | variables.Add(new Variable("TestSamplesEnd", new IntValue()));
|
---|
169 | RegisterEventHandlers();
|
---|
170 | }
|
---|
171 |
|
---|
172 | [StorableConstructor]
|
---|
173 | private DataAnalysisProblemData(bool deserializing) : base() { }
|
---|
174 |
|
---|
175 | [StorableHook(HookType.AfterDeserialization)]
|
---|
176 | private void AfterDeserializationHook() {
|
---|
177 | RegisterEventHandlers();
|
---|
178 | }
|
---|
179 |
|
---|
180 | #region events
|
---|
181 | private void RegisterEventHandlers() {
|
---|
182 | RegisterDatasetEventHandlers();
|
---|
183 | RegisterInputVariablesEventHandlers();
|
---|
184 | RegisterStringValueEventHandlers(TargetVariable);
|
---|
185 | RegisterValueTypeEventHandlers(TrainingSamplesStart);
|
---|
186 | RegisterValueTypeEventHandlers(TrainingSamplesEnd);
|
---|
187 | RegisterValueTypeEventHandlers(TestSamplesStart);
|
---|
188 | RegisterValueTypeEventHandlers(TestSamplesEnd);
|
---|
189 | }
|
---|
190 |
|
---|
191 | public event EventHandler ProblemDataChanged;
|
---|
192 | protected virtual void OnProblemDataChanged(EventArgs e) {
|
---|
193 | var listeners = ProblemDataChanged;
|
---|
194 | if (listeners != null) listeners(this, e);
|
---|
195 | }
|
---|
196 |
|
---|
197 |
|
---|
198 | private void RegisterValueTypeEventHandlers<T>(ValueTypeValue<T> value) where T : struct {
|
---|
199 | value.ValueChanged += new EventHandler(value_ValueChanged);
|
---|
200 | }
|
---|
201 |
|
---|
202 | private void DeregisterValueTypeEventHandlers<T>(ValueTypeValue<T> value) where T : struct {
|
---|
203 | value.ValueChanged -= new EventHandler(value_ValueChanged);
|
---|
204 | }
|
---|
205 |
|
---|
206 | void value_ValueChanged(object sender, EventArgs e) {
|
---|
207 | OnProblemDataChanged(e);
|
---|
208 | }
|
---|
209 |
|
---|
210 | private void RegisterStringValueEventHandlers(StringValue value) {
|
---|
211 | value.ValueChanged += new EventHandler(value_ValueChanged);
|
---|
212 | }
|
---|
213 |
|
---|
214 | private void DeregisterStringValueEventHandlers(StringValue value) {
|
---|
215 | value.ValueChanged -= new EventHandler(value_ValueChanged);
|
---|
216 | }
|
---|
217 |
|
---|
218 | private void RegisterDatasetEventHandlers() {
|
---|
219 | Dataset.DataChanged += new EventHandler<EventArgs<int, int>>(Dataset_DataChanged);
|
---|
220 | Dataset.Reset += new EventHandler(Dataset_Reset);
|
---|
221 | Dataset.ColumnNamesChanged += new EventHandler(Dataset_ColumnNamesChanged);
|
---|
222 | }
|
---|
223 |
|
---|
224 | private void DeregisterDatasetEventHandlers() {
|
---|
225 | Dataset.DataChanged -= new EventHandler<EventArgs<int, int>>(Dataset_DataChanged);
|
---|
226 | Dataset.Reset -= new EventHandler(Dataset_Reset);
|
---|
227 | Dataset.ColumnNamesChanged -= new EventHandler(Dataset_ColumnNamesChanged);
|
---|
228 | }
|
---|
229 |
|
---|
230 | void Dataset_ColumnNamesChanged(object sender, EventArgs e) {
|
---|
231 | OnProblemDataChanged(e);
|
---|
232 | }
|
---|
233 |
|
---|
234 | void Dataset_Reset(object sender, EventArgs e) {
|
---|
235 | OnProblemDataChanged(e);
|
---|
236 | }
|
---|
237 |
|
---|
238 | void Dataset_DataChanged(object sender, EventArgs<int, int> e) {
|
---|
239 | OnProblemDataChanged(e);
|
---|
240 | }
|
---|
241 |
|
---|
242 | private void RegisterInputVariablesEventHandlers() {
|
---|
243 | InputVariables.CollectionReset += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<HeuristicLab.Collections.IndexedItem<StringValue>>(InputVariables_CollectionReset);
|
---|
244 | InputVariables.ItemsAdded += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<HeuristicLab.Collections.IndexedItem<StringValue>>(InputVariables_ItemsAdded);
|
---|
245 | InputVariables.ItemsRemoved += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<HeuristicLab.Collections.IndexedItem<StringValue>>(InputVariables_ItemsRemoved);
|
---|
246 | InputVariables.ItemsReplaced += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<HeuristicLab.Collections.IndexedItem<StringValue>>(InputVariables_ItemsReplaced);
|
---|
247 | foreach (var item in InputVariables)
|
---|
248 | item.ValueChanged += new EventHandler(InputVariables_Value_ValueChanged);
|
---|
249 | }
|
---|
250 |
|
---|
251 | private void DeregisterInputVariablesEventHandlers() {
|
---|
252 | InputVariables.CollectionReset -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<HeuristicLab.Collections.IndexedItem<StringValue>>(InputVariables_CollectionReset);
|
---|
253 | InputVariables.ItemsAdded -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<HeuristicLab.Collections.IndexedItem<StringValue>>(InputVariables_ItemsAdded);
|
---|
254 | InputVariables.ItemsRemoved -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<HeuristicLab.Collections.IndexedItem<StringValue>>(InputVariables_ItemsRemoved);
|
---|
255 | InputVariables.ItemsReplaced -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<HeuristicLab.Collections.IndexedItem<StringValue>>(InputVariables_ItemsReplaced);
|
---|
256 | foreach (var item in InputVariables) {
|
---|
257 | item.ValueChanged -= new EventHandler(InputVariables_Value_ValueChanged);
|
---|
258 | }
|
---|
259 | }
|
---|
260 |
|
---|
261 | void InputVariables_ItemsReplaced(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<HeuristicLab.Collections.IndexedItem<StringValue>> e) {
|
---|
262 | foreach (var indexedItem in e.OldItems)
|
---|
263 | indexedItem.Value.ValueChanged -= new EventHandler(InputVariables_Value_ValueChanged);
|
---|
264 | foreach (var indexedItem in e.Items)
|
---|
265 | indexedItem.Value.ValueChanged += new EventHandler(InputVariables_Value_ValueChanged);
|
---|
266 | OnProblemDataChanged(e);
|
---|
267 | }
|
---|
268 |
|
---|
269 | void InputVariables_ItemsRemoved(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<HeuristicLab.Collections.IndexedItem<StringValue>> e) {
|
---|
270 | foreach (var indexedItem in e.Items)
|
---|
271 | indexedItem.Value.ValueChanged -= new EventHandler(InputVariables_Value_ValueChanged);
|
---|
272 | OnProblemDataChanged(e);
|
---|
273 | }
|
---|
274 |
|
---|
275 | void InputVariables_ItemsAdded(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<HeuristicLab.Collections.IndexedItem<StringValue>> e) {
|
---|
276 | foreach (var indexedItem in e.Items)
|
---|
277 | indexedItem.Value.ValueChanged += new EventHandler(InputVariables_Value_ValueChanged);
|
---|
278 | OnProblemDataChanged(e);
|
---|
279 | }
|
---|
280 |
|
---|
281 | void InputVariables_CollectionReset(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<HeuristicLab.Collections.IndexedItem<StringValue>> e) {
|
---|
282 | foreach (var indexedItem in e.OldItems)
|
---|
283 | indexedItem.Value.ValueChanged -= new EventHandler(InputVariables_Value_ValueChanged);
|
---|
284 |
|
---|
285 | OnProblemDataChanged(e);
|
---|
286 | }
|
---|
287 | void InputVariables_Value_ValueChanged(object sender, EventArgs e) {
|
---|
288 | OnProblemDataChanged(e);
|
---|
289 | }
|
---|
290 |
|
---|
291 | #endregion
|
---|
292 |
|
---|
293 | public virtual void ImportFromFile(string fileName) {
|
---|
294 | var csvFileParser = new CsvFileParser();
|
---|
295 | csvFileParser.Parse(fileName);
|
---|
296 | Name = "Data imported from " + Path.GetFileName(fileName);
|
---|
297 | Dataset = new Dataset(csvFileParser.VariableNames, csvFileParser.Values);
|
---|
298 | Dataset.Name = Path.GetFileName(fileName);
|
---|
299 | TargetVariable = new StringValue(Dataset.VariableNames.First());
|
---|
300 | InputVariables = new ItemList<StringValue>(Dataset.VariableNames.Skip(1).Select(s => new StringValue(s)));
|
---|
301 | int middle = (int)(csvFileParser.Rows * 0.5);
|
---|
302 | TrainingSamplesStart = new IntValue(0);
|
---|
303 | TrainingSamplesEnd = new IntValue(middle);
|
---|
304 | TestSamplesStart = new IntValue(middle);
|
---|
305 | TestSamplesEnd = new IntValue(csvFileParser.Rows);
|
---|
306 | }
|
---|
307 |
|
---|
308 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
309 | DataAnalysisProblemData clone = (DataAnalysisProblemData)base.Clone(cloner);
|
---|
310 | clone.variables = (VariableCollection)variables.Clone(cloner);
|
---|
311 |
|
---|
312 | clone.RegisterEventHandlers();
|
---|
313 | return clone;
|
---|
314 | }
|
---|
315 | }
|
---|
316 | }
|
---|