Changeset 15013
- Timestamp:
- 05/31/17 16:15:08 (7 years ago)
- Location:
- trunk/sources/HeuristicLab.Problems.DataAnalysis/3.4
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Problems.DataAnalysis/3.4/Dataset.cs
r14864 r15013 74 74 throw new ArgumentException(message); 75 75 } 76 DatasetUtil.ValidateInputData(variableValues); // the validation call checks if every values IList is actually a list of the supported type77 76 rows = variableValues.First().Count; 78 77 this.variableNames = new List<string>(variableNames); … … 175 174 } 176 175 public IEnumerable<string> DoubleVariables { 177 get { return variableValues.Where(p => p.Value is List<double>).Select(p => p.Key); }176 get { return variableValues.Where(p => p.Value is IList<double>).Select(p => p.Key); } 178 177 } 179 178 180 179 public IEnumerable<string> StringVariables { 181 get { return variableValues.Where(p => p.Value is List<string>).Select(p => p.Key); }180 get { return variableValues.Where(p => p.Value is IList<string>).Select(p => p.Key); } 182 181 } 183 182 … … 194 193 public ReadOnlyCollection<double> GetReadOnlyDoubleValues(string variableName) { 195 194 var values = GetValues<double>(variableName); 196 return values.AsReadOnly();195 return new ReadOnlyCollection<double>(values); 197 196 } 198 197 public double GetDoubleValue(string variableName, int row) { … … 214 213 public ReadOnlyCollection<string> GetReadOnlyStringValues(string variableName) { 215 214 var values = GetValues<string>(variableName); 216 return values.AsReadOnly();215 return new ReadOnlyCollection<string>(values); 217 216 } 218 217 … … 221 220 return rows.Select(x => values[x]); 222 221 } 223 private List<T> GetValues<T>(string variableName) {222 private IList<T> GetValues<T>(string variableName) { 224 223 IList list; 225 224 if (!variableValues.TryGetValue(variableName, out list)) 226 225 throw new ArgumentException("The variable " + variableName + " does not exist in the dataset."); 227 List<T> values = list asList<T>;226 IList<T> values = list as IList<T>; 228 227 if (values == null) throw new ArgumentException("The variable " + variableName + " is not a " + typeof(T) + " variable."); 229 228 return values; -
trunk/sources/HeuristicLab.Problems.DataAnalysis/3.4/DatasetUtil.cs
r14857 r15013 38 38 /// <returns>A new list containing shuffled copies of the original value lists.</returns> 39 39 public static List<IList> ShuffleLists(this List<IList> values, IRandom random) { 40 ValidateInputData(values);41 42 40 int count = values.First().Count; 43 41 int[] indices = Enumerable.Range(0, count).Shuffle(random).ToArray(); … … 45 43 for (int col = 0; col < values.Count; col++) { 46 44 47 if (values[col] is List<double>)45 if (values[col] is IList<double>) 48 46 shuffled.Add(new List<double>()); 49 else if (values[col] is List<DateTime>)47 else if (values[col] is IList<DateTime>) 50 48 shuffled.Add(new List<DateTime>()); 51 else if (values[col] is List<string>)49 else if (values[col] is IList<string>) 52 50 shuffled.Add(new List<string>()); 53 51 else … … 59 57 } 60 58 return shuffled; 61 }62 59 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 DateTime65 /// </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 types82 }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 60 } 96 61 }
Note: See TracChangeset
for help on using the changeset viewer.