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

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

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

File size: 7.8 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()
46      : base() {
47      Name = "-";
48      VariableNames = Enumerable.Empty<string>();
49      data = new double[0, 0];
50    }
51
52    public Dataset(IEnumerable<string> variableNames, double[,] data)
53      : base() {
54      Name = "-";
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      }
58      this.data = (double[,])data.Clone();
59      VariableNames = variableNames;
60    }
61
62
63    private Dictionary<string, int> variableNameToVariableIndexMapping;
64    private Dictionary<int, string> variableIndexToVariableNameMapping;
65    [Storable]
66    public IEnumerable<string> VariableNames {
67      get {
68        // convert KeyCollection to an array first for persistence
69        return variableNameToVariableIndexMapping.Keys.ToArray();
70      }
71      private set {
72        if (variableNameToVariableIndexMapping != null) throw new InvalidOperationException("VariableNames can only be set once.");
73        this.variableNameToVariableIndexMapping = new Dictionary<string, int>();
74        this.variableIndexToVariableNameMapping = new Dictionary<int, string>();
75        int i = 0;
76        foreach (string variableName in value) {
77          this.variableNameToVariableIndexMapping.Add(variableName, i);
78          this.variableIndexToVariableNameMapping.Add(i, variableName);
79          i++;
80        }
81      }
82    }
83
84    [Storable]
85    private double[,] data;
86    private double[,] Data {
87      get { return data; }
88    }
89
90    // elementwise access
91    public double this[int rowIndex, int columnIndex] {
92      get { return data[rowIndex, columnIndex]; }
93    }
94    public double this[string variableName, int rowIndex] {
95      get {
96        int columnIndex = GetVariableIndex(variableName);
97        return data[rowIndex, columnIndex];
98      }
99    }
100
101    public double[] GetVariableValues(int variableIndex) {
102      return GetVariableValues(variableIndex, 0, Rows);
103    }
104    public double[] GetVariableValues(string variableName) {
105      return GetVariableValues(GetVariableIndex(variableName), 0, Rows);
106    }
107    public double[] GetVariableValues(int variableIndex, int start, int end) {
108      return GetEnumeratedVariableValues(variableIndex, start, end).ToArray();
109    }
110    public double[] GetVariableValues(string variableName, int start, int end) {
111      return GetVariableValues(GetVariableIndex(variableName), start, end);
112    }
113
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 + ").");
122
123      for (int i = start; i < end; i++)
124        yield return data[i, variableIndex];
125    }
126    public IEnumerable<double> GetEnumeratedVariableValues(int variableIndex, IEnumerable<int> rows) {
127      foreach (int row in rows)
128        yield return data[row, variableIndex];
129    }
130
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    }
137    public IEnumerable<double> GetEnumeratedVariableValues(string variableName, IEnumerable<int> rows) {
138      return GetEnumeratedVariableValues(GetVariableIndex(variableName), rows);
139    }
140
141    public string GetVariableName(int variableIndex) {
142      try {
143        return variableIndexToVariableNameMapping[variableIndex];
144      }
145      catch (KeyNotFoundException ex) {
146        throw new ArgumentException("The variable index " + variableIndex + " was not found.", ex);
147      }
148    }
149    public int GetVariableIndex(string variableName) {
150      try {
151        return variableNameToVariableIndexMapping[variableName];
152      }
153      catch (KeyNotFoundException ex) {
154        throw new ArgumentException("The variable name " + variableName + " was not found.", ex);
155      }
156    }
157
158    #region IStringConvertibleMatrix Members
159    public int Rows {
160      get { return data.GetLength(0); }
161      set { throw new NotSupportedException(); }
162    }
163    public int Columns {
164      get { return data.GetLength(1); }
165      set { throw new NotSupportedException(); }
166    }
167
168    public bool SortableView {
169      get { return false; }
170      set { throw new NotSupportedException(); }
171    }
172    public bool ReadOnly {
173      get { return true; }
174    }
175
176    IEnumerable<string> IStringConvertibleMatrix.ColumnNames {
177      get { return this.VariableNames; }
178      set { throw new NotSupportedException(); }
179    }
180    IEnumerable<string> IStringConvertibleMatrix.RowNames {
181      get { return Enumerable.Empty<string>(); }
182      set { throw new NotSupportedException(); }
183    }
184
185    public string GetValue(int rowIndex, int columnIndex) {
186      return data[rowIndex, columnIndex].ToString();
187    }
188    public bool SetValue(string value, int rowIndex, int columnIndex) {
189      throw new NotSupportedException();
190    }
191    public bool Validate(string value, out string errorMessage) {
192      throw new NotSupportedException();
193    }
194
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 { } }
201    public event EventHandler Reset { add { } remove { } }
202    #endregion
203  }
204}
Note: See TracBrowser for help on using the repository browser.