Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.DatasetRefactor/sources/HeuristicLab.Problems.DataAnalysis/3.4/ModifiableDataset.cs @ 12032

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

#2276: Extended functionality of ModifiableDataset with three new methods: AddVariable, RemoveVariable and ChangeVariableValue.

File size: 5.0 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.Persistence.Default.CompositeSerializers.Storable;
31
32namespace HeuristicLab.Problems.DataAnalysis {
33  [Item("ModifiableDataset", "Represents a dataset containing data that should be analyzed, which can be modified by adding or replacing variables and values.")]
34  [StorableClass]
35  public class ModifiableDataset : Dataset {
36    [StorableConstructor]
37    private ModifiableDataset(bool deserializing) : base(deserializing) { }
38    private ModifiableDataset(ModifiableDataset original, Cloner cloner) : base(original, cloner) { }
39    public override IDeepCloneable Clone(Cloner cloner) { return new ModifiableDataset(this, cloner); }
40    public ModifiableDataset() : base() { }
41    public ModifiableDataset(IEnumerable<string> variableNames, IEnumerable<IList> variableValues) : base(variableNames, variableValues) { }
42
43    public void ReplaceRow(int row, IEnumerable<object> values) {
44      var list = values.ToList();
45      if (list.Count != variableNames.Count)
46        throw new ArgumentException("The number of values must be equal to the number of variable names.");
47      // check if all the values are of the correct type
48      for (int i = 0; i < list.Count; ++i) {
49        if (list[i].GetType() != GetVariableType(variableNames[i])) {
50          throw new ArgumentException("The type of the provided value does not match the variable type.");
51        }
52      }
53      // replace values
54      for (int i = 0; i < list.Count; ++i) {
55        variableValues[variableNames[i]][row] = list[i];
56      }
57    }
58
59    public void AddRow(IEnumerable<object> values) {
60      var list = values.ToList();
61      if (list.Count != variableNames.Count)
62        throw new ArgumentException("The number of values must be equal to the number of variable names.");
63      // check if all the values are of the correct type
64      for (int i = 0; i < list.Count; ++i) {
65        if (list[i].GetType() != GetVariableType(variableNames[i])) {
66          throw new ArgumentException("The type of the provided value does not match the variable type.");
67        }
68      }
69      // add values
70      for (int i = 0; i < list.Count; ++i) {
71        variableValues[variableNames[i]].Add(list[i]);
72      }
73      rows++;
74    }
75
76    // adds a new variable to the dataset
77    public void AddVariable<T>(string variableName, IEnumerable<T> values) {
78      if (variableValues.ContainsKey(variableName))
79        throw new ArgumentException("Variable " + variableName + " is already present in the dataset.");
80      int count = values.Count();
81      if (count != rows)
82        throw new ArgumentException("The number of values must exactly match the number of rows in the dataset.");
83      variableValues[variableName] = new List<T>(values);
84      variableNames.Add(variableName);
85    }
86
87    public void RemoveVariable(string variableName) {
88      if (!variableValues.ContainsKey(variableName))
89        throw new ArgumentException("The variable " + variableName + " does not exist in the dataset.");
90      variableValues.Remove(variableName);
91      variableNames.Remove(variableName);
92    }
93
94    // slow, avoid to use this
95    public void RemoveRow(int row) {
96      foreach (var list in variableValues.Values)
97        list.RemoveAt(row);
98      rows--;
99    }
100
101    public void ChangeVariableValue(object value, string variableName, int row) {
102      IList list;
103      variableValues.TryGetValue(variableName, out list);
104      if (list == null)
105        throw new ArgumentException("The variable " + variableName + " does not exist in the dataset.");
106      if (row < 0 || list.Count < row)
107        throw new ArgumentOutOfRangeException("Invalid row value");
108      list[row] = value;
109    }
110
111    private Type GetVariableType(string variableName) {
112      IList list;
113      variableValues.TryGetValue(variableName, out list);
114      if (list == null)
115        throw new ArgumentException("The variable " + variableName + " does not exist in the dataset.");
116      return list[0].GetType();
117    }
118  }
119}
Note: See TracBrowser for help on using the repository browser.