Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2276: Changed the GetVariableType method so that it also works when there are no rows in the dataset.

File size: 6.5 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 class ModifiableDataset : Dataset, IStringConvertibleMatrix {
37    [StorableConstructor]
38    private ModifiableDataset(bool deserializing) : base(deserializing) { }
39    private ModifiableDataset(ModifiableDataset original, Cloner cloner) : base(original, cloner) { }
40    public override IDeepCloneable Clone(Cloner cloner) { return new ModifiableDataset(this, cloner); }
41    public ModifiableDataset() : base() { }
42    public ModifiableDataset(IEnumerable<string> variableNames, IEnumerable<IList> variableValues) : base(variableNames, variableValues) { }
43
44    public void ReplaceRow(int row, IEnumerable<object> values) {
45      var list = values.ToList();
46      if (list.Count != variableNames.Count)
47        throw new ArgumentException("The number of values must be equal to the number of variable names.");
48      // check if all the values are of the correct type
49      for (int i = 0; i < list.Count; ++i) {
50        if (list[i].GetType() != GetVariableType(variableNames[i])) {
51          throw new ArgumentException("The type of the provided value does not match the variable type.");
52        }
53      }
54      // replace values
55      for (int i = 0; i < list.Count; ++i) {
56        variableValues[variableNames[i]][row] = list[i];
57      }
58    }
59
60    public void AddRow(IEnumerable<object> values) {
61      var list = values.ToList();
62      if (list.Count != variableNames.Count)
63        throw new ArgumentException("The number of values must be equal to the number of variable names.");
64      // check if all the values are of the correct type
65      for (int i = 0; i < list.Count; ++i) {
66        if (list[i].GetType() != GetVariableType(variableNames[i])) {
67          throw new ArgumentException("The type of the provided value does not match the variable type.");
68        }
69      }
70      // add values
71      for (int i = 0; i < list.Count; ++i) {
72        variableValues[variableNames[i]].Add(list[i]);
73      }
74      rows++;
75    }
76
77    // adds a new variable to the dataset
78    public void AddVariable<T>(string variableName, IEnumerable<T> values) {
79      if (variableValues.ContainsKey(variableName))
80        throw new ArgumentException("Variable " + variableName + " is already present in the dataset.");
81      int count = values.Count();
82      if (count != rows)
83        throw new ArgumentException("The number of values must exactly match the number of rows in the dataset.");
84      variableValues[variableName] = new List<T>(values);
85      variableNames.Add(variableName);
86    }
87
88    public void RemoveVariable(string variableName) {
89      if (!variableValues.ContainsKey(variableName))
90        throw new ArgumentException("The variable " + variableName + " does not exist in the dataset.");
91      variableValues.Remove(variableName);
92      variableNames.Remove(variableName);
93    }
94
95    // slow, avoid to use this
96    public void RemoveRow(int row) {
97      foreach (var list in variableValues.Values)
98        list.RemoveAt(row);
99      rows--;
100    }
101
102    public void SetVariableValue(object value, string variableName, int row) {
103      IList list;
104      variableValues.TryGetValue(variableName, out list);
105      if (list == null)
106        throw new ArgumentException("The variable " + variableName + " does not exist in the dataset.");
107      if (row < 0 || list.Count < row)
108        throw new ArgumentOutOfRangeException("Invalid row value");
109      if (GetVariableType(variableName) != value.GetType())
110        throw new ArgumentException("The type of the provided value does not match the variable type.");
111
112      list[row] = value;
113    }
114
115    private Type GetVariableType(string variableName) {
116      IList list;
117      variableValues.TryGetValue(variableName, out list);
118      if (list == null)
119        throw new ArgumentException("The variable " + variableName + " does not exist in the dataset.");
120      return list.GetType().GetGenericArguments()[0];
121    }
122
123    bool IStringConvertibleMatrix.SetValue(string value, int rowIndex, int columnIndex) {
124      var variableName = variableNames[columnIndex];
125      // if value represents a double
126      double dv;
127      if (double.TryParse(value, out dv)) {
128        SetVariableValue(dv, variableName, rowIndex);
129        return true;
130      }
131      // if value represents a DateTime object
132      DateTime dt;
133      if (DateTime.TryParse(value, out dt)) {
134        SetVariableValue(dt, variableName, rowIndex);
135        return true;
136      }
137      // if value is simply a string
138      SetVariableValue(value, variableName, rowIndex);
139      return true;
140    }
141
142    bool IStringConvertibleMatrix.Validate(string value, out string errorMessage) {
143      return ValidateValue(value, out errorMessage);
144    }
145
146    private static bool ValidateValue(string value, out string errorMessage) {
147      double dv;
148      if (double.TryParse(value, out dv)) {
149        errorMessage = string.Empty;
150        return true;
151      }
152      DateTime dt;
153      if (DateTime.TryParse(value, out dt)) {
154        errorMessage = string.Empty;
155        return true;
156      }
157      errorMessage = "Value could not be validated. Please ensure that the value is either a string, a double, or a DateTime value.";
158      return false;
159    }
160  }
161}
Note: See TracBrowser for help on using the repository browser.