Changeset 16692 for branches/2521_ProblemRefactoring/HeuristicLab.Problems.DataAnalysis/3.4/Dataset.cs
- Timestamp:
- 03/18/19 17:24:30 (5 years ago)
- Location:
- branches/2521_ProblemRefactoring
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/2521_ProblemRefactoring
- Property svn:ignore
-
old new 24 24 protoc.exe 25 25 obj 26 .vs
-
- Property svn:mergeinfo changed
- Property svn:ignore
-
branches/2521_ProblemRefactoring/HeuristicLab.Problems.DataAnalysis
- Property svn:mergeinfo changed
-
branches/2521_ProblemRefactoring/HeuristicLab.Problems.DataAnalysis/3.4/Dataset.cs
r12509 r16692 1 1 #region License Information 2 2 /* HeuristicLab 3 * Copyright (C) 2002-201 5Heuristic and Evolutionary Algorithms Laboratory (HEAL)3 * Copyright (C) 2002-2018 Heuristic and Evolutionary Algorithms Laboratory (HEAL) 4 4 * 5 5 * This file is part of HeuristicLab. … … 52 52 } 53 53 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> 54 59 public Dataset(IEnumerable<string> variableNames, IEnumerable<IList> variableValues) 55 60 : base() { … … 69 74 throw new ArgumentException(message); 70 75 } 71 72 76 rows = variableValues.First().Count; 73 77 this.variableNames = new List<string>(variableNames); … … 75 79 for (int i = 0; i < this.variableNames.Count; i++) { 76 80 var values = variableValues.ElementAt(i); 77 IList clonedValues = null; 78 if (values is IList<double>) 79 clonedValues = new List<double>(values.Cast<double>()); 80 else if (values is IList<string>) 81 clonedValues = new List<string>(values.Cast<string>()); 82 else if (values is IList<DateTime>) 83 clonedValues = new List<DateTime>(values.Cast<DateTime>()); 84 else { 85 this.variableNames = new List<string>(); 86 this.variableValues = new Dictionary<string, IList>(); 87 throw new ArgumentException("The variable values must be of type IList<double>, IList<string> or IList<DateTime>"); 88 } 89 this.variableValues.Add(this.variableNames[i], clonedValues); 81 this.variableValues.Add(this.variableNames[i], values); 90 82 } 91 83 } … … 116 108 this.variableValues.Add(columName, values); 117 109 } 110 } 111 112 public ModifiableDataset ToModifiable() { 113 var values = new List<IList>(); 114 foreach (var v in variableNames) { 115 if (VariableHasType<double>(v)) { 116 values.Add(new List<double>((IList<double>)variableValues[v])); 117 } else if (VariableHasType<string>(v)) { 118 values.Add(new List<string>((IList<string>)variableValues[v])); 119 } else if (VariableHasType<DateTime>(v)) { 120 values.Add(new List<DateTime>((IList<DateTime>)variableValues[v])); 121 } else { 122 throw new ArgumentException("Unknown variable type."); 123 } 124 } 125 return new ModifiableDataset(variableNames, values); 126 } 127 /// <summary> 128 /// Shuffle a dataset's rows 129 /// </summary> 130 /// <param name="random">Random number generator used for shuffling.</param> 131 /// <returns>A shuffled copy of the current dataset.</returns> 132 public Dataset Shuffle(IRandom random) { 133 var values = variableNames.Select(x => variableValues[x]).ToList(); 134 return new Dataset(variableNames, values.ShuffleLists(random)); 118 135 } 119 136 … … 157 174 } 158 175 public IEnumerable<string> DoubleVariables { 159 get { return variableValues.Where(p => p.Value is List<double>).Select(p => p.Key); } 160 } 176 get { return variableValues.Where(p => p.Value is IList<double>).Select(p => p.Key); } 177 } 178 179 public IEnumerable<string> StringVariables { 180 get { return variableValues.Where(p => p.Value is IList<string>).Select(p => p.Key); } 181 } 182 183 public IEnumerable<string> DateTimeVariables { 184 get { return variableValues.Where(p => p.Value is IList<DateTime>).Select(p => p.Key); } 185 } 186 161 187 public IEnumerable<double> GetDoubleValues(string variableName) { 162 188 return GetValues<double>(variableName); … … 171 197 public ReadOnlyCollection<double> GetReadOnlyDoubleValues(string variableName) { 172 198 var values = GetValues<double>(variableName); 173 return values.AsReadOnly();199 return new ReadOnlyCollection<double>(values); 174 200 } 175 201 public double GetDoubleValue(string variableName, int row) { … … 180 206 return GetValues<double>(variableName, rows); 181 207 } 208 209 public string GetStringValue(string variableName, int row) { 210 var values = GetValues<string>(variableName); 211 return values[row]; 212 } 213 214 public IEnumerable<string> GetStringValues(string variableName, IEnumerable<int> rows) { 215 return GetValues<string>(variableName, rows); 216 } 217 public ReadOnlyCollection<string> GetReadOnlyStringValues(string variableName) { 218 var values = GetValues<string>(variableName); 219 return new ReadOnlyCollection<string>(values); 220 } 221 222 public DateTime GetDateTimeValue(string variableName, int row) { 223 var values = GetValues<DateTime>(variableName); 224 return values[row]; 225 } 226 public IEnumerable<DateTime> GetDateTimeValues(string variableName, IEnumerable<int> rows) { 227 return GetValues<DateTime>(variableName, rows); 228 } 229 public ReadOnlyCollection<DateTime> GetReadOnlyDateTimeValues(string variableName) { 230 var values = GetValues<DateTime>(variableName); 231 return new ReadOnlyCollection<DateTime>(values); 232 } 233 234 182 235 private IEnumerable<T> GetValues<T>(string variableName, IEnumerable<int> rows) { 183 236 var values = GetValues<T>(variableName); 184 237 return rows.Select(x => values[x]); 185 238 } 186 private List<T> GetValues<T>(string variableName) {239 private IList<T> GetValues<T>(string variableName) { 187 240 IList list; 188 241 if (!variableValues.TryGetValue(variableName, out list)) 189 242 throw new ArgumentException("The variable " + variableName + " does not exist in the dataset."); 190 List<T> values = list asList<T>;243 IList<T> values = list as IList<T>; 191 244 if (values == null) throw new ArgumentException("The variable " + variableName + " is not a " + typeof(T) + " variable."); 192 245 return values; … … 201 254 public int Rows { 202 255 get { return rows; } 203 set { throw new NotSupportedException(); } 204 } 256 } 257 int IStringConvertibleMatrix.Rows { 258 get { return Rows; } 259 set { throw new NotSupportedException(); } 260 } 261 205 262 public int Columns { 206 263 get { return variableNames.Count; } 207 set { throw new NotSupportedException(); } 208 } 209 public bool SortableView { 264 } 265 int IStringConvertibleMatrix.Columns { 266 get { return Columns; } 267 set { throw new NotSupportedException(); } 268 } 269 bool IStringConvertibleMatrix.SortableView { 210 270 get { return false; } 211 271 set { throw new NotSupportedException(); } 212 272 } 213 public boolReadOnly {273 bool IStringConvertibleMatrix.ReadOnly { 214 274 get { return true; } 215 275 } … … 222 282 set { throw new NotSupportedException(); } 223 283 } 224 public stringGetValue(int rowIndex, int columnIndex) {284 string IStringConvertibleMatrix.GetValue(int rowIndex, int columnIndex) { 225 285 return variableValues[variableNames[columnIndex]][rowIndex].ToString(); 226 286 }
Note: See TracChangeset
for help on using the changeset viewer.