Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Hive_Milestone2/sources/HeuristicLab.Visualization/3.2/FloatingAvgAggregator.cs @ 6004

Last change on this file since 6004 was 1818, checked in by cbahner, 16 years ago

#519 added floating average

File size: 4.0 KB
Line 
1using System;
2using System.Collections.Generic;
3
4namespace HeuristicLab.Visualization {
5  public class FloatingAvgAggregator : DataRowBase {
6
7    private readonly List<double> dataRow = new List<double>();
8    readonly List<IDataRow> dataRowWatches = new List<IDataRow>();
9
10    #region IAggregator Members
11
12    public void AddWatch(IDataRow watchDataRow) {
13      dataRowWatches.Add(watchDataRow);
14      watchDataRow.ValueChanged += dataRow_ValueChanged;
15      watchDataRow.ValuesChanged += dataRow_ValuesChanged;
16      watchDataRow.DataRowChanged += dataRow_DataRowChanged;
17    }
18
19    public void RemoveWatch(IDataRow watchDataRow) {
20      dataRowWatches.Remove(watchDataRow);
21      watchDataRow.DataRowChanged -= dataRow_DataRowChanged;
22      watchDataRow.ValuesChanged -= dataRow_ValuesChanged;
23      watchDataRow.ValueChanged -= dataRow_ValueChanged;
24    }
25
26    #endregion
27
28    void dataRow_ValueChanged(IDataRow row, double value, int index, Action action) {
29      switch (action) {
30        case Action.Added:
31          refreshLastValues(row);
32          break;
33        case Action.Modified:
34          refreshValue();
35          break;
36        case Action.Deleted:
37          refreshLastValues(row);
38          break;
39        default:
40          throw new ArgumentOutOfRangeException("action");
41      }
42    }
43
44    void dataRow_ValuesChanged(IDataRow row, double[] values, int index, Action action) {
45      refreshValue();
46    }
47
48    void dataRow_DataRowChanged(IDataRow row) {
49      refreshValue();
50    }
51
52    private int area = 5;
53    private void refreshValue() {
54
55      if (dataRowWatches.Count >= 1) {
56        IDataRow watchedRow = dataRowWatches[0];
57       
58        dataRow.Clear();
59        OnDataRowChanged(this);
60
61        for (int i = 0; i < watchedRow.Count; i++) {
62
63          double avgVal = 0;
64          int count = 0;
65          for (int j = Math.Max(0, i-area); j < Math.Min(watchedRow.Count, i+area); j++) {
66            avgVal += watchedRow[j];
67            count++;
68          }
69
70          if (count >= 1)
71            avgVal /= count;
72
73          dataRow.Add(avgVal);
74
75          OnValueChanged(avgVal, dataRow.Count - 1, Action.Added);
76        }
77      }
78      //OnValueChanged(avgVal, dataRow.Count - 1, Action.Added);
79      OnValuesChanged(dataRow.ToArray(), 0, Action.Modified);
80      //OnDataRowChanged(this);                                     
81    }
82
83#pragma warning disable 168
84    private void refreshLastValues(IDataRow row) {
85#pragma warning restore 168
86      refreshValue();
87    }
88
89    #region IDataRow Members
90
91    public override void AddValue(double value) {
92      throw new NotSupportedException();
93//      dataRow.Add(value);
94//      OnValueChanged(value, dataRow.Count - 1, Action.Added);
95    }
96
97    public override void AddValue(double value, int index) {
98      throw new NotSupportedException();
99    }
100
101    public override void AddValues(double[] values) {
102      throw new NotSupportedException();
103    }
104
105    public override void AddValues(double[] values, int index) {
106      throw new NotSupportedException();
107    }
108
109    public override void ModifyValue(double value, int index) {
110      throw new NotSupportedException();
111    }
112
113    public override void ModifyValues(double[] values, int index) {
114      throw new NotSupportedException();
115    }
116
117    public override void RemoveValue(int index) {
118      throw new NotSupportedException();
119    }
120
121    public override void RemoveValues(int index, int countVals) {
122      throw new NotSupportedException();
123    }
124
125    public override int Count {
126      get { return dataRow.Count; } //return dataRowWatches.Count; }
127    }
128
129    public override double this[int index] {
130      get { return dataRow[index]; }
131      set {
132        dataRow[index] = value;
133        OnValueChanged(value, index, Action.Modified);
134      }
135    }
136
137    public override double MinValue {
138      get { return 0; }
139    }
140
141    public override double MaxValue {
142      get { return 0; }
143    }
144
145    #endregion
146  }
147}
Note: See TracBrowser for help on using the repository browser.