Free cookie consent management tool by TermsFeed Policy Generator

source: branches/QAPAlgorithms/HeuristicLab.Problems.DataAnalysis.Views/3.4/Regression/RegressionSolutionScatterPlotView.cs @ 6452

Last change on this file since 6452 was 6238, checked in by gkronber, 14 years ago

#1450 adapted views for regression solution to work for ensembles of regression solutions as well.

File size: 9.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2011 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21using System;
22using System.Collections.Generic;
23using System.Drawing;
24using System.Linq;
25using System.Windows.Forms;
26using System.Windows.Forms.DataVisualization.Charting;
27using HeuristicLab.Core.Views;
28using HeuristicLab.MainForm;
29using HeuristicLab.MainForm.WindowsForms;
30
31namespace HeuristicLab.Problems.DataAnalysis.Views {
32  [View("Scatter Plot")]
33  [Content(typeof(IRegressionSolution))]
34  public partial class RegressionSolutionScatterPlotView : ItemView, IRegressionSolutionEvaluationView {
35    private const string ALL_SERIES = "All samples";
36    private const string TRAINING_SERIES = "Training samples";
37    private const string TEST_SERIES = "Test samples";
38
39    public new IRegressionSolution Content {
40      get { return (IRegressionSolution)base.Content; }
41      set { base.Content = value; }
42    }
43
44    public RegressionSolutionScatterPlotView()
45      : base() {
46      InitializeComponent();
47
48      this.chart.Series.Add(ALL_SERIES);
49      this.chart.Series[ALL_SERIES].LegendText = ALL_SERIES;
50      this.chart.Series[ALL_SERIES].ChartType = SeriesChartType.FastPoint;
51
52      this.chart.Series.Add(TRAINING_SERIES);
53      this.chart.Series[TRAINING_SERIES].LegendText = TRAINING_SERIES;
54      this.chart.Series[TRAINING_SERIES].ChartType = SeriesChartType.FastPoint;
55      this.chart.Series[TRAINING_SERIES].Points.Add(1.0);
56
57      this.chart.Series.Add(TEST_SERIES);
58      this.chart.Series[TEST_SERIES].LegendText = TEST_SERIES;
59      this.chart.Series[TEST_SERIES].ChartType = SeriesChartType.FastPoint;
60
61      this.chart.TextAntiAliasingQuality = TextAntiAliasingQuality.High;
62      this.chart.AxisViewChanged += new EventHandler<System.Windows.Forms.DataVisualization.Charting.ViewEventArgs>(chart_AxisViewChanged);
63
64      //configure axis
65      this.chart.CustomizeAllChartAreas();
66      this.chart.ChartAreas[0].AxisX.Title = "Estimated Values";
67      this.chart.ChartAreas[0].CursorX.IsUserSelectionEnabled = true;
68      this.chart.ChartAreas[0].AxisX.ScaleView.Zoomable = true;
69      this.chart.ChartAreas[0].CursorX.Interval = 1;
70      this.chart.ChartAreas[0].CursorY.Interval = 1;
71
72      this.chart.ChartAreas[0].AxisY.Title = "Target Values";
73      this.chart.ChartAreas[0].CursorY.IsUserSelectionEnabled = true;
74      this.chart.ChartAreas[0].AxisY.ScaleView.Zoomable = true;
75      this.chart.ChartAreas[0].AxisY.IsStartedFromZero = true;
76    }
77
78    protected override void RegisterContentEvents() {
79      base.RegisterContentEvents();
80      Content.ModelChanged += new EventHandler(Content_ModelChanged);
81      Content.ProblemDataChanged += new EventHandler(Content_ProblemDataChanged);
82    }
83    protected override void DeregisterContentEvents() {
84      base.DeregisterContentEvents();
85      Content.ModelChanged -= new EventHandler(Content_ModelChanged);
86      Content.ProblemDataChanged -= new EventHandler(Content_ProblemDataChanged);
87    }
88
89
90    private void Content_ProblemDataChanged(object sender, EventArgs e) {
91      UpdateChart();
92    }
93    private void Content_ModelChanged(object sender, EventArgs e) {
94      UpdateSeries();
95    }
96
97    protected override void OnContentChanged() {
98      base.OnContentChanged();
99      UpdateChart();
100    }
101
102    private void UpdateChart() {
103      if (InvokeRequired) Invoke((Action)UpdateChart);
104      else {
105        if (Content != null) {
106          this.UpdateSeries();
107          if (!this.chart.Series.Any(s => s.Points.Count > 0))
108            this.ClearChart();
109        }
110      }
111    }
112
113    private void UpdateCursorInterval() {
114      var estimatedValues = this.chart.Series[ALL_SERIES].Points.Select(x => x.XValue).DefaultIfEmpty(1.0);
115      var targetValues = this.chart.Series[ALL_SERIES].Points.Select(x => x.YValues[0]).DefaultIfEmpty(1.0);
116      double estimatedValuesRange = estimatedValues.Max() - estimatedValues.Min();
117      double targetValuesRange = targetValues.Max() - targetValues.Min();
118      double interestingValuesRange = Math.Min(Math.Max(targetValuesRange, 1.0), Math.Max(estimatedValuesRange, 1.0));
119      double digits = (int)Math.Log10(interestingValuesRange) - 3;
120      double zoomInterval = Math.Max(Math.Pow(10, digits), 10E-5);
121      this.chart.ChartAreas[0].CursorX.Interval = zoomInterval;
122      this.chart.ChartAreas[0].CursorY.Interval = zoomInterval;
123    }
124
125
126    private void UpdateSeries() {
127      if (InvokeRequired) Invoke((Action)UpdateSeries);
128      else {
129        string targetVariableName = Content.ProblemData.TargetVariable;
130        Dataset dataset = Content.ProblemData.Dataset;
131        if (this.chart.Series[ALL_SERIES].Points.Count > 0)
132          this.chart.Series[ALL_SERIES].Points.DataBindXY(Content.EstimatedValues.ToArray(), "",
133            dataset.GetVariableValues(targetVariableName), "");
134        if (this.chart.Series[TRAINING_SERIES].Points.Count > 0)
135          this.chart.Series[TRAINING_SERIES].Points.DataBindXY(Content.EstimatedTrainingValues.ToArray(), "",
136            dataset.GetEnumeratedVariableValues(targetVariableName, Content.ProblemData.TrainingIndizes).ToArray(), "");
137        if (this.chart.Series[TEST_SERIES].Points.Count > 0)
138          this.chart.Series[TEST_SERIES].Points.DataBindXY(Content.EstimatedTestValues.ToArray(), "",
139           dataset.GetEnumeratedVariableValues(targetVariableName, Content.ProblemData.TestIndizes).ToArray(), "");
140
141        double max = Content.EstimatedTrainingValues.Concat(Content.EstimatedTestValues.Concat(Content.EstimatedValues.Concat(dataset.GetVariableValues(targetVariableName)))).Max();
142        double min = Content.EstimatedTrainingValues.Concat(Content.EstimatedTestValues.Concat(Content.EstimatedValues.Concat(dataset.GetVariableValues(targetVariableName)))).Min();
143
144        max = max + 0.2 * Math.Abs(max);
145        min = min - 0.2 * Math.Abs(min);
146
147        double interestingValuesRange = max - min;
148        int digits = Math.Max(0, 3 - (int)Math.Log10(interestingValuesRange));
149
150        max = Math.Round(max, digits);
151        min = Math.Round(min, digits);
152
153        this.chart.ChartAreas[0].AxisX.Maximum = max;
154        this.chart.ChartAreas[0].AxisX.Minimum = min;
155        this.chart.ChartAreas[0].AxisY.Maximum = max;
156        this.chart.ChartAreas[0].AxisY.Minimum = min;
157        UpdateCursorInterval();
158      }
159    }
160
161    private void ClearChart() {
162      this.chart.Series[ALL_SERIES].Points.Clear();
163      this.chart.Series[TRAINING_SERIES].Points.Clear();
164      this.chart.Series[TEST_SERIES].Points.Clear();
165    }
166
167    private void ToggleSeriesData(Series series) {
168      if (series.Points.Count > 0) {  //checks if series is shown
169        if (this.chart.Series.Any(s => s != series && s.Points.Count > 0)) {
170          series.Points.Clear();
171        }
172      } else if (Content != null) {
173        string targetVariableName = Content.ProblemData.TargetVariable;
174
175        IEnumerable<double> predictedValues = null;
176        IEnumerable<double> targetValues = null;
177        switch (series.Name) {
178          case ALL_SERIES:
179            predictedValues = Content.EstimatedValues.ToArray();
180            targetValues = Content.ProblemData.Dataset.GetVariableValues(targetVariableName);
181            break;
182          case TRAINING_SERIES:
183            predictedValues = Content.EstimatedTrainingValues.ToArray();
184            targetValues = Content.ProblemData.Dataset.GetEnumeratedVariableValues(targetVariableName, Content.ProblemData.TrainingIndizes).ToArray();
185            break;
186          case TEST_SERIES:
187            predictedValues = Content.EstimatedTestValues.ToArray();
188            targetValues = Content.ProblemData.Dataset.GetEnumeratedVariableValues(targetVariableName, Content.ProblemData.TestIndizes).ToArray();
189            break;
190        }
191        series.Points.DataBindXY(predictedValues, "", targetValues, "");
192        this.chart.Legends[series.Legend].ForeColor = Color.Black;
193        UpdateCursorInterval();
194      }
195    }
196
197    private void chart_MouseDown(object sender, MouseEventArgs e) {
198      HitTestResult result = chart.HitTest(e.X, e.Y);
199      if (result.ChartElementType == ChartElementType.LegendItem) {
200        this.ToggleSeriesData(result.Series);
201      }
202    }
203
204    private void chart_MouseMove(object sender, MouseEventArgs e) {
205      HitTestResult result = chart.HitTest(e.X, e.Y);
206      if (result.ChartElementType == ChartElementType.LegendItem)
207        this.Cursor = Cursors.Hand;
208      else
209        this.Cursor = Cursors.Default;
210    }
211
212    private void chart_AxisViewChanged(object sender, System.Windows.Forms.DataVisualization.Charting.ViewEventArgs e) {
213      this.chart.ChartAreas[0].AxisX.ScaleView.Size = e.NewSize;
214      this.chart.ChartAreas[0].AxisY.ScaleView.Size = e.NewSize;
215    }
216
217    private void chart_CustomizeLegend(object sender, CustomizeLegendEventArgs e) {
218      e.LegendItems[0].Cells[1].ForeColor = this.chart.Series[ALL_SERIES].Points.Count == 0 ? Color.Gray : Color.Black;
219      e.LegendItems[1].Cells[1].ForeColor = this.chart.Series[TRAINING_SERIES].Points.Count == 0 ? Color.Gray : Color.Black;
220      e.LegendItems[2].Cells[1].ForeColor = this.chart.Series[TEST_SERIES].Points.Count == 0 ? Color.Gray : Color.Black;
221    }
222  }
223}
Note: See TracBrowser for help on using the repository browser.