Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Problems.DataAnalysis/3.4/ModifiableDataset.cs @ 16189

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

#2935: Merged r16063 into stable.

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