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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Xml;
|
---|
25 | using System.Globalization;
|
---|
26 | using System.Text;
|
---|
27 | using System.Linq;
|
---|
28 | using HeuristicLab.Common;
|
---|
29 | using HeuristicLab.Core;
|
---|
30 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
31 | using HeuristicLab.Data;
|
---|
32 |
|
---|
33 | namespace 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 | // empty constructor for cloning
|
---|
38 | private Dataset()
|
---|
39 | : base() {
|
---|
40 | }
|
---|
41 |
|
---|
42 | [StorableConstructor]
|
---|
43 | protected Dataset(bool deserializing)
|
---|
44 | : base(deserializing) {
|
---|
45 | }
|
---|
46 |
|
---|
47 | public Dataset(IEnumerable<string> variableNames, double[,] data)
|
---|
48 | : base() {
|
---|
49 | Name = "-";
|
---|
50 | if (variableNames.Count() != data.GetLength(1)) {
|
---|
51 | throw new ArgumentException("Number of variable names doesn't match the number of columns of data");
|
---|
52 | }
|
---|
53 | this.data = (double[,])data.Clone();
|
---|
54 | this.variableNames = variableNames.ToArray();
|
---|
55 | }
|
---|
56 |
|
---|
57 | [Storable]
|
---|
58 | private string[] variableNames;
|
---|
59 | public IEnumerable<string> VariableNames {
|
---|
60 | get { return variableNames; }
|
---|
61 | }
|
---|
62 |
|
---|
63 | [Storable]
|
---|
64 | private double[,] data;
|
---|
65 | private double[,] Data {
|
---|
66 | get { return data; }
|
---|
67 | }
|
---|
68 |
|
---|
69 | // elementwise access
|
---|
70 | public double this[int rowIndex, int columnIndex] {
|
---|
71 | get { return data[rowIndex, columnIndex]; }
|
---|
72 | }
|
---|
73 | public double this[string variableName, int rowIndex] {
|
---|
74 | get {
|
---|
75 | int columnIndex = GetVariableIndex(variableName);
|
---|
76 | return data[rowIndex, columnIndex];
|
---|
77 | }
|
---|
78 | }
|
---|
79 |
|
---|
80 | public double[] GetVariableValues(int variableIndex) {
|
---|
81 | return GetVariableValues(variableIndex, 0, Rows);
|
---|
82 | }
|
---|
83 | public double[] GetVariableValues(int variableIndex, int start, int end) {
|
---|
84 | if (start < 0 || !(start <= end))
|
---|
85 | throw new ArgumentException("Start must be between 0 and end (" + end + ").");
|
---|
86 | if (end > Rows || end < start)
|
---|
87 | throw new ArgumentException("End must be between start (" + start + ") and dataset rows (" + Rows + ").");
|
---|
88 |
|
---|
89 | double[] values = new double[end - start];
|
---|
90 | for (int i = 0; i < end - start; i++)
|
---|
91 | values[i] = data[i + start, variableIndex];
|
---|
92 | return values;
|
---|
93 | }
|
---|
94 | public double[] GetVariableValues(string variableName) {
|
---|
95 | return GetVariableValues(GetVariableIndex(variableName), 0, Rows);
|
---|
96 | }
|
---|
97 | public double[] GetVariableValues(string variableName, int start, int end) {
|
---|
98 | return GetVariableValues(GetVariableIndex(variableName), start, end);
|
---|
99 | }
|
---|
100 |
|
---|
101 | public IEnumerable<double> GetEnumeratedVariableValues(int variableIndex) {
|
---|
102 | return GetEnumeratedVariableValues(variableIndex, 0, Rows);
|
---|
103 | }
|
---|
104 | public IEnumerable<double> GetEnumeratedVariableValues(int variableIndex, int start, int end) {
|
---|
105 | if (start < 0 || !(start <= end))
|
---|
106 | throw new ArgumentException("Start must be between 0 and end (" + end + ").");
|
---|
107 | if (end > Rows || end < start)
|
---|
108 | throw new ArgumentException("End must be between start (" + start + ") and dataset rows (" + Rows + ").");
|
---|
109 | for (int i = 0; i < end - start; i++)
|
---|
110 | yield return data[i + start, variableIndex];
|
---|
111 | }
|
---|
112 | public IEnumerable<double> GetEnumeratedVariableValues(string variableName) {
|
---|
113 | return GetEnumeratedVariableValues(GetVariableIndex(variableName), 0, Rows);
|
---|
114 | }
|
---|
115 | public IEnumerable<double> GetEnumeratedVariableValues(string variableName, int start, int end) {
|
---|
116 | return GetEnumeratedVariableValues(GetVariableIndex(variableName), start, end);
|
---|
117 | }
|
---|
118 |
|
---|
119 |
|
---|
120 |
|
---|
121 | public string GetVariableName(int variableIndex) {
|
---|
122 | return variableNames[variableIndex];
|
---|
123 | }
|
---|
124 |
|
---|
125 | public int GetVariableIndex(string variableName) {
|
---|
126 | for (int i = 0; i < variableNames.Length; i++) {
|
---|
127 | if (variableNames[i].Equals(variableName)) return i;
|
---|
128 | }
|
---|
129 | throw new ArgumentException("The variable name " + variableName + " was not found.");
|
---|
130 | }
|
---|
131 |
|
---|
132 | public double[,] GetClonedData() {
|
---|
133 | return (double[,])data.Clone();
|
---|
134 | }
|
---|
135 |
|
---|
136 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
137 | Dataset clone = (Dataset)base.Clone(cloner);
|
---|
138 | clone.variableNames = variableNames;
|
---|
139 | clone.data = data;
|
---|
140 | return clone;
|
---|
141 | }
|
---|
142 |
|
---|
143 | public event EventHandler Reset;
|
---|
144 | private void OnReset(EventArgs e) {
|
---|
145 | var listeners = Reset;
|
---|
146 | if (listeners != null) listeners(this, e);
|
---|
147 | }
|
---|
148 |
|
---|
149 | #region IStringConvertibleMatrix Members
|
---|
150 |
|
---|
151 | public int Rows {
|
---|
152 | get { return data.GetLength(0); }
|
---|
153 | set { throw new NotSupportedException(); }
|
---|
154 | }
|
---|
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 |
|
---|
166 | public bool ReadOnly {
|
---|
167 | get { return true; }
|
---|
168 | }
|
---|
169 |
|
---|
170 | IEnumerable<string> IStringConvertibleMatrix.ColumnNames {
|
---|
171 | get { return this.VariableNames; }
|
---|
172 | set { throw new NotSupportedException(); }
|
---|
173 | }
|
---|
174 |
|
---|
175 | IEnumerable<string> IStringConvertibleMatrix.RowNames {
|
---|
176 | get { return new List<string>(); }
|
---|
177 | set { throw new NotSupportedException(); }
|
---|
178 | }
|
---|
179 |
|
---|
180 | public bool Validate(string value, out string errorMessage) {
|
---|
181 | throw new NotSupportedException();
|
---|
182 | }
|
---|
183 |
|
---|
184 | public string GetValue(int rowIndex, int columnIndex) {
|
---|
185 | return data[rowIndex, columnIndex].ToString();
|
---|
186 | }
|
---|
187 |
|
---|
188 | public bool SetValue(string value, int rowIndex, int columnIndex) {
|
---|
189 | throw new NotSupportedException();
|
---|
190 | }
|
---|
191 |
|
---|
192 | public event EventHandler ColumnNamesChanged;
|
---|
193 | private void OnColumnNamesChanged() {
|
---|
194 | EventHandler handler = ColumnNamesChanged;
|
---|
195 | if (handler != null)
|
---|
196 | handler(this, EventArgs.Empty);
|
---|
197 | }
|
---|
198 | public event EventHandler RowNamesChanged;
|
---|
199 | private void OnRowNamesChanged() {
|
---|
200 | EventHandler handler = RowNamesChanged;
|
---|
201 | if (handler != null)
|
---|
202 | handler(this, EventArgs.Empty);
|
---|
203 | }
|
---|
204 | public event EventHandler SortableViewChanged;
|
---|
205 | private void OnSortableViewChanged() {
|
---|
206 | EventHandler handler = SortableViewChanged;
|
---|
207 | if (handler != null)
|
---|
208 | handler(this, EventArgs.Empty);
|
---|
209 | }
|
---|
210 | public event EventHandler<EventArgs<int, int>> ItemChanged;
|
---|
211 | private void OnItemChanged(int rowIndex, int columnIndex) {
|
---|
212 | if (ItemChanged != null)
|
---|
213 | ItemChanged(this, new EventArgs<int, int>(rowIndex, columnIndex));
|
---|
214 | OnToStringChanged();
|
---|
215 | }
|
---|
216 | #endregion
|
---|
217 | }
|
---|
218 | }
|
---|