Free cookie consent management tool by TermsFeed Policy Generator

source: branches/OptimizationNetworks/HeuristicLab.Problems.DataAnalysis/3.4/Dataset.cs @ 12214

Last change on this file since 12214 was 11576, checked in by swagner, 10 years ago

#2205: Merged changes r11062:11557 from trunk/sources into branches/OptimizationNetworks

File size: 11.8 KB
RevLine 
[2]1#region License Information
2/* HeuristicLab
[11576]3 * Copyright (C) 2002-2014 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
22using System;
[6740]23using System.Collections;
[2]24using System.Collections.Generic;
[6740]25using System.Collections.ObjectModel;
[2285]26using System.Linq;
[3376]27using HeuristicLab.Common;
[3253]28using HeuristicLab.Core;
[4068]29using HeuristicLab.Data;
[3253]30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
[2]31
[3253]32namespace HeuristicLab.Problems.DataAnalysis {
33  [Item("Dataset", "Represents a dataset containing data that should be analyzed.")]
34  [StorableClass]
35  public sealed class Dataset : NamedItem, IStringConvertibleMatrix {
[3933]36    [StorableConstructor]
[4722]37    private Dataset(bool deserializing) : base(deserializing) { }
38    private Dataset(Dataset original, Cloner cloner)
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
[6740]120    #region Backwards compatible code, remove with 3.5
121    private double[,] storableData;
122    //name alias used to suppport backwards compatibility
123    [Storable(Name = "data", AllowOneWay = true)]
124    private double[,] StorableData { set { storableData = value; } }
[2]125
[6740]126    [StorableHook(HookType.AfterDeserialization)]
127    private void AfterDeserialization() {
128      if (variableValues == null) {
129        rows = storableData.GetLength(0);
130        variableValues = new Dictionary<string, IList>();
131        for (int col = 0; col < storableData.GetLength(1); col++) {
132          string columName = variableNames[col];
[7921]133          var values = new List<double>(rows);
134          for (int row = 0; row < rows; row++) {
[6740]135            values.Add(storableData[row, col]);
136          }
137          variableValues.Add(columName, values);
138        }
139        storableData = null;
[3839]140      }
141    }
[6740]142    #endregion
[3933]143
[6749]144    [Storable(Name = "VariableValues")]
[6740]145    private Dictionary<string, IList> variableValues;
[6749]146
[6740]147    private List<string> variableNames;
148    [Storable]
149    public IEnumerable<string> VariableNames {
150      get { return variableNames; }
151      private set {
152        if (variableNames != null) throw new InvalidOperationException();
153        variableNames = new List<string>(value);
154      }
[1287]155    }
[2311]156
[6740]157    public IEnumerable<string> DoubleVariables {
158      get { return variableValues.Where(p => p.Value is List<double>).Select(p => p.Key); }
[3994]159    }
[5552]160
[6740]161    public IEnumerable<double> GetDoubleValues(string variableName) {
162      IList list;
163      if (!variableValues.TryGetValue(variableName, out list))
164        throw new ArgumentException("The variable " + variableName + " does not exist in the dataset.");
165      List<double> values = list as List<double>;
166      if (values == null) throw new ArgumentException("The variable " + variableName + " is not a double variable.");
[4031]167
[6740]168      //mkommend yield return used to enable lazy evaluation
169      foreach (double value in values)
170        yield return value;
[3994]171    }
[11576]172
173    public IEnumerable<string> GetStringValues(string variableName) {
174      IList list;
175      if (!variableValues.TryGetValue(variableName, out list))
176        throw new ArgumentException("The variable " + variableName + " does not exist in the dataset.");
177      List<string> values = list as List<string>;
178      if (values == null) throw new ArgumentException("The variable " + variableName + " is not a string variable.");
179
180      //mkommend yield return used to enable lazy evaluation
181      foreach (string value in values)
182        yield return value;
183    }
184
185    public IEnumerable<DateTime> GetDateTimeValues(string variableName) {
186      IList list;
187      if (!variableValues.TryGetValue(variableName, out list))
188        throw new ArgumentException("The variable " + variableName + " does not exist in the dataset.");
189      List<DateTime> values = list as List<DateTime>;
190      if (values == null) throw new ArgumentException("The variable " + variableName + " is not a datetime variable.");
191
192      //mkommend yield return used to enable lazy evaluation
193      foreach (DateTime value in values)
194        yield return value;
195    }
196
[6740]197    public ReadOnlyCollection<double> GetReadOnlyDoubleValues(string variableName) {
198      IList list;
199      if (!variableValues.TryGetValue(variableName, out list))
200        throw new ArgumentException("The variable " + variableName + " does not exist in the dataset.");
201      List<double> values = list as List<double>;
202      if (values == null) throw new ArgumentException("The variable " + variableName + " is not a double variable.");
203      return values.AsReadOnly();
[3994]204    }
[6740]205    public double GetDoubleValue(string variableName, int row) {
206      IList list;
207      if (!variableValues.TryGetValue(variableName, out list))
208        throw new ArgumentException("The variable " + variableName + " does not exist in the dataset.");
209      List<double> values = list as List<double>;
210      if (values == null) throw new ArgumentException("The variable " + variableName + " is not a double variable.");
211      return values[row];
[4031]212    }
[6740]213    public IEnumerable<double> GetDoubleValues(string variableName, IEnumerable<int> rows) {
214      IList list;
215      if (!variableValues.TryGetValue(variableName, out list))
216        throw new ArgumentException("The variable " + variableName + " does not exist in the dataset.");
217      List<double> values = list as List<double>;
[9217]218      if (values == null) throw new ArgumentException("The variable " + variableName + " is not a double variable.");
[3994]219
[8798]220      return rows.Select(index => values[index]);
[2319]221    }
[2310]222
[11576]223    public bool VariableHasType<T>(string variableName) {
224      return variableValues[variableName] is IList<T>;
225    }
226
[3253]227    #region IStringConvertibleMatrix Members
[6740]228    [Storable]
229    private int rows;
[3253]230    public int Rows {
[6740]231      get { return rows; }
[3933]232      set { throw new NotSupportedException(); }
[2]233    }
[3253]234    public int Columns {
[6740]235      get { return variableNames.Count; }
[3933]236      set { throw new NotSupportedException(); }
[2]237    }
238
[3321]239    public bool SortableView {
[3933]240      get { return false; }
241      set { throw new NotSupportedException(); }
[3321]242    }
[3430]243    public bool ReadOnly {
[3933]244      get { return true; }
[3430]245    }
246
[3308]247    IEnumerable<string> IStringConvertibleMatrix.ColumnNames {
248      get { return this.VariableNames; }
[3933]249      set { throw new NotSupportedException(); }
[3308]250    }
[3311]251    IEnumerable<string> IStringConvertibleMatrix.RowNames {
[5552]252      get { return Enumerable.Empty<string>(); }
[3933]253      set { throw new NotSupportedException(); }
[3311]254    }
255
[3253]256    public string GetValue(int rowIndex, int columnIndex) {
[6740]257      return variableValues[variableNames[columnIndex]][rowIndex].ToString();
[2]258    }
[3253]259    public bool SetValue(string value, int rowIndex, int columnIndex) {
[3933]260      throw new NotSupportedException();
[237]261    }
[5552]262    public bool Validate(string value, out string errorMessage) {
263      throw new NotSupportedException();
264    }
[237]265
[5552]266    public event EventHandler ColumnsChanged { add { } remove { } }
267    public event EventHandler RowsChanged { add { } remove { } }
268    public event EventHandler ColumnNamesChanged { add { } remove { } }
269    public event EventHandler RowNamesChanged { add { } remove { } }
270    public event EventHandler SortableViewChanged { add { } remove { } }
271    public event EventHandler<EventArgs<int, int>> ItemChanged { add { } remove { } }
[5554]272    public event EventHandler Reset { add { } remove { } }
[2012]273    #endregion
[2]274  }
275}
Note: See TracBrowser for help on using the repository browser.