Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 13027 was 13027, checked in by bburlacu, 9 years ago

#2489: Clone values in the ModifiableDataset.

File size: 7.8 KB
Line 
1#region License Information
2
3/* HeuristicLab
4 * Copyright (C) 2002-2015 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      var variables = variableValues.Keys.ToList();
42      foreach (var v in variables) {
43        var type = GetVariableType(v);
44        if (type == typeof(DateTime)) {
45          variableValues[v] = GetDateTimeValues(v).ToList();
46        } else if (type == typeof(double)) {
47          variableValues[v] = GetDoubleValues(v).ToList();
48        }
49      }
50    }
51    public override IDeepCloneable Clone(Cloner cloner) { return new ModifiableDataset(this, cloner); }
52    public ModifiableDataset() : base() { }
53
54    public ModifiableDataset(Dataset dataset) : base(dataset) { }
55    public ModifiableDataset(IEnumerable<string> variableNames, IEnumerable<IList> variableValues) : base(variableNames, variableValues) { }
56
57    public void ReplaceRow(int row, 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      // replace values
68      for (int i = 0; i < list.Count; ++i) {
69        variableValues[variableNames[i]][row] = list[i];
70      }
71      OnReset();
72    }
73
74    public void AddRow(IEnumerable<object> values) {
75      var list = values.ToList();
76      if (list.Count != variableNames.Count)
77        throw new ArgumentException("The number of values must be equal to the number of variable names.");
78      // check if all the values are of the correct type
79      for (int i = 0; i < list.Count; ++i) {
80        if (list[i].GetType() != GetVariableType(variableNames[i])) {
81          throw new ArgumentException("The type of the provided value does not match the variable type.");
82        }
83      }
84      // add values
85      for (int i = 0; i < list.Count; ++i) {
86        variableValues[variableNames[i]].Add(list[i]);
87      }
88      rows++;
89      OnRowsChanged();
90      OnReset();
91    }
92
93    // adds a new variable to the dataset
94    public void AddVariable<T>(string variableName, IEnumerable<T> values) {
95      if (variableValues.ContainsKey(variableName))
96        throw new ArgumentException("Variable " + variableName + " is already present in the dataset.");
97      int count = values.Count();
98      if (count != rows)
99        throw new ArgumentException("The number of values must exactly match the number of rows in the dataset.");
100      variableValues[variableName] = new List<T>(values);
101      variableNames.Add(variableName);
102      OnColumnsChanged();
103      OnColumnNamesChanged();
104      OnReset();
105    }
106
107    public void RemoveVariable(string variableName) {
108      if (!variableValues.ContainsKey(variableName))
109        throw new ArgumentException("The variable " + variableName + " does not exist in the dataset.");
110      variableValues.Remove(variableName);
111      variableNames.Remove(variableName);
112      OnColumnsChanged();
113      OnColumnNamesChanged();
114      OnReset();
115    }
116
117    // slow, avoid to use this
118    public void RemoveRow(int row) {
119      foreach (var list in variableValues.Values)
120        list.RemoveAt(row);
121      rows--;
122      OnRowsChanged();
123      OnReset();
124    }
125
126    public void SetVariableValue(object value, string variableName, int row) {
127      IList list;
128      variableValues.TryGetValue(variableName, out list);
129      if (list == null)
130        throw new ArgumentException("The variable " + variableName + " does not exist in the dataset.");
131      if (row < 0 || list.Count < row)
132        throw new ArgumentOutOfRangeException("Invalid row value");
133      if (GetVariableType(variableName) != value.GetType())
134        throw new ArgumentException("The type of the provided value does not match the variable type.");
135
136      list[row] = value;
137      OnItemChanged(row, variableNames.IndexOf(variableName));
138    }
139
140    private Type GetVariableType(string variableName) {
141      IList list;
142      variableValues.TryGetValue(variableName, out list);
143      if (list == null)
144        throw new ArgumentException("The variable " + variableName + " does not exist in the dataset.");
145      return list.GetType().GetGenericArguments()[0];
146    }
147
148    bool IStringConvertibleMatrix.SetValue(string value, int rowIndex, int columnIndex) {
149      var variableName = variableNames[columnIndex];
150      // if value represents a double
151      double dv;
152      if (double.TryParse(value, out dv)) {
153        SetVariableValue(dv, variableName, rowIndex);
154        return true;
155      }
156      // if value represents a DateTime object
157      DateTime dt;
158      if (DateTime.TryParse(value, out dt)) {
159        SetVariableValue(dt, variableName, rowIndex);
160        return true;
161      }
162      // if value is simply a string
163      SetVariableValue(value, variableName, rowIndex);
164      return true;
165    }
166
167    bool IStringConvertibleMatrix.Validate(string value, out string errorMessage) {
168      errorMessage = string.Empty;
169      return true;
170    }
171
172    #region event handlers
173    public override event EventHandler RowsChanged;
174    private void OnRowsChanged() {
175      var handler = RowsChanged;
176      if (handler != null)
177        handler(this, EventArgs.Empty);
178    }
179
180    public override event EventHandler ColumnsChanged;
181    private void OnColumnsChanged() {
182      var handler = ColumnsChanged;
183      if (handler != null)
184        handler(this, EventArgs.Empty);
185    }
186
187    public override event EventHandler ColumnNamesChanged;
188    private void OnColumnNamesChanged() {
189      var handler = ColumnNamesChanged;
190      if (handler != null)
191        handler(this, EventArgs.Empty);
192    }
193
194    public override event EventHandler Reset;
195    private void OnReset() {
196      var handler = Reset;
197      if (handler != null)
198        handler(this, EventArgs.Empty);
199    }
200
201    public override event EventHandler<EventArgs<int, int>> ItemChanged;
202    private void OnItemChanged(int rowIndex, int columnIndex) {
203      var handler = ItemChanged;
204      if (handler != null) {
205        handler(this, new EventArgs<int, int>(rowIndex, columnIndex));
206      }
207    }
208    #endregion
209  }
210}
Note: See TracBrowser for help on using the repository browser.