Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.DataAnalysis/3.3/Dataset.cs @ 3933

Last change on this file since 3933 was 3933, checked in by mkommend, 14 years ago

removed cloning of dataset and made it readonly (ticket #938)

File size: 6.2 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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.Xml;
25using System.Globalization;
26using System.Text;
27using System.Linq;
28using HeuristicLab.Common;
29using HeuristicLab.Core;
30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
31using HeuristicLab.Data;
32
33namespace HeuristicLab.Problems.DataAnalysis {
34  [Item("Dataset", "Represents a dataset containing data that should be analyzed.")]
35  [StorableClass]
36  public sealed class Dataset : NamedItem, IStringConvertibleMatrix {
37    [StorableConstructor]
38    protected Dataset(bool deserializing)
39      : base(deserializing) {
40    }
41
42    public Dataset(IEnumerable<string> variableNames, double[,] data)
43      : base() {
44      Name = "-";
45      if (variableNames.Count() != data.GetLength(1)) {
46        throw new ArgumentException("Number of variable names doesn't match the number of columns of data");
47      }
48      this.data = (double[,])data.Clone();
49      this.variableNames = variableNames.ToArray();
50    }
51
52    [Storable]
53    private string[] variableNames;
54    public IEnumerable<string> VariableNames {
55      get { return variableNames; }
56    }
57
58    [Storable]
59    private double[,] data;
60    private double[,] Data {
61      get { return data; }
62    }
63
64    // elementwise access
65    public double this[int rowIndex, int columnIndex] {
66      get { return data[rowIndex, columnIndex]; }
67    }
68
69    public double this[string variableName, int rowIndex] {
70      get {
71        int columnIndex = GetVariableIndex(variableName);
72        return data[rowIndex, columnIndex];
73      }
74    }
75
76    public double[] GetVariableValues(int variableIndex) {
77      return GetVariableValues(variableIndex, 0, Rows);
78    }
79
80    public double[] GetVariableValues(int variableIndex, int start, int end) {
81      if (start < 0 || !(start <= end))
82        throw new ArgumentException("Start must be between 0 and end (" + end + ").");
83      if (end > Rows || end < start)
84        throw new ArgumentException("End must be between start (" + start + ") and dataset rows (" + Rows + ").");
85
86      double[] values = new double[end - start];
87      for (int i = 0; i < end - start; i++)
88        values[i] = data[i + start, variableIndex];
89      return values;
90    }
91
92    public double[] GetVariableValues(string variableName) {
93      return GetVariableValues(GetVariableIndex(variableName), 0, Rows);
94    }
95
96    public double[] GetVariableValues(string variableName, int start, int end) {
97      return GetVariableValues(GetVariableIndex(variableName), start, end);
98    }
99
100    #region Variable name methods
101    public string GetVariableName(int variableIndex) {
102      return variableNames[variableIndex];
103    }
104
105    public int GetVariableIndex(string variableName) {
106      for (int i = 0; i < variableNames.Length; i++) {
107        if (variableNames[i].Equals(variableName)) return i;
108      }
109      throw new ArgumentException("The variable name " + variableName + " was not found.");
110    }
111    #endregion
112
113    public double[,] GetClonedData() {
114      return (double[,])data.Clone();
115    }
116
117    public override IDeepCloneable Clone(Cloner cloner) {
118      return this;
119    }
120
121    public event EventHandler Reset;
122    private void OnReset(EventArgs e) {
123      var listeners = Reset;
124      if (listeners != null) listeners(this, e);
125    }
126
127    #region IStringConvertibleMatrix Members
128
129    public int Rows {
130      get { return data.GetLength(0); }
131      set { throw new NotSupportedException(); }
132    }
133
134    public int Columns {
135      get { return data.GetLength(1); }
136      set { throw new NotSupportedException(); }
137    }
138
139    public bool SortableView {
140      get { return false; }
141      set { throw new NotSupportedException(); }
142    }
143
144    public bool ReadOnly {
145      get { return true; }
146    }
147
148    IEnumerable<string> IStringConvertibleMatrix.ColumnNames {
149      get { return this.VariableNames; }
150      set { throw new NotSupportedException(); }
151    }
152
153    IEnumerable<string> IStringConvertibleMatrix.RowNames {
154      get { return new List<string>(); }
155      set { throw new NotSupportedException(); }
156    }
157
158    public bool Validate(string value, out string errorMessage) {
159      throw new NotSupportedException();
160    }
161
162    public string GetValue(int rowIndex, int columnIndex) {
163      return data[rowIndex, columnIndex].ToString();
164    }
165
166    public bool SetValue(string value, int rowIndex, int columnIndex) {
167      throw new NotSupportedException();
168    }
169
170    public event EventHandler ColumnNamesChanged;
171    private void OnColumnNamesChanged() {
172      EventHandler handler = ColumnNamesChanged;
173      if (handler != null)
174        handler(this, EventArgs.Empty);
175    }
176    public event EventHandler RowNamesChanged;
177    private void OnRowNamesChanged() {
178      EventHandler handler = RowNamesChanged;
179      if (handler != null)
180        handler(this, EventArgs.Empty);
181    }
182    public event EventHandler SortableViewChanged;
183    private void OnSortableViewChanged() {
184      EventHandler handler = SortableViewChanged;
185      if (handler != null)
186        handler(this, EventArgs.Empty);
187    }
188    public event EventHandler<EventArgs<int, int>> ItemChanged;
189    private void OnItemChanged(int rowIndex, int columnIndex) {
190      if (ItemChanged != null)
191        ItemChanged(this, new EventArgs<int, int>(rowIndex, columnIndex));
192      OnToStringChanged();
193    }
194    #endregion
195  }
196}
Note: See TracBrowser for help on using the repository browser.