Changeset 3308
- Timestamp:
- 04/12/10 14:14:39 (15 years ago)
- Location:
- trunk/sources
- Files:
-
- 7 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Data/3.3/BoolMatrix.cs
r3160 r3308 20 20 #endregion 21 21 22 using System.Collections.Generic; 22 23 using System.Text; 23 24 using HeuristicLab.Core; … … 73 74 set { Columns = value; } 74 75 } 76 IEnumerable<string> IStringConvertibleMatrix.ColumnNames { 77 get { return this.ColumnNames; } 78 set { this.ColumnNames = value; } 79 } 75 80 bool IStringConvertibleMatrix.Validate(string value, out string errorMessage) { 76 81 return Validate(value, out errorMessage); -
trunk/sources/HeuristicLab.Data/3.3/DoubleMatrix.cs
r3160 r3308 20 20 #endregion 21 21 22 using System.Collections.Generic; 22 23 using System.Text; 23 24 using HeuristicLab.Core; … … 73 74 set { Columns = value; } 74 75 } 76 IEnumerable<string> IStringConvertibleMatrix.ColumnNames { 77 get { return this.ColumnNames; } 78 set { this.ColumnNames = value; } 79 } 75 80 bool IStringConvertibleMatrix.Validate(string value, out string errorMessage) { 76 81 return Validate(value, out errorMessage); -
trunk/sources/HeuristicLab.Data/3.3/IStringConvertibleMatrix.cs
r3048 r3308 22 22 using System; 23 23 using HeuristicLab.Common; 24 using System.Collections.Generic; 24 25 25 26 namespace HeuristicLab.Data { … … 27 28 int Rows { get; set; } 28 29 int Columns { get; set; } 30 31 IEnumerable<string> ColumnNames { get; set; } 32 //bool SortableView { get; } 29 33 30 34 bool Validate(string value, out string errorMessage); -
trunk/sources/HeuristicLab.Data/3.3/IntMatrix.cs
r3160 r3308 20 20 #endregion 21 21 22 using System.Collections.Generic; 22 23 using System.Text; 23 24 using HeuristicLab.Core; … … 73 74 set { Columns = value; } 74 75 } 76 IEnumerable<string> IStringConvertibleMatrix.ColumnNames { 77 get { return this.ColumnNames; } 78 set { this.ColumnNames = value; } 79 } 75 80 bool IStringConvertibleMatrix.Validate(string value, out string errorMessage) { 76 81 return Validate(value, out errorMessage); -
trunk/sources/HeuristicLab.Data/3.3/StringMatrix.cs
r3306 r3308 22 22 using System; 23 23 using System.Collections; 24 using System.Collections.Generic; 24 25 using System.Drawing; 26 using System.Linq; 25 27 using System.Text; 26 28 using HeuristicLab.Common; … … 38 40 [Storable] 39 41 protected string[,] matrix; 42 43 [Storable] 44 protected List<string> columnNames; 45 public IEnumerable<string> ColumnNames { 46 get { return this.columnNames; } 47 set { 48 if (value == null || value.Count() == 0) 49 columnNames = new List<string>(); 50 else if (value.Count() != Columns) 51 throw new ArgumentException("A columnName must be for each column specified."); 52 else 53 columnNames = new List<string>(value); 54 } 55 } 40 56 41 57 public virtual int Rows { … … 76 92 public StringMatrix() { 77 93 matrix = new string[0, 0]; 94 columnNames = new List<string>(); 78 95 } 79 96 public StringMatrix(int rows, int columns) { … … 83 100 matrix[i, j] = string.Empty; 84 101 } 102 columnNames = new List<string>(); 103 } 104 protected StringMatrix(int rows, int columns, IEnumerable<string> columnNames) 105 : this(rows, columns) { 106 ColumnNames = columnNames; 85 107 } 86 108 public StringMatrix(string[,] elements) { … … 91 113 matrix[i, j] = elements[i, j] == null ? string.Empty : elements[i, j]; 92 114 } 115 columnNames = new List<string>(); 116 } 117 protected StringMatrix(string[,] elements, IEnumerable<string> columnNames) 118 : this(elements) { 119 ColumnNames = columnNames; 93 120 } 94 121 … … 162 189 set { Columns = value; } 163 190 } 191 IEnumerable<string> IStringConvertibleMatrix.ColumnNames { 192 get { return this.ColumnNames; } 193 set { this.ColumnNames = value; } 194 } 164 195 bool IStringConvertibleMatrix.Validate(string value, out string errorMessage) { 165 196 return Validate(value, out errorMessage); -
trunk/sources/HeuristicLab.Data/3.3/ValueTypeMatrix.cs
r3306 r3308 22 22 using System; 23 23 using System.Collections; 24 using System.Collections.Generic; 24 25 using System.Drawing; 26 using System.Linq; 25 27 using System.Text; 26 28 using HeuristicLab.Common; … … 38 40 [Storable] 39 41 protected T[,] matrix; 42 43 [Storable] 44 protected List<string> columnNames; 45 public IEnumerable<string> ColumnNames { 46 get { return this.columnNames; } 47 set { 48 if (value == null || value.Count() == 0) 49 columnNames = new List<string>(); 50 else if (value.Count() != Columns) 51 throw new ArgumentException("A columnName must be for each column specified."); 52 else 53 columnNames = new List<string>(value); 54 } 55 } 40 56 41 57 public virtual int Rows { … … 74 90 protected ValueTypeMatrix() { 75 91 matrix = new T[0, 0]; 92 columnNames = new List<string>(); 76 93 } 77 94 protected ValueTypeMatrix(int rows, int columns) { 78 95 matrix = new T[rows, columns]; 96 columnNames = new List<string>(); 97 } 98 protected ValueTypeMatrix(int rows, int columns, IEnumerable<string> columnNames) 99 : this(rows, columns) { 100 ColumnNames = columnNames; 79 101 } 80 102 protected ValueTypeMatrix(T[,] elements) { 81 103 if (elements == null) throw new ArgumentNullException(); 82 104 matrix = (T[,])elements.Clone(); 105 columnNames = new List<string>(); 106 } 107 protected ValueTypeMatrix(T[,] elements, IEnumerable<string> columnNames) 108 : this(elements) { 109 ColumnNames = columnNames; 83 110 } 84 111 -
trunk/sources/HeuristicLab.Problems.DataAnalysis/3.3/Dataset.cs
r3294 r3308 45 45 throw new ArgumentException("Number of variable names doesn't match the number of columns of data"); 46 46 } 47 Data = new DoubleMatrix(data);48 this.variableNames = new StringArray(variableNames.ToArray());47 Data = data; 48 this.variableNames = variableNames.ToArray(); 49 49 } 50 50 51 51 [Storable] 52 private StringArrayvariableNames;52 private string[] variableNames; 53 53 public IEnumerable<string> VariableNames { 54 54 get { return variableNames; } … … 56 56 57 57 [Storable] 58 private DoubleMatrixdata;59 private DoubleMatrixData {58 private double[,] data; 59 private double[,] Data { 60 60 get { return data; } 61 61 set { 62 62 if (data != value) { 63 63 if (value == null) throw new ArgumentNullException(); 64 if (data != null) DeregisterDataEvents();65 64 this.data = value; 66 RegisterDataEvents();67 65 OnReset(EventArgs.Empty); 68 66 } … … 70 68 } 71 69 72 private void RegisterDataEvents() {73 data.Reset += new EventHandler(data_Reset);74 data.ItemChanged += new EventHandler<EventArgs<int, int>>(data_ItemChanged);75 }76 77 private void DeregisterDataEvents() {78 data.Reset -= new EventHandler(data_Reset);79 data.ItemChanged -= new EventHandler<EventArgs<int, int>>(data_ItemChanged);80 }81 70 // elementwise access 82 71 public double this[int rowIndex, int columnIndex] { … … 91 80 // access to full columns 92 81 public double[] this[string variableName] { 93 get { return GetVariableValues(GetVariableIndex(variableName), 0, data.Rows); }82 get { return GetVariableValues(GetVariableIndex(variableName), 0, Rows); } 94 83 } 95 84 … … 97 86 if (start < 0 || !(start <= end)) 98 87 throw new ArgumentException("Start must be between 0 and end (" + end + ")."); 99 if (end > data.Rows || end < start)100 throw new ArgumentException("End must be between start (" + start + ") and dataset rows (" + data.Rows + ").");88 if (end > Rows || end < start) 89 throw new ArgumentException("End must be between start (" + start + ") and dataset rows (" + Rows + ")."); 101 90 102 91 double[] values = new double[end - start]; … … 123 112 124 113 public void SetVariableName(int variableIndex, string name) { 114 if (name == null) throw new ArgumentNullException("Cannot set variable name to null for variable at index " + variableIndex + " variableIndex"); 125 115 if (variableNames.Contains(name)) throw new ArgumentException("The data set already contains a variable with name " + name + "."); 116 if (variableIndex < 0 || variableIndex >= variableNames.Length) throw new ArgumentException(" Cannot set name of not existent variable at index " + variableIndex + "."); 126 117 variableNames[variableIndex] = name; 127 118 } … … 139 130 140 131 public double GetMean(int variableIndex) { 141 return GetMean(variableIndex, 0, data.Rows);132 return GetMean(variableIndex, 0, Rows); 142 133 } 143 134 … … 151 142 152 143 public double GetRange(int variableIndex) { 153 return GetRange(variableIndex, 0, data.Rows);144 return GetRange(variableIndex, 0, Rows); 154 145 } 155 146 … … 168 159 169 160 public double GetMax(int variableIndex) { 170 return GetMax(variableIndex, 0, data.Rows);161 return GetMax(variableIndex, 0, Rows); 171 162 } 172 163 … … 184 175 185 176 public double GetMin(int variableIndex) { 186 return GetMin(variableIndex, 0, data.Rows);177 return GetMin(variableIndex, 0, Rows); 187 178 } 188 179 … … 199 190 } 200 191 public int GetMissingValues(int variableIndex) { 201 return GetMissingValues(variableIndex, 0, data.Rows);192 return GetMissingValues(variableIndex, 0, Rows); 202 193 } 203 194 … … 214 205 public override IDeepCloneable Clone(Cloner cloner) { 215 206 Dataset clone = (Dataset)base.Clone(cloner); 216 clone.data = ( DoubleMatrix)data.Clone(cloner);217 clone.variableNames = ( StringArray)variableNames.Clone(cloner);207 clone.data = (double[,])data.Clone(); 208 clone.variableNames = (string[])variableNames.Clone(); 218 209 return clone; 219 210 } … … 244 235 public int Rows { 245 236 get { 246 return data. Rows + 1;237 return data.GetLength(0); 247 238 } 248 239 set { 249 240 if (value == 0) throw new ArgumentException("Number of rows must be at least one (for variable names)"); 250 if (value - 1 != data.Rows) {251 var newValues = new double[value - 1, data.Columns];252 for (int row = 0; row < Math.Min( data.Rows, value - 1); row++) {253 for (int column = 0; column < data.Columns; column++) {241 if (value != Rows) { 242 var newValues = new double[value, Columns]; 243 for (int row = 0; row < Math.Min(Rows, value); row++) { 244 for (int column = 0; column < Columns; column++) { 254 245 newValues[row, column] = data[row, column]; 255 246 } 256 247 } 257 Data = new DoubleMatrix(newValues);248 Data = newValues; 258 249 } 259 250 } … … 262 253 public int Columns { 263 254 get { 264 return data. Columns;265 } 266 set { 267 if (value != data.Columns) {268 var newValues = new double[ data.Rows, value];255 return data.GetLength(1); 256 } 257 set { 258 if (value != Columns) { 259 var newValues = new double[Rows, value]; 269 260 var newVariableNames = new string[value]; 270 for (int row = 0; row < data.Rows; row++) {271 for (int column = 0; column < Math.Min(value, data.Columns); column++) {261 for (int row = 0; row < Rows; row++) { 262 for (int column = 0; column < Math.Min(value, Columns); column++) { 272 263 newValues[row, column] = data[row, column]; 273 264 } … … 275 266 string formatString = new StringBuilder().Append('0', (int)Math.Log10(value) + 1).ToString(); // >= 100 variables => ### 276 267 for (int column = 0; column < value; column++) { 277 if (column < data.Columns)268 if (column < Columns) 278 269 newVariableNames[column] = variableNames[column]; 279 270 else 280 271 newVariableNames[column] = "Var" + column.ToString(formatString); 281 272 } 282 variableNames = new StringArray(newVariableNames); 283 Data = new DoubleMatrix(newValues); 273 variableNames = newVariableNames; 274 Data = newValues; 275 } 276 } 277 } 278 279 IEnumerable<string> IStringConvertibleMatrix.ColumnNames { 280 get { return this.VariableNames; } 281 set { 282 int i = 0; 283 foreach (string variableName in value) { 284 SetVariableName(i, variableName); 285 i++; 284 286 } 285 287 } … … 292 294 293 295 public string GetValue(int rowIndex, int columnIndex) { 294 if (rowIndex == 0) { 295 // return variable name 296 return variableNames[columnIndex]; 297 } else { 298 return data[rowIndex - 1, columnIndex].ToString(); 299 } 296 return data[rowIndex, columnIndex].ToString(); 300 297 } 301 298 302 299 public bool SetValue(string value, int rowIndex, int columnIndex) { 303 if (rowIndex == 0) { 304 // check if the variable name is already used 305 if (variableNames.Contains(value)) { 306 return false; 307 } else { 308 variableNames[columnIndex] = value; 309 return true; 310 } 311 } else { 312 double v; 313 if (double.TryParse(value, out v)) { 314 data[rowIndex - 1, columnIndex] = v; 315 return true; 316 } else return false; 317 } 300 double v; 301 if (double.TryParse(value, out v)) { 302 data[rowIndex, columnIndex] = v; 303 return true; 304 } else return false; 318 305 } 319 306 320 307 public event EventHandler<EventArgs<int, int>> ItemChanged; 321 322 308 #endregion 323 309 }
Note: See TracChangeset
for help on using the changeset viewer.