Free cookie consent management tool by TermsFeed Policy Generator

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

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

#3040 Added new benchmark and some minor bugfixes.

File size: 9.2 KB
RevLine 
[11589]1#region License Information
2
3/* HeuristicLab
[17180]4 * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[11589]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;
[12141]30using HeuristicLab.Data;
[16565]31using HEAL.Attic;
[11589]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.")]
[16565]35  [StorableType("4B9DA9DD-10C4-4609-8F87-B35ECD7A7487")]
[12511]36  public sealed class ModifiableDataset : Dataset, IStringConvertibleMatrix {
[11589]37    [StorableConstructor]
[16565]38    private ModifiableDataset(StorableConstructorFlag _) : base(_) { }
[13027]39
40    private ModifiableDataset(ModifiableDataset original, Cloner cloner) : base(original, cloner) {
[15829]41      variableNames = new List<string>(original.variableNames);
42      variableValues = CloneValues(original.variableValues);
[13027]43    }
[15829]44
[11589]45    public override IDeepCloneable Clone(Cloner cloner) { return new ModifiableDataset(this, cloner); }
[12508]46
[15829]47    public ModifiableDataset() { }
[11589]48
[16063]49    public ModifiableDataset(IEnumerable<string> variableNames, IEnumerable<IList> variableValues, bool cloneValues = false) :
50      base(variableNames, variableValues, cloneValues) { }
[15829]51
[16063]52    public Dataset ToDataset() {
53      return new Dataset(variableNames, variableNames.Select(v => variableValues[v]));
54    }
[16118]55
56
[16120]57    public IEnumerable<object> GetRow(int row) {
[16242]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
[16120]61      return variableValues.Select(x => x.Value[row]);
62    }
63
[16118]64    public void AddRow(IEnumerable<object> values) {
[11589]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      }
[16118]74      // add values
[11589]75      for (int i = 0; i < list.Count; ++i) {
[16118]76        variableValues[variableNames[i]].Add(list[i]);
[11589]77      }
[16118]78      Rows++;
79      OnRowsChanged();
[12489]80      OnReset();
[11589]81    }
82
[16118]83    public void ReplaceRow(int row, IEnumerable<object> values) {
[11589]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      }
[16118]93      // replace values
[11589]94      for (int i = 0; i < list.Count; ++i) {
[16118]95        variableValues[variableNames[i]][row] = list[i];
[11589]96      }
[16118]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--;
[12489]105      OnRowsChanged();
106      OnReset();
[11589]107    }
108
[12032]109    // adds a new variable to the dataset
[15769]110    public void AddVariable(string variableName, IList values) {
[16084]111      InsertVariable(variableName, Columns, values);
[12032]112    }
113
[16063]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
[16084]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.");
[16063]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
[16118]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.");
[17741]143      //if (GetVariableType(variableName) != values[0].GetType())
144      if (!GetVariableType(variableName).IsInstanceOfType(values[0]))
[16118]145        throw new ArgumentException("The type of the provided value does not match the variable type.");
146      variableValues[variableName] = values;
147    }
148
149
[12032]150    public void RemoveVariable(string variableName) {
151      if (!variableValues.ContainsKey(variableName))
[15769]152        throw new ArgumentException(string.Format("The variable {0} does not exist in the dataset.", variableName));
[12032]153      variableValues.Remove(variableName);
154      variableNames.Remove(variableName);
[12489]155      OnColumnsChanged();
156      OnColumnNamesChanged();
157      OnReset();
[12032]158    }
159
[16120]160    public void ClearValues() {
161      foreach (var list in variableValues.Values) {
162        list.Clear();
163      }
164      Rows = 0;
165      OnRowsChanged();
166      OnReset();
167    }
[11589]168
[16120]169
[12141]170    public void SetVariableValue(object value, string variableName, int row) {
[12032]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");
[12141]177      if (GetVariableType(variableName) != value.GetType())
178        throw new ArgumentException("The type of the provided value does not match the variable type.");
179
[12032]180      list[row] = value;
[12508]181      OnItemChanged(row, variableNames.IndexOf(variableName));
[12032]182    }
183
[12141]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) {
[12508]204      errorMessage = string.Empty;
205      return true;
[12141]206    }
207
[12489]208    #region event handlers
209    public override event EventHandler RowsChanged;
210    private void OnRowsChanged() {
[12508]211      var handler = RowsChanged;
212      if (handler != null)
213        handler(this, EventArgs.Empty);
[12489]214    }
215
216    public override event EventHandler ColumnsChanged;
217    private void OnColumnsChanged() {
[12508]218      var handler = ColumnsChanged;
219      if (handler != null)
220        handler(this, EventArgs.Empty);
[12489]221    }
222
223    public override event EventHandler ColumnNamesChanged;
224    private void OnColumnNamesChanged() {
[12508]225      var handler = ColumnNamesChanged;
226      if (handler != null)
227        handler(this, EventArgs.Empty);
[12489]228    }
229
230    public override event EventHandler Reset;
231    private void OnReset() {
[12508]232      var handler = Reset;
233      if (handler != null)
234        handler(this, EventArgs.Empty);
[12489]235    }
236
237    public override event EventHandler<EventArgs<int, int>> ItemChanged;
238    private void OnItemChanged(int rowIndex, int columnIndex) {
[12508]239      var handler = ItemChanged;
240      if (handler != null) {
241        handler(this, new EventArgs<int, int>(rowIndex, columnIndex));
[12489]242      }
243    }
244    #endregion
[11589]245  }
246}
Note: See TracBrowser for help on using the repository browser.