Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.DataAnalysis.Views/3.4/Regression/RegressionEnsembleSolutionScatterPlotView.cs @ 6184

Last change on this file since 6184 was 6184, checked in by gkronber, 13 years ago

#1450: merged r5816 from the branch and implemented first version of ensemble solutions for regression. The ensembles are only produced by cross validation.

File size: 10.4 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(IRegressionEnsembleSolution))]
34  public partial class RegressionEnsembleSolutionScatterPlotView : ItemView, IRegressionEnsembleSolutionEvaluationView {
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 IRegressionEnsembleSolution Content {
40      get { return (IRegressionEnsembleSolution)base.Content; }
41      set { base.Content = value; }
42    }
43
44    public RegressionEnsembleSolutionScatterPlotView()
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        var trainingIndizes = Enumerable.Range(Content.ProblemData.TrainingPartition.Start, Content.ProblemData.TrainingPartition.End - Content.ProblemData.TrainingPartition.Start);
132        var testIndizes = Enumerable.Range(Content.ProblemData.TestPartition.Start, Content.ProblemData.TestPartition.End - Content.ProblemData.TestPartition.Start);
133        if (this.chart.Series[ALL_SERIES].Points.Count > 0)
134          this.chart.Series[ALL_SERIES].Points.DataBindXY(Content.EstimatedValues.ToArray(), "",
135            dataset.GetVariableValues(targetVariableName), "");
136        if (this.chart.Series[TRAINING_SERIES].Points.Count > 0)
137          this.chart.Series[TRAINING_SERIES].Points.DataBindXY(Content.EstimatedTrainingValues.ToArray(), "",
138            dataset.GetEnumeratedVariableValues(targetVariableName, trainingIndizes).ToArray(), "");
139        if (this.chart.Series[TEST_SERIES].Points.Count > 0)
140          this.chart.Series[TEST_SERIES].Points.DataBindXY(Content.EstimatedTestValues.ToArray(), "",
141           dataset.GetEnumeratedVariableValues(targetVariableName, testIndizes).ToArray(), "");
142
143        double max = Math.Max(Content.EstimatedValues.Max(), dataset.GetVariableValues(targetVariableName).Max());
144        double min = Math.Min(Content.EstimatedValues.Min(), dataset.GetVariableValues(targetVariableName).Min());
145
146        max = max + 0.2 * Math.Abs(max);
147        min = min - 0.2 * Math.Abs(min);
148
149        double interestingValuesRange = max - min;
150        int digits = Math.Max(0, 3 - (int)Math.Log10(interestingValuesRange));
151
152        max = Math.Round(max, digits);
153        min = Math.Round(min, digits);
154
155        this.chart.ChartAreas[0].AxisX.Maximum = max;
156        this.chart.ChartAreas[0].AxisX.Minimum = min;
157        this.chart.ChartAreas[0].AxisY.Maximum = max;
158        this.chart.ChartAreas[0].AxisY.Minimum = min;
159        UpdateCursorInterval();
160      }
161    }
162
163    private void ClearChart() {
164      this.chart.Series[ALL_SERIES].Points.Clear();
165      this.chart.Series[TRAINING_SERIES].Points.Clear();
166      this.chart.Series[TEST_SERIES].Points.Clear();
167    }
168
169    private void ToggleSeriesData(Series series) {
170      if (series.Points.Count > 0) {  //checks if series is shown
171        if (this.chart.Series.Any(s => s != series && s.Points.Count > 0)) {
172          series.Points.Clear();
173        }
174      } else if (Content != null) {
175        string targetVariableName = Content.ProblemData.TargetVariable;
176
177        IEnumerable<double> predictedValues = null;
178        IEnumerable<double> targetValues = null;
179        var trainingIndizes = Enumerable.Range(Content.ProblemData.TrainingPartition.Start, Content.ProblemData.TrainingPartition.End - Content.ProblemData.TrainingPartition.Start);
180        var testIndizes = Enumerable.Range(Content.ProblemData.TestPartition.Start, Content.ProblemData.TestPartition.End - Content.ProblemData.TestPartition.Start);
181
182        switch (series.Name) {
183          case ALL_SERIES:
184            predictedValues = Content.EstimatedValues.ToArray();
185            targetValues = Content.ProblemData.Dataset.GetVariableValues(targetVariableName);
186            break;
187          case TRAINING_SERIES:
188            predictedValues = Content.EstimatedTrainingValues.ToArray();
189            targetValues = Content.ProblemData.Dataset.GetEnumeratedVariableValues(targetVariableName, trainingIndizes).ToArray();
190            break;
191          case TEST_SERIES:
192            predictedValues = Content.EstimatedTestValues.ToArray();
193            targetValues = Content.ProblemData.Dataset.GetEnumeratedVariableValues(targetVariableName, testIndizes).ToArray();
194            break;
195        }
196        series.Points.DataBindXY(predictedValues, "", targetValues, "");
197        this.chart.Legends[series.Legend].ForeColor = Color.Black;
198        UpdateCursorInterval();
199      }
200    }
201
202    private void chart_MouseDown(object sender, MouseEventArgs e) {
203      HitTestResult result = chart.HitTest(e.X, e.Y);
204      if (result.ChartElementType == ChartElementType.LegendItem) {
205        this.ToggleSeriesData(result.Series);
206      }
207    }
208
209    private void chart_MouseMove(object sender, MouseEventArgs e) {
210      HitTestResult result = chart.HitTest(e.X, e.Y);
211      if (result.ChartElementType == ChartElementType.LegendItem)
212        this.Cursor = Cursors.Hand;
213      else
214        this.Cursor = Cursors.Default;
215    }
216
217    private void chart_AxisViewChanged(object sender, System.Windows.Forms.DataVisualization.Charting.ViewEventArgs e) {
218      this.chart.ChartAreas[0].AxisX.ScaleView.Size = e.NewSize;
219      this.chart.ChartAreas[0].AxisY.ScaleView.Size = e.NewSize;
220    }
221
222    private void chart_CustomizeLegend(object sender, CustomizeLegendEventArgs e) {
223      e.LegendItems[0].Cells[1].ForeColor = this.chart.Series[ALL_SERIES].Points.Count == 0 ? Color.Gray : Color.Black;
224      e.LegendItems[1].Cells[1].ForeColor = this.chart.Series[TRAINING_SERIES].Points.Count == 0 ? Color.Gray : Color.Black;
225      e.LegendItems[2].Cells[1].ForeColor = this.chart.Series[TEST_SERIES].Points.Count == 0 ? Color.Gray : Color.Black;
226    }
227  }
228}
Note: See TracBrowser for help on using the repository browser.