Free cookie consent management tool by TermsFeed Policy Generator

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

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

Updated year of copyrights (#1406)

File size: 8.0 KB
RevLine 
[2]1#region License Information
2/* HeuristicLab
[5445]3 * Copyright (C) 2002-2011 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[2]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;
[2285]24using System.Linq;
[3376]25using HeuristicLab.Common;
[3253]26using HeuristicLab.Core;
[4068]27using HeuristicLab.Data;
[3253]28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
[2]29
[3253]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 {
[3933]34    [StorableConstructor]
[4722]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;
[2319]40    }
[3264]41    public Dataset(IEnumerable<string> variableNames, double[,] data)
[3933]42      : base() {
[2319]43      Name = "-";
[3264]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      }
[3933]47      this.data = (double[,])data.Clone();
[3442]48      this.variableNames = variableNames.ToArray();
[2038]49    }
50
[3294]51    [Storable]
[3308]52    private string[] variableNames;
[2319]53    public IEnumerable<string> VariableNames {
54      get { return variableNames; }
[333]55    }
56
[3294]57    [Storable]
[3308]58    private double[,] data;
59    private double[,] Data {
[3253]60      get { return data; }
[2]61    }
62
[3253]63    // elementwise access
64    public double this[int rowIndex, int columnIndex] {
65      get { return data[rowIndex, columnIndex]; }
[2]66    }
[3839]67    public double this[string variableName, int rowIndex] {
68      get {
69        int columnIndex = GetVariableIndex(variableName);
70        return data[rowIndex, columnIndex];
71      }
72    }
[3933]73
74    public double[] GetVariableValues(int variableIndex) {
75      return GetVariableValues(variableIndex, 0, Rows);
[1287]76    }
[3294]77    public double[] GetVariableValues(int variableIndex, int start, int end) {
[2311]78      if (start < 0 || !(start <= end))
79        throw new ArgumentException("Start must be between 0 and end (" + end + ").");
[3308]80      if (end > Rows || end < start)
81        throw new ArgumentException("End must be between start (" + start + ") and dataset rows (" + Rows + ").");
[2311]82
83      double[] values = new double[end - start];
84      for (int i = 0; i < end - start; i++)
[3253]85        values[i] = data[i + start, variableIndex];
[2311]86      return values;
87    }
[3933]88    public double[] GetVariableValues(string variableName) {
89      return GetVariableValues(GetVariableIndex(variableName), 0, Rows);
90    }
[3294]91    public double[] GetVariableValues(string variableName, int start, int end) {
92      return GetVariableValues(GetVariableIndex(variableName), start, end);
[2311]93    }
94
[3994]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    }
[4031]106    public IEnumerable<double> GetEnumeratedVariableValues(int variableIndex, IEnumerable<int> rows) {
107      foreach (int row in rows)
108        yield return data[row, variableIndex];
109    }
110
[3994]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    }
[4031]117    public IEnumerable<double> GetEnumeratedVariableValues(string variableName, IEnumerable<int> rows) {
118      return GetEnumeratedVariableValues(GetVariableIndex(variableName), rows);
119    }
[3994]120
[3294]121    public string GetVariableName(int variableIndex) {
[2319]122      return variableNames[variableIndex];
123    }
124
[3294]125    public int GetVariableIndex(string variableName) {
[2319]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    }
[2310]131
[3933]132    public double[,] GetClonedData() {
133      return (double[,])data.Clone();
[2]134    }
135
[3253]136    public override IDeepCloneable Clone(Cloner cloner) {
[4722]137      return new Dataset(this, cloner);
[2319]138    }
139
[3253]140    public event EventHandler Reset;
141    private void OnReset(EventArgs e) {
142      var listeners = Reset;
143      if (listeners != null) listeners(this, e);
[2319]144    }
[2]145
[3253]146    #region IStringConvertibleMatrix Members
[237]147
[3253]148    public int Rows {
[3933]149      get { return data.GetLength(0); }
150      set { throw new NotSupportedException(); }
[2]151    }
152
[3253]153    public int Columns {
[3933]154      get { return data.GetLength(1); }
155      set { throw new NotSupportedException(); }
[2]156    }
157
[3321]158    public bool SortableView {
[3933]159      get { return false; }
160      set { throw new NotSupportedException(); }
[3321]161    }
162
[3430]163    public bool ReadOnly {
[3933]164      get { return true; }
[3430]165    }
166
[3308]167    IEnumerable<string> IStringConvertibleMatrix.ColumnNames {
168      get { return this.VariableNames; }
[3933]169      set { throw new NotSupportedException(); }
[3308]170    }
171
[3311]172    IEnumerable<string> IStringConvertibleMatrix.RowNames {
173      get { return new List<string>(); }
[3933]174      set { throw new NotSupportedException(); }
[3311]175    }
176
[3253]177    public bool Validate(string value, out string errorMessage) {
[3933]178      throw new NotSupportedException();
[3253]179    }
[2]180
[3253]181    public string GetValue(int rowIndex, int columnIndex) {
[3308]182      return data[rowIndex, columnIndex].ToString();
[2]183    }
184
[3253]185    public bool SetValue(string value, int rowIndex, int columnIndex) {
[3933]186      throw new NotSupportedException();
[237]187    }
188
[5150]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    }
[3321]199    public event EventHandler ColumnNamesChanged;
200    private void OnColumnNamesChanged() {
[4722]201      EventHandler listeners = ColumnNamesChanged;
202      if (listeners != null)
203        listeners(this, EventArgs.Empty);
[3321]204    }
205    public event EventHandler RowNamesChanged;
206    private void OnRowNamesChanged() {
[4722]207      EventHandler listeners = RowNamesChanged;
208      if (listeners != null)
209        listeners(this, EventArgs.Empty);
[3321]210    }
211    public event EventHandler SortableViewChanged;
212    private void OnSortableViewChanged() {
[4722]213      EventHandler listeners = SortableViewChanged;
214      if (listeners != null)
215        listeners(this, EventArgs.Empty);
[3321]216    }
[3253]217    public event EventHandler<EventArgs<int, int>> ItemChanged;
[3321]218    private void OnItemChanged(int rowIndex, int columnIndex) {
[4722]219      var listeners = ItemChanged;
220      if (listeners != null)
221        listeners(this, new EventArgs<int, int>(rowIndex, columnIndex));
[3321]222      OnToStringChanged();
223    }
[2012]224    #endregion
[2]225  }
226}
Note: See TracBrowser for help on using the repository browser.