Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1418: worked on different ProblemData classes.

File size: 7.5 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 { return variableNameToVariableIndexMapping.Keys; }
61      private set {
62        if (variableNameToVariableIndexMapping != null) throw new InvalidOperationException("VariableNames can only be set once.");
63        this.variableNameToVariableIndexMapping = new Dictionary<string, int>();
64        int i = 0;
65        foreach (string variableName in value) {
66          this.variableNameToVariableIndexMapping.Add(variableName, i);
67          i++;
68        }
69        variableNameToVariableIndexMapping = variableIndexToVariableNameMapping.ToDictionary(x => x.Value, x => x.Key);
70      }
71    }
72
73    [Storable]
74    private double[,] data;
75    private double[,] Data {
76      get { return data; }
77    }
78
79    // elementwise access
80    public double this[int rowIndex, int columnIndex] {
81      get { return data[rowIndex, columnIndex]; }
82    }
83    public double this[string variableName, int rowIndex] {
84      get {
85        int columnIndex = GetVariableIndex(variableName);
86        return data[rowIndex, columnIndex];
87      }
88    }
89
90    public double[] GetVariableValues(int variableIndex) {
91      return GetVariableValues(variableIndex, 0, Rows);
92    }
93    public double[] GetVariableValues(string variableName) {
94      return GetVariableValues(GetVariableIndex(variableName), 0, Rows);
95    }
96    public double[] GetVariableValues(int variableIndex, int start, int end) {
97      return GetEnumeratedVariableValues(variableIndex, start, end).ToArray();
98    }
99    public double[] GetVariableValues(string variableName, int start, int end) {
100      return GetVariableValues(GetVariableIndex(variableName), start, end);
101    }
102
103    public IEnumerable<double> GetEnumeratedVariableValues(int variableIndex) {
104      return GetEnumeratedVariableValues(variableIndex, 0, Rows);
105    }
106    public IEnumerable<double> GetEnumeratedVariableValues(int variableIndex, int start, int end) {
107      if (start < 0 || !(start <= end))
108        throw new ArgumentException("Start must be between 0 and end (" + end + ").");
109      if (end > Rows || end < start)
110        throw new ArgumentException("End must be between start (" + start + ") and dataset rows (" + Rows + ").");
111
112      for (int i = start; i < end; i++)
113        yield return data[i, variableIndex];
114    }
115    public IEnumerable<double> GetEnumeratedVariableValues(int variableIndex, IEnumerable<int> rows) {
116      foreach (int row in rows)
117        yield return data[row, variableIndex];
118    }
119
120    public IEnumerable<double> GetEnumeratedVariableValues(string variableName) {
121      return GetEnumeratedVariableValues(GetVariableIndex(variableName), 0, Rows);
122    }
123    public IEnumerable<double> GetEnumeratedVariableValues(string variableName, int start, int end) {
124      return GetEnumeratedVariableValues(GetVariableIndex(variableName), start, end);
125    }
126    public IEnumerable<double> GetEnumeratedVariableValues(string variableName, IEnumerable<int> rows) {
127      return GetEnumeratedVariableValues(GetVariableIndex(variableName), rows);
128    }
129
130    public string GetVariableName(int variableIndex) {
131      try {
132        return variableIndexToVariableNameMapping[variableIndex];
133      }
134      catch (KeyNotFoundException ex) {
135        throw new ArgumentException("The variable index " + variableIndex + " was not found.", ex);
136      }
137    }
138    public int GetVariableIndex(string variableName) {
139      try {
140        return variableNameToVariableIndexMapping[variableName];
141      }
142      catch (KeyNotFoundException ex) {
143        throw new ArgumentException("The variable name " + variableName + " was not found.", ex);
144      }
145    }
146
147    #region IStringConvertibleMatrix Members
148    public int Rows {
149      get { return data.GetLength(0); }
150      set { throw new NotSupportedException(); }
151    }
152    public int Columns {
153      get { return data.GetLength(1); }
154      set { throw new NotSupportedException(); }
155    }
156
157    public bool SortableView {
158      get { return false; }
159      set { throw new NotSupportedException(); }
160    }
161    public bool ReadOnly {
162      get { return true; }
163    }
164
165    IEnumerable<string> IStringConvertibleMatrix.ColumnNames {
166      get { return this.VariableNames; }
167      set { throw new NotSupportedException(); }
168    }
169    IEnumerable<string> IStringConvertibleMatrix.RowNames {
170      get { return Enumerable.Empty<string>(); }
171      set { throw new NotSupportedException(); }
172    }
173
174    public string GetValue(int rowIndex, int columnIndex) {
175      return data[rowIndex, columnIndex].ToString();
176    }
177    public bool SetValue(string value, int rowIndex, int columnIndex) {
178      throw new NotSupportedException();
179    }
180    public bool Validate(string value, out string errorMessage) {
181      throw new NotSupportedException();
182    }
183
184    public event EventHandler ColumnsChanged { add { } remove { } }
185    public event EventHandler RowsChanged { add { } remove { } }
186    public event EventHandler ColumnNamesChanged { add { } remove { } }
187    public event EventHandler RowNamesChanged { add { } remove { } }
188    public event EventHandler SortableViewChanged { add { } remove { } }
189    public event EventHandler<EventArgs<int, int>> ItemChanged { add { } remove { } }
190    public event EventHandler Reset { add { } remove { } }
191    #endregion
192  }
193}
Note: See TracBrowser for help on using the repository browser.