Free cookie consent management tool by TermsFeed Policy Generator

source: branches/gp-crossover/HeuristicLab.Problems.DataAnalysis/3.3/Dataset.cs @ 7437

Last change on this file since 7437 was 5445, checked in by swagner, 14 years ago

Updated year of copyrights (#1406)

File size: 8.0 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2011 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.Linq;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29
30namespace HeuristicLab.Problems.DataAnalysis {
31  [Item("Dataset", "Represents a dataset containing data that should be analyzed.")]
32  [StorableClass]
33  public sealed class Dataset : NamedItem, IStringConvertibleMatrix {
34    [StorableConstructor]
35    private Dataset(bool deserializing) : base(deserializing) { }
36    private Dataset(Dataset original, Cloner cloner)
37      : base(original, cloner) {
38      variableNames = original.variableNames;
39      data = original.data;
40    }
41    public Dataset(IEnumerable<string> variableNames, double[,] data)
42      : base() {
43      Name = "-";
44      if (variableNames.Count() != data.GetLength(1)) {
45        throw new ArgumentException("Number of variable names doesn't match the number of columns of data");
46      }
47      this.data = (double[,])data.Clone();
48      this.variableNames = variableNames.ToArray();
49    }
50
51    [Storable]
52    private string[] variableNames;
53    public IEnumerable<string> VariableNames {
54      get { return variableNames; }
55    }
56
57    [Storable]
58    private double[,] data;
59    private double[,] Data {
60      get { return data; }
61    }
62
63    // elementwise access
64    public double this[int rowIndex, int columnIndex] {
65      get { return data[rowIndex, columnIndex]; }
66    }
67    public double this[string variableName, int rowIndex] {
68      get {
69        int columnIndex = GetVariableIndex(variableName);
70        return data[rowIndex, columnIndex];
71      }
72    }
73
74    public double[] GetVariableValues(int variableIndex) {
75      return GetVariableValues(variableIndex, 0, Rows);
76    }
77    public double[] GetVariableValues(int variableIndex, int start, int end) {
78      if (start < 0 || !(start <= end))
79        throw new ArgumentException("Start must be between 0 and end (" + end + ").");
80      if (end > Rows || end < start)
81        throw new ArgumentException("End must be between start (" + start + ") and dataset rows (" + Rows + ").");
82
83      double[] values = new double[end - start];
84      for (int i = 0; i < end - start; i++)
85        values[i] = data[i + start, variableIndex];
86      return values;
87    }
88    public double[] GetVariableValues(string variableName) {
89      return GetVariableValues(GetVariableIndex(variableName), 0, Rows);
90    }
91    public double[] GetVariableValues(string variableName, int start, int end) {
92      return GetVariableValues(GetVariableIndex(variableName), start, end);
93    }
94
95    public IEnumerable<double> GetEnumeratedVariableValues(int variableIndex) {
96      return GetEnumeratedVariableValues(variableIndex, 0, Rows);
97    }
98    public IEnumerable<double> GetEnumeratedVariableValues(int variableIndex, int start, int end) {
99      if (start < 0 || !(start <= end))
100        throw new ArgumentException("Start must be between 0 and end (" + end + ").");
101      if (end > Rows || end < start)
102        throw new ArgumentException("End must be between start (" + start + ") and dataset rows (" + Rows + ").");
103      for (int i = 0; i < end - start; i++)
104        yield return data[i + start, variableIndex];
105    }
106    public IEnumerable<double> GetEnumeratedVariableValues(int variableIndex, IEnumerable<int> rows) {
107      foreach (int row in rows)
108        yield return data[row, variableIndex];
109    }
110
111    public IEnumerable<double> GetEnumeratedVariableValues(string variableName) {
112      return GetEnumeratedVariableValues(GetVariableIndex(variableName), 0, Rows);
113    }
114    public IEnumerable<double> GetEnumeratedVariableValues(string variableName, int start, int end) {
115      return GetEnumeratedVariableValues(GetVariableIndex(variableName), start, end);
116    }
117    public IEnumerable<double> GetEnumeratedVariableValues(string variableName, IEnumerable<int> rows) {
118      return GetEnumeratedVariableValues(GetVariableIndex(variableName), rows);
119    }
120
121    public string GetVariableName(int variableIndex) {
122      return variableNames[variableIndex];
123    }
124
125    public int GetVariableIndex(string variableName) {
126      for (int i = 0; i < variableNames.Length; i++) {
127        if (variableNames[i].Equals(variableName)) return i;
128      }
129      throw new ArgumentException("The variable name " + variableName + " was not found.");
130    }
131
132    public double[,] GetClonedData() {
133      return (double[,])data.Clone();
134    }
135
136    public override IDeepCloneable Clone(Cloner cloner) {
137      return new Dataset(this, cloner);
138    }
139
140    public event EventHandler Reset;
141    private void OnReset(EventArgs e) {
142      var listeners = Reset;
143      if (listeners != null) listeners(this, e);
144    }
145
146    #region IStringConvertibleMatrix Members
147
148    public int Rows {
149      get { return data.GetLength(0); }
150      set { throw new NotSupportedException(); }
151    }
152
153    public int Columns {
154      get { return data.GetLength(1); }
155      set { throw new NotSupportedException(); }
156    }
157
158    public bool SortableView {
159      get { return false; }
160      set { throw new NotSupportedException(); }
161    }
162
163    public bool ReadOnly {
164      get { return true; }
165    }
166
167    IEnumerable<string> IStringConvertibleMatrix.ColumnNames {
168      get { return this.VariableNames; }
169      set { throw new NotSupportedException(); }
170    }
171
172    IEnumerable<string> IStringConvertibleMatrix.RowNames {
173      get { return new List<string>(); }
174      set { throw new NotSupportedException(); }
175    }
176
177    public bool Validate(string value, out string errorMessage) {
178      throw new NotSupportedException();
179    }
180
181    public string GetValue(int rowIndex, int columnIndex) {
182      return data[rowIndex, columnIndex].ToString();
183    }
184
185    public bool SetValue(string value, int rowIndex, int columnIndex) {
186      throw new NotSupportedException();
187    }
188
189    public event EventHandler ColumnsChanged;
190    private void OnColumnsChanged() {
191      var handler = ColumnsChanged;
192      if (handler != null) handler(this, EventArgs.Empty);
193    }
194    public event EventHandler RowsChanged;
195    private void OnRowsChanged() {
196      var handler = RowsChanged;
197      if (handler != null) handler(this, EventArgs.Empty);
198    }
199    public event EventHandler ColumnNamesChanged;
200    private void OnColumnNamesChanged() {
201      EventHandler listeners = ColumnNamesChanged;
202      if (listeners != null)
203        listeners(this, EventArgs.Empty);
204    }
205    public event EventHandler RowNamesChanged;
206    private void OnRowNamesChanged() {
207      EventHandler listeners = RowNamesChanged;
208      if (listeners != null)
209        listeners(this, EventArgs.Empty);
210    }
211    public event EventHandler SortableViewChanged;
212    private void OnSortableViewChanged() {
213      EventHandler listeners = SortableViewChanged;
214      if (listeners != null)
215        listeners(this, EventArgs.Empty);
216    }
217    public event EventHandler<EventArgs<int, int>> ItemChanged;
218    private void OnItemChanged(int rowIndex, int columnIndex) {
219      var listeners = ItemChanged;
220      if (listeners != null)
221        listeners(this, new EventArgs<int, int>(rowIndex, columnIndex));
222      OnToStringChanged();
223    }
224    #endregion
225  }
226}
Note: See TracBrowser for help on using the repository browser.