Changeset 278 for branches/3.0/sources/HeuristicLab.DataAnalysis
- Timestamp:
- 05/30/08 13:08:42 (16 years ago)
- Location:
- branches/3.0/sources/HeuristicLab.DataAnalysis
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/3.0/sources/HeuristicLab.DataAnalysis/Dataset.cs
r132 r278 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 43 42 44 public int Rows { … … 50 52 set { columns = value; } 51 53 } 52 private Dictionary<int, double[]>[] ranges;53 private Dictionary<int, double[]>[] means;54 54 55 55 public double GetValue(int i, int j) { … … 94 94 // keep a means and ranges dictionary for each column (possible target variable) of the dataset. 95 95 96 means = new Dictionary<int, double[]>[columns];97 ranges = new Dictionary<int, double[]>[columns];96 cachedMeans = new Dictionary<int, Dictionary<int, double>>[columns]; 97 cachedRanges = new Dictionary<int, Dictionary<int, double>>[columns]; 98 98 99 99 for(int i = 0; i < columns; i++) { 100 means[i] = new Dictionary<int, double[]>();101 ranges[i] = new Dictionary<int, double[]>();100 cachedMeans[i] = new Dictionary<int, Dictionary<int, double>>(); 101 cachedRanges[i] = new Dictionary<int, Dictionary<int, double>>(); 102 102 } 103 103 } … … 200 200 } 201 201 202 // return value of GetMean should be memoized because it is called repeatedly in Evaluators203 202 public double GetMean(int column, int from, int to) { 204 Dictionary<int, double[]> columnMeans = means[column]; 205 if(columnMeans.ContainsKey(from)) { 206 double[] fromMeans = columnMeans[from]; 207 if(fromMeans[to-from] >= 0.0) { 208 // already calculated 209 return fromMeans[to-from]; 210 } else { 211 // not yet calculated => calculate 212 fromMeans[to-from] = CalculateMean(column, from, to); 213 return fromMeans[to-from]; 214 } 203 if(!cachedMeans[column].ContainsKey(from) || !cachedMeans[column][from].ContainsKey(to)) { 204 double[] values = new double[to - from + 1]; 205 for(int sample = from; sample <= to; sample++) { 206 values[sample - from] = GetValue(sample, column); 207 } 208 double mean = Statistics.Mean(values); 209 if(!cachedMeans[column].ContainsKey(from)) cachedMeans[column][from] = new Dictionary<int, double>(); 210 cachedMeans[column][from][to] = mean; 211 return mean; 215 212 } else { 216 // never saw this from-index => create a new array, initialize and recalculate for to-index 217 double[] fromMeans = new double[rows - from]; 218 // fill with negative values to indicate which means have already been calculated 219 for(int i=0;i<fromMeans.Length;i++) {fromMeans[i] = -1.0;} 220 // store new array in the dictionary 221 columnMeans[from] = fromMeans; 222 // calculate for specific to-index 223 fromMeans[to-from] = CalculateMean(column, from, to); 224 return fromMeans[to-from]; 225 } 226 } 227 228 private double CalculateMean(int column, int from, int to) { 229 double[] values = new double[to - from +1]; 230 for(int sample = from; sample <= to; sample++) { 231 values[sample - from] = GetValue(sample, column); 232 } 233 234 return Statistics.Mean(values); 213 return cachedMeans[column][from][to]; 214 } 235 215 } 236 216 … … 239 219 } 240 220 241 // return value of GetRange should be memoized because it is called repeatedly in Evaluators242 221 public double GetRange(int column, int from, int to) { 243 Dictionary<int, double[]> columnRanges = ranges[column]; 244 if(columnRanges.ContainsKey(from)) { 245 double[] fromRanges = columnRanges[from]; 246 if(fromRanges[to-from] >= 0.0) { 247 // already calculated 248 return fromRanges[to-from]; 249 } else { 250 // not yet calculated => calculate 251 fromRanges[to-from] = CalculateRange(column, from, to); 252 return fromRanges[to-from]; 253 } 222 if(!cachedRanges[column].ContainsKey(from) || !cachedRanges[column][from].ContainsKey(to)) { 223 double[] values = new double[to - from + 1]; 224 for(int sample = from; sample <= to; sample++) { 225 values[sample - from] = GetValue(sample, column); 226 } 227 double range = Statistics.Range(values); 228 if(!cachedRanges[column].ContainsKey(from)) cachedRanges[column][from] = new Dictionary<int, double>(); 229 cachedRanges[column][from][to] = range; 230 return range; 254 231 } else { 255 // never saw this from-index => create a new array, initialize and recalculate for to-index 256 double[] fromRanges = new double[rows - from]; 257 // fill with negative values to indicate which means have already been calculated 258 for(int i = 0; i < fromRanges.Length; i++) { fromRanges[i] = -1.0; } 259 // store in dictionary 260 columnRanges[from] = fromRanges; 261 // calculate for specific to-index 262 fromRanges[to-from] = CalculateRange(column, from, to); 263 return fromRanges[to-from]; 264 } 265 } 266 267 private double CalculateRange(int column, int from, int to) { 268 double[] values = new double[to - from + 1]; 269 for(int sample = from; sample <= to; sample++) { 270 values[sample - from] = GetValue(sample, column); 271 } 272 273 return Statistics.Range(values); 232 return cachedRanges[column][from][to]; 233 } 274 234 } 275 235 } -
branches/3.0/sources/HeuristicLab.DataAnalysis/DatasetView.cs
r2 r278 33 33 set { 34 34 Item = value; 35 Item.Changed += new EventHandler(Item_Changed);35 Refresh(); 36 36 } 37 37 } 38 38 39 void Item_Changed(object sender, EventArgs e) { 40 Refresh(); 41 } 42 43 public DatasetView() : base() { 39 public DatasetView() 40 : base() { 44 41 InitializeComponent(); 45 42 openFileDialog = new OpenFileDialog(); 46 43 } 47 44 48 public DatasetView(Dataset dataset) :this() { 45 public DatasetView(Dataset dataset) 46 : this() { 49 47 this.Dataset = dataset; 50 48 } … … 52 50 protected override void UpdateControls() { 53 51 base.UpdateControls(); 54 if (Dataset != null) {52 if (Dataset != null) { 55 53 int rows = Dataset.Rows; 56 54 int columns = Dataset.Columns; 57 58 55 nameTextBox.Text = Dataset.Name; 59 56 rowsTextBox.Text = rows + ""; … … 61 58 dataGridView.ColumnCount = columns; 62 59 dataGridView.RowCount = rows; 63 for (int i = 0; i < rows; i++) {64 for (int j = 0; j < columns; j++) {60 for (int i = 0; i < rows; i++) { 61 for (int j = 0; j < columns; j++) { 65 62 dataGridView.Rows[i].Cells[j].Value = Dataset.GetValue(i, j); 66 63 } … … 85 82 86 83 private void dataGridView_CellValidating(object sender, DataGridViewCellValidatingEventArgs e) { 87 if (ValidateData((string)e.FormattedValue)) {84 if (ValidateData((string)e.FormattedValue)) { 88 85 SetArrayElement(e.RowIndex, e.ColumnIndex, (string)e.FormattedValue); 89 86 e.Cancel = false; … … 97 94 double result; 98 95 double.TryParse(element, out result); 99 if (result != Dataset.GetValue(row, column)) {96 if (result != Dataset.GetValue(row, column)) { 100 97 Dataset.SetValue(row, column, result); 101 98 Dataset.FireChanged();
Note: See TracChangeset
for help on using the changeset viewer.