Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.DatasetRefactor/sources/HeuristicLab.Problems.DataAnalysis/3.4/SimpleDataset.cs @ 11589

Last change on this file since 11589 was 11589, checked in by bburlacu, 10 years ago

#2276: Unsealed Dataset class, refactored GetDoubleValues, GetStringValues, GetDateTimeValues methods to internally use the same generic method, added ModifiableDataset class which adds the functionality of replacing, adding or removing rows in the dataset.

File size: 5.4 KB
Line 
1#region License Information
2
3/* HeuristicLab
4 * Copyright (C) 2002-2014 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.Generic;
26using System.Collections.ObjectModel;
27using System.Globalization;
28using System.Linq;
29using HeuristicLab.Common;
30using HeuristicLab.Core;
31using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
32
33namespace HeuristicLab.Problems.DataAnalysis {
34  [Item("SimpleDataset", "A dataset which allows access to a single row of updateable variables")]
35  [StorableClass]
36  public class SimpleDataset : NamedItem, IDataset {
37    [Storable]
38    private List<string> names;
39
40    [Storable]
41    private List<double> values;
42
43    public int Rows {
44      get { return 1; }
45      set { throw new InvalidOperationException(); }
46    }
47
48    public int Columns {
49      get { return names.Count; }
50      set { throw new InvalidOperationException(); }
51    }
52
53    public IEnumerable<string> ColumnNames { get; set; }
54    public IEnumerable<string> RowNames { get; set; }
55    public bool SortableView { get; set; }
56    public bool ReadOnly { get; private set; }
57    public bool Validate(string value, out string errorMessage) { throw new NotImplementedException(); }
58    public string GetValue(int rowIndex, int columnIndex) { throw new NotImplementedException(); }
59    public bool SetValue(string value, int rowIndex, int columnIndex) { throw new NotImplementedException(); }
60
61    public event EventHandler ColumnsChanged;
62    public event EventHandler RowsChanged;
63    public event EventHandler ColumnNamesChanged;
64    public event EventHandler RowNamesChanged;
65    public event EventHandler SortableViewChanged;
66    public event EventHandler<EventArgs<int, int>> ItemChanged;
67    public event EventHandler Reset;
68
69    public SimpleDataset() { }
70
71    public SimpleDataset(IEnumerable<string> variableNames, IEnumerable<double> variableValues) {
72      if (variableNames.Count() != variableValues.Count())
73        throw new ArgumentException("The variable names and values should be the same length.");
74      names = new List<string>(variableNames);
75      values = new List<double>(variableValues);
76    }
77
78    private SimpleDataset(SimpleDataset original, Cloner cloner)
79      : base(original, cloner) {
80    }
81
82    public override IDeepCloneable Clone(Cloner cloner) {
83      return new SimpleDataset(this, cloner);
84    }
85
86    [StorableConstructor]
87    private SimpleDataset(bool deserializing)
88      : base(deserializing) {
89    }
90
91    public double GetDoubleValue(string variableName, int row) {
92      int i = names.IndexOf(variableName);
93      if (i == -1)
94        throw new ArgumentException("The given variable name is not present in the dataset.");
95      return values[names.IndexOf(variableName)];
96    }
97
98    public IEnumerable<T> GetValues<T>(string variableName, IEnumerable<int> rows) {
99      throw new NotImplementedException();
100    }
101
102    public IEnumerable<T> GetValues<T>(string variableName) {
103      throw new NotImplementedException();
104    }
105
106    public IEnumerable<double> GetDoubleValues(string variableName, IEnumerable<int> rows) {
107      yield return GetDoubleValue(variableName, 0);
108    }
109
110    public IEnumerable<double> GetDoubleValues(string variableName) {
111      yield return GetDoubleValue(variableName, 0);
112    }
113
114    public ReadOnlyCollection<double> GetReadOnlyDoubleValues(string variableName) {
115      return GetDoubleValues(variableName, Enumerable.Empty<int>()).ToList().AsReadOnly();
116    }
117
118    public IEnumerable<string> GetStringValues(string variableName) {
119      yield return GetDoubleValue(variableName, 0).ToString(CultureInfo.InvariantCulture);
120    }
121
122    public IEnumerable<DateTime> GetDateTimeValues(string variableName) {
123      throw new NotImplementedException();
124    }
125
126    public void SetDoubleValue(string variableName, double value) {
127      int i = names.IndexOf(variableName);
128      if (i == -1)
129        throw new ArgumentException("The given variable name is not present in the dataset.");
130    }
131
132    public IEnumerable<string> VariableNames {
133      get { return names; }
134      set {
135        if (value.Count() != values.Count())
136          throw new ArgumentException("The names count should equal the values count.");
137        names = value.ToList();
138      }
139    }
140
141    public IEnumerable<double> VariableValues {
142      get { return values; }
143      set {
144        if (value.Count() != names.Count())
145          throw new ArgumentException("The values count should equal the names count.");
146        values = new List<double>(value);
147      }
148    }
149
150    public IEnumerable<string> DoubleVariables {
151      get { return names; } // all variables are of type double
152    }
153  }
154}
Note: See TracBrowser for help on using the repository browser.