Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/HeuristicLab.Problems.DataAnalysis/3.4/ModifiableDataset.cs @ 16118

Last change on this file since 16118 was 16118, checked in by mkommend, 6 years ago

#2939: Organized methods in ModifiableDataset for row and variable modification.

File size: 8.7 KB
Line 
1#region License Information
2
3/* HeuristicLab
4 * Copyright (C) 2002-2018 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 HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
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  [StorableClass]
36  public sealed class ModifiableDataset : Dataset, IStringConvertibleMatrix {
37    [StorableConstructor]
38    private ModifiableDataset(bool deserializing) : base(deserializing) { }
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 void AddRow(IEnumerable<object> values) {
58      var list = values.ToList();
59      if (list.Count != variableNames.Count)
60        throw new ArgumentException("The number of values must be equal to the number of variable names.");
61      // check if all the values are of the correct type
62      for (int i = 0; i < list.Count; ++i) {
63        if (list[i].GetType() != GetVariableType(variableNames[i])) {
64          throw new ArgumentException("The type of the provided value does not match the variable type.");
65        }
66      }
67      // add values
68      for (int i = 0; i < list.Count; ++i) {
69        variableValues[variableNames[i]].Add(list[i]);
70      }
71      Rows++;
72      OnRowsChanged();
73      OnReset();
74    }
75
76    public void ReplaceRow(int row, IEnumerable<object> values) {
77      var list = values.ToList();
78      if (list.Count != variableNames.Count)
79        throw new ArgumentException("The number of values must be equal to the number of variable names.");
80      // check if all the values are of the correct type
81      for (int i = 0; i < list.Count; ++i) {
82        if (list[i].GetType() != GetVariableType(variableNames[i])) {
83          throw new ArgumentException("The type of the provided value does not match the variable type.");
84        }
85      }
86      // replace values
87      for (int i = 0; i < list.Count; ++i) {
88        variableValues[variableNames[i]][row] = list[i];
89      }
90      OnReset();
91    }
92
93    // slow, avoid using this
94    public void RemoveRow(int row) {
95      foreach (var list in variableValues.Values)
96        list.RemoveAt(row);
97      Rows--;
98      OnRowsChanged();
99      OnReset();
100    }
101
102    // adds a new variable to the dataset
103    public void AddVariable(string variableName, IList values) {
104      InsertVariable(variableName, Columns, values);
105    }
106
107    public void InsertVariable(string variableName, int position, IList values) {
108      if (variableValues.ContainsKey(variableName))
109        throw new ArgumentException(string.Format("Variable {0} is already present in the dataset.", variableName));
110
111      if (position < 0 || position > Columns)
112        throw new ArgumentException(string.Format("Incorrect position {0} specified. The position must be between 0 and {1}.", position, Columns));
113
114      if (values == null)
115        throw new ArgumentNullException("values", "Values must not be null. At least an empty list of values has to be provided.");
116
117      if (values.Count != Rows)
118        throw new ArgumentException(string.Format("{0} values are provided, but {1} rows are present in the dataset.", values.Count, Rows));
119
120      if (!IsAllowedType(values))
121        throw new ArgumentException(string.Format("Unsupported type {0} for variable {1}.", GetElementType(values), variableName));
122
123      variableNames.Insert(position, variableName);
124      variableValues[variableName] = values;
125
126      OnColumnsChanged();
127      OnColumnNamesChanged();
128      OnReset();
129    }
130
131    public void ReplaceVariable(string variableName, IList values) {
132      if (!variableValues.ContainsKey(variableName))
133        throw new ArgumentException(string.Format("Variable {0} is not present in the dataset.", variableName));
134      if (values.Count != variableValues[variableName].Count)
135        throw new ArgumentException("The number of values must coincide with the number of dataset rows.");
136      if (GetVariableType(variableName) != values[0].GetType())
137        throw new ArgumentException("The type of the provided value does not match the variable type.");
138      variableValues[variableName] = values;
139    }
140
141
142    public void RemoveVariable(string variableName) {
143      if (!variableValues.ContainsKey(variableName))
144        throw new ArgumentException(string.Format("The variable {0} does not exist in the dataset.", variableName));
145      variableValues.Remove(variableName);
146      variableNames.Remove(variableName);
147      OnColumnsChanged();
148      OnColumnNamesChanged();
149      OnReset();
150    }
151
152
153    public void SetVariableValue(object value, string variableName, int row) {
154      IList list;
155      variableValues.TryGetValue(variableName, out list);
156      if (list == null)
157        throw new ArgumentException("The variable " + variableName + " does not exist in the dataset.");
158      if (row < 0 || list.Count < row)
159        throw new ArgumentOutOfRangeException("Invalid row value");
160      if (GetVariableType(variableName) != value.GetType())
161        throw new ArgumentException("The type of the provided value does not match the variable type.");
162
163      list[row] = value;
164      OnItemChanged(row, variableNames.IndexOf(variableName));
165    }
166
167    bool IStringConvertibleMatrix.SetValue(string value, int rowIndex, int columnIndex) {
168      var variableName = variableNames[columnIndex];
169      // if value represents a double
170      double dv;
171      if (double.TryParse(value, out dv)) {
172        SetVariableValue(dv, variableName, rowIndex);
173        return true;
174      }
175      // if value represents a DateTime object
176      DateTime dt;
177      if (DateTime.TryParse(value, out dt)) {
178        SetVariableValue(dt, variableName, rowIndex);
179        return true;
180      }
181      // if value is simply a string
182      SetVariableValue(value, variableName, rowIndex);
183      return true;
184    }
185
186    bool IStringConvertibleMatrix.Validate(string value, out string errorMessage) {
187      errorMessage = string.Empty;
188      return true;
189    }
190
191    #region event handlers
192    public override event EventHandler RowsChanged;
193    private void OnRowsChanged() {
194      var handler = RowsChanged;
195      if (handler != null)
196        handler(this, EventArgs.Empty);
197    }
198
199    public override event EventHandler ColumnsChanged;
200    private void OnColumnsChanged() {
201      var handler = ColumnsChanged;
202      if (handler != null)
203        handler(this, EventArgs.Empty);
204    }
205
206    public override event EventHandler ColumnNamesChanged;
207    private void OnColumnNamesChanged() {
208      var handler = ColumnNamesChanged;
209      if (handler != null)
210        handler(this, EventArgs.Empty);
211    }
212
213    public override event EventHandler Reset;
214    private void OnReset() {
215      var handler = Reset;
216      if (handler != null)
217        handler(this, EventArgs.Empty);
218    }
219
220    public override event EventHandler<EventArgs<int, int>> ItemChanged;
221    private void OnItemChanged(int rowIndex, int columnIndex) {
222      var handler = ItemChanged;
223      if (handler != null) {
224        handler(this, new EventArgs<int, int>(rowIndex, columnIndex));
225      }
226    }
227    #endregion
228  }
229}
Note: See TracBrowser for help on using the repository browser.