- Timestamp:
- 05/13/08 14:36:44 (17 years ago)
- Location:
- trunk/sources/HeuristicLab.DataAnalysis
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.DataAnalysis/Dataset.cs
r232 r237 39 39 private double[] samples; 40 40 private int rows; 41 Dictionary<int, Dictionary<int, double>>[] cachedMeans; 42 Dictionary<int, Dictionary<int, double>>[] cachedRanges; 41 private Dictionary<int, Dictionary<int, double>>[] cachedMeans; 42 private Dictionary<int, Dictionary<int, double>>[] cachedRanges; 43 private double[] scalingFactor; 44 private double[] scalingOffset; 43 45 44 46 public int Rows { … … 67 69 public double[] Samples { 68 70 get { return samples; } 69 set { 71 set { 72 scalingFactor = new double[columns]; 73 scalingOffset = new double[columns]; 74 for(int i = 0; i < scalingFactor.Length; i++) { 75 scalingFactor[i] = 1.0; 76 scalingOffset[i] = 0.0; 77 } 70 78 samples = value; 71 79 CreateDictionaries(); … … 82 90 public Dataset() { 83 91 Name = "-"; 84 VariableNames = new string[] { "Var0"};92 VariableNames = new string[] { "Var0" }; 85 93 Columns = 1; 86 94 Rows = 1; 87 95 Samples = new double[1]; 96 scalingOffset = new double[] { 0.0 }; 97 scalingFactor = new double[] { 1.0 }; 88 98 } 89 99 90 100 private void CreateDictionaries() { 91 101 // keep a means and ranges dictionary for each column (possible target variable) of the dataset. 92 93 102 cachedMeans = new Dictionary<int, Dictionary<int, double>>[columns]; 94 103 cachedRanges = new Dictionary<int, Dictionary<int, double>>[columns]; 95 96 104 for(int i = 0; i < columns; i++) { 97 105 cachedMeans[i] = new Dictionary<int, Dictionary<int, double>>(); … … 115 123 clone.VariableNames = new string[VariableNames.Length]; 116 124 Array.Copy(VariableNames, clone.VariableNames, VariableNames.Length); 125 Array.Copy(scalingFactor, clone.scalingFactor, columns); 126 Array.Copy(scalingOffset, clone.scalingOffset, columns); 117 127 return clone; 118 128 } … … 129 139 dim2.Value = columns.ToString(CultureInfo.InvariantCulture.NumberFormat); 130 140 node.Attributes.Append(dim2); 131 132 141 XmlAttribute variableNames = document.CreateAttribute("VariableNames"); 133 142 variableNames.Value = GetVariableNamesString(); 134 143 node.Attributes.Append(variableNames); 135 144 XmlAttribute scalingFactorsAttribute = document.CreateAttribute("ScalingFactors"); 145 scalingFactorsAttribute.Value = GetString(scalingFactor); 146 node.Attributes.Append(scalingFactorsAttribute); 147 XmlAttribute scalingOffsetsAttribute = document.CreateAttribute("ScalingOffsets"); 148 scalingOffsetsAttribute.Value = GetString(scalingOffset); 149 node.Attributes.Append(scalingOffsetsAttribute); 136 150 node.InnerText = ToString(CultureInfo.InvariantCulture.NumberFormat); 137 151 return node; … … 143 157 rows = int.Parse(node.Attributes["Dimension1"].Value, CultureInfo.InvariantCulture.NumberFormat); 144 158 columns = int.Parse(node.Attributes["Dimension2"].Value, CultureInfo.InvariantCulture.NumberFormat); 145 159 146 160 VariableNames = ParseVariableNamesString(node.Attributes["VariableNames"].Value); 161 if(node.Attributes["ScalingFactors"] != null) 162 scalingFactor = ParseDoubleString(node.Attributes["ScalingFactors"].Value); 163 else { 164 scalingFactor = new double[columns]; // compatibility with old serialization format 165 for(int i = 0; i < scalingFactor.Length; i++) scalingFactor[i] = 1.0; 166 } 167 if(node.Attributes["ScalingOffsets"] != null) 168 scalingOffset = ParseDoubleString(node.Attributes["ScalingOffsets"].Value); 169 else { 170 scalingOffset = new double[columns]; // compatibility with old serialization format 171 for(int i = 0; i < scalingOffset.Length; i++) scalingOffset[i] = 0.0; 172 } 147 173 148 174 string[] tokens = node.InnerText.Split(';'); … … 151 177 for(int row = 0; row < rows; row++) { 152 178 for(int column = 0; column < columns; column++) { 153 if(double.TryParse(tokens[row * columns + column], NumberStyles.Float, CultureInfo.InvariantCulture.NumberFormat, out samples[row *columns + column]) == false) {179 if(double.TryParse(tokens[row * columns + column], NumberStyles.Float, CultureInfo.InvariantCulture.NumberFormat, out samples[row * columns + column]) == false) { 154 180 throw new FormatException("Can't parse " + tokens[row * columns + column] + " as double value."); 155 181 } … … 168 194 for(int column = 0; column < columns; column++) { 169 195 builder.Append(";"); 170 builder.Append(samples[row *columns+column].ToString(format));196 builder.Append(samples[row * columns + column].ToString(format)); 171 197 } 172 198 } … … 177 203 private string GetVariableNamesString() { 178 204 string s = ""; 179 for 205 for(int i = 0; i < variableNames.Length; i++) { 180 206 s += variableNames[i] + "; "; 181 207 } 182 208 183 if (variableNames.Length > 0) { 209 if(variableNames.Length > 0) { 210 s = s.TrimEnd(';', ' '); 211 } 212 return s; 213 } 214 private string GetString(double[] xs) { 215 string s = ""; 216 for(int i = 0; i < xs.Length; i++) { 217 s += xs[i].ToString(CultureInfo.InvariantCulture) + "; "; 218 } 219 220 if(xs.Length > 0) { 184 221 s = s.TrimEnd(';', ' '); 185 222 } … … 189 226 private string[] ParseVariableNamesString(string p) { 190 227 p = p.Trim(); 191 string[] tokens = p.Split(new char[] { ';'}, StringSplitOptions.RemoveEmptyEntries);228 string[] tokens = p.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries); 192 229 return tokens; 193 230 } 231 private double[] ParseDoubleString(string s) { 232 s = s.Trim(); 233 string[] ss = s.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries); 234 double[] xs = new double[ss.Length]; 235 for(int i = 0; i < xs.Length; i++) { 236 xs[i] = double.Parse(ss[i], CultureInfo.InvariantCulture); 237 } 238 return xs; 239 } 194 240 195 241 public double GetMean(int column) { 196 return GetMean(column, 0, Rows -1);242 return GetMean(column, 0, Rows - 1); 197 243 } 198 244 … … 213 259 214 260 public double GetRange(int column) { 215 return GetRange(column, 0, Rows -1);261 return GetRange(column, 0, Rows - 1); 216 262 } 217 263 … … 248 294 return min; 249 295 } 296 297 internal void ScaleVariable(int column) { 298 if(scalingFactor[column] == 1.0) { 299 double min = GetMinimum(column); 300 double max = GetMaximum(column); 301 double range = max - min; 302 scalingFactor[column] = range; 303 scalingOffset[column] = min; 304 for(int i = 0; i < Rows; i++) { 305 double origValue = samples[i * columns + column]; 306 samples[i * columns + column] = (origValue - min) / range; 307 } 308 } 309 CreateDictionaries(); 310 FireChanged(); 311 } 312 313 internal void UnscaleVariable(int column) { 314 if(scalingFactor[column] != 1.0) { 315 for(int i = 0; i < rows; i++) { 316 double scaledValue = samples[i * columns + column]; 317 samples[i * columns + column] = scaledValue * scalingFactor[column] + scalingOffset[column]; 318 } 319 scalingFactor[column] = 1.0; 320 scalingOffset[column] = 0.0; 321 } 322 } 250 323 } 251 324 } -
trunk/sources/HeuristicLab.DataAnalysis/DatasetView.cs
r234 r237 34 34 } 35 35 } 36 37 private double[] scalingFactor;38 private double[] scalingOffset;39 36 public DatasetView() 40 37 : base() { … … 56 53 protected override void UpdateControls() { 57 54 base.UpdateControls(); 58 if(this.scalingFactor == null) {59 this.scalingFactor = new double[Dataset.Columns];60 this.scalingOffset = new double[Dataset.Columns];61 for(int i = 0; i < scalingFactor.Length; i++) {62 scalingFactor[i] = 1.0;63 scalingOffset[i] = 0.0;64 }65 }66 55 if (Dataset != null) { 67 56 int rows = Dataset.Rows; … … 105 94 if (result != Dataset.GetValue(row, column)) { 106 95 Dataset.SetValue(row, column, result); 107 Dataset.FireChanged();108 96 } 109 97 } … … 116 104 private void scaleValuesToolStripMenuItem_Click(object sender, EventArgs e) { 117 105 foreach(DataGridViewColumn column in dataGridView.SelectedColumns) { 118 if(scalingFactor[column.Index] == 1.0) { 119 double min = Dataset.GetMinimum(column.Index); 120 double max = Dataset.GetMaximum(column.Index); 121 double range = max - min; 122 scalingFactor[column.Index] = range; 123 scalingOffset[column.Index] = min; 124 column.Name = GetColumnName(column.Index) + " [scaled]"; 125 for(int i = 0; i < Dataset.Rows; i++) { 126 Dataset.SetValue(i, column.Index, (Dataset.GetValue(i, column.Index)-min) / range); 127 } 128 } 106 Dataset.ScaleVariable(column.Index); 107 column.Name = GetColumnName(column.Index) + " [scaled]"; 129 108 } 130 109 Refresh(); … … 133 112 private void originalValuesToolStripMenuItem_Click(object sender, EventArgs e) { 134 113 foreach(DataGridViewColumn column in dataGridView.SelectedColumns) { 135 if(scalingFactor[column.Index] != 1.0) { 136 column.Name = GetColumnName(column.Index); 137 for(int i = 0; i < Dataset.Rows; i++) { 138 Dataset.SetValue(i, column.Index, Dataset.GetValue(i, column.Index) * scalingFactor[column.Index] + scalingOffset[column.Index]); 139 } 140 scalingFactor[column.Index] = 1.0; 141 scalingOffset[column.Index] = 0.0; 142 } 114 Dataset.UnscaleVariable(column.Index); 115 column.Name = GetColumnName(column.Index); 143 116 } 144 117 Refresh();
Note: See TracChangeset
for help on using the changeset viewer.