Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 5662 was 5570, checked in by gkronber, 13 years ago

#1418 fixed bug in dataset

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