Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DataAnalysis Refactoring/HeuristicLab.Problems.DataAnalysis/3.4/Dataset.cs @ 5728

Last change on this file since 5728 was 5689, checked in by gkronber, 14 years ago

#1418 fixed persistence problems.

File size: 7.6 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2011 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
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.Linq;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29
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 {
34    [StorableConstructor]
35    private Dataset(bool deserializing) : base(deserializing) { }
36    private Dataset(Dataset original, Cloner cloner)
37      : base(original, cloner) {
38      variableNameToVariableIndexMapping = original.variableNameToVariableIndexMapping;
39      data = original.data;
40    }
41    public override IDeepCloneable Clone(Cloner cloner) {
42      return new Dataset(this, cloner);
43    }
44
45    public Dataset(IEnumerable<string> variableNames, double[,] data)
46      : base() {
47      Name = "-";
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      }
51      this.data = (double[,])data.Clone();
52      VariableNames = variableNames;
53    }
54
55
56    private Dictionary<string, int> variableNameToVariableIndexMapping;
57    private Dictionary<int, string> variableIndexToVariableNameMapping;
58    [Storable]
59    public IEnumerable<string> VariableNames {
60      get {
61        // convert KeyCollection to an array first for persistence
62        return variableNameToVariableIndexMapping.Keys.ToArray();
63      }
64      private set {
65        if (variableNameToVariableIndexMapping != null) throw new InvalidOperationException("VariableNames can only be set once.");
66        this.variableNameToVariableIndexMapping = new Dictionary<string, int>();
67        this.variableIndexToVariableNameMapping = new Dictionary<int, string>();
68        int i = 0;
69        foreach (string variableName in value) {
70          this.variableNameToVariableIndexMapping.Add(variableName, i);
71          this.variableIndexToVariableNameMapping.Add(i, variableName);
72          i++;
73        }
74      }
75    }
76
77    [Storable]
78    private double[,] data;
79    private double[,] Data {
80      get { return data; }
81    }
82
83    // elementwise access
84    public double this[int rowIndex, int columnIndex] {
85      get { return data[rowIndex, columnIndex]; }
86    }
87    public double this[string variableName, int rowIndex] {
88      get {
89        int columnIndex = GetVariableIndex(variableName);
90        return data[rowIndex, columnIndex];
91      }
92    }
93
94    public double[] GetVariableValues(int variableIndex) {
95      return GetVariableValues(variableIndex, 0, Rows);
96    }
97    public double[] GetVariableValues(string variableName) {
98      return GetVariableValues(GetVariableIndex(variableName), 0, Rows);
99    }
100    public double[] GetVariableValues(int variableIndex, int start, int end) {
101      return GetEnumeratedVariableValues(variableIndex, start, end).ToArray();
102    }
103    public double[] GetVariableValues(string variableName, int start, int end) {
104      return GetVariableValues(GetVariableIndex(variableName), start, end);
105    }
106
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 + ").");
115
116      for (int i = start; i < end; i++)
117        yield return data[i, variableIndex];
118    }
119    public IEnumerable<double> GetEnumeratedVariableValues(int variableIndex, IEnumerable<int> rows) {
120      foreach (int row in rows)
121        yield return data[row, variableIndex];
122    }
123
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    }
130    public IEnumerable<double> GetEnumeratedVariableValues(string variableName, IEnumerable<int> rows) {
131      return GetEnumeratedVariableValues(GetVariableIndex(variableName), rows);
132    }
133
134    public string GetVariableName(int variableIndex) {
135      try {
136        return variableIndexToVariableNameMapping[variableIndex];
137      }
138      catch (KeyNotFoundException ex) {
139        throw new ArgumentException("The variable index " + variableIndex + " was not found.", ex);
140      }
141    }
142    public int GetVariableIndex(string variableName) {
143      try {
144        return variableNameToVariableIndexMapping[variableName];
145      }
146      catch (KeyNotFoundException ex) {
147        throw new ArgumentException("The variable name " + variableName + " was not found.", ex);
148      }
149    }
150
151    #region IStringConvertibleMatrix Members
152    public int Rows {
153      get { return data.GetLength(0); }
154      set { throw new NotSupportedException(); }
155    }
156    public int Columns {
157      get { return data.GetLength(1); }
158      set { throw new NotSupportedException(); }
159    }
160
161    public bool SortableView {
162      get { return false; }
163      set { throw new NotSupportedException(); }
164    }
165    public bool ReadOnly {
166      get { return true; }
167    }
168
169    IEnumerable<string> IStringConvertibleMatrix.ColumnNames {
170      get { return this.VariableNames; }
171      set { throw new NotSupportedException(); }
172    }
173    IEnumerable<string> IStringConvertibleMatrix.RowNames {
174      get { return Enumerable.Empty<string>(); }
175      set { throw new NotSupportedException(); }
176    }
177
178    public string GetValue(int rowIndex, int columnIndex) {
179      return data[rowIndex, columnIndex].ToString();
180    }
181    public bool SetValue(string value, int rowIndex, int columnIndex) {
182      throw new NotSupportedException();
183    }
184    public bool Validate(string value, out string errorMessage) {
185      throw new NotSupportedException();
186    }
187
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 { } }
194    public event EventHandler Reset { add { } remove { } }
195    #endregion
196  }
197}
Note: See TracBrowser for help on using the repository browser.