[2] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[12012] | 3 | * Copyright (C) 2002-2015 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 |
|
---|
[6740] | 54 | public Dataset(IEnumerable<string> variableNames, IEnumerable<IList> variableValues)
|
---|
[3933] | 55 | : base() {
|
---|
[2319] | 56 | Name = "-";
|
---|
[6740] | 57 | if (!variableNames.Any()) {
|
---|
| 58 | this.variableNames = Enumerable.Range(0, variableValues.Count()).Select(x => "Column " + x).ToList();
|
---|
| 59 | } else if (variableNames.Count() != variableValues.Count()) {
|
---|
| 60 | throw new ArgumentException("Number of variable names doesn't match the number of columns of variableValues");
|
---|
| 61 | } else if (!variableValues.All(list => list.Count == variableValues.First().Count)) {
|
---|
| 62 | throw new ArgumentException("The number of values must be equal for every variable");
|
---|
| 63 | } else if (variableNames.Distinct().Count() != variableNames.Count()) {
|
---|
| 64 | var duplicateVariableNames =
|
---|
| 65 | variableNames.GroupBy(v => v).Where(g => g.Count() > 1).Select(g => g.Key).ToList();
|
---|
| 66 | string message = "The dataset cannot contain duplicate variables names: " + Environment.NewLine;
|
---|
| 67 | foreach (var duplicateVariableName in duplicateVariableNames)
|
---|
| 68 | message += duplicateVariableName + Environment.NewLine;
|
---|
| 69 | throw new ArgumentException(message);
|
---|
[3264] | 70 | }
|
---|
[6740] | 71 |
|
---|
| 72 | rows = variableValues.First().Count;
|
---|
| 73 | this.variableNames = new List<string>(variableNames);
|
---|
[7736] | 74 | this.variableValues = new Dictionary<string, IList>(this.variableNames.Count);
|
---|
[6740] | 75 | for (int i = 0; i < this.variableNames.Count; i++) {
|
---|
| 76 | var values = variableValues.ElementAt(i);
|
---|
| 77 | IList clonedValues = null;
|
---|
[10406] | 78 | if (values is IList<double>)
|
---|
[6740] | 79 | clonedValues = new List<double>(values.Cast<double>());
|
---|
[10406] | 80 | else if (values is IList<string>)
|
---|
[6740] | 81 | clonedValues = new List<string>(values.Cast<string>());
|
---|
[10406] | 82 | else if (values is IList<DateTime>)
|
---|
[6740] | 83 | clonedValues = new List<DateTime>(values.Cast<DateTime>());
|
---|
| 84 | else {
|
---|
| 85 | this.variableNames = new List<string>();
|
---|
| 86 | this.variableValues = new Dictionary<string, IList>();
|
---|
[10406] | 87 | throw new ArgumentException("The variable values must be of type IList<double>, IList<string> or IList<DateTime>");
|
---|
[6740] | 88 | }
|
---|
| 89 | this.variableValues.Add(this.variableNames[i], clonedValues);
|
---|
| 90 | }
|
---|
[2038] | 91 | }
|
---|
| 92 |
|
---|
[6740] | 93 | public Dataset(IEnumerable<string> variableNames, double[,] variableValues) {
|
---|
| 94 | Name = "-";
|
---|
| 95 | if (variableNames.Count() != variableValues.GetLength(1)) {
|
---|
| 96 | throw new ArgumentException("Number of variable names doesn't match the number of columns of variableValues");
|
---|
| 97 | }
|
---|
| 98 | if (variableNames.Distinct().Count() != variableNames.Count()) {
|
---|
| 99 | var duplicateVariableNames = variableNames.GroupBy(v => v).Where(g => g.Count() > 1).Select(g => g.Key).ToList();
|
---|
| 100 | string message = "The dataset cannot contain duplicate variables names: " + Environment.NewLine;
|
---|
| 101 | foreach (var duplicateVariableName in duplicateVariableNames)
|
---|
| 102 | message += duplicateVariableName + Environment.NewLine;
|
---|
| 103 | throw new ArgumentException(message);
|
---|
| 104 | }
|
---|
[5552] | 105 |
|
---|
[6740] | 106 | rows = variableValues.GetLength(0);
|
---|
| 107 | this.variableNames = new List<string>(variableNames);
|
---|
| 108 |
|
---|
[7736] | 109 | this.variableValues = new Dictionary<string, IList>(variableValues.GetLength(1));
|
---|
[6740] | 110 | for (int col = 0; col < variableValues.GetLength(1); col++) {
|
---|
| 111 | string columName = this.variableNames[col];
|
---|
[7736] | 112 | var values = new List<double>(variableValues.GetLength(0));
|
---|
[6740] | 113 | for (int row = 0; row < variableValues.GetLength(0); row++) {
|
---|
| 114 | values.Add(variableValues[row, col]);
|
---|
[5552] | 115 | }
|
---|
[6740] | 116 | this.variableValues.Add(columName, values);
|
---|
[5552] | 117 | }
|
---|
[333] | 118 | }
|
---|
| 119 |
|
---|
[12509] | 120 | protected Dataset(Dataset dataset) : this(dataset.variableNames, dataset.variableValues.Values) { }
|
---|
| 121 |
|
---|
[6740] | 122 | #region Backwards compatible code, remove with 3.5
|
---|
| 123 | private double[,] storableData;
|
---|
| 124 | //name alias used to suppport backwards compatibility
|
---|
| 125 | [Storable(Name = "data", AllowOneWay = true)]
|
---|
| 126 | private double[,] StorableData { set { storableData = value; } }
|
---|
[2] | 127 |
|
---|
[6740] | 128 | [StorableHook(HookType.AfterDeserialization)]
|
---|
| 129 | private void AfterDeserialization() {
|
---|
| 130 | if (variableValues == null) {
|
---|
| 131 | rows = storableData.GetLength(0);
|
---|
| 132 | variableValues = new Dictionary<string, IList>();
|
---|
| 133 | for (int col = 0; col < storableData.GetLength(1); col++) {
|
---|
| 134 | string columName = variableNames[col];
|
---|
[7921] | 135 | var values = new List<double>(rows);
|
---|
| 136 | for (int row = 0; row < rows; row++) {
|
---|
[6740] | 137 | values.Add(storableData[row, col]);
|
---|
| 138 | }
|
---|
| 139 | variableValues.Add(columName, values);
|
---|
| 140 | }
|
---|
| 141 | storableData = null;
|
---|
[3839] | 142 | }
|
---|
| 143 | }
|
---|
[6740] | 144 | #endregion
|
---|
[3933] | 145 |
|
---|
[6749] | 146 | [Storable(Name = "VariableValues")]
|
---|
[12509] | 147 | protected Dictionary<string, IList> variableValues;
|
---|
[6749] | 148 |
|
---|
[12509] | 149 | protected List<string> variableNames;
|
---|
[6740] | 150 | [Storable]
|
---|
| 151 | public IEnumerable<string> VariableNames {
|
---|
| 152 | get { return variableNames; }
|
---|
[12509] | 153 | protected set {
|
---|
[6740] | 154 | if (variableNames != null) throw new InvalidOperationException();
|
---|
| 155 | variableNames = new List<string>(value);
|
---|
| 156 | }
|
---|
[1287] | 157 | }
|
---|
[6740] | 158 | public IEnumerable<string> DoubleVariables {
|
---|
| 159 | get { return variableValues.Where(p => p.Value is List<double>).Select(p => p.Key); }
|
---|
[3994] | 160 | }
|
---|
[6740] | 161 | public IEnumerable<double> GetDoubleValues(string variableName) {
|
---|
[12509] | 162 | return GetValues<double>(variableName);
|
---|
[3994] | 163 | }
|
---|
[11114] | 164 | public IEnumerable<string> GetStringValues(string variableName) {
|
---|
[12509] | 165 | return GetValues<string>(variableName);
|
---|
[11114] | 166 | }
|
---|
| 167 | public IEnumerable<DateTime> GetDateTimeValues(string variableName) {
|
---|
[12509] | 168 | return GetValues<DateTime>(variableName);
|
---|
[11114] | 169 | }
|
---|
| 170 |
|
---|
[6740] | 171 | public ReadOnlyCollection<double> GetReadOnlyDoubleValues(string variableName) {
|
---|
[12509] | 172 | var values = GetValues<double>(variableName);
|
---|
[6740] | 173 | return values.AsReadOnly();
|
---|
[3994] | 174 | }
|
---|
[6740] | 175 | public double GetDoubleValue(string variableName, int row) {
|
---|
[12509] | 176 | var values = GetValues<double>(variableName);
|
---|
[6740] | 177 | return values[row];
|
---|
[4031] | 178 | }
|
---|
[6740] | 179 | public IEnumerable<double> GetDoubleValues(string variableName, IEnumerable<int> rows) {
|
---|
[12509] | 180 | return GetValues<double>(variableName, rows);
|
---|
| 181 | }
|
---|
| 182 | private IEnumerable<T> GetValues<T>(string variableName, IEnumerable<int> rows) {
|
---|
| 183 | var values = GetValues<T>(variableName);
|
---|
| 184 | return rows.Select(x => values[x]);
|
---|
| 185 | }
|
---|
| 186 | private List<T> GetValues<T>(string variableName) {
|
---|
[6740] | 187 | IList list;
|
---|
| 188 | if (!variableValues.TryGetValue(variableName, out list))
|
---|
| 189 | throw new ArgumentException("The variable " + variableName + " does not exist in the dataset.");
|
---|
[12509] | 190 | List<T> values = list as List<T>;
|
---|
| 191 | if (values == null) throw new ArgumentException("The variable " + variableName + " is not a " + typeof(T) + " variable.");
|
---|
| 192 | return values;
|
---|
[2319] | 193 | }
|
---|
[11156] | 194 | public bool VariableHasType<T>(string variableName) {
|
---|
[11114] | 195 | return variableValues[variableName] is IList<T>;
|
---|
| 196 | }
|
---|
| 197 |
|
---|
[3253] | 198 | #region IStringConvertibleMatrix Members
|
---|
[6740] | 199 | [Storable]
|
---|
[12509] | 200 | protected int rows;
|
---|
[3253] | 201 | public int Rows {
|
---|
[6740] | 202 | get { return rows; }
|
---|
[3933] | 203 | set { throw new NotSupportedException(); }
|
---|
[2] | 204 | }
|
---|
[3253] | 205 | public int Columns {
|
---|
[6740] | 206 | get { return variableNames.Count; }
|
---|
[3933] | 207 | set { throw new NotSupportedException(); }
|
---|
[2] | 208 | }
|
---|
[3321] | 209 | public bool SortableView {
|
---|
[3933] | 210 | get { return false; }
|
---|
| 211 | set { throw new NotSupportedException(); }
|
---|
[3321] | 212 | }
|
---|
[3430] | 213 | public bool ReadOnly {
|
---|
[3933] | 214 | get { return true; }
|
---|
[3430] | 215 | }
|
---|
[3308] | 216 | IEnumerable<string> IStringConvertibleMatrix.ColumnNames {
|
---|
| 217 | get { return this.VariableNames; }
|
---|
[3933] | 218 | set { throw new NotSupportedException(); }
|
---|
[3308] | 219 | }
|
---|
[3311] | 220 | IEnumerable<string> IStringConvertibleMatrix.RowNames {
|
---|
[5552] | 221 | get { return Enumerable.Empty<string>(); }
|
---|
[3933] | 222 | set { throw new NotSupportedException(); }
|
---|
[3311] | 223 | }
|
---|
[3253] | 224 | public string GetValue(int rowIndex, int columnIndex) {
|
---|
[6740] | 225 | return variableValues[variableNames[columnIndex]][rowIndex].ToString();
|
---|
[2] | 226 | }
|
---|
[12509] | 227 | bool IStringConvertibleMatrix.SetValue(string value, int rowIndex, int columnIndex) {
|
---|
[3933] | 228 | throw new NotSupportedException();
|
---|
[237] | 229 | }
|
---|
[12509] | 230 | bool IStringConvertibleMatrix.Validate(string value, out string errorMessage) {
|
---|
[5552] | 231 | throw new NotSupportedException();
|
---|
| 232 | }
|
---|
[237] | 233 |
|
---|
[12509] | 234 | public virtual event EventHandler ColumnsChanged { add { } remove { } }
|
---|
| 235 | public virtual event EventHandler RowsChanged { add { } remove { } }
|
---|
| 236 | public virtual event EventHandler ColumnNamesChanged { add { } remove { } }
|
---|
| 237 | public virtual event EventHandler RowNamesChanged { add { } remove { } }
|
---|
| 238 | public virtual event EventHandler SortableViewChanged { add { } remove { } }
|
---|
| 239 | public virtual event EventHandler<EventArgs<int, int>> ItemChanged { add { } remove { } }
|
---|
| 240 | public virtual event EventHandler Reset { add { } remove { } }
|
---|
[2012] | 241 | #endregion
|
---|
[2] | 242 | }
|
---|
| 243 | }
|
---|