Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2276: Commit initial version of IDataset interface and code refactoring.

File size: 5.0 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<double> GetDoubleValues(string variableName, IEnumerable<int> rows) {
99      yield return GetDoubleValue(variableName, 0);
100    }
101
102    public IEnumerable<double> GetDoubleValues(string variableName) {
103      yield return GetDoubleValue(variableName, 0);
104    }
105
106    public ReadOnlyCollection<double> GetReadOnlyDoubleValues(string variableName) {
107      return GetDoubleValues(variableName, Enumerable.Empty<int>()).ToList().AsReadOnly();
108    }
109
110    public IEnumerable<string> GetStringValues(string variableName) {
111      yield return GetDoubleValue(variableName, 0).ToString(CultureInfo.InvariantCulture);
112    }
113
114    public void SetDoubleValue(string variableName, double value) {
115      int i = names.IndexOf(variableName);
116      if (i == -1)
117        throw new ArgumentException("The given variable name is not present in the dataset.");
118    }
119
120    public IEnumerable<string> VariableNames {
121      get { return names; }
122      set {
123        if (value.Count() != values.Count())
124          throw new ArgumentException("The names count should equal the values count.");
125        names = value.ToList();
126      }
127    }
128
129    public IEnumerable<double> VariableValues {
130      get { return values; }
131      set {
132        if (value.Count() != names.Count())
133          throw new ArgumentException("The values count should equal the names count.");
134        values = new List<double>(value);
135      }
136    }
137
138    public IEnumerable<string> DoubleVariables {
139      get { return names; } // all variables are of type double
140    }
141  }
142}
Note: See TracBrowser for help on using the repository browser.