Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2971_named_intervals/HeuristicLab.Problems.DataAnalysis/3.4/ModifiableDataset.cs @ 16640

Last change on this file since 16640 was 16640, checked in by gkronber, 5 years ago

#2971: merged r16565:16631 from trunk/HeuristicLab.Problems.DataAnalysis to branch/HeuristicLab.Problems.DataAnalysis (resolving all conflicts)

File size: 9.2 KB
Line 
1#region License Information
2
3/* HeuristicLab
4 * Copyright (C) 2002-2019 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;
32using HEAL.Attic;
33
34namespace HeuristicLab.Problems.DataAnalysis {
35  [Item("ModifiableDataset", "Represents a dataset containing data that should be analyzed, which can be modified by adding or replacing variables and values.")]
36  [StorableType("4B9DA9DD-10C4-4609-8F87-B35ECD7A7487")]
37  public sealed class ModifiableDataset : Dataset, IStringConvertibleMatrix {
38    [StorableConstructor]
39    private ModifiableDataset(StorableConstructorFlag _) : base(_) { }
40
41    private ModifiableDataset(ModifiableDataset original, Cloner cloner) : base(original, cloner) {
42      variableNames = new List<string>(original.variableNames);
43      variableValues = CloneValues(original.variableValues);
44    }
45
46    public override IDeepCloneable Clone(Cloner cloner) { return new ModifiableDataset(this, cloner); }
47
48    public ModifiableDataset() { }
49
50    public ModifiableDataset(IEnumerable<string> variableNames, IEnumerable<IList> variableValues, bool cloneValues = false) :
51      base(variableNames, variableValues, cloneValues) { }
52
53    public Dataset ToDataset() {
54      return new Dataset(variableNames, variableNames.Select(v => variableValues[v]));
55    }
56
57
58    public IEnumerable<object> GetRow(int row) {
59      if (row < 0 || row >= Rows)
60        throw new ArgumentException(string.Format("Invalid row {0} specified. The dataset contains {1} row(s).", row, Rows));
61
62      return variableValues.Select(x => x.Value[row]);
63    }
64
65    public void AddRow(IEnumerable<object> values) {
66      var list = values.ToList();
67      if (list.Count != variableNames.Count)
68        throw new ArgumentException("The number of values must be equal to the number of variable names.");
69      // check if all the values are of the correct type
70      for (int i = 0; i < list.Count; ++i) {
71        if (list[i].GetType() != GetVariableType(variableNames[i])) {
72          throw new ArgumentException("The type of the provided value does not match the variable type.");
73        }
74      }
75      // add values
76      for (int i = 0; i < list.Count; ++i) {
77        variableValues[variableNames[i]].Add(list[i]);
78      }
79      Rows++;
80      OnRowsChanged();
81      OnReset();
82    }
83
84    public void ReplaceRow(int row, IEnumerable<object> values) {
85      var list = values.ToList();
86      if (list.Count != variableNames.Count)
87        throw new ArgumentException("The number of values must be equal to the number of variable names.");
88      // check if all the values are of the correct type
89      for (int i = 0; i < list.Count; ++i) {
90        if (list[i].GetType() != GetVariableType(variableNames[i])) {
91          throw new ArgumentException("The type of the provided value does not match the variable type.");
92        }
93      }
94      // replace values
95      for (int i = 0; i < list.Count; ++i) {
96        variableValues[variableNames[i]][row] = list[i];
97      }
98      OnReset();
99    }
100
101    // slow, avoid using this
102    public void RemoveRow(int row) {
103      foreach (var list in variableValues.Values)
104        list.RemoveAt(row);
105      Rows--;
106      OnRowsChanged();
107      OnReset();
108    }
109
110    // adds a new variable to the dataset
111    public void AddVariable(string variableName, IList values) {
112      InsertVariable(variableName, Columns, values);
113    }
114
115    public void InsertVariable(string variableName, int position, IList values) {
116      if (variableValues.ContainsKey(variableName))
117        throw new ArgumentException(string.Format("Variable {0} is already present in the dataset.", variableName));
118
119      if (position < 0 || position > Columns)
120        throw new ArgumentException(string.Format("Incorrect position {0} specified. The position must be between 0 and {1}.", position, Columns));
121
122      if (values == null)
123        throw new ArgumentNullException("values", "Values must not be null. At least an empty list of values has to be provided.");
124
125      if (values.Count != Rows)
126        throw new ArgumentException(string.Format("{0} values are provided, but {1} rows are present in the dataset.", values.Count, Rows));
127
128      if (!IsAllowedType(values))
129        throw new ArgumentException(string.Format("Unsupported type {0} for variable {1}.", GetElementType(values), variableName));
130
131      variableNames.Insert(position, variableName);
132      variableValues[variableName] = values;
133
134      OnColumnsChanged();
135      OnColumnNamesChanged();
136      OnReset();
137    }
138
139    public void ReplaceVariable(string variableName, IList values) {
140      if (!variableValues.ContainsKey(variableName))
141        throw new ArgumentException(string.Format("Variable {0} is not present in the dataset.", variableName));
142      if (values.Count != variableValues[variableName].Count)
143        throw new ArgumentException("The number of values must coincide with the number of dataset rows.");
144      if (GetVariableType(variableName) != values[0].GetType())
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.