Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
05/30/08 13:08:42 (16 years ago)
Author:
gkronber
Message:

merged changesets r166 r168 r195:196 r209 from the trunk into the stable branch

Location:
branches/3.0/sources/HeuristicLab.DataAnalysis
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/3.0/sources/HeuristicLab.DataAnalysis/Dataset.cs

    r132 r278  
    3939    private double[] samples;
    4040    private int rows;
     41    Dictionary<int, Dictionary<int, double>>[] cachedMeans;
     42    Dictionary<int, Dictionary<int, double>>[] cachedRanges;
    4143
    4244    public int Rows {
     
    5052      set { columns = value; }
    5153    }
    52     private Dictionary<int, double[]>[] ranges;
    53     private Dictionary<int, double[]>[] means;
    5454
    5555    public double GetValue(int i, int j) {
     
    9494      // keep a means and ranges dictionary for each column (possible target variable) of the dataset.
    9595
    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];
    9898
    9999      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>>();
    102102      }
    103103    }
     
    200200    }
    201201
    202     // return value of GetMean should be memoized because it is called repeatedly in Evaluators
    203202    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;
    215212      } 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      }
    235215    }
    236216
     
    239219    }
    240220
    241     // return value of GetRange should be memoized because it is called repeatedly in Evaluators
    242221    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;
    254231      } 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      }
    274234    }
    275235  }
  • branches/3.0/sources/HeuristicLab.DataAnalysis/DatasetView.cs

    r2 r278  
    3333      set {
    3434        Item = value;
    35         Item.Changed += new EventHandler(Item_Changed);
     35        Refresh();
    3636      }
    3737    }
    3838
    39     void Item_Changed(object sender, EventArgs e) {
    40       Refresh();
    41     }
    42 
    43     public DatasetView() : base() {
     39    public DatasetView()
     40      : base() {
    4441      InitializeComponent();
    4542      openFileDialog = new OpenFileDialog();
    4643    }
    4744
    48     public DatasetView(Dataset dataset) :this() {
     45    public DatasetView(Dataset dataset)
     46      : this() {
    4947      this.Dataset = dataset;
    5048    }
     
    5250    protected override void UpdateControls() {
    5351      base.UpdateControls();
    54       if(Dataset != null) {
     52      if (Dataset != null) {
    5553        int rows = Dataset.Rows;
    5654        int columns = Dataset.Columns;
    57 
    5855        nameTextBox.Text = Dataset.Name;
    5956        rowsTextBox.Text = rows + "";
     
    6158        dataGridView.ColumnCount = columns;
    6259        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++) {
    6562            dataGridView.Rows[i].Cells[j].Value = Dataset.GetValue(i, j);
    6663          }
     
    8582
    8683    private void dataGridView_CellValidating(object sender, DataGridViewCellValidatingEventArgs e) {
    87       if(ValidateData((string)e.FormattedValue)) {
     84      if (ValidateData((string)e.FormattedValue)) {
    8885        SetArrayElement(e.RowIndex, e.ColumnIndex, (string)e.FormattedValue);
    8986        e.Cancel = false;
     
    9794      double result;
    9895      double.TryParse(element, out result);
    99       if(result != Dataset.GetValue(row, column)) {
     96      if (result != Dataset.GetValue(row, column)) {
    10097        Dataset.SetValue(row, column, result);
    10198        Dataset.FireChanged();
Note: See TracChangeset for help on using the changeset viewer.