Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 5809 was 5809, checked in by mkommend, 13 years ago

#1418: Reintegrated branch into trunk.

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