Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PersistenceReintegration/HeuristicLab.Problems.DataAnalysis/3.4/DatasetUtil.cs @ 15517

Last change on this file since 15517 was 14857, checked in by bburlacu, 8 years ago

#2723: Add DatasetUtil static class with useful methods for dealing with Datasets. Add input validation to Dataset constructor.

File size: 3.6 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2017 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
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;
23using System.Collections;
24using System.Collections.Generic;
25using System.ComponentModel;
26using System.Linq;
27using System.Text;
28using HeuristicLab.Core;
29using HeuristicLab.Random;
30
31namespace HeuristicLab.Problems.DataAnalysis {
32  public static class DatasetUtil {
33    /// <summary>
34    /// Shuffle all the lists with the same shuffling.
35    /// </summary>
36    /// <param name="values">The value lists to be shuffled.</param>
37    /// <param name="random">The random number generator</param>
38    /// <returns>A new list containing shuffled copies of the original value lists.</returns>
39    public static List<IList> ShuffleLists(this List<IList> values, IRandom random) {
40      ValidateInputData(values);
41
42      int count = values.First().Count;
43      int[] indices = Enumerable.Range(0, count).Shuffle(random).ToArray();
44      List<IList> shuffled = new List<IList>(values.Count);
45      for (int col = 0; col < values.Count; col++) {
46
47        if (values[col] is List<double>)
48          shuffled.Add(new List<double>());
49        else if (values[col] is List<DateTime>)
50          shuffled.Add(new List<DateTime>());
51        else if (values[col] is List<string>)
52          shuffled.Add(new List<string>());
53        else
54          throw new InvalidOperationException();
55
56        for (int i = 0; i < count; i++) {
57          shuffled[col].Add(values[col][indices[i]]);
58        }
59      }
60      return shuffled;
61    }
62
63    /// <summary>
64    /// This method checks if the provided lists of values are actually of the type List<T>, where T is a double, string or DateTime
65    /// </summary>
66    /// <param name="values">The values lists</param>
67    internal static void ValidateInputData(IEnumerable<IList> values) {
68      if (!values.Any())
69        throw new InvalidEnumArgumentException("The provided list of values is empty.");
70
71      var errorIndices = new List<int>();
72      int col = 0;
73      foreach (var v in values) {
74        var doubleList = v as List<double>;
75        var stringList = v as List<string>;
76        var dateTimeList = v as List<DateTime>;
77
78        var typedCollections = new IList[] { doubleList, stringList, dateTimeList };
79
80        if (typedCollections.All(x => x == null)) {
81          errorIndices.Add(col); // the values are not a) a list and b) of any of the supported types
82        }
83        ++col;
84      }
85
86      if (errorIndices.Any()) {
87        var sb = new StringBuilder();
88        for (int i = 0; i < errorIndices.Count; ++i) {
89          sb.Append(i);
90          sb.Append(i < errorIndices.Count - 1 ? ", " : " ");
91        }
92        var error = string.Format("Invalid input values. The following columns are not lists of double, string or DateTime values: {0}", sb);
93        throw new ArgumentException(error);
94      }
95    }
96  }
97}
Note: See TracBrowser for help on using the repository browser.