Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.CEDMA.Charting/ModelView.cs @ 575

Last change on this file since 575 was 567, checked in by gkronber, 16 years ago

implemented a view that shows the model itself and a line chart and a scatter plot showing original and predicted values. #269 (Functionality to open a chart with predicted vs. original values of any model)

File size: 4.7 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.ComponentModel;
4using System.Drawing;
5using System.Data;
6using System.Linq;
7using System.Text;
8using System.Windows.Forms;
9using HeuristicLab.Core;
10using HeuristicLab.Charting;
11using HeuristicLab.Functions;
12using HeuristicLab.Logging;
13using HeuristicLab.DataAnalysis;
14using HeuristicLab.Charting.Data;
15
16namespace HeuristicLab.CEDMA.Charting {
17  public partial class ModelView : ViewBase {
18    private Dataset dataset;
19    private IFunctionTree model;
20    private int targetVariable;
21
22    public ModelView(Dataset dataset, IFunctionTree model, int targetVariable) {
23      InitializeComponent();
24      this.dataset = dataset;
25      this.model = model;
26      this.targetVariable = targetVariable;
27
28      splitContainer.Panel1.Controls.Add((Control)model.CreateView());
29      splitContainer.Panel1.Controls[0].Dock = DockStyle.Fill;
30      lowerSplitContainer.Panel1.Controls.Add(CreateLineChart());
31      lowerSplitContainer.Panel1.Controls[0].Dock = DockStyle.Fill;
32      lowerSplitContainer.Panel2.Controls.Add(CreateScatterPlot());
33      lowerSplitContainer.Panel2.Controls[0].Dock = DockStyle.Fill;
34    }
35
36    private Control CreateScatterPlot() {
37      double minX = double.PositiveInfinity;
38      double maxX = double.NegativeInfinity;
39
40      Datachart chart = new Datachart(0, 0, 1, 1);
41      chart.Title = "Scatter plot";
42      chart.UpdateEnabled = false;
43      Pen pen = new Pen(Color.FromArgb(80, Color.Blue));
44      chart.AddDataRow(DataRowType.Points, pen, pen.Brush);
45      IEvaluator eval = model.CreateEvaluator();
46      eval.ResetEvaluator(model, dataset);
47      for(int i = 0; i < dataset.Rows; i += 10) {
48        double predicted = eval.Evaluate(i);
49        double original = dataset.GetValue(i, targetVariable);
50        if(double.IsInfinity(predicted) || predicted == double.MaxValue || predicted == double.MinValue) predicted = double.NaN;
51        if(double.IsInfinity(original) || original == double.MaxValue || original == double.MinValue) original = double.NaN;
52        if(!double.IsNaN(predicted) && !double.IsNaN(original)) {
53          chart.AddDataPoint(0, original, predicted);
54          if(original < minX) minX = original;
55          if(original > maxX) maxX = original;
56          if(predicted < minX) minX = predicted;
57          if(predicted > maxX) maxX = predicted;
58        }
59      }
60      chart.UpdateEnabled = true;
61      chart.Group.Add(new Axis(chart, (maxX - minX) / 2 + minX, (maxX - minX) / 2+minX, AxisType.Both));
62      DataChartControl control = new DataChartControl();
63      control.Chart = chart;
64      if(minX < maxX) {
65        double xExcess = (maxX - minX) * 0.1;
66        chart.ZoomIn(minX - xExcess, minX-xExcess, maxX + xExcess, maxX + xExcess);
67        control.ScaleOnResize = false;
68      }
69      return control;
70    }
71
72    private Control CreateLineChart() {
73      double minY = double.PositiveInfinity;
74      double maxY = double.NegativeInfinity;
75
76      Datachart chart = new Datachart(0, 0, 1, 1);
77      chart.Title = "Linechart";
78      chart.UpdateEnabled = false;
79      Pen redPen = new Pen(Color.FromArgb(180, Color.Red));
80      Pen bluePen = new Pen(Color.FromArgb(180, Color.Blue));
81      chart.AddDataRow(DataRowType.Lines, bluePen, bluePen.Brush);
82      chart.AddDataRow(DataRowType.Lines, redPen, redPen.Brush);
83      IEvaluator eval = model.CreateEvaluator();
84      eval.ResetEvaluator(model, dataset);
85      int n = 0;
86      for(int i = 0; i < dataset.Rows; i += 10) {
87        double predicted = eval.Evaluate(i);
88        double original = dataset.GetValue(i, targetVariable);
89        if(double.IsInfinity(predicted) || predicted == double.MaxValue || predicted == double.MinValue) predicted = double.NaN;
90        if(double.IsInfinity(original) || original == double.MaxValue || original == double.MinValue) original = double.NaN;
91        if(!double.IsNaN(predicted) && !double.IsNaN(original)) {
92          chart.AddDataPoint(0, i, predicted);
93          chart.AddDataPoint(1, i, original);
94          n++;
95          if(original < minY) minY = original;
96          if(original > maxY) maxY = original;
97          if(predicted < minY) minY = predicted;
98          if(predicted > maxY) maxY = predicted;
99        }
100      }
101      chart.UpdateEnabled = true;
102      chart.Group.Add(new Axis(chart, 0, minY, AxisType.Both));
103      DataChartControl control = new DataChartControl();
104      control.Chart = chart;
105      if(minY < maxY) {
106        double xExcess = dataset.Rows * 0.05;
107        double yExcess = (maxY - minY) * 0.1;
108        chart.ZoomIn(0.0 - xExcess, minY - yExcess, (double)dataset.Rows + xExcess, maxY + yExcess);
109        control.ScaleOnResize = false;
110      }
111      return control;
112    }
113  }
114}
Note: See TracBrowser for help on using the repository browser.