[2] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[13760] | 3 | * Copyright (C) 2002-2016 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 |
|
---|
| 77 | rows = variableValues.First().Count;
|
---|
| 78 | this.variableNames = new List<string>(variableNames);
|
---|
[7736] | 79 | this.variableValues = new Dictionary<string, IList>(this.variableNames.Count);
|
---|
[6740] | 80 | for (int i = 0; i < this.variableNames.Count; i++) {
|
---|
| 81 | var values = variableValues.ElementAt(i);
|
---|
[13419] | 82 | this.variableValues.Add(this.variableNames[i], values);
|
---|
[6740] | 83 | }
|
---|
[2038] | 84 | }
|
---|
| 85 |
|
---|
[6740] | 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 | }
|
---|
[5552] | 98 |
|
---|
[6740] | 99 | rows = variableValues.GetLength(0);
|
---|
| 100 | this.variableNames = new List<string>(variableNames);
|
---|
| 101 |
|
---|
[7736] | 102 | this.variableValues = new Dictionary<string, IList>(variableValues.GetLength(1));
|
---|
[6740] | 103 | for (int col = 0; col < variableValues.GetLength(1); col++) {
|
---|
| 104 | string columName = this.variableNames[col];
|
---|
[7736] | 105 | var values = new List<double>(variableValues.GetLength(0));
|
---|
[6740] | 106 | for (int row = 0; row < variableValues.GetLength(0); row++) {
|
---|
| 107 | values.Add(variableValues[row, col]);
|
---|
[5552] | 108 | }
|
---|
[6740] | 109 | this.variableValues.Add(columName, values);
|
---|
[5552] | 110 | }
|
---|
[333] | 111 | }
|
---|
| 112 |
|
---|
[13760] | 113 | public ModifiableDataset ToModifiable() {
|
---|
| 114 | var values = new List<IList>();
|
---|
| 115 | foreach (var v in variableNames) {
|
---|
| 116 | if (VariableHasType<double>(v)) {
|
---|
[13761] | 117 | values.Add(new List<double>((List<double>)variableValues[v]));
|
---|
[13760] | 118 | } else if (VariableHasType<string>(v)) {
|
---|
[13761] | 119 | values.Add(new List<string>((List<string>)variableValues[v]));
|
---|
[13760] | 120 | } else if (VariableHasType<DateTime>(v)) {
|
---|
[13761] | 121 | values.Add(new List<DateTime>((List<DateTime>)variableValues[v]));
|
---|
[13760] | 122 | } else {
|
---|
| 123 | throw new ArgumentException("Unknown variable type.");
|
---|
| 124 | }
|
---|
| 125 | }
|
---|
| 126 | return new ModifiableDataset(variableNames, values);
|
---|
| 127 | }
|
---|
| 128 |
|
---|
[12509] | 129 | protected Dataset(Dataset dataset) : this(dataset.variableNames, dataset.variableValues.Values) { }
|
---|
| 130 |
|
---|
[6740] | 131 | #region Backwards compatible code, remove with 3.5
|
---|
| 132 | private double[,] storableData;
|
---|
| 133 | //name alias used to suppport backwards compatibility
|
---|
| 134 | [Storable(Name = "data", AllowOneWay = true)]
|
---|
| 135 | private double[,] StorableData { set { storableData = value; } }
|
---|
[2] | 136 |
|
---|
[6740] | 137 | [StorableHook(HookType.AfterDeserialization)]
|
---|
| 138 | private void AfterDeserialization() {
|
---|
| 139 | if (variableValues == null) {
|
---|
| 140 | rows = storableData.GetLength(0);
|
---|
| 141 | variableValues = new Dictionary<string, IList>();
|
---|
| 142 | for (int col = 0; col < storableData.GetLength(1); col++) {
|
---|
| 143 | string columName = variableNames[col];
|
---|
[7921] | 144 | var values = new List<double>(rows);
|
---|
| 145 | for (int row = 0; row < rows; row++) {
|
---|
[6740] | 146 | values.Add(storableData[row, col]);
|
---|
| 147 | }
|
---|
| 148 | variableValues.Add(columName, values);
|
---|
| 149 | }
|
---|
| 150 | storableData = null;
|
---|
[3839] | 151 | }
|
---|
| 152 | }
|
---|
[6740] | 153 | #endregion
|
---|
[3933] | 154 |
|
---|
[6749] | 155 | [Storable(Name = "VariableValues")]
|
---|
[12509] | 156 | protected Dictionary<string, IList> variableValues;
|
---|
[6749] | 157 |
|
---|
[12509] | 158 | protected List<string> variableNames;
|
---|
[6740] | 159 | [Storable]
|
---|
| 160 | public IEnumerable<string> VariableNames {
|
---|
| 161 | get { return variableNames; }
|
---|
[12509] | 162 | protected set {
|
---|
[6740] | 163 | if (variableNames != null) throw new InvalidOperationException();
|
---|
| 164 | variableNames = new List<string>(value);
|
---|
| 165 | }
|
---|
[1287] | 166 | }
|
---|
[6740] | 167 | public IEnumerable<string> DoubleVariables {
|
---|
| 168 | get { return variableValues.Where(p => p.Value is List<double>).Select(p => p.Key); }
|
---|
[3994] | 169 | }
|
---|
[6740] | 170 | public IEnumerable<double> GetDoubleValues(string variableName) {
|
---|
[12509] | 171 | return GetValues<double>(variableName);
|
---|
[3994] | 172 | }
|
---|
[11114] | 173 | public IEnumerable<string> GetStringValues(string variableName) {
|
---|
[12509] | 174 | return GetValues<string>(variableName);
|
---|
[11114] | 175 | }
|
---|
| 176 | public IEnumerable<DateTime> GetDateTimeValues(string variableName) {
|
---|
[12509] | 177 | return GetValues<DateTime>(variableName);
|
---|
[11114] | 178 | }
|
---|
| 179 |
|
---|
[6740] | 180 | public ReadOnlyCollection<double> GetReadOnlyDoubleValues(string variableName) {
|
---|
[12509] | 181 | var values = GetValues<double>(variableName);
|
---|
[6740] | 182 | return values.AsReadOnly();
|
---|
[3994] | 183 | }
|
---|
[6740] | 184 | public double GetDoubleValue(string variableName, int row) {
|
---|
[12509] | 185 | var values = GetValues<double>(variableName);
|
---|
[6740] | 186 | return values[row];
|
---|
[4031] | 187 | }
|
---|
[6740] | 188 | public IEnumerable<double> GetDoubleValues(string variableName, IEnumerable<int> rows) {
|
---|
[12509] | 189 | return GetValues<double>(variableName, rows);
|
---|
| 190 | }
|
---|
| 191 | private IEnumerable<T> GetValues<T>(string variableName, IEnumerable<int> rows) {
|
---|
| 192 | var values = GetValues<T>(variableName);
|
---|
| 193 | return rows.Select(x => values[x]);
|
---|
| 194 | }
|
---|
| 195 | private List<T> GetValues<T>(string variableName) {
|
---|
[6740] | 196 | IList list;
|
---|
| 197 | if (!variableValues.TryGetValue(variableName, out list))
|
---|
| 198 | throw new ArgumentException("The variable " + variableName + " does not exist in the dataset.");
|
---|
[12509] | 199 | List<T> values = list as List<T>;
|
---|
| 200 | if (values == null) throw new ArgumentException("The variable " + variableName + " is not a " + typeof(T) + " variable.");
|
---|
| 201 | return values;
|
---|
[2319] | 202 | }
|
---|
[11156] | 203 | public bool VariableHasType<T>(string variableName) {
|
---|
[11114] | 204 | return variableValues[variableName] is IList<T>;
|
---|
| 205 | }
|
---|
| 206 |
|
---|
[3253] | 207 | #region IStringConvertibleMatrix Members
|
---|
[6740] | 208 | [Storable]
|
---|
[12509] | 209 | protected int rows;
|
---|
[3253] | 210 | public int Rows {
|
---|
[6740] | 211 | get { return rows; }
|
---|
[13539] | 212 | }
|
---|
| 213 | int IStringConvertibleMatrix.Rows {
|
---|
| 214 | get { return Rows; }
|
---|
[3933] | 215 | set { throw new NotSupportedException(); }
|
---|
[2] | 216 | }
|
---|
[13539] | 217 |
|
---|
[3253] | 218 | public int Columns {
|
---|
[6740] | 219 | get { return variableNames.Count; }
|
---|
[13539] | 220 | }
|
---|
| 221 | int IStringConvertibleMatrix.Columns {
|
---|
| 222 | get { return Columns; }
|
---|
[3933] | 223 | set { throw new NotSupportedException(); }
|
---|
[2] | 224 | }
|
---|
[13427] | 225 | bool IStringConvertibleMatrix.SortableView {
|
---|
[3933] | 226 | get { return false; }
|
---|
| 227 | set { throw new NotSupportedException(); }
|
---|
[3321] | 228 | }
|
---|
[13427] | 229 | bool IStringConvertibleMatrix.ReadOnly {
|
---|
[3933] | 230 | get { return true; }
|
---|
[3430] | 231 | }
|
---|
[3308] | 232 | IEnumerable<string> IStringConvertibleMatrix.ColumnNames {
|
---|
| 233 | get { return this.VariableNames; }
|
---|
[3933] | 234 | set { throw new NotSupportedException(); }
|
---|
[3308] | 235 | }
|
---|
[3311] | 236 | IEnumerable<string> IStringConvertibleMatrix.RowNames {
|
---|
[5552] | 237 | get { return Enumerable.Empty<string>(); }
|
---|
[3933] | 238 | set { throw new NotSupportedException(); }
|
---|
[3311] | 239 | }
|
---|
[13427] | 240 | string IStringConvertibleMatrix.GetValue(int rowIndex, int columnIndex) {
|
---|
[6740] | 241 | return variableValues[variableNames[columnIndex]][rowIndex].ToString();
|
---|
[2] | 242 | }
|
---|
[12509] | 243 | bool IStringConvertibleMatrix.SetValue(string value, int rowIndex, int columnIndex) {
|
---|
[3933] | 244 | throw new NotSupportedException();
|
---|
[237] | 245 | }
|
---|
[12509] | 246 | bool IStringConvertibleMatrix.Validate(string value, out string errorMessage) {
|
---|
[5552] | 247 | throw new NotSupportedException();
|
---|
| 248 | }
|
---|
[237] | 249 |
|
---|
[12509] | 250 | public virtual event EventHandler ColumnsChanged { add { } remove { } }
|
---|
| 251 | public virtual event EventHandler RowsChanged { add { } remove { } }
|
---|
| 252 | public virtual event EventHandler ColumnNamesChanged { add { } remove { } }
|
---|
| 253 | public virtual event EventHandler RowNamesChanged { add { } remove { } }
|
---|
| 254 | public virtual event EventHandler SortableViewChanged { add { } remove { } }
|
---|
| 255 | public virtual event EventHandler<EventArgs<int, int>> ItemChanged { add { } remove { } }
|
---|
| 256 | public virtual event EventHandler Reset { add { } remove { } }
|
---|
[2012] | 257 | #endregion
|
---|
[2] | 258 | }
|
---|
| 259 | }
|
---|