Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/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
RevLine 
[3408]1#region License Information
2/* HeuristicLab
[5445]3 * Copyright (C) 2002-2011 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[3408]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;
[5829]27using HeuristicLab.Core.Views;
[3408]28using HeuristicLab.MainForm;
[3442]29using HeuristicLab.MainForm.WindowsForms;
[3408]30
[3442]31namespace HeuristicLab.Problems.DataAnalysis.Views {
[5975]32  [View("Scatter Plot")]
[5663]33  [Content(typeof(IRegressionSolution))]
[5829]34  public partial class RegressionSolutionScatterPlotView : ItemView, IRegressionSolutionEvaluationView {
[5663]35    private const string ALL_SERIES = "All samples";
36    private const string TRAINING_SERIES = "Training samples";
37    private const string TEST_SERIES = "Test samples";
[3408]38
[5663]39    public new IRegressionSolution Content {
40      get { return (IRegressionSolution)base.Content; }
[3442]41      set { base.Content = value; }
42    }
43
[5663]44    public RegressionSolutionScatterPlotView()
[3408]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;
[3867]55      this.chart.Series[TRAINING_SERIES].Points.Add(1.0);
[3408]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
[4651]64      //configure axis
65      this.chart.CustomizeAllChartAreas();
[3408]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;
[3707]69      this.chart.ChartAreas[0].CursorX.Interval = 1;
70      this.chart.ChartAreas[0].CursorY.Interval = 1;
[3408]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
[3442]78    protected override void RegisterContentEvents() {
79      base.RegisterContentEvents();
[5663]80      Content.ModelChanged += new EventHandler(Content_ModelChanged);
[3442]81      Content.ProblemDataChanged += new EventHandler(Content_ProblemDataChanged);
82    }
83    protected override void DeregisterContentEvents() {
84      base.DeregisterContentEvents();
[5663]85      Content.ModelChanged -= new EventHandler(Content_ModelChanged);
[3442]86      Content.ProblemDataChanged -= new EventHandler(Content_ProblemDataChanged);
87    }
88
89
[3904]90    private void Content_ProblemDataChanged(object sender, EventArgs e) {
[3462]91      UpdateChart();
[3442]92    }
[5663]93    private void Content_ModelChanged(object sender, EventArgs e) {
[3462]94      UpdateSeries();
[3442]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) {
[3408]106          this.UpdateSeries();
107          if (!this.chart.Series.Any(s => s.Points.Count > 0))
[3764]108            this.ClearChart();
[3408]109        }
110      }
111    }
112
[3707]113    private void UpdateCursorInterval() {
[3710]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);
[3707]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
[3408]126    private void UpdateSeries() {
[3462]127      if (InvokeRequired) Invoke((Action)UpdateSeries);
128      else {
[5663]129        string targetVariableName = Content.ProblemData.TargetVariable;
[3462]130        Dataset dataset = Content.ProblemData.Dataset;
[3933]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), "");
[3462]134        if (this.chart.Series[TRAINING_SERIES].Points.Count > 0)
[3933]135          this.chart.Series[TRAINING_SERIES].Points.DataBindXY(Content.EstimatedTrainingValues.ToArray(), "",
[4468]136            dataset.GetEnumeratedVariableValues(targetVariableName, Content.ProblemData.TrainingIndizes).ToArray(), "");
[3462]137        if (this.chart.Series[TEST_SERIES].Points.Count > 0)
[3933]138          this.chart.Series[TEST_SERIES].Points.DataBindXY(Content.EstimatedTestValues.ToArray(), "",
[4468]139           dataset.GetEnumeratedVariableValues(targetVariableName, Content.ProblemData.TestIndizes).ToArray(), "");
[3408]140
[6238]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();
[3408]143
[4972]144        max = max + 0.2 * Math.Abs(max);
145        min = min - 0.2 * Math.Abs(min);
[3408]146
[4972]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
[3462]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;
[3707]157        UpdateCursorInterval();
[3462]158      }
[3408]159    }
160
161    private void ClearChart() {
162      this.chart.Series[ALL_SERIES].Points.Clear();
163      this.chart.Series[TRAINING_SERIES].Points.Clear();
[3710]164      this.chart.Series[TEST_SERIES].Points.Clear();
[3408]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)) {
[3442]170          series.Points.Clear();
[3408]171        }
[3442]172      } else if (Content != null) {
[5663]173        string targetVariableName = Content.ProblemData.TargetVariable;
[3442]174
[3408]175        IEnumerable<double> predictedValues = null;
176        IEnumerable<double> targetValues = null;
177        switch (series.Name) {
178          case ALL_SERIES:
[4468]179            predictedValues = Content.EstimatedValues.ToArray();
180            targetValues = Content.ProblemData.Dataset.GetVariableValues(targetVariableName);
[3408]181            break;
182          case TRAINING_SERIES:
[4468]183            predictedValues = Content.EstimatedTrainingValues.ToArray();
184            targetValues = Content.ProblemData.Dataset.GetEnumeratedVariableValues(targetVariableName, Content.ProblemData.TrainingIndizes).ToArray();
[3408]185            break;
186          case TEST_SERIES:
[4468]187            predictedValues = Content.EstimatedTestValues.ToArray();
188            targetValues = Content.ProblemData.Dataset.GetEnumeratedVariableValues(targetVariableName, Content.ProblemData.TestIndizes).ToArray();
[3408]189            break;
190        }
[3442]191        series.Points.DataBindXY(predictedValues, "", targetValues, "");
[3408]192        this.chart.Legends[series.Legend].ForeColor = Color.Black;
[3707]193        UpdateCursorInterval();
[3408]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;
[3442]220      e.LegendItems[2].Cells[1].ForeColor = this.chart.Series[TEST_SERIES].Points.Count == 0 ? Color.Gray : Color.Black;
[3408]221    }
222  }
223}
Note: See TracBrowser for help on using the repository browser.