[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) {
|
---|
[6740] | 40 | variableValues = new Dictionary<string, IList>(original.variableValues);
|
---|
| 41 | variableNames = new List<string>(original.variableNames);
|
---|
| 42 | rows = original.rows;
|
---|
[2319] | 43 | }
|
---|
[6740] | 44 | public override IDeepCloneable Clone(Cloner cloner) { return new Dataset(this, cloner); }
|
---|
[5552] | 45 |
|
---|
[5847] | 46 | public Dataset()
|
---|
| 47 | : base() {
|
---|
| 48 | Name = "-";
|
---|
| 49 | VariableNames = Enumerable.Empty<string>();
|
---|
[6740] | 50 | variableValues = new Dictionary<string, IList>();
|
---|
| 51 | rows = 0;
|
---|
[5847] | 52 | }
|
---|
| 53 |
|
---|
[13419] | 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>
|
---|
[6740] | 59 | public Dataset(IEnumerable<string> variableNames, IEnumerable<IList> variableValues)
|
---|
[3933] | 60 | : base() {
|
---|
[2319] | 61 | Name = "-";
|
---|
[6740] | 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);
|
---|
[3264] | 75 | }
|
---|
[6740] | 76 | rows = variableValues.First().Count;
|
---|
| 77 | this.variableNames = new List<string>(variableNames);
|
---|
[7736] | 78 | this.variableValues = new Dictionary<string, IList>(this.variableNames.Count);
|
---|
[6740] | 79 | for (int i = 0; i < this.variableNames.Count; i++) {
|
---|
[15769] | 80 | var variableName = this.variableNames[i];
|
---|
[6740] | 81 | var values = variableValues.ElementAt(i);
|
---|
[15769] | 82 |
|
---|
| 83 | if (!IsAllowedType(values)) {
|
---|
| 84 | throw new ArgumentException(string.Format("Unsupported type {0} for variable {1}.", GetElementType(values), variableName));
|
---|
| 85 | }
|
---|
| 86 |
|
---|
| 87 | this.variableValues.Add(variableName, values);
|
---|
[6740] | 88 | }
|
---|
[2038] | 89 | }
|
---|
| 90 |
|
---|
[6740] | 91 | public Dataset(IEnumerable<string> variableNames, double[,] variableValues) {
|
---|
| 92 | Name = "-";
|
---|
| 93 | if (variableNames.Count() != variableValues.GetLength(1)) {
|
---|
| 94 | throw new ArgumentException("Number of variable names doesn't match the number of columns of variableValues");
|
---|
| 95 | }
|
---|
| 96 | if (variableNames.Distinct().Count() != variableNames.Count()) {
|
---|
| 97 | var duplicateVariableNames = variableNames.GroupBy(v => v).Where(g => g.Count() > 1).Select(g => g.Key).ToList();
|
---|
| 98 | string message = "The dataset cannot contain duplicate variables names: " + Environment.NewLine;
|
---|
| 99 | foreach (var duplicateVariableName in duplicateVariableNames)
|
---|
| 100 | message += duplicateVariableName + Environment.NewLine;
|
---|
| 101 | throw new ArgumentException(message);
|
---|
| 102 | }
|
---|
[5552] | 103 |
|
---|
[6740] | 104 | rows = variableValues.GetLength(0);
|
---|
| 105 | this.variableNames = new List<string>(variableNames);
|
---|
| 106 |
|
---|
[7736] | 107 | this.variableValues = new Dictionary<string, IList>(variableValues.GetLength(1));
|
---|
[6740] | 108 | for (int col = 0; col < variableValues.GetLength(1); col++) {
|
---|
| 109 | string columName = this.variableNames[col];
|
---|
[7736] | 110 | var values = new List<double>(variableValues.GetLength(0));
|
---|
[6740] | 111 | for (int row = 0; row < variableValues.GetLength(0); row++) {
|
---|
| 112 | values.Add(variableValues[row, col]);
|
---|
[5552] | 113 | }
|
---|
[6740] | 114 | this.variableValues.Add(columName, values);
|
---|
[5552] | 115 | }
|
---|
[333] | 116 | }
|
---|
| 117 |
|
---|
[13760] | 118 | public ModifiableDataset ToModifiable() {
|
---|
| 119 | var values = new List<IList>();
|
---|
| 120 | foreach (var v in variableNames) {
|
---|
| 121 | if (VariableHasType<double>(v)) {
|
---|
[15015] | 122 | values.Add(new List<double>((IList<double>)variableValues[v]));
|
---|
[13760] | 123 | } else if (VariableHasType<string>(v)) {
|
---|
[15015] | 124 | values.Add(new List<string>((IList<string>)variableValues[v]));
|
---|
[13760] | 125 | } else if (VariableHasType<DateTime>(v)) {
|
---|
[15015] | 126 | values.Add(new List<DateTime>((IList<DateTime>)variableValues[v]));
|
---|
[13760] | 127 | } else {
|
---|
| 128 | throw new ArgumentException("Unknown variable type.");
|
---|
| 129 | }
|
---|
| 130 | }
|
---|
| 131 | return new ModifiableDataset(variableNames, values);
|
---|
| 132 | }
|
---|
[15769] | 133 |
|
---|
[14864] | 134 | /// <summary>
|
---|
| 135 | /// Shuffle a dataset's rows
|
---|
| 136 | /// </summary>
|
---|
| 137 | /// <param name="random">Random number generator used for shuffling.</param>
|
---|
| 138 | /// <returns>A shuffled copy of the current dataset.</returns>
|
---|
| 139 | public Dataset Shuffle(IRandom random) {
|
---|
| 140 | var values = variableNames.Select(x => variableValues[x]).ToList();
|
---|
| 141 | return new Dataset(variableNames, values.ShuffleLists(random));
|
---|
| 142 | }
|
---|
[13760] | 143 |
|
---|
[15885] | 144 | public Dataset(Dataset dataset) : this(dataset.variableNames, dataset.variableValues.Values) { }
|
---|
[12509] | 145 |
|
---|
[6740] | 146 | #region Backwards compatible code, remove with 3.5
|
---|
| 147 | private double[,] storableData;
|
---|
| 148 | //name alias used to suppport backwards compatibility
|
---|
| 149 | [Storable(Name = "data", AllowOneWay = true)]
|
---|
| 150 | private double[,] StorableData { set { storableData = value; } }
|
---|
[2] | 151 |
|
---|
[6740] | 152 | [StorableHook(HookType.AfterDeserialization)]
|
---|
| 153 | private void AfterDeserialization() {
|
---|
| 154 | if (variableValues == null) {
|
---|
| 155 | rows = storableData.GetLength(0);
|
---|
| 156 | variableValues = new Dictionary<string, IList>();
|
---|
| 157 | for (int col = 0; col < storableData.GetLength(1); col++) {
|
---|
| 158 | string columName = variableNames[col];
|
---|
[7921] | 159 | var values = new List<double>(rows);
|
---|
| 160 | for (int row = 0; row < rows; row++) {
|
---|
[6740] | 161 | values.Add(storableData[row, col]);
|
---|
| 162 | }
|
---|
| 163 | variableValues.Add(columName, values);
|
---|
| 164 | }
|
---|
| 165 | storableData = null;
|
---|
[3839] | 166 | }
|
---|
| 167 | }
|
---|
[6740] | 168 | #endregion
|
---|
[3933] | 169 |
|
---|
[6749] | 170 | [Storable(Name = "VariableValues")]
|
---|
[12509] | 171 | protected Dictionary<string, IList> variableValues;
|
---|
[6749] | 172 |
|
---|
[12509] | 173 | protected List<string> variableNames;
|
---|
[6740] | 174 | [Storable]
|
---|
| 175 | public IEnumerable<string> VariableNames {
|
---|
| 176 | get { return variableNames; }
|
---|
[12509] | 177 | protected set {
|
---|
[6740] | 178 | if (variableNames != null) throw new InvalidOperationException();
|
---|
| 179 | variableNames = new List<string>(value);
|
---|
| 180 | }
|
---|
[1287] | 181 | }
|
---|
[6740] | 182 | public IEnumerable<string> DoubleVariables {
|
---|
[15013] | 183 | get { return variableValues.Where(p => p.Value is IList<double>).Select(p => p.Key); }
|
---|
[3994] | 184 | }
|
---|
[14826] | 185 |
|
---|
| 186 | public IEnumerable<string> StringVariables {
|
---|
[15013] | 187 | get { return variableValues.Where(p => p.Value is IList<string>).Select(p => p.Key); }
|
---|
[14826] | 188 | }
|
---|
| 189 |
|
---|
[15094] | 190 | public IEnumerable<string> DateTimeVariables {
|
---|
| 191 | get { return variableValues.Where(p => p.Value is IList<DateTime>).Select(p => p.Key); }
|
---|
| 192 | }
|
---|
| 193 |
|
---|
[6740] | 194 | public IEnumerable<double> GetDoubleValues(string variableName) {
|
---|
[12509] | 195 | return GetValues<double>(variableName);
|
---|
[3994] | 196 | }
|
---|
[11114] | 197 | public IEnumerable<string> GetStringValues(string variableName) {
|
---|
[12509] | 198 | return GetValues<string>(variableName);
|
---|
[11114] | 199 | }
|
---|
| 200 | public IEnumerable<DateTime> GetDateTimeValues(string variableName) {
|
---|
[12509] | 201 | return GetValues<DateTime>(variableName);
|
---|
[11114] | 202 | }
|
---|
| 203 |
|
---|
[6740] | 204 | public ReadOnlyCollection<double> GetReadOnlyDoubleValues(string variableName) {
|
---|
[12509] | 205 | var values = GetValues<double>(variableName);
|
---|
[15013] | 206 | return new ReadOnlyCollection<double>(values);
|
---|
[3994] | 207 | }
|
---|
[6740] | 208 | public double GetDoubleValue(string variableName, int row) {
|
---|
[12509] | 209 | var values = GetValues<double>(variableName);
|
---|
[6740] | 210 | return values[row];
|
---|
[4031] | 211 | }
|
---|
[6740] | 212 | public IEnumerable<double> GetDoubleValues(string variableName, IEnumerable<int> rows) {
|
---|
[12509] | 213 | return GetValues<double>(variableName, rows);
|
---|
| 214 | }
|
---|
[14826] | 215 |
|
---|
| 216 | public string GetStringValue(string variableName, int row) {
|
---|
| 217 | var values = GetValues<string>(variableName);
|
---|
| 218 | return values[row];
|
---|
| 219 | }
|
---|
| 220 |
|
---|
| 221 | public IEnumerable<string> GetStringValues(string variableName, IEnumerable<int> rows) {
|
---|
| 222 | return GetValues<string>(variableName, rows);
|
---|
| 223 | }
|
---|
| 224 | public ReadOnlyCollection<string> GetReadOnlyStringValues(string variableName) {
|
---|
| 225 | var values = GetValues<string>(variableName);
|
---|
[15013] | 226 | return new ReadOnlyCollection<string>(values);
|
---|
[14826] | 227 | }
|
---|
| 228 |
|
---|
[15094] | 229 | public DateTime GetDateTimeValue(string variableName, int row) {
|
---|
| 230 | var values = GetValues<DateTime>(variableName);
|
---|
| 231 | return values[row];
|
---|
| 232 | }
|
---|
| 233 | public IEnumerable<DateTime> GetDateTimeValues(string variableName, IEnumerable<int> rows) {
|
---|
| 234 | return GetValues<DateTime>(variableName, rows);
|
---|
| 235 | }
|
---|
| 236 | public ReadOnlyCollection<DateTime> GetReadOnlyDateTimeValues(string variableName) {
|
---|
| 237 | var values = GetValues<DateTime>(variableName);
|
---|
| 238 | return new ReadOnlyCollection<DateTime>(values);
|
---|
| 239 | }
|
---|
| 240 |
|
---|
| 241 |
|
---|
[12509] | 242 | private IEnumerable<T> GetValues<T>(string variableName, IEnumerable<int> rows) {
|
---|
| 243 | var values = GetValues<T>(variableName);
|
---|
| 244 | return rows.Select(x => values[x]);
|
---|
| 245 | }
|
---|
[15013] | 246 | private IList<T> GetValues<T>(string variableName) {
|
---|
[6740] | 247 | IList list;
|
---|
| 248 | if (!variableValues.TryGetValue(variableName, out list))
|
---|
| 249 | throw new ArgumentException("The variable " + variableName + " does not exist in the dataset.");
|
---|
[15013] | 250 | IList<T> values = list as IList<T>;
|
---|
[12509] | 251 | if (values == null) throw new ArgumentException("The variable " + variableName + " is not a " + typeof(T) + " variable.");
|
---|
| 252 | return values;
|
---|
[2319] | 253 | }
|
---|
[11156] | 254 | public bool VariableHasType<T>(string variableName) {
|
---|
[11114] | 255 | return variableValues[variableName] is IList<T>;
|
---|
| 256 | }
|
---|
| 257 |
|
---|
[15769] | 258 | protected Type GetVariableType(string variableName) {
|
---|
| 259 | IList list;
|
---|
| 260 | variableValues.TryGetValue(variableName, out list);
|
---|
| 261 | if (list == null)
|
---|
| 262 | throw new ArgumentException("The variable " + variableName + " does not exist in the dataset.");
|
---|
| 263 | return GetElementType(list);
|
---|
| 264 | }
|
---|
| 265 |
|
---|
| 266 | protected Type GetElementType(IList list) {
|
---|
| 267 | var type = list.GetType();
|
---|
| 268 | return type.IsGenericType ? type.GetGenericArguments()[0] : type.GetElementType();
|
---|
| 269 | }
|
---|
| 270 |
|
---|
| 271 | protected bool IsAllowedType(IList list) {
|
---|
| 272 | var type = GetElementType(list);
|
---|
| 273 | return IsAllowedType(type);
|
---|
| 274 | }
|
---|
| 275 |
|
---|
| 276 | protected bool IsAllowedType(Type type) {
|
---|
| 277 | return type == typeof(double) || type == typeof(string) || type == typeof(DateTime);
|
---|
| 278 | }
|
---|
| 279 |
|
---|
[3253] | 280 | #region IStringConvertibleMatrix Members
|
---|
[6740] | 281 | [Storable]
|
---|
[12509] | 282 | protected int rows;
|
---|
[3253] | 283 | public int Rows {
|
---|
[6740] | 284 | get { return rows; }
|
---|
[13539] | 285 | }
|
---|
| 286 | int IStringConvertibleMatrix.Rows {
|
---|
| 287 | get { return Rows; }
|
---|
[3933] | 288 | set { throw new NotSupportedException(); }
|
---|
[2] | 289 | }
|
---|
[13539] | 290 |
|
---|
[3253] | 291 | public int Columns {
|
---|
[6740] | 292 | get { return variableNames.Count; }
|
---|
[13539] | 293 | }
|
---|
| 294 | int IStringConvertibleMatrix.Columns {
|
---|
| 295 | get { return Columns; }
|
---|
[3933] | 296 | set { throw new NotSupportedException(); }
|
---|
[2] | 297 | }
|
---|
[13427] | 298 | bool IStringConvertibleMatrix.SortableView {
|
---|
[3933] | 299 | get { return false; }
|
---|
| 300 | set { throw new NotSupportedException(); }
|
---|
[3321] | 301 | }
|
---|
[13427] | 302 | bool IStringConvertibleMatrix.ReadOnly {
|
---|
[3933] | 303 | get { return true; }
|
---|
[3430] | 304 | }
|
---|
[3308] | 305 | IEnumerable<string> IStringConvertibleMatrix.ColumnNames {
|
---|
| 306 | get { return this.VariableNames; }
|
---|
[3933] | 307 | set { throw new NotSupportedException(); }
|
---|
[3308] | 308 | }
|
---|
[3311] | 309 | IEnumerable<string> IStringConvertibleMatrix.RowNames {
|
---|
[5552] | 310 | get { return Enumerable.Empty<string>(); }
|
---|
[3933] | 311 | set { throw new NotSupportedException(); }
|
---|
[3311] | 312 | }
|
---|
[13427] | 313 | string IStringConvertibleMatrix.GetValue(int rowIndex, int columnIndex) {
|
---|
[6740] | 314 | return variableValues[variableNames[columnIndex]][rowIndex].ToString();
|
---|
[2] | 315 | }
|
---|
[12509] | 316 | bool IStringConvertibleMatrix.SetValue(string value, int rowIndex, int columnIndex) {
|
---|
[3933] | 317 | throw new NotSupportedException();
|
---|
[237] | 318 | }
|
---|
[12509] | 319 | bool IStringConvertibleMatrix.Validate(string value, out string errorMessage) {
|
---|
[5552] | 320 | throw new NotSupportedException();
|
---|
| 321 | }
|
---|
[237] | 322 |
|
---|
[12509] | 323 | public virtual event EventHandler ColumnsChanged { add { } remove { } }
|
---|
| 324 | public virtual event EventHandler RowsChanged { add { } remove { } }
|
---|
| 325 | public virtual event EventHandler ColumnNamesChanged { add { } remove { } }
|
---|
| 326 | public virtual event EventHandler RowNamesChanged { add { } remove { } }
|
---|
| 327 | public virtual event EventHandler SortableViewChanged { add { } remove { } }
|
---|
| 328 | public virtual event EventHandler<EventArgs<int, int>> ItemChanged { add { } remove { } }
|
---|
| 329 | public virtual event EventHandler Reset { add { } remove { } }
|
---|
[2012] | 330 | #endregion
|
---|
[2] | 331 | }
|
---|
| 332 | }
|
---|