Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3040_VectorBasedGP/HeuristicLab.Problems.DataAnalysis/3.4/ModifiableDataset.cs @ 17741

Last change on this file since 17741 was 17741, checked in by pfleck, 4 years ago

#3040 Added new benchmark and some minor bugfixes.

File size: 9.2 KB
Line 
1#region License Information
2
3/* HeuristicLab
4 * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
5 *
6 * This file is part of HeuristicLab.
7 *
8 * HeuristicLab is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * HeuristicLab is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22#endregion
23
24using System;
25using System.Collections;
26using System.Collections.Generic;
27using System.Linq;
28using HeuristicLab.Common;
29using HeuristicLab.Core;
30using HeuristicLab.Data;
31using HEAL.Attic;
32
33namespace HeuristicLab.Problems.DataAnalysis {
34  [Item("ModifiableDataset", "Represents a dataset containing data that should be analyzed, which can be modified by adding or replacing variables and values.")]
35  [StorableType("4B9DA9DD-10C4-4609-8F87-B35ECD7A7487")]
36  public sealed class ModifiableDataset : Dataset, IStringConvertibleMatrix {
37    [StorableConstructor]
38    private ModifiableDataset(StorableConstructorFlag _) : base(_) { }
39
40    private ModifiableDataset(ModifiableDataset original, Cloner cloner) : base(original, cloner) {
41      variableNames = new List<string>(original.variableNames);
42      variableValues = CloneValues(original.variableValues);
43    }
44
45    public override IDeepCloneable Clone(Cloner cloner) { return new ModifiableDataset(this, cloner); }
46
47    public ModifiableDataset() { }
48
49    public ModifiableDataset(IEnumerable<string> variableNames, IEnumerable<IList> variableValues, bool cloneValues = false) :
50      base(variableNames, variableValues, cloneValues) { }
51
52    public Dataset ToDataset() {
53      return new Dataset(variableNames, variableNames.Select(v => variableValues[v]));
54    }
55
56
57    public IEnumerable<object> GetRow(int row) {
58      if (row < 0 || row >= Rows)
59        throw new ArgumentException(string.Format("Invalid row {0} specified. The dataset contains {1} row(s).", row, Rows));
60
61      return variableValues.Select(x => x.Value[row]);
62    }
63
64    public void AddRow(IEnumerable<object> values) {
65      var list = values.ToList();
66      if (list.Count != variableNames.Count)
67        throw new ArgumentException("The number of values must be equal to the number of variable names.");
68      // check if all the values are of the correct type
69      for (int i = 0; i < list.Count; ++i) {
70        if (list[i].GetType() != GetVariableType(variableNames[i])) {
71          throw new ArgumentException("The type of the provided value does not match the variable type.");
72        }
73      }
74      // add values
75      for (int i = 0; i < list.Count; ++i) {
76        variableValues[variableNames[i]].Add(list[i]);
77      }
78      Rows++;
79      OnRowsChanged();
80      OnReset();
81    }
82
83    public void ReplaceRow(int row, IEnumerable<object> values) {
84      var list = values.ToList();
85      if (list.Count != variableNames.Count)
86        throw new ArgumentException("The number of values must be equal to the number of variable names.");
87      // check if all the values are of the correct type
88      for (int i = 0; i < list.Count; ++i) {
89        if (list[i].GetType() != GetVariableType(variableNames[i])) {
90          throw new ArgumentException("The type of the provided value does not match the variable type.");
91        }
92      }
93      // replace values
94      for (int i = 0; i < list.Count; ++i) {
95        variableValues[variableNames[i]][row] = list[i];
96      }
97      OnReset();
98    }
99
100    // slow, avoid using this
101    public void RemoveRow(int row) {
102      foreach (var list in variableValues.Values)
103        list.RemoveAt(row);
104      Rows--;
105      OnRowsChanged();
106      OnReset();
107    }
108
109    // adds a new variable to the dataset
110    public void AddVariable(string variableName, IList values) {
111      InsertVariable(variableName, Columns, values);
112    }
113
114    public void InsertVariable(string variableName, int position, IList values) {
115      if (variableValues.ContainsKey(variableName))
116        throw new ArgumentException(string.Format("Variable {0} is already present in the dataset.", variableName));
117
118      if (position < 0 || position > Columns)
119        throw new ArgumentException(string.Format("Incorrect position {0} specified. The position must be between 0 and {1}.", position, Columns));
120
121      if (values == null)
122        throw new ArgumentNullException("values", "Values must not be null. At least an empty list of values has to be provided.");
123
124      if (values.Count != Rows)
125        throw new ArgumentException(string.Format("{0} values are provided, but {1} rows are present in the dataset.", values.Count, Rows));
126
127      if (!IsAllowedType(values))
128        throw new ArgumentException(string.Format("Unsupported type {0} for variable {1}.", GetElementType(values), variableName));
129
130      variableNames.Insert(position, variableName);
131      variableValues[variableName] = values;
132
133      OnColumnsChanged();
134      OnColumnNamesChanged();
135      OnReset();
136    }
137
138    public void ReplaceVariable(string variableName, IList values) {
139      if (!variableValues.ContainsKey(variableName))
140        throw new ArgumentException(string.Format("Variable {0} is not present in the dataset.", variableName));
141      if (values.Count != variableValues[variableName].Count)
142        throw new ArgumentException("The number of values must coincide with the number of dataset rows.");
143      //if (GetVariableType(variableName) != values[0].GetType())
144      if (!GetVariableType(variableName).IsInstanceOfType(values[0]))
145        throw new ArgumentException("The type of the provided value does not match the variable type.");
146      variableValues[variableName] = values;
147    }
148
149
150    public void RemoveVariable(string variableName) {
151      if (!variableValues.ContainsKey(variableName))
152        throw new ArgumentException(string.Format("The variable {0} does not exist in the dataset.", variableName));
153      variableValues.Remove(variableName);
154      variableNames.Remove(variableName);
155      OnColumnsChanged();
156      OnColumnNamesChanged();
157      OnReset();
158    }
159
160    public void ClearValues() {
161      foreach (var list in variableValues.Values) {
162        list.Clear();
163      }
164      Rows = 0;
165      OnRowsChanged();
166      OnReset();
167    }
168
169
170    public void SetVariableValue(object value, string variableName, int row) {
171      IList list;
172      variableValues.TryGetValue(variableName, out list);
173      if (list == null)
174        throw new ArgumentException("The variable " + variableName + " does not exist in the dataset.");
175      if (row < 0 || list.Count < row)
176        throw new ArgumentOutOfRangeException("Invalid row value");
177      if (GetVariableType(variableName) != value.GetType())
178        throw new ArgumentException("The type of the provided value does not match the variable type.");
179
180      list[row] = value;
181      OnItemChanged(row, variableNames.IndexOf(variableName));
182    }
183
184    bool IStringConvertibleMatrix.SetValue(string value, int rowIndex, int columnIndex) {
185      var variableName = variableNames[columnIndex];
186      // if value represents a double
187      double dv;
188      if (double.TryParse(value, out dv)) {
189        SetVariableValue(dv, variableName, rowIndex);
190        return true;
191      }
192      // if value represents a DateTime object
193      DateTime dt;
194      if (DateTime.TryParse(value, out dt)) {
195        SetVariableValue(dt, variableName, rowIndex);
196        return true;
197      }
198      // if value is simply a string
199      SetVariableValue(value, variableName, rowIndex);
200      return true;
201    }
202
203    bool IStringConvertibleMatrix.Validate(string value, out string errorMessage) {
204      errorMessage = string.Empty;
205      return true;
206    }
207
208    #region event handlers
209    public override event EventHandler RowsChanged;
210    private void OnRowsChanged() {
211      var handler = RowsChanged;
212      if (handler != null)
213        handler(this, EventArgs.Empty);
214    }
215
216    public override event EventHandler ColumnsChanged;
217    private void OnColumnsChanged() {
218      var handler = ColumnsChanged;
219      if (handler != null)
220        handler(this, EventArgs.Empty);
221    }
222
223    public override event EventHandler ColumnNamesChanged;
224    private void OnColumnNamesChanged() {
225      var handler = ColumnNamesChanged;
226      if (handler != null)
227        handler(this, EventArgs.Empty);
228    }
229
230    public override event EventHandler Reset;
231    private void OnReset() {
232      var handler = Reset;
233      if (handler != null)
234        handler(this, EventArgs.Empty);
235    }
236
237    public override event EventHandler<EventArgs<int, int>> ItemChanged;
238    private void OnItemChanged(int rowIndex, int columnIndex) {
239      var handler = ItemChanged;
240      if (handler != null) {
241        handler(this, new EventArgs<int, int>(rowIndex, columnIndex));
242      }
243    }
244    #endregion
245  }
246}
Note: See TracBrowser for help on using the repository browser.