Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DataAnalysis Refactoring/HeuristicLab.Problems.DataAnalysis/3.4/DataAnalysisProblemData.cs @ 5559

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

#1418: worked on different ProblemData classes.

File size: 5.8 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2011 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;
28
29namespace HeuristicLab.Problems.DataAnalysis {
30  [StorableClass]
31  public abstract class DataAnalysisProblemData : NamedItem, IDataAnalysisProblemData {
32    #region propeties
33    [Storable]
34    private Dataset dataset;
35    public Dataset Dataset {
36      get { return dataset; }
37    }
38
39    [Storable]
40    private HashSet<string> allowedInputVariables;
41    public IEnumerable<string> AllowedInputVariables {
42      get { return allowedInputVariables; }
43    }
44
45    [Storable]
46    private int trainingPartitionStart;
47    public int TrainingPartitionStart {
48      get { return trainingPartitionStart; }
49      set {
50        if (0 < value || value > dataset.Rows)
51          throw new ArgumentException(string.Format("The training partition start must be between 0 and the number of rows of the dataset ({0})", dataset.Rows));
52        if (trainingPartitionStart != value) {
53          trainingPartitionStart = value;
54          OnChanged();
55        }
56      }
57    }
58    [Storable]
59    private int trainingPartitionEnd;
60    public int TrainingPartitionEnd {
61      get { return trainingPartitionEnd; }
62      set {
63        if (0 < value || value > dataset.Rows)
64          throw new ArgumentException(string.Format("The training partition end must be between 0 and the number of rows of the dataset ({0})", dataset.Rows));
65        if (trainingPartitionEnd != value) {
66          trainingPartitionEnd = value;
67          OnChanged();
68        }
69      }
70    }
71
72    [Storable]
73    private int testPartitionStart;
74    public int TestPartitionStart {
75      get { return testPartitionStart; }
76      set {
77        if (0 < value || value > dataset.Rows)
78          throw new ArgumentException(string.Format("The test partition start must be between 0 and the number of rows of the dataset ({0})", dataset.Rows));
79        if (testPartitionStart != value) {
80          testPartitionStart = value;
81          OnChanged();
82        }
83      }
84    }
85    [Storable]
86    private int testPartitionEnd;
87    public int TestPartitionEnd {
88      get { return testPartitionEnd; }
89      set {
90        if (0 < value || value > dataset.Rows)
91          throw new ArgumentException(string.Format("The test partition end must be between 0 and the number of rows of the dataset ({0})", dataset.Rows));
92        if (testPartitionEnd != value) {
93          testPartitionEnd = value;
94          OnChanged();
95        }
96      }
97    }
98
99    public IEnumerable<int> TrainingIndizes {
100      get {
101        return Enumerable.Range(TrainingPartitionStart, TrainingPartitionEnd - TrainingPartitionStart)
102                         .Where(i => i >= 0 && i < Dataset.Rows && (i < TestPartitionStart || TestPartitionEnd <= i));
103      }
104    }
105    public IEnumerable<int> TestIndizes {
106      get {
107        return Enumerable.Range(TestPartitionStart, TestPartitionEnd - TestPartitionStart)
108           .Where(i => i >= 0 && i < Dataset.Rows);
109      }
110    }
111    #endregion
112
113    protected DataAnalysisProblemData(DataAnalysisProblemData original, Cloner cloner) : base(original, cloner) { }
114    [StorableConstructor]
115    protected DataAnalysisProblemData(bool deserializing) : base(deserializing) { }
116
117    protected DataAnalysisProblemData(Dataset dataset, IEnumerable<string> allowedInputVariables) {
118      if (dataset == null) throw new ArgumentNullException("The dataset must not be null.");
119      if (allowedInputVariables == null) throw new ArgumentNullException("The allowedInputVariables must not be null.");
120
121      if (allowedInputVariables.Except(dataset.VariableNames).Any())
122        throw new ArgumentException("All allowed input variables must be present in the dataset.");
123
124      this.dataset = dataset;
125      allowedInputVariables = new HashSet<string>(allowedInputVariables);
126      trainingPartitionStart = 0;
127      trainingPartitionEnd = dataset.Rows / 2;
128      testPartitionStart = dataset.Rows / 2;
129      testPartitionEnd = dataset.Rows;
130    }
131
132    public bool AddAllowedInputVariable(string inputVariable) {
133      if (!Dataset.VariableNames.Contains(inputVariable))
134        throw new ArgumentException("The allowed input variable must be present in the dataset.");
135      if (allowedInputVariables.Contains(inputVariable)) return false;
136
137      allowedInputVariables.Add(inputVariable);
138      return true;
139    }
140    public bool RemoveAllowedInputVariable(string inputVariable) {
141      if (!Dataset.VariableNames.Contains(inputVariable))
142        throw new ArgumentException("The allowed input variable must be present in the dataset.");
143      if (!allowedInputVariables.Contains(inputVariable)) return false;
144
145      allowedInputVariables.Remove(inputVariable);
146      return true;
147    }
148
149    public event EventHandler Changed;
150    protected virtual void OnChanged() {
151      var listeners = Changed;
152      if (listeners != null) listeners(this, EventArgs.Empty);
153    }
154  }
155}
Note: See TracBrowser for help on using the repository browser.