Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DataAnalysis/HeuristicLab.Problems.DataAnalysis/3.3/Dataset.cs @ 5098

Last change on this file since 5098 was 4341, checked in by gkronber, 14 years ago

Merged changesets from revisions r4249, r4250, r4251, r4291, r4295 from trunk into data analysis exploration #1142.

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