1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2016 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;
|
---|
24 | using System.Collections.Generic;
|
---|
25 | using System.Collections.ObjectModel;
|
---|
26 | using System.Linq;
|
---|
27 | using HeuristicLab.Common;
|
---|
28 | using HeuristicLab.Core;
|
---|
29 | using HeuristicLab.Data;
|
---|
30 | using HeuristicLab.Persistence;
|
---|
31 |
|
---|
32 | namespace HeuristicLab.Problems.DataAnalysis {
|
---|
33 | [Item("Dataset", "Represents a dataset containing data that should be analyzed.")]
|
---|
34 | [StorableType("b762712e-454c-4fe6-8e1d-0d24dcc2eaea")]
|
---|
35 | public class Dataset : NamedItem, IDataset {
|
---|
36 | [StorableConstructor]
|
---|
37 | protected Dataset(StorableConstructorFlag deserializing) : base(deserializing) { }
|
---|
38 | protected Dataset(Dataset original, Cloner cloner)
|
---|
39 | : base(original, cloner) {
|
---|
40 | variableValues = new Dictionary<string, IList>(original.variableValues);
|
---|
41 | variableNames = new List<string>(original.variableNames);
|
---|
42 | rows = original.rows;
|
---|
43 | }
|
---|
44 | public override IDeepCloneable Clone(Cloner cloner) { return new Dataset(this, cloner); }
|
---|
45 |
|
---|
46 | public Dataset()
|
---|
47 | : base() {
|
---|
48 | Name = "-";
|
---|
49 | VariableNames = Enumerable.Empty<string>();
|
---|
50 | variableValues = new Dictionary<string, IList>();
|
---|
51 | rows = 0;
|
---|
52 | }
|
---|
53 |
|
---|
54 | /// <summary>
|
---|
55 | /// Creates a new dataset. The variableValues are not cloned.
|
---|
56 | /// </summary>
|
---|
57 | /// <param name="variableNames">The names of the variables in the dataset</param>
|
---|
58 | /// <param name="variableValues">The values for the variables (column-oriented storage). Values are not cloned!</param>
|
---|
59 | public Dataset(IEnumerable<string> variableNames, IEnumerable<IList> variableValues)
|
---|
60 | : base() {
|
---|
61 | Name = "-";
|
---|
62 | if (!variableNames.Any()) {
|
---|
63 | this.variableNames = Enumerable.Range(0, variableValues.Count()).Select(x => "Column " + x).ToList();
|
---|
64 | } else if (variableNames.Count() != variableValues.Count()) {
|
---|
65 | throw new ArgumentException("Number of variable names doesn't match the number of columns of variableValues");
|
---|
66 | } else if (!variableValues.All(list => list.Count == variableValues.First().Count)) {
|
---|
67 | throw new ArgumentException("The number of values must be equal for every variable");
|
---|
68 | } else if (variableNames.Distinct().Count() != variableNames.Count()) {
|
---|
69 | var duplicateVariableNames =
|
---|
70 | variableNames.GroupBy(v => v).Where(g => g.Count() > 1).Select(g => g.Key).ToList();
|
---|
71 | string message = "The dataset cannot contain duplicate variables names: " + Environment.NewLine;
|
---|
72 | foreach (var duplicateVariableName in duplicateVariableNames)
|
---|
73 | message += duplicateVariableName + Environment.NewLine;
|
---|
74 | throw new ArgumentException(message);
|
---|
75 | }
|
---|
76 | DatasetUtil.ValidateInputData(variableValues); // the validation call checks if every values IList is actually a list of the supported type
|
---|
77 | rows = variableValues.First().Count;
|
---|
78 | this.variableNames = new List<string>(variableNames);
|
---|
79 | this.variableValues = new Dictionary<string, IList>(this.variableNames.Count);
|
---|
80 | for (int i = 0; i < this.variableNames.Count; i++) {
|
---|
81 | var values = variableValues.ElementAt(i);
|
---|
82 | this.variableValues.Add(this.variableNames[i], values);
|
---|
83 | }
|
---|
84 | }
|
---|
85 |
|
---|
86 | public Dataset(IEnumerable<string> variableNames, double[,] variableValues) {
|
---|
87 | Name = "-";
|
---|
88 | if (variableNames.Count() != variableValues.GetLength(1)) {
|
---|
89 | throw new ArgumentException("Number of variable names doesn't match the number of columns of variableValues");
|
---|
90 | }
|
---|
91 | if (variableNames.Distinct().Count() != variableNames.Count()) {
|
---|
92 | var duplicateVariableNames = variableNames.GroupBy(v => v).Where(g => g.Count() > 1).Select(g => g.Key).ToList();
|
---|
93 | string message = "The dataset cannot contain duplicate variables names: " + Environment.NewLine;
|
---|
94 | foreach (var duplicateVariableName in duplicateVariableNames)
|
---|
95 | message += duplicateVariableName + Environment.NewLine;
|
---|
96 | throw new ArgumentException(message);
|
---|
97 | }
|
---|
98 |
|
---|
99 | rows = variableValues.GetLength(0);
|
---|
100 | this.variableNames = new List<string>(variableNames);
|
---|
101 |
|
---|
102 | this.variableValues = new Dictionary<string, IList>(variableValues.GetLength(1));
|
---|
103 | for (int col = 0; col < variableValues.GetLength(1); col++) {
|
---|
104 | string columName = this.variableNames[col];
|
---|
105 | var values = new List<double>(variableValues.GetLength(0));
|
---|
106 | for (int row = 0; row < variableValues.GetLength(0); row++) {
|
---|
107 | values.Add(variableValues[row, col]);
|
---|
108 | }
|
---|
109 | this.variableValues.Add(columName, values);
|
---|
110 | }
|
---|
111 | }
|
---|
112 |
|
---|
113 | public ModifiableDataset ToModifiable() {
|
---|
114 | var values = new List<IList>();
|
---|
115 | foreach (var v in variableNames) {
|
---|
116 | if (VariableHasType<double>(v)) {
|
---|
117 | values.Add(new List<double>((List<double>)variableValues[v]));
|
---|
118 | } else if (VariableHasType<string>(v)) {
|
---|
119 | values.Add(new List<string>((List<string>)variableValues[v]));
|
---|
120 | } else if (VariableHasType<DateTime>(v)) {
|
---|
121 | values.Add(new List<DateTime>((List<DateTime>)variableValues[v]));
|
---|
122 | } else {
|
---|
123 | throw new ArgumentException("Unknown variable type.");
|
---|
124 | }
|
---|
125 | }
|
---|
126 | return new ModifiableDataset(variableNames, values);
|
---|
127 | }
|
---|
128 | /// <summary>
|
---|
129 | /// Shuffle a dataset's rows
|
---|
130 | /// </summary>
|
---|
131 | /// <param name="random">Random number generator used for shuffling.</param>
|
---|
132 | /// <returns>A shuffled copy of the current dataset.</returns>
|
---|
133 | public Dataset Shuffle(IRandom random) {
|
---|
134 | var values = variableNames.Select(x => variableValues[x]).ToList();
|
---|
135 | return new Dataset(variableNames, values.ShuffleLists(random));
|
---|
136 | }
|
---|
137 |
|
---|
138 | protected Dataset(Dataset dataset) : this(dataset.variableNames, dataset.variableValues.Values) { }
|
---|
139 |
|
---|
140 | #region Backwards compatible code, remove with 3.5
|
---|
141 | private double[,] storableData;
|
---|
142 | //name alias used to suppport backwards compatibility
|
---|
143 | [Storable(Name = "data", AllowOneWay = true)]
|
---|
144 | private double[,] StorableData { set { storableData = value; } }
|
---|
145 |
|
---|
146 | [StorableHook(HookType.AfterDeserialization)]
|
---|
147 | private void AfterDeserialization() {
|
---|
148 | if (variableValues == null) {
|
---|
149 | rows = storableData.GetLength(0);
|
---|
150 | variableValues = new Dictionary<string, IList>();
|
---|
151 | for (int col = 0; col < storableData.GetLength(1); col++) {
|
---|
152 | string columName = variableNames[col];
|
---|
153 | var values = new List<double>(rows);
|
---|
154 | for (int row = 0; row < rows; row++) {
|
---|
155 | values.Add(storableData[row, col]);
|
---|
156 | }
|
---|
157 | variableValues.Add(columName, values);
|
---|
158 | }
|
---|
159 | storableData = null;
|
---|
160 | }
|
---|
161 | }
|
---|
162 | #endregion
|
---|
163 |
|
---|
164 | [Storable(Name = "VariableValues")]
|
---|
165 | protected Dictionary<string, IList> variableValues;
|
---|
166 |
|
---|
167 | protected List<string> variableNames;
|
---|
168 | [Storable]
|
---|
169 | public IEnumerable<string> VariableNames {
|
---|
170 | get { return variableNames; }
|
---|
171 | protected set {
|
---|
172 | if (variableNames != null) throw new InvalidOperationException();
|
---|
173 | variableNames = new List<string>(value);
|
---|
174 | }
|
---|
175 | }
|
---|
176 | public IEnumerable<string> DoubleVariables {
|
---|
177 | get { return variableValues.Where(p => p.Value is List<double>).Select(p => p.Key); }
|
---|
178 | }
|
---|
179 |
|
---|
180 | public IEnumerable<string> StringVariables {
|
---|
181 | get { return variableValues.Where(p => p.Value is List<string>).Select(p => p.Key); }
|
---|
182 | }
|
---|
183 |
|
---|
184 | public IEnumerable<double> GetDoubleValues(string variableName) {
|
---|
185 | return GetValues<double>(variableName);
|
---|
186 | }
|
---|
187 | public IEnumerable<string> GetStringValues(string variableName) {
|
---|
188 | return GetValues<string>(variableName);
|
---|
189 | }
|
---|
190 | public IEnumerable<DateTime> GetDateTimeValues(string variableName) {
|
---|
191 | return GetValues<DateTime>(variableName);
|
---|
192 | }
|
---|
193 |
|
---|
194 | public ReadOnlyCollection<double> GetReadOnlyDoubleValues(string variableName) {
|
---|
195 | var values = GetValues<double>(variableName);
|
---|
196 | return values.AsReadOnly();
|
---|
197 | }
|
---|
198 | public double GetDoubleValue(string variableName, int row) {
|
---|
199 | var values = GetValues<double>(variableName);
|
---|
200 | return values[row];
|
---|
201 | }
|
---|
202 | public IEnumerable<double> GetDoubleValues(string variableName, IEnumerable<int> rows) {
|
---|
203 | return GetValues<double>(variableName, rows);
|
---|
204 | }
|
---|
205 |
|
---|
206 | public string GetStringValue(string variableName, int row) {
|
---|
207 | var values = GetValues<string>(variableName);
|
---|
208 | return values[row];
|
---|
209 | }
|
---|
210 |
|
---|
211 | public IEnumerable<string> GetStringValues(string variableName, IEnumerable<int> rows) {
|
---|
212 | return GetValues<string>(variableName, rows);
|
---|
213 | }
|
---|
214 | public ReadOnlyCollection<string> GetReadOnlyStringValues(string variableName) {
|
---|
215 | var values = GetValues<string>(variableName);
|
---|
216 | return values.AsReadOnly();
|
---|
217 | }
|
---|
218 |
|
---|
219 | private IEnumerable<T> GetValues<T>(string variableName, IEnumerable<int> rows) {
|
---|
220 | var values = GetValues<T>(variableName);
|
---|
221 | return rows.Select(x => values[x]);
|
---|
222 | }
|
---|
223 | private List<T> GetValues<T>(string variableName) {
|
---|
224 | IList list;
|
---|
225 | if (!variableValues.TryGetValue(variableName, out list))
|
---|
226 | throw new ArgumentException("The variable " + variableName + " does not exist in the dataset.");
|
---|
227 | List<T> values = list as List<T>;
|
---|
228 | if (values == null) throw new ArgumentException("The variable " + variableName + " is not a " + typeof(T) + " variable.");
|
---|
229 | return values;
|
---|
230 | }
|
---|
231 | public bool VariableHasType<T>(string variableName) {
|
---|
232 | return variableValues[variableName] is IList<T>;
|
---|
233 | }
|
---|
234 |
|
---|
235 | #region IStringConvertibleMatrix Members
|
---|
236 | [Storable]
|
---|
237 | protected int rows;
|
---|
238 | public int Rows {
|
---|
239 | get { return rows; }
|
---|
240 | }
|
---|
241 | int IStringConvertibleMatrix.Rows {
|
---|
242 | get { return Rows; }
|
---|
243 | set { throw new NotSupportedException(); }
|
---|
244 | }
|
---|
245 |
|
---|
246 | public int Columns {
|
---|
247 | get { return variableNames.Count; }
|
---|
248 | }
|
---|
249 | int IStringConvertibleMatrix.Columns {
|
---|
250 | get { return Columns; }
|
---|
251 | set { throw new NotSupportedException(); }
|
---|
252 | }
|
---|
253 | bool IStringConvertibleMatrix.SortableView {
|
---|
254 | get { return false; }
|
---|
255 | set { throw new NotSupportedException(); }
|
---|
256 | }
|
---|
257 | bool IStringConvertibleMatrix.ReadOnly {
|
---|
258 | get { return true; }
|
---|
259 | }
|
---|
260 | IEnumerable<string> IStringConvertibleMatrix.ColumnNames {
|
---|
261 | get { return this.VariableNames; }
|
---|
262 | set { throw new NotSupportedException(); }
|
---|
263 | }
|
---|
264 | IEnumerable<string> IStringConvertibleMatrix.RowNames {
|
---|
265 | get { return Enumerable.Empty<string>(); }
|
---|
266 | set { throw new NotSupportedException(); }
|
---|
267 | }
|
---|
268 | string IStringConvertibleMatrix.GetValue(int rowIndex, int columnIndex) {
|
---|
269 | return variableValues[variableNames[columnIndex]][rowIndex].ToString();
|
---|
270 | }
|
---|
271 | bool IStringConvertibleMatrix.SetValue(string value, int rowIndex, int columnIndex) {
|
---|
272 | throw new NotSupportedException();
|
---|
273 | }
|
---|
274 | bool IStringConvertibleMatrix.Validate(string value, out string errorMessage) {
|
---|
275 | throw new NotSupportedException();
|
---|
276 | }
|
---|
277 |
|
---|
278 | public virtual event EventHandler ColumnsChanged { add { } remove { } }
|
---|
279 | public virtual event EventHandler RowsChanged { add { } remove { } }
|
---|
280 | public virtual event EventHandler ColumnNamesChanged { add { } remove { } }
|
---|
281 | public virtual event EventHandler RowNamesChanged { add { } remove { } }
|
---|
282 | public virtual event EventHandler SortableViewChanged { add { } remove { } }
|
---|
283 | public virtual event EventHandler<EventArgs<int, int>> ItemChanged { add { } remove { } }
|
---|
284 | public virtual event EventHandler Reset { add { } remove { } }
|
---|
285 | #endregion
|
---|
286 | }
|
---|
287 | }
|
---|