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
RevLine 
[2]1#region License Information
2/* HeuristicLab
[3253]3 * Copyright (C) 2002-2010 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;
24using System.Xml;
25using System.Globalization;
26using System.Text;
[2285]27using System.Linq;
[3376]28using HeuristicLab.Common;
[3253]29using HeuristicLab.Core;
30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
31using HeuristicLab.Data;
[2]32
[3253]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 {
[3981]37    // empty constructor for cloning
[3994]38    private Dataset()
39      : base() {
[3981]40    }
[3994]41
[3933]42    [StorableConstructor]
[4031]43    private Dataset(bool deserializing)
[3933]44      : base(deserializing) {
[2319]45    }
[2]46
[3264]47    public Dataset(IEnumerable<string> variableNames, double[,] data)
[3933]48      : base() {
[2319]49      Name = "-";
[3264]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      }
[3933]53      this.data = (double[,])data.Clone();
[3442]54      this.variableNames = variableNames.ToArray();
[2038]55    }
56
[3294]57    [Storable]
[3308]58    private string[] variableNames;
[2319]59    public IEnumerable<string> VariableNames {
60      get { return variableNames; }
[333]61    }
62
[3294]63    [Storable]
[3308]64    private double[,] data;
65    private double[,] Data {
[3253]66      get { return data; }
[2]67    }
68
[3253]69    // elementwise access
70    public double this[int rowIndex, int columnIndex] {
71      get { return data[rowIndex, columnIndex]; }
[2]72    }
[3839]73    public double this[string variableName, int rowIndex] {
74      get {
75        int columnIndex = GetVariableIndex(variableName);
76        return data[rowIndex, columnIndex];
77      }
78    }
[3933]79
80    public double[] GetVariableValues(int variableIndex) {
81      return GetVariableValues(variableIndex, 0, Rows);
[1287]82    }
[3294]83    public double[] GetVariableValues(int variableIndex, int start, int end) {
[2311]84      if (start < 0 || !(start <= end))
85        throw new ArgumentException("Start must be between 0 and end (" + end + ").");
[3308]86      if (end > Rows || end < start)
87        throw new ArgumentException("End must be between start (" + start + ") and dataset rows (" + Rows + ").");
[2311]88
89      double[] values = new double[end - start];
90      for (int i = 0; i < end - start; i++)
[3253]91        values[i] = data[i + start, variableIndex];
[2311]92      return values;
93    }
[3933]94    public double[] GetVariableValues(string variableName) {
95      return GetVariableValues(GetVariableIndex(variableName), 0, Rows);
96    }
[3294]97    public double[] GetVariableValues(string variableName, int start, int end) {
98      return GetVariableValues(GetVariableIndex(variableName), start, end);
[2311]99    }
100
[3994]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    }
[4031]112    public IEnumerable<double> GetEnumeratedVariableValues(int variableIndex, IEnumerable<int> rows) {
113      foreach (int row in rows)
114        yield return data[row, variableIndex];
115    }
116
[3994]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    }
[4031]123    public IEnumerable<double> GetEnumeratedVariableValues(string variableName, IEnumerable<int> rows) {
124      return GetEnumeratedVariableValues(GetVariableIndex(variableName), rows);
125    }
[3994]126
127
128
[3294]129    public string GetVariableName(int variableIndex) {
[2319]130      return variableNames[variableIndex];
131    }
132
[3294]133    public int GetVariableIndex(string variableName) {
[2319]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    }
[2310]139
[3933]140    public double[,] GetClonedData() {
141      return (double[,])data.Clone();
[2]142    }
143
[3253]144    public override IDeepCloneable Clone(Cloner cloner) {
[3981]145      Dataset clone = (Dataset)base.Clone(cloner);
146      clone.variableNames = variableNames;
147      clone.data = data;
148      return clone;
[2319]149    }
150
[3253]151    public event EventHandler Reset;
152    private void OnReset(EventArgs e) {
153      var listeners = Reset;
154      if (listeners != null) listeners(this, e);
[2319]155    }
[2]156
[3253]157    #region IStringConvertibleMatrix Members
[237]158
[3253]159    public int Rows {
[3933]160      get { return data.GetLength(0); }
161      set { throw new NotSupportedException(); }
[2]162    }
163
[3253]164    public int Columns {
[3933]165      get { return data.GetLength(1); }
166      set { throw new NotSupportedException(); }
[2]167    }
168
[3321]169    public bool SortableView {
[3933]170      get { return false; }
171      set { throw new NotSupportedException(); }
[3321]172    }
173
[3430]174    public bool ReadOnly {
[3933]175      get { return true; }
[3430]176    }
177
[3308]178    IEnumerable<string> IStringConvertibleMatrix.ColumnNames {
179      get { return this.VariableNames; }
[3933]180      set { throw new NotSupportedException(); }
[3308]181    }
182
[3311]183    IEnumerable<string> IStringConvertibleMatrix.RowNames {
184      get { return new List<string>(); }
[3933]185      set { throw new NotSupportedException(); }
[3311]186    }
187
[3253]188    public bool Validate(string value, out string errorMessage) {
[3933]189      throw new NotSupportedException();
[3253]190    }
[2]191
[3253]192    public string GetValue(int rowIndex, int columnIndex) {
[3308]193      return data[rowIndex, columnIndex].ToString();
[2]194    }
195
[3253]196    public bool SetValue(string value, int rowIndex, int columnIndex) {
[3933]197      throw new NotSupportedException();
[237]198    }
199
[3321]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    }
[3253]218    public event EventHandler<EventArgs<int, int>> ItemChanged;
[3321]219    private void OnItemChanged(int rowIndex, int columnIndex) {
220      if (ItemChanged != null)
221        ItemChanged(this, new EventArgs<int, int>(rowIndex, columnIndex));
222      OnToStringChanged();
223    }
[2012]224    #endregion
[2]225  }
226}
Note: See TracBrowser for help on using the repository browser.