[2] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[15583] | 3 | * Copyright (C) 2002-2018 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[2] | 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;
|
---|
[6740] | 23 | using System.Collections;
|
---|
[2] | 24 | using System.Collections.Generic;
|
---|
[6740] | 25 | using System.Collections.ObjectModel;
|
---|
[2285] | 26 | using System.Linq;
|
---|
[3376] | 27 | using HeuristicLab.Common;
|
---|
[3253] | 28 | using HeuristicLab.Core;
|
---|
[4068] | 29 | using HeuristicLab.Data;
|
---|
[3253] | 30 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
[2] | 31 |
|
---|
[3253] | 32 | namespace HeuristicLab.Problems.DataAnalysis {
|
---|
| 33 | [Item("Dataset", "Represents a dataset containing data that should be analyzed.")]
|
---|
| 34 | [StorableClass]
|
---|
[12509] | 35 | public class Dataset : NamedItem, IDataset {
|
---|
[3933] | 36 | [StorableConstructor]
|
---|
[12509] | 37 | protected Dataset(bool deserializing) : base(deserializing) { }
|
---|
| 38 | protected Dataset(Dataset original, Cloner cloner)
|
---|
[4722] | 39 | : base(original, cloner) {
|
---|
[15829] | 40 | // no need to clone the variable values because these can't be modified
|
---|
[6740] | 41 | variableValues = new Dictionary<string, IList>(original.variableValues);
|
---|
| 42 | variableNames = new List<string>(original.variableNames);
|
---|
| 43 | rows = original.rows;
|
---|
[2319] | 44 | }
|
---|
[15829] | 45 |
|
---|
[6740] | 46 | public override IDeepCloneable Clone(Cloner cloner) { return new Dataset(this, cloner); }
|
---|
[5552] | 47 |
|
---|
[5847] | 48 | public Dataset()
|
---|
| 49 | : base() {
|
---|
| 50 | Name = "-";
|
---|
| 51 | VariableNames = Enumerable.Empty<string>();
|
---|
[6740] | 52 | variableValues = new Dictionary<string, IList>();
|
---|
| 53 | rows = 0;
|
---|
[5847] | 54 | }
|
---|
| 55 |
|
---|
[13419] | 56 | /// <summary>
|
---|
| 57 | /// Creates a new dataset. The variableValues are not cloned.
|
---|
| 58 | /// </summary>
|
---|
| 59 | /// <param name="variableNames">The names of the variables in the dataset</param>
|
---|
| 60 | /// <param name="variableValues">The values for the variables (column-oriented storage). Values are not cloned!</param>
|
---|
[6740] | 61 | public Dataset(IEnumerable<string> variableNames, IEnumerable<IList> variableValues)
|
---|
[15829] | 62 | : this(variableNames, variableValues, cloneValues: true) {
|
---|
| 63 | }
|
---|
| 64 |
|
---|
| 65 | protected Dataset(IEnumerable<string> variableNames, IEnumerable<IList> variableValues, bool cloneValues = false) {
|
---|
[2319] | 66 | Name = "-";
|
---|
[15829] | 67 |
|
---|
| 68 | if (variableNames.Any()) {
|
---|
| 69 | this.variableNames = new List<string>(variableNames);
|
---|
| 70 | } else {
|
---|
[6740] | 71 | this.variableNames = Enumerable.Range(0, variableValues.Count()).Select(x => "Column " + x).ToList();
|
---|
[3264] | 72 | }
|
---|
[15829] | 73 | // check if the arguments are consistent (no duplicate variables, same number of rows, correct data types, ...)
|
---|
| 74 | CheckArguments(this.variableNames, variableValues);
|
---|
| 75 |
|
---|
[6740] | 76 | rows = variableValues.First().Count;
|
---|
[15769] | 77 |
|
---|
[15829] | 78 | if (cloneValues) {
|
---|
| 79 | this.variableValues = CloneValues(this.variableNames, variableValues);
|
---|
| 80 | } else {
|
---|
| 81 | this.variableValues = new Dictionary<string, IList>(this.variableNames.Count);
|
---|
| 82 | for (int i = 0; i < this.variableNames.Count; i++) {
|
---|
| 83 | var variableName = this.variableNames[i];
|
---|
| 84 | var values = variableValues.ElementAt(i);
|
---|
| 85 | this.variableValues.Add(variableName, values);
|
---|
[15769] | 86 | }
|
---|
[6740] | 87 | }
|
---|
[2038] | 88 | }
|
---|
| 89 |
|
---|
[6740] | 90 | public Dataset(IEnumerable<string> variableNames, double[,] variableValues) {
|
---|
| 91 | Name = "-";
|
---|
| 92 | if (variableNames.Count() != variableValues.GetLength(1)) {
|
---|
| 93 | throw new ArgumentException("Number of variable names doesn't match the number of columns of variableValues");
|
---|
| 94 | }
|
---|
| 95 | if (variableNames.Distinct().Count() != variableNames.Count()) {
|
---|
| 96 | var duplicateVariableNames = variableNames.GroupBy(v => v).Where(g => g.Count() > 1).Select(g => g.Key).ToList();
|
---|
| 97 | string message = "The dataset cannot contain duplicate variables names: " + Environment.NewLine;
|
---|
| 98 | foreach (var duplicateVariableName in duplicateVariableNames)
|
---|
| 99 | message += duplicateVariableName + Environment.NewLine;
|
---|
| 100 | throw new ArgumentException(message);
|
---|
| 101 | }
|
---|
[5552] | 102 |
|
---|
[6740] | 103 | rows = variableValues.GetLength(0);
|
---|
| 104 | this.variableNames = new List<string>(variableNames);
|
---|
| 105 |
|
---|
[7736] | 106 | this.variableValues = new Dictionary<string, IList>(variableValues.GetLength(1));
|
---|
[6740] | 107 | for (int col = 0; col < variableValues.GetLength(1); col++) {
|
---|
| 108 | string columName = this.variableNames[col];
|
---|
[7736] | 109 | var values = new List<double>(variableValues.GetLength(0));
|
---|
[6740] | 110 | for (int row = 0; row < variableValues.GetLength(0); row++) {
|
---|
| 111 | values.Add(variableValues[row, col]);
|
---|
[5552] | 112 | }
|
---|
[6740] | 113 | this.variableValues.Add(columName, values);
|
---|
[5552] | 114 | }
|
---|
[333] | 115 | }
|
---|
| 116 |
|
---|
[13760] | 117 | public ModifiableDataset ToModifiable() {
|
---|
[16063] | 118 | return new ModifiableDataset(variableNames, variableNames.Select(v => variableValues[v]),true);
|
---|
[13760] | 119 | }
|
---|
[15769] | 120 |
|
---|
[14864] | 121 | /// <summary>
|
---|
| 122 | /// Shuffle a dataset's rows
|
---|
| 123 | /// </summary>
|
---|
| 124 | /// <param name="random">Random number generator used for shuffling.</param>
|
---|
| 125 | /// <returns>A shuffled copy of the current dataset.</returns>
|
---|
| 126 | public Dataset Shuffle(IRandom random) {
|
---|
| 127 | var values = variableNames.Select(x => variableValues[x]).ToList();
|
---|
| 128 | return new Dataset(variableNames, values.ShuffleLists(random));
|
---|
| 129 | }
|
---|
[13760] | 130 |
|
---|
[12509] | 131 |
|
---|
[16063] | 132 |
|
---|
[6740] | 133 | #region Backwards compatible code, remove with 3.5
|
---|
| 134 | private double[,] storableData;
|
---|
| 135 | //name alias used to suppport backwards compatibility
|
---|
| 136 | [Storable(Name = "data", AllowOneWay = true)]
|
---|
| 137 | private double[,] StorableData { set { storableData = value; } }
|
---|
[2] | 138 |
|
---|
[6740] | 139 | [StorableHook(HookType.AfterDeserialization)]
|
---|
| 140 | private void AfterDeserialization() {
|
---|
| 141 | if (variableValues == null) {
|
---|
| 142 | rows = storableData.GetLength(0);
|
---|
| 143 | variableValues = new Dictionary<string, IList>();
|
---|
| 144 | for (int col = 0; col < storableData.GetLength(1); col++) {
|
---|
| 145 | string columName = variableNames[col];
|
---|
[7921] | 146 | var values = new List<double>(rows);
|
---|
| 147 | for (int row = 0; row < rows; row++) {
|
---|
[6740] | 148 | values.Add(storableData[row, col]);
|
---|
| 149 | }
|
---|
| 150 | variableValues.Add(columName, values);
|
---|
| 151 | }
|
---|
| 152 | storableData = null;
|
---|
[3839] | 153 | }
|
---|
| 154 | }
|
---|
[6740] | 155 | #endregion
|
---|
[3933] | 156 |
|
---|
[6749] | 157 | [Storable(Name = "VariableValues")]
|
---|
[12509] | 158 | protected Dictionary<string, IList> variableValues;
|
---|
[6749] | 159 |
|
---|
[12509] | 160 | protected List<string> variableNames;
|
---|
[6740] | 161 | [Storable]
|
---|
| 162 | public IEnumerable<string> VariableNames {
|
---|
| 163 | get { return variableNames; }
|
---|
[12509] | 164 | protected set {
|
---|
[6740] | 165 | if (variableNames != null) throw new InvalidOperationException();
|
---|
| 166 | variableNames = new List<string>(value);
|
---|
| 167 | }
|
---|
[1287] | 168 | }
|
---|
[6740] | 169 | public IEnumerable<string> DoubleVariables {
|
---|
[15013] | 170 | get { return variableValues.Where(p => p.Value is IList<double>).Select(p => p.Key); }
|
---|
[3994] | 171 | }
|
---|
[14826] | 172 |
|
---|
| 173 | public IEnumerable<string> StringVariables {
|
---|
[15013] | 174 | get { return variableValues.Where(p => p.Value is IList<string>).Select(p => p.Key); }
|
---|
[14826] | 175 | }
|
---|
| 176 |
|
---|
[15094] | 177 | public IEnumerable<string> DateTimeVariables {
|
---|
| 178 | get { return variableValues.Where(p => p.Value is IList<DateTime>).Select(p => p.Key); }
|
---|
| 179 | }
|
---|
| 180 |
|
---|
[6740] | 181 | public IEnumerable<double> GetDoubleValues(string variableName) {
|
---|
[12509] | 182 | return GetValues<double>(variableName);
|
---|
[3994] | 183 | }
|
---|
[11114] | 184 | public IEnumerable<string> GetStringValues(string variableName) {
|
---|
[12509] | 185 | return GetValues<string>(variableName);
|
---|
[11114] | 186 | }
|
---|
| 187 | public IEnumerable<DateTime> GetDateTimeValues(string variableName) {
|
---|
[12509] | 188 | return GetValues<DateTime>(variableName);
|
---|
[11114] | 189 | }
|
---|
| 190 |
|
---|
[6740] | 191 | public ReadOnlyCollection<double> GetReadOnlyDoubleValues(string variableName) {
|
---|
[12509] | 192 | var values = GetValues<double>(variableName);
|
---|
[15013] | 193 | return new ReadOnlyCollection<double>(values);
|
---|
[3994] | 194 | }
|
---|
[6740] | 195 | public double GetDoubleValue(string variableName, int row) {
|
---|
[12509] | 196 | var values = GetValues<double>(variableName);
|
---|
[6740] | 197 | return values[row];
|
---|
[4031] | 198 | }
|
---|
[6740] | 199 | public IEnumerable<double> GetDoubleValues(string variableName, IEnumerable<int> rows) {
|
---|
[12509] | 200 | return GetValues<double>(variableName, rows);
|
---|
| 201 | }
|
---|
[14826] | 202 |
|
---|
| 203 | public string GetStringValue(string variableName, int row) {
|
---|
| 204 | var values = GetValues<string>(variableName);
|
---|
| 205 | return values[row];
|
---|
| 206 | }
|
---|
| 207 |
|
---|
| 208 | public IEnumerable<string> GetStringValues(string variableName, IEnumerable<int> rows) {
|
---|
| 209 | return GetValues<string>(variableName, rows);
|
---|
| 210 | }
|
---|
| 211 | public ReadOnlyCollection<string> GetReadOnlyStringValues(string variableName) {
|
---|
| 212 | var values = GetValues<string>(variableName);
|
---|
[15013] | 213 | return new ReadOnlyCollection<string>(values);
|
---|
[14826] | 214 | }
|
---|
| 215 |
|
---|
[15094] | 216 | public DateTime GetDateTimeValue(string variableName, int row) {
|
---|
| 217 | var values = GetValues<DateTime>(variableName);
|
---|
| 218 | return values[row];
|
---|
| 219 | }
|
---|
| 220 | public IEnumerable<DateTime> GetDateTimeValues(string variableName, IEnumerable<int> rows) {
|
---|
| 221 | return GetValues<DateTime>(variableName, rows);
|
---|
| 222 | }
|
---|
| 223 | public ReadOnlyCollection<DateTime> GetReadOnlyDateTimeValues(string variableName) {
|
---|
| 224 | var values = GetValues<DateTime>(variableName);
|
---|
| 225 | return new ReadOnlyCollection<DateTime>(values);
|
---|
| 226 | }
|
---|
[12509] | 227 | private IEnumerable<T> GetValues<T>(string variableName, IEnumerable<int> rows) {
|
---|
| 228 | var values = GetValues<T>(variableName);
|
---|
| 229 | return rows.Select(x => values[x]);
|
---|
| 230 | }
|
---|
[15013] | 231 | private IList<T> GetValues<T>(string variableName) {
|
---|
[6740] | 232 | IList list;
|
---|
| 233 | if (!variableValues.TryGetValue(variableName, out list))
|
---|
| 234 | throw new ArgumentException("The variable " + variableName + " does not exist in the dataset.");
|
---|
[15013] | 235 | IList<T> values = list as IList<T>;
|
---|
[12509] | 236 | if (values == null) throw new ArgumentException("The variable " + variableName + " is not a " + typeof(T) + " variable.");
|
---|
| 237 | return values;
|
---|
[2319] | 238 | }
|
---|
[11156] | 239 | public bool VariableHasType<T>(string variableName) {
|
---|
[11114] | 240 | return variableValues[variableName] is IList<T>;
|
---|
| 241 | }
|
---|
[15769] | 242 | protected Type GetVariableType(string variableName) {
|
---|
| 243 | IList list;
|
---|
| 244 | variableValues.TryGetValue(variableName, out list);
|
---|
| 245 | if (list == null)
|
---|
| 246 | throw new ArgumentException("The variable " + variableName + " does not exist in the dataset.");
|
---|
| 247 | return GetElementType(list);
|
---|
| 248 | }
|
---|
[15829] | 249 | protected static Type GetElementType(IList list) {
|
---|
[15769] | 250 | var type = list.GetType();
|
---|
| 251 | return type.IsGenericType ? type.GetGenericArguments()[0] : type.GetElementType();
|
---|
| 252 | }
|
---|
[15829] | 253 | protected static bool IsAllowedType(IList list) {
|
---|
[15769] | 254 | var type = GetElementType(list);
|
---|
| 255 | return IsAllowedType(type);
|
---|
| 256 | }
|
---|
[15829] | 257 | protected static bool IsAllowedType(Type type) {
|
---|
[15769] | 258 | return type == typeof(double) || type == typeof(string) || type == typeof(DateTime);
|
---|
| 259 | }
|
---|
| 260 |
|
---|
[15829] | 261 | protected static void CheckArguments(IEnumerable<string> variableNames, IEnumerable<IList> variableValues) {
|
---|
| 262 | if (variableNames.Count() != variableValues.Count()) {
|
---|
| 263 | throw new ArgumentException("Number of variable names doesn't match the number of columns of variableValues");
|
---|
| 264 | } else if (!variableValues.All(list => list.Count == variableValues.First().Count)) {
|
---|
| 265 | throw new ArgumentException("The number of values must be equal for every variable");
|
---|
| 266 | } else if (variableNames.Distinct().Count() != variableNames.Count()) {
|
---|
| 267 | var duplicateVariableNames =
|
---|
| 268 | variableNames.GroupBy(v => v).Where(g => g.Count() > 1).Select(g => g.Key).ToList();
|
---|
| 269 | string message = "The dataset cannot contain duplicate variables names: " + Environment.NewLine;
|
---|
| 270 | foreach (var duplicateVariableName in duplicateVariableNames)
|
---|
| 271 | message += duplicateVariableName + Environment.NewLine;
|
---|
| 272 | throw new ArgumentException(message);
|
---|
| 273 | }
|
---|
| 274 | // check if all the variables are supported
|
---|
| 275 | foreach (var t in variableNames.Zip(variableValues, Tuple.Create)) {
|
---|
| 276 | var variableName = t.Item1;
|
---|
| 277 | var values = t.Item2;
|
---|
| 278 |
|
---|
| 279 | if (!IsAllowedType(values)) {
|
---|
| 280 | throw new ArgumentException(string.Format("Unsupported type {0} for variable {1}.", GetElementType(values), variableName));
|
---|
| 281 | }
|
---|
| 282 | }
|
---|
| 283 | }
|
---|
| 284 |
|
---|
| 285 | protected static Dictionary<string, IList> CloneValues(Dictionary<string, IList> variableValues) {
|
---|
| 286 | return variableValues.ToDictionary(x => x.Key, x => CloneValues(x.Value));
|
---|
| 287 | }
|
---|
| 288 |
|
---|
| 289 | protected static Dictionary<string, IList> CloneValues(IEnumerable<string> variableNames, IEnumerable<IList> variableValues) {
|
---|
| 290 | return variableNames.Zip(variableValues, Tuple.Create).ToDictionary(x => x.Item1, x => CloneValues(x.Item2));
|
---|
| 291 | }
|
---|
| 292 |
|
---|
| 293 | protected static IList CloneValues(IList values) {
|
---|
| 294 | var doubleValues = values as IList<double>;
|
---|
| 295 | if (doubleValues != null) return new List<double>(doubleValues);
|
---|
| 296 |
|
---|
| 297 | var stringValues = values as IList<string>;
|
---|
| 298 | if (stringValues != null) return new List<string>(stringValues);
|
---|
| 299 |
|
---|
| 300 | var dateTimeValues = values as IList<DateTime>;
|
---|
| 301 | if (dateTimeValues != null) return new List<DateTime>(dateTimeValues);
|
---|
| 302 |
|
---|
| 303 | throw new ArgumentException(string.Format("Unsupported variable type {0}.", GetElementType(values)));
|
---|
| 304 | }
|
---|
| 305 |
|
---|
[3253] | 306 | #region IStringConvertibleMatrix Members
|
---|
[6740] | 307 | [Storable]
|
---|
[16063] | 308 | private int rows;
|
---|
[3253] | 309 | public int Rows {
|
---|
[6740] | 310 | get { return rows; }
|
---|
[16063] | 311 | protected set { rows = value; }
|
---|
[13539] | 312 | }
|
---|
| 313 | int IStringConvertibleMatrix.Rows {
|
---|
| 314 | get { return Rows; }
|
---|
[3933] | 315 | set { throw new NotSupportedException(); }
|
---|
[2] | 316 | }
|
---|
[13539] | 317 |
|
---|
[3253] | 318 | public int Columns {
|
---|
[6740] | 319 | get { return variableNames.Count; }
|
---|
[13539] | 320 | }
|
---|
| 321 | int IStringConvertibleMatrix.Columns {
|
---|
| 322 | get { return Columns; }
|
---|
[3933] | 323 | set { throw new NotSupportedException(); }
|
---|
[2] | 324 | }
|
---|
[13427] | 325 | bool IStringConvertibleMatrix.SortableView {
|
---|
[3933] | 326 | get { return false; }
|
---|
| 327 | set { throw new NotSupportedException(); }
|
---|
[3321] | 328 | }
|
---|
[13427] | 329 | bool IStringConvertibleMatrix.ReadOnly {
|
---|
[3933] | 330 | get { return true; }
|
---|
[3430] | 331 | }
|
---|
[3308] | 332 | IEnumerable<string> IStringConvertibleMatrix.ColumnNames {
|
---|
| 333 | get { return this.VariableNames; }
|
---|
[3933] | 334 | set { throw new NotSupportedException(); }
|
---|
[3308] | 335 | }
|
---|
[3311] | 336 | IEnumerable<string> IStringConvertibleMatrix.RowNames {
|
---|
[5552] | 337 | get { return Enumerable.Empty<string>(); }
|
---|
[3933] | 338 | set { throw new NotSupportedException(); }
|
---|
[3311] | 339 | }
|
---|
[13427] | 340 | string IStringConvertibleMatrix.GetValue(int rowIndex, int columnIndex) {
|
---|
[6740] | 341 | return variableValues[variableNames[columnIndex]][rowIndex].ToString();
|
---|
[2] | 342 | }
|
---|
[12509] | 343 | bool IStringConvertibleMatrix.SetValue(string value, int rowIndex, int columnIndex) {
|
---|
[3933] | 344 | throw new NotSupportedException();
|
---|
[237] | 345 | }
|
---|
[12509] | 346 | bool IStringConvertibleMatrix.Validate(string value, out string errorMessage) {
|
---|
[5552] | 347 | throw new NotSupportedException();
|
---|
| 348 | }
|
---|
[237] | 349 |
|
---|
[12509] | 350 | public virtual event EventHandler ColumnsChanged { add { } remove { } }
|
---|
| 351 | public virtual event EventHandler RowsChanged { add { } remove { } }
|
---|
| 352 | public virtual event EventHandler ColumnNamesChanged { add { } remove { } }
|
---|
| 353 | public virtual event EventHandler RowNamesChanged { add { } remove { } }
|
---|
| 354 | public virtual event EventHandler SortableViewChanged { add { } remove { } }
|
---|
| 355 | public virtual event EventHandler<EventArgs<int, int>> ItemChanged { add { } remove { } }
|
---|
| 356 | public virtual event EventHandler Reset { add { } remove { } }
|
---|
[2012] | 357 | #endregion
|
---|
[2] | 358 | }
|
---|
| 359 | }
|
---|