Changeset 562
- Timestamp:
- 09/13/08 20:32:22 (16 years ago)
- Location:
- trunk/sources/HeuristicLab.CEDMA.Charting
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.CEDMA.Charting/BubbleChart.cs
r561 r562 26 26 using System.Linq; 27 27 using HeuristicLab.Charting; 28 using System.Windows.Forms; 28 29 29 30 namespace HeuristicLab.CEDMA.Charting { … … 36 37 private const int minAlpha = 50; 37 38 private static readonly Color defaultColor = Color.Blue; 38 39 private List<string> dimensions; 40 private Dictionary<string, List<double>> values; 39 private static readonly Color selectionColor = Color.Red; 40 41 41 private double xJitterFactor = 0.0; 42 42 private double yJitterFactor = 0.0; … … 49 49 private double maxX = double.NegativeInfinity; 50 50 private double maxY = double.NegativeInfinity; 51 51 private List<Record> records; 52 private ResultList results; 53 private Dictionary<IPrimitive, Record> primitiveToRecordDictionary; 52 54 private Random random = new Random(); 53 54 public BubbleChart(PointD lowerLeft, PointD upperRight) 55 private Group points; 56 57 public BubbleChart(ResultList results, PointD lowerLeft, PointD upperRight) 55 58 : base(lowerLeft, upperRight) { 56 dimensions = new List<string>(); 57 values = new Dictionary<string, List<double>>(); 58 } 59 public BubbleChart(double x1, double y1, double x2, double y2) 60 : this(new PointD(x1, y1), new PointD(x2, y2)) { 61 } 62 63 public void AddDimension(string name) { 64 dimensions.Add(name); 65 values.Add(name, new List<double>()); 66 } 67 public void RemoveDimension(string name) { 68 dimensions.Remove(name); 69 values.Remove(name); 70 } 71 72 public void AddDataPoint(string dimension, double value) { 73 values[dimension].Add(value); 74 } 59 records = new List<Record>(); 60 primitiveToRecordDictionary = new Dictionary<IPrimitive, Record>(); 61 this.results = results; 62 results.OnRecordAdded += new EventHandler<RecordAddedEventArgs>(results_OnRecordAdded); 63 } 64 65 public BubbleChart(ResultList results, double x1, double y1, double x2, double y2) 66 : this(results, new PointD(x1, y1), new PointD(x2, y2)) { 67 } 68 69 void results_OnRecordAdded(object sender, RecordAddedEventArgs e) { 70 lock(records) { 71 e.Record.OnSelectionChanged += new EventHandler(Record_OnSelectionChanged); 72 records.Add(e.Record); 73 } 74 } 75 76 void Record_OnSelectionChanged(object sender, EventArgs e) { 77 Record r = (Record)sender; 78 foreach(KeyValuePair<IPrimitive, Record> pair in primitiveToRecordDictionary) { 79 if(pair.Value == r) { 80 IPrimitive primitive = pair.Key; 81 if(r.Selected) { 82 int alpha = primitive.Pen.Color.A; 83 primitive.Pen.Color = Color.FromArgb(alpha, selectionColor); 84 primitive.Brush = primitive.Pen.Brush; 85 primitive.IntoForeground(); 86 } else { 87 int alpha = primitive.Pen.Color.A; 88 primitive.Pen.Color = Color.FromArgb(alpha, defaultColor); 89 primitive.Brush = primitive.Pen.Brush; 90 primitive.IntoBackground(); 91 } 92 primitive.EnforceUpdate(); 93 } 94 } 95 } 96 97 //public void AddDimension(string name) { 98 // dimensions.Add(name); 99 // values.Add(name, new List<double>()); 100 //} 101 //public void RemoveDimension(string name) { 102 // dimensions.Remove(name); 103 // values.Remove(name); 104 //} 105 106 //public void AddDataPoint(string dimension, double value) { 107 // values[dimension].Add(value); 108 //} 75 109 76 110 public void SetBubbleSizeDimension(string dimension, bool inverted) { … … 91 125 } 92 126 93 public override void MouseClick(Point point, System.Windows.Forms.MouseButtons button) {94 // should select the model in the underlying model95 }96 97 public override void MouseDoubleClick(Point point, System.Windows.Forms.MouseButtons button) {98 // should open the model99 }100 101 127 internal void SetJitter(double xJitterFactor, double yJitterFactor) { 102 128 this.xJitterFactor = xJitterFactor * maxXJitterPercent * Size.Width; … … 107 133 108 134 private void Repaint() { 109 110 List<double> xs = values[xDimension]; 111 List<double> ys = values[yDimension]; 112 113 List<double> sizes = null; 114 double maxSize = 1; 115 double minSize = 1; 116 if(sizeDimension != null && values.ContainsKey(sizeDimension)) { 117 sizes = values[sizeDimension]; 118 maxSize = sizes.Max(); 119 minSize = sizes.Min(); 120 } 121 UpdateEnabled = false; 122 Group.Clear(); 123 Group.Add(new Axis(this, 0, 0, AxisType.Both)); 124 UpdateViewSize(0, 0, 5); 125 Group points = new Group(this); 126 int n = Math.Min(xs.Count, ys.Count); 127 if(sizes != null) n = Math.Min(n, sizes.Count); 128 for(int i = 0; i < n; i++) { 129 double x = xs[i] + (random.NextDouble() * 2.0 - 1.0) * xJitterFactor; 130 double y = ys[i] + (random.NextDouble() * 2.0 - 1.0) * yJitterFactor; 131 int size; 132 if(sizes != null) { 133 size = CalculateSize(sizes[i], minSize, maxSize); 134 } else { 135 size = minBubbleSize; 136 } 137 138 if(double.IsInfinity(x) || x == double.MaxValue || x == double.MinValue) x = double.NaN; 139 if(double.IsInfinity(y) || y == double.MaxValue || y == double.MinValue) y = double.NaN; 140 if(!double.IsNaN(x) && !double.IsNaN(y)) { 141 UpdateViewSize(x, y, size); 142 int alpha = CalculateAlpha(size); 143 Pen pen = new Pen(Color.FromArgb(alpha, defaultColor)); 144 Brush brush = pen.Brush; 145 points.Add(new FixedSizeCircle(this, x, y, size, pen, brush)); 146 } 147 } 148 Group.Add(points); 149 UpdateEnabled = true; 135 lock(records) { 136 double maxSize = 1; 137 double minSize = 1; 138 if(sizeDimension != null) { 139 var sizes = records.Select(r => r.Get(sizeDimension)); 140 maxSize = sizes.Max(); 141 minSize = sizes.Min(); 142 } 143 UpdateEnabled = false; 144 Group.Clear(); 145 primitiveToRecordDictionary.Clear(); 146 points = new Group(this); 147 Group.Add(new Axis(this, 0, 0, AxisType.Both)); 148 UpdateViewSize(0, 0, 5); 149 foreach(Record r in records) { 150 double x = r.Get(xDimension) + (random.NextDouble() * 2.0 - 1.0) * xJitterFactor; 151 double y = r.Get(yDimension) + (random.NextDouble() * 2.0 - 1.0) * yJitterFactor; 152 int size = CalculateSize(r.Get(sizeDimension), minSize, maxSize); 153 154 if(double.IsInfinity(x) || x == double.MaxValue || x == double.MinValue) x = double.NaN; 155 if(double.IsInfinity(y) || y == double.MaxValue || y == double.MinValue) y = double.NaN; 156 if(!double.IsNaN(x) && !double.IsNaN(y)) { 157 UpdateViewSize(x, y, size); 158 int alpha = CalculateAlpha(size); 159 Pen pen = new Pen(Color.FromArgb(alpha, defaultColor)); 160 Brush brush = pen.Brush; 161 FixedSizeCircle c = new FixedSizeCircle(this, x, y, size, pen, brush); 162 c.ToolTipText = CreateToolTipText(r); 163 points.Add(c); 164 primitiveToRecordDictionary[c] = r; 165 } 166 } 167 Group.Add(points); 168 UpdateEnabled = true; 169 } 170 } 171 172 private string CreateToolTipText(Record r) { 173 StringBuilder b = new StringBuilder(); 174 foreach(KeyValuePair<string, double> v in r.Values) { 175 b.Append(v.Key).Append(" = ").Append(v.Value).AppendLine(); 176 } 177 return b.ToString(); 150 178 } 151 179 152 180 private int CalculateSize(double size, double minSize, double maxSize) { 181 if(double.IsNaN(size)) return minBubbleSize; 153 182 double sizeDifference = ((size - minSize) / (maxSize - minSize) * (maxBubbleSize - minBubbleSize)); 154 183 if(invertSize) return maxBubbleSize - (int)sizeDifference; … … 186 215 maxY = double.NegativeInfinity; 187 216 } 217 218 public override void MouseClick(Point point, MouseButtons button) { 219 if(button == MouseButtons.Left) { 220 Record r; 221 IPrimitive p = points.GetPrimitive(TransformPixelToWorld(point)); 222 if(p != null) { 223 primitiveToRecordDictionary.TryGetValue(p, out r); 224 if(r != null) r.ToggleSelected(); 225 } 226 } else base.MouseClick(point, button); 227 } 228 229 public override void MouseDoubleClick(Point point, MouseButtons button) { 230 if(button == MouseButtons.Left) { 231 Record r; 232 IPrimitive p = points.GetPrimitive(TransformPixelToWorld(point)); 233 if(p != null) { 234 primitiveToRecordDictionary.TryGetValue(p, out r); 235 if(r != null) results.OpenModel(r); 236 } 237 } else base.MouseDoubleClick(point, button); 238 } 188 239 } 189 240 } -
trunk/sources/HeuristicLab.CEDMA.Charting/ResultList.cs
r561 r562 30 30 using System.Runtime.Serialization; 31 31 using System.IO; 32 using HeuristicLab.PluginInfrastructure; 32 33 33 34 namespace HeuristicLab.CEDMA.Charting { 35 public class Record { 36 private Dictionary<string, double> values = new Dictionary<string, double>(); 37 public Dictionary<string,double> Values { 38 get { 39 return values; 40 } 41 } 42 43 private bool selected = false; 44 public bool Selected { get { return selected; } } 45 46 private string uri; 47 public string Uri { get { return uri; } } 48 public Record(string uri) { 49 this.uri = uri; 50 } 51 52 public void Set(string name, double value) { 53 Values.Add(name, value); 54 } 55 56 public double Get(string name) { 57 if(name==null || !Values.ContainsKey(name)) return double.NaN; 58 return Values[name]; 59 } 60 61 public void ToggleSelected() { 62 selected = !selected; 63 if(OnSelectionChanged != null) OnSelectionChanged(this, new EventArgs()); 64 } 65 66 public event EventHandler OnSelectionChanged; 67 } 68 69 public class RecordAddedEventArgs : EventArgs { 70 private Record record; 71 public Record Record { get { return record; } } 72 public RecordAddedEventArgs(Record r) 73 : base() { 74 this.record = r; 75 } 76 } 77 34 78 public class ResultList : ItemBase { 35 79 private const string cedmaNS = "http://www.heuristiclab.com/cedma/"; … … 54 98 private readonly Entity treeSizePredicate = new Entity(cedmaNS + "TreeSize"); 55 99 private readonly Entity treeHeightPredicate = new Entity(cedmaNS + "TreeHeight"); 100 private readonly Entity rawDataPredicate = new Entity(cedmaNS + "rawData"); 56 101 private readonly Entity anyEntity = new Entity(null); 57 102 … … 74 119 } 75 120 } 76 private Dictionary<string, List<double>> allVariables; 121 122 public event EventHandler<RecordAddedEventArgs> OnRecordAdded; 123 124 private List<Record> records; 77 125 78 126 private void ReloadList() { 79 List<double> trainingMAPE = new List<double>();80 List<double> validationMAPE = new List<double>();81 List<double> testMAPE = new List<double>();82 List<double> trainingR2 = new List<double>();83 List<double> validationR2 = new List<double>();84 List<double> testR2 = new List<double>();85 List<double> size = new List<double>();86 List<double> height = new List<double>();87 List<double> targetVariable = new List<double>();88 89 lock(allVariables) {90 allVariables.Clear();91 allVariables[MAPE_TRAINING] = trainingMAPE;92 allVariables[MAPE_VALIDATION] = validationMAPE;93 allVariables[MAPE_TEST] = testMAPE;94 allVariables[R2_TRAINING] = trainingR2;95 allVariables[R2_VALIDATION] = validationR2;96 allVariables[R2_TEST] = testR2;97 allVariables[TREE_SIZE] = size;98 allVariables[TREE_HEIGHT] = height;99 allVariables[TARGET_VARIABLE] = targetVariable;100 }101 102 127 var results = store.Select(new Statement(anyEntity, new Entity(cedmaNS + "instanceOf"), new Literal("class:GpFunctionTree"))) 103 128 .Select(x => store.Select(new SelectFilter( … … 108 133 new Resource[] { anyEntity }))); 109 134 110 Random random = new Random(); // for adding random noise to int values111 135 foreach(Statement[] ss in results) { 112 lock(allVariables) { 113 targetVariable.Add(double.NaN); 114 size.Add(double.NaN); 115 height.Add(double.NaN); 116 trainingMAPE.Add(double.NaN); 117 validationMAPE.Add(double.NaN); 118 testMAPE.Add(double.NaN); 119 trainingR2.Add(double.NaN); 120 validationR2.Add(double.NaN); 121 testR2.Add(double.NaN); 136 if(ss.Length > 0) { 137 Record r = new Record(ss[0].Subject.Uri); 122 138 foreach(Statement s in ss) { 123 139 if(s.Predicate.Equals(targetVariablePredicate)) { 124 ReplaceLastItem(targetVariable, (double)(int)((Literal)s.Property).Value);140 r.Set(TARGET_VARIABLE, (double)(int)((Literal)s.Property).Value); 125 141 } else if(s.Predicate.Equals(treeSizePredicate)) { 126 ReplaceLastItem(size, (double)(int)((Literal)s.Property).Value);142 r.Set(TREE_SIZE, (double)(int)((Literal)s.Property).Value); 127 143 } else if(s.Predicate.Equals(treeHeightPredicate)) { 128 ReplaceLastItem(height, (double)(int)((Literal)s.Property).Value);144 r.Set(TREE_HEIGHT, (double)(int)((Literal)s.Property).Value); 129 145 } else if(s.Predicate.Equals(trainingMAPEPredicate)) { 130 ReplaceLastItem(trainingMAPE, (double)((Literal)s.Property).Value);146 r.Set(MAPE_TRAINING, (double)((Literal)s.Property).Value); 131 147 } else if(s.Predicate.Equals(validationMAPEPredicate)) { 132 ReplaceLastItem(validationMAPE, (double)((Literal)s.Property).Value);148 r.Set(MAPE_VALIDATION, (double)((Literal)s.Property).Value); 133 149 } else if(s.Predicate.Equals(testMAPEPredicate)) { 134 ReplaceLastItem(testMAPE, (double)((Literal)s.Property).Value);150 r.Set(MAPE_TEST, (double)((Literal)s.Property).Value); 135 151 } else if(s.Predicate.Equals(trainingR2Predicate)) { 136 ReplaceLastItem(trainingR2, (double)((Literal)s.Property).Value);152 r.Set(R2_TRAINING, (double)((Literal)s.Property).Value); 137 153 } else if(s.Predicate.Equals(validationR2Predicate)) { 138 ReplaceLastItem(validationR2, (double)((Literal)s.Property).Value);154 r.Set(R2_VALIDATION, (double)((Literal)s.Property).Value); 139 155 } else if(s.Predicate.Equals(testR2Predicate)) { 140 ReplaceLastItem(testR2, (double)((Literal)s.Property).Value);156 r.Set(R2_TEST, (double)((Literal)s.Property).Value); 141 157 } 142 158 } 159 records.Add(r); 160 FireRecordAdded(r); 143 161 } 144 FireChanged();145 162 } 146 163 } 147 164 148 private void ReplaceLastItem(List<double> xs, double x) {149 xs.RemoveAt(xs.Count - 1); xs.Add(x);165 private void FireRecordAdded(Record r) { 166 if(OnRecordAdded != null) OnRecordAdded(this, new RecordAddedEventArgs(r)); 150 167 } 151 168 152 169 public ResultList() 153 170 : base() { 154 allVariables = new Dictionary<string, List<double>>();171 records = new List<Record>(); 155 172 } 156 173 … … 159 176 } 160 177 161 internal Histogram GetHistogram(string variableName) { 162 Histogram h = new Histogram(50); 163 lock(allVariables) { 164 h.AddValues(allVariables[variableName]); 178 internal void OpenModel(Record r) { 179 IList<Statement> s = store.Select(new Statement(new Entity(r.Uri), rawDataPredicate, anyEntity)); 180 if(s.Count == 1) { 181 string rawData = ((SerializedLiteral)s[0].Property).RawData; 182 XmlDocument doc = new XmlDocument(); 183 doc.LoadXml(rawData); 184 IItem item = (IItem)PersistenceManager.Restore(doc.ChildNodes[1], new Dictionary<Guid, IStorable>()); 185 PluginManager.ControlManager.ShowControl(item.CreateView()); 165 186 } 166 return h;167 }168 169 internal IList<double> GetValues(string variableName) {170 List<double> result = new List<double>();171 lock(allVariables) {172 result.AddRange(allVariables[variableName]);173 }174 return result;175 187 } 176 188 } -
trunk/sources/HeuristicLab.CEDMA.Charting/ResultListView.cs
r561 r562 41 41 42 42 private void InitChart() { 43 dataChart.Chart = new BubbleChart(0, 0, 100, 100); 44 foreach(string dim in results.VariableNames) { 45 dataChart.Chart.AddDimension(dim); 46 IList<double> xs = results.GetValues(dim); 47 for(int i = 0; i < xs.Count; i++) { 48 double x = xs[i]; 49 if(double.IsInfinity(x) || x == double.MaxValue || x == double.MinValue) x = double.NaN; 50 if(!double.IsNaN(x)) { 51 dataChart.Chart.AddDataPoint(dim, x); 52 } 53 } 54 } 43 dataChart.Chart = new BubbleChart(results, 0, 0, 100, 100); 55 44 } 56 45 … … 79 68 // TASK 80 69 } 81 70 82 71 private void jitterTrackBar_ValueChanged(object sender, EventArgs e) { 83 72 if(dataChart.Chart != null) { 84 double xJitterFactor = xTrackBar.Value / 100.0 85 double yJitterFactor = yTrackBar.Value / 100.0 73 double xJitterFactor = xTrackBar.Value / 100.0; 74 double yJitterFactor = yTrackBar.Value / 100.0; 86 75 dataChart.Chart.SetJitter(xJitterFactor, yJitterFactor); 87 76 }
Note: See TracChangeset
for help on using the changeset viewer.