Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.DataAnalysis/3.3/Dataset.cs @ 4031

Last change on this file since 4031 was 4031, checked in by mkommend, 14 years ago
  • changed access modifier of serializable ctor to private in the Dataset because it is a sealed class
  • added variable values access methods

(ticket #1082)

File size: 7.7 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Collections.Generic;
24using System.Xml;
25using System.Globalization;
26using System.Text;
27using System.Linq;
28using HeuristicLab.Common;
29using HeuristicLab.Core;
30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
31using HeuristicLab.Data;
32
33namespace HeuristicLab.Problems.DataAnalysis {
34  [Item("Dataset", "Represents a dataset containing data that should be analyzed.")]
35  [StorableClass]
36  public sealed class Dataset : NamedItem, IStringConvertibleMatrix {
37    // empty constructor for cloning
38    private Dataset()
39      : base() {
40    }
41
42    [StorableConstructor]
43    private Dataset(bool deserializing)
44      : base(deserializing) {
45    }
46
47    public Dataset(IEnumerable<string> variableNames, double[,] data)
48      : base() {
49      Name = "-";
50      if (variableNames.Count() != data.GetLength(1)) {
51        throw new ArgumentException("Number of variable names doesn't match the number of columns of data");
52      }
53      this.data = (double[,])data.Clone();
54      this.variableNames = variableNames.ToArray();
55    }
56
57    [Storable]
58    private string[] variableNames;
59    public IEnumerable<string> VariableNames {
60      get { return variableNames; }
61    }
62
63    [Storable]
64    private double[,] data;
65    private double[,] Data {
66      get { return data; }
67    }
68
69    // elementwise access
70    public double this[int rowIndex, int columnIndex] {
71      get { return data[rowIndex, columnIndex]; }
72    }
73    public double this[string variableName, int rowIndex] {
74      get {
75        int columnIndex = GetVariableIndex(variableName);
76        return data[rowIndex, columnIndex];
77      }
78    }
79
80    public double[] GetVariableValues(int variableIndex) {
81      return GetVariableValues(variableIndex, 0, Rows);
82    }
83    public double[] GetVariableValues(int variableIndex, int start, int end) {
84      if (start < 0 || !(start <= end))
85        throw new ArgumentException("Start must be between 0 and end (" + end + ").");
86      if (end > Rows || end < start)
87        throw new ArgumentException("End must be between start (" + start + ") and dataset rows (" + Rows + ").");
88
89      double[] values = new double[end - start];
90      for (int i = 0; i < end - start; i++)
91        values[i] = data[i + start, variableIndex];
92      return values;
93    }
94    public double[] GetVariableValues(string variableName) {
95      return GetVariableValues(GetVariableIndex(variableName), 0, Rows);
96    }
97    public double[] GetVariableValues(string variableName, int start, int end) {
98      return GetVariableValues(GetVariableIndex(variableName), start, end);
99    }
100
101    public IEnumerable<double> GetEnumeratedVariableValues(int variableIndex) {
102      return GetEnumeratedVariableValues(variableIndex, 0, Rows);
103    }
104    public IEnumerable<double> GetEnumeratedVariableValues(int variableIndex, int start, int end) {
105      if (start < 0 || !(start <= end))
106        throw new ArgumentException("Start must be between 0 and end (" + end + ").");
107      if (end > Rows || end < start)
108        throw new ArgumentException("End must be between start (" + start + ") and dataset rows (" + Rows + ").");
109      for (int i = 0; i < end - start; i++)
110        yield return data[i + start, variableIndex];
111    }
112    public IEnumerable<double> GetEnumeratedVariableValues(int variableIndex, IEnumerable<int> rows) {
113      foreach (int row in rows)
114        yield return data[row, variableIndex];
115    }
116
117    public IEnumerable<double> GetEnumeratedVariableValues(string variableName) {
118      return GetEnumeratedVariableValues(GetVariableIndex(variableName), 0, Rows);
119    }
120    public IEnumerable<double> GetEnumeratedVariableValues(string variableName, int start, int end) {
121      return GetEnumeratedVariableValues(GetVariableIndex(variableName), start, end);
122    }
123    public IEnumerable<double> GetEnumeratedVariableValues(string variableName, IEnumerable<int> rows) {
124      return GetEnumeratedVariableValues(GetVariableIndex(variableName), rows);
125    }
126
127
128
129    public string GetVariableName(int variableIndex) {
130      return variableNames[variableIndex];
131    }
132
133    public int GetVariableIndex(string variableName) {
134      for (int i = 0; i < variableNames.Length; i++) {
135        if (variableNames[i].Equals(variableName)) return i;
136      }
137      throw new ArgumentException("The variable name " + variableName + " was not found.");
138    }
139
140    public double[,] GetClonedData() {
141      return (double[,])data.Clone();
142    }
143
144    public override IDeepCloneable Clone(Cloner cloner) {
145      Dataset clone = (Dataset)base.Clone(cloner);
146      clone.variableNames = variableNames;
147      clone.data = data;
148      return clone;
149    }
150
151    public event EventHandler Reset;
152    private void OnReset(EventArgs e) {
153      var listeners = Reset;
154      if (listeners != null) listeners(this, e);
155    }
156
157    #region IStringConvertibleMatrix Members
158
159    public int Rows {
160      get { return data.GetLength(0); }
161      set { throw new NotSupportedException(); }
162    }
163
164    public int Columns {
165      get { return data.GetLength(1); }
166      set { throw new NotSupportedException(); }
167    }
168
169    public bool SortableView {
170      get { return false; }
171      set { throw new NotSupportedException(); }
172    }
173
174    public bool ReadOnly {
175      get { return true; }
176    }
177
178    IEnumerable<string> IStringConvertibleMatrix.ColumnNames {
179      get { return this.VariableNames; }
180      set { throw new NotSupportedException(); }
181    }
182
183    IEnumerable<string> IStringConvertibleMatrix.RowNames {
184      get { return new List<string>(); }
185      set { throw new NotSupportedException(); }
186    }
187
188    public bool Validate(string value, out string errorMessage) {
189      throw new NotSupportedException();
190    }
191
192    public string GetValue(int rowIndex, int columnIndex) {
193      return data[rowIndex, columnIndex].ToString();
194    }
195
196    public bool SetValue(string value, int rowIndex, int columnIndex) {
197      throw new NotSupportedException();
198    }
199
200    public event EventHandler ColumnNamesChanged;
201    private void OnColumnNamesChanged() {
202      EventHandler handler = ColumnNamesChanged;
203      if (handler != null)
204        handler(this, EventArgs.Empty);
205    }
206    public event EventHandler RowNamesChanged;
207    private void OnRowNamesChanged() {
208      EventHandler handler = RowNamesChanged;
209      if (handler != null)
210        handler(this, EventArgs.Empty);
211    }
212    public event EventHandler SortableViewChanged;
213    private void OnSortableViewChanged() {
214      EventHandler handler = SortableViewChanged;
215      if (handler != null)
216        handler(this, EventArgs.Empty);
217    }
218    public event EventHandler<EventArgs<int, int>> ItemChanged;
219    private void OnItemChanged(int rowIndex, int columnIndex) {
220      if (ItemChanged != null)
221        ItemChanged(this, new EventArgs<int, int>(rowIndex, columnIndex));
222      OnToStringChanged();
223    }
224    #endregion
225  }
226}
Note: See TracBrowser for help on using the repository browser.