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