Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.DataAnalysis/3.4/Dataset.cs @ 6415

Last change on this file since 6415 was 5847, checked in by mkommend, 14 years ago

#1418: Adapted data analysis classes to new parameter ctors.

File size: 7.8 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) {
[5559]38      variableNameToVariableIndexMapping = original.variableNameToVariableIndexMapping;
[4722]39      data = original.data;
[2319]40    }
[5552]41    public override IDeepCloneable Clone(Cloner cloner) {
42      return new Dataset(this, cloner);
43    }
44
[5847]45    public Dataset()
46      : base() {
47      Name = "-";
48      VariableNames = Enumerable.Empty<string>();
49      data = new double[0, 0];
50    }
51
[3264]52    public Dataset(IEnumerable<string> variableNames, double[,] data)
[3933]53      : base() {
[2319]54      Name = "-";
[3264]55      if (variableNames.Count() != data.GetLength(1)) {
56        throw new ArgumentException("Number of variable names doesn't match the number of columns of data");
57      }
[3933]58      this.data = (double[,])data.Clone();
[5552]59      VariableNames = variableNames;
[2038]60    }
61
[5552]62
[5559]63    private Dictionary<string, int> variableNameToVariableIndexMapping;
64    private Dictionary<int, string> variableIndexToVariableNameMapping;
[3294]65    [Storable]
[2319]66    public IEnumerable<string> VariableNames {
[5689]67      get {
68        // convert KeyCollection to an array first for persistence
69        return variableNameToVariableIndexMapping.Keys.ToArray();
70      }
[5552]71      private set {
[5559]72        if (variableNameToVariableIndexMapping != null) throw new InvalidOperationException("VariableNames can only be set once.");
73        this.variableNameToVariableIndexMapping = new Dictionary<string, int>();
[5570]74        this.variableIndexToVariableNameMapping = new Dictionary<int, string>();
[5552]75        int i = 0;
76        foreach (string variableName in value) {
[5559]77          this.variableNameToVariableIndexMapping.Add(variableName, i);
[5570]78          this.variableIndexToVariableNameMapping.Add(i, variableName);
[5552]79          i++;
80        }
81      }
[333]82    }
83
[3294]84    [Storable]
[3308]85    private double[,] data;
86    private double[,] Data {
[3253]87      get { return data; }
[2]88    }
89
[3253]90    // elementwise access
91    public double this[int rowIndex, int columnIndex] {
92      get { return data[rowIndex, columnIndex]; }
[2]93    }
[3839]94    public double this[string variableName, int rowIndex] {
95      get {
96        int columnIndex = GetVariableIndex(variableName);
97        return data[rowIndex, columnIndex];
98      }
99    }
[3933]100
101    public double[] GetVariableValues(int variableIndex) {
102      return GetVariableValues(variableIndex, 0, Rows);
[1287]103    }
[3933]104    public double[] GetVariableValues(string variableName) {
105      return GetVariableValues(GetVariableIndex(variableName), 0, Rows);
106    }
[5552]107    public double[] GetVariableValues(int variableIndex, int start, int end) {
108      return GetEnumeratedVariableValues(variableIndex, start, end).ToArray();
109    }
[3294]110    public double[] GetVariableValues(string variableName, int start, int end) {
111      return GetVariableValues(GetVariableIndex(variableName), start, end);
[2311]112    }
113
[3994]114    public IEnumerable<double> GetEnumeratedVariableValues(int variableIndex) {
115      return GetEnumeratedVariableValues(variableIndex, 0, Rows);
116    }
117    public IEnumerable<double> GetEnumeratedVariableValues(int variableIndex, int start, int end) {
118      if (start < 0 || !(start <= end))
119        throw new ArgumentException("Start must be between 0 and end (" + end + ").");
120      if (end > Rows || end < start)
121        throw new ArgumentException("End must be between start (" + start + ") and dataset rows (" + Rows + ").");
[5552]122
123      for (int i = start; i < end; i++)
124        yield return data[i, variableIndex];
[3994]125    }
[4031]126    public IEnumerable<double> GetEnumeratedVariableValues(int variableIndex, IEnumerable<int> rows) {
127      foreach (int row in rows)
128        yield return data[row, variableIndex];
129    }
130
[3994]131    public IEnumerable<double> GetEnumeratedVariableValues(string variableName) {
132      return GetEnumeratedVariableValues(GetVariableIndex(variableName), 0, Rows);
133    }
134    public IEnumerable<double> GetEnumeratedVariableValues(string variableName, int start, int end) {
135      return GetEnumeratedVariableValues(GetVariableIndex(variableName), start, end);
136    }
[4031]137    public IEnumerable<double> GetEnumeratedVariableValues(string variableName, IEnumerable<int> rows) {
138      return GetEnumeratedVariableValues(GetVariableIndex(variableName), rows);
139    }
[3994]140
[3294]141    public string GetVariableName(int variableIndex) {
[5559]142      try {
143        return variableIndexToVariableNameMapping[variableIndex];
144      }
145      catch (KeyNotFoundException ex) {
146        throw new ArgumentException("The variable index " + variableIndex + " was not found.", ex);
147      }
[2319]148    }
[3294]149    public int GetVariableIndex(string variableName) {
[5552]150      try {
[5559]151        return variableNameToVariableIndexMapping[variableName];
[2319]152      }
[5552]153      catch (KeyNotFoundException ex) {
154        throw new ArgumentException("The variable name " + variableName + " was not found.", ex);
155      }
[2319]156    }
[2310]157
[3253]158    #region IStringConvertibleMatrix Members
159    public int Rows {
[3933]160      get { return data.GetLength(0); }
161      set { throw new NotSupportedException(); }
[2]162    }
[3253]163    public int Columns {
[3933]164      get { return data.GetLength(1); }
165      set { throw new NotSupportedException(); }
[2]166    }
167
[3321]168    public bool SortableView {
[3933]169      get { return false; }
170      set { throw new NotSupportedException(); }
[3321]171    }
[3430]172    public bool ReadOnly {
[3933]173      get { return true; }
[3430]174    }
175
[3308]176    IEnumerable<string> IStringConvertibleMatrix.ColumnNames {
177      get { return this.VariableNames; }
[3933]178      set { throw new NotSupportedException(); }
[3308]179    }
[3311]180    IEnumerable<string> IStringConvertibleMatrix.RowNames {
[5552]181      get { return Enumerable.Empty<string>(); }
[3933]182      set { throw new NotSupportedException(); }
[3311]183    }
184
[3253]185    public string GetValue(int rowIndex, int columnIndex) {
[3308]186      return data[rowIndex, columnIndex].ToString();
[2]187    }
[3253]188    public bool SetValue(string value, int rowIndex, int columnIndex) {
[3933]189      throw new NotSupportedException();
[237]190    }
[5552]191    public bool Validate(string value, out string errorMessage) {
192      throw new NotSupportedException();
193    }
[237]194
[5552]195    public event EventHandler ColumnsChanged { add { } remove { } }
196    public event EventHandler RowsChanged { add { } remove { } }
197    public event EventHandler ColumnNamesChanged { add { } remove { } }
198    public event EventHandler RowNamesChanged { add { } remove { } }
199    public event EventHandler SortableViewChanged { add { } remove { } }
200    public event EventHandler<EventArgs<int, int>> ItemChanged { add { } remove { } }
[5554]201    public event EventHandler Reset { add { } remove { } }
[2012]202    #endregion
[2]203  }
204}
Note: See TracBrowser for help on using the repository browser.