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