Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/HeuristicLab.Problems.DataAnalysis.Views/3.4/Regression/RegressionSolutionScatterPlotView.cs @ 17712

Last change on this file since 17712 was 17180, checked in by swagner, 5 years ago

#2875: Removed years in copyrights

File size: 13.1 KB
RevLine 
[3408]1#region License Information
2/* HeuristicLab
[17180]3 * Copyright (C) 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.Drawing;
23using System.Linq;
24using System.Windows.Forms;
25using System.Windows.Forms.DataVisualization.Charting;
26using HeuristicLab.MainForm;
[3442]27using HeuristicLab.MainForm.WindowsForms;
[13764]28using HeuristicLab.Visualization.ChartControlsExtensions;
[3408]29
[3442]30namespace HeuristicLab.Problems.DataAnalysis.Views {
[5975]31  [View("Scatter Plot")]
[5663]32  [Content(typeof(IRegressionSolution))]
[6642]33  public partial class RegressionSolutionScatterPlotView : DataAnalysisSolutionEvaluationView {
[5663]34    private const string ALL_SERIES = "All samples";
35    private const string TRAINING_SERIES = "Training samples";
36    private const string TEST_SERIES = "Test samples";
[3408]37
[13958]38    private const int OPACITY_LEVEL = 150;
39
[5663]40    public new IRegressionSolution Content {
41      get { return (IRegressionSolution)base.Content; }
[3442]42      set { base.Content = value; }
43    }
44
[5663]45    public RegressionSolutionScatterPlotView()
[3408]46      : base() {
47      InitializeComponent();
48
49      this.chart.Series.Add(ALL_SERIES);
50      this.chart.Series[ALL_SERIES].LegendText = ALL_SERIES;
51      this.chart.Series[ALL_SERIES].ChartType = SeriesChartType.FastPoint;
52
53      this.chart.Series.Add(TRAINING_SERIES);
54      this.chart.Series[TRAINING_SERIES].LegendText = TRAINING_SERIES;
55      this.chart.Series[TRAINING_SERIES].ChartType = SeriesChartType.FastPoint;
[3867]56      this.chart.Series[TRAINING_SERIES].Points.Add(1.0);
[3408]57
58      this.chart.Series.Add(TEST_SERIES);
59      this.chart.Series[TEST_SERIES].LegendText = TEST_SERIES;
60      this.chart.Series[TEST_SERIES].ChartType = SeriesChartType.FastPoint;
61
62      this.chart.TextAntiAliasingQuality = TextAntiAliasingQuality.High;
63      this.chart.AxisViewChanged += new EventHandler<System.Windows.Forms.DataVisualization.Charting.ViewEventArgs>(chart_AxisViewChanged);
64
[13958]65      //make series colors semi transparent
66      this.chart.ApplyPaletteColors();
67      this.chart.Series[ALL_SERIES].Color = Color.FromArgb(OPACITY_LEVEL, this.chart.Series[ALL_SERIES].Color);
68      this.chart.Series[TRAINING_SERIES].Color = Color.FromArgb(OPACITY_LEVEL, this.chart.Series[TRAINING_SERIES].Color);
69      this.chart.Series[TEST_SERIES].Color = Color.FromArgb(OPACITY_LEVEL, this.chart.Series[TEST_SERIES].Color);
70
71      //change all markers to circles
72      this.chart.Series[ALL_SERIES].MarkerStyle = MarkerStyle.Circle;
73      this.chart.Series[TRAINING_SERIES].MarkerStyle = MarkerStyle.Circle;
74      this.chart.Series[TEST_SERIES].MarkerStyle = MarkerStyle.Circle;
75
[4651]76      //configure axis
77      this.chart.CustomizeAllChartAreas();
[14405]78      this.chart.ChartAreas[0].AxisY.Title = "Estimated Values";
79      this.chart.ChartAreas[0].CursorY.IsUserSelectionEnabled = true;
80      this.chart.ChartAreas[0].AxisY.ScaleView.Zoomable = true;
[3707]81      this.chart.ChartAreas[0].CursorX.Interval = 1;
82      this.chart.ChartAreas[0].CursorY.Interval = 1;
[3408]83
[14405]84      this.chart.ChartAreas[0].AxisX.Title = "Target Values";
85      this.chart.ChartAreas[0].CursorX.IsUserSelectionEnabled = true;
86      this.chart.ChartAreas[0].AxisX.ScaleView.Zoomable = true;
87      this.chart.ChartAreas[0].AxisX.IsStartedFromZero = true;
[3408]88    }
89
[3442]90    protected override void RegisterContentEvents() {
91      base.RegisterContentEvents();
[5663]92      Content.ModelChanged += new EventHandler(Content_ModelChanged);
[3442]93      Content.ProblemDataChanged += new EventHandler(Content_ProblemDataChanged);
94    }
95    protected override void DeregisterContentEvents() {
96      base.DeregisterContentEvents();
[5663]97      Content.ModelChanged -= new EventHandler(Content_ModelChanged);
[3442]98      Content.ProblemDataChanged -= new EventHandler(Content_ProblemDataChanged);
99    }
100
101
[3904]102    private void Content_ProblemDataChanged(object sender, EventArgs e) {
[3462]103      UpdateChart();
[3442]104    }
[5663]105    private void Content_ModelChanged(object sender, EventArgs e) {
[3462]106      UpdateSeries();
[3442]107    }
108
109    protected override void OnContentChanged() {
110      base.OnContentChanged();
111      UpdateChart();
112    }
113
114    private void UpdateChart() {
115      if (InvokeRequired) Invoke((Action)UpdateChart);
116      else {
117        if (Content != null) {
[3408]118          this.UpdateSeries();
119          if (!this.chart.Series.Any(s => s.Points.Count > 0))
[3764]120            this.ClearChart();
[3408]121        }
122      }
123    }
124
[3707]125    private void UpdateCursorInterval() {
[3710]126      var estimatedValues = this.chart.Series[ALL_SERIES].Points.Select(x => x.XValue).DefaultIfEmpty(1.0);
127      var targetValues = this.chart.Series[ALL_SERIES].Points.Select(x => x.YValues[0]).DefaultIfEmpty(1.0);
[3707]128      double estimatedValuesRange = estimatedValues.Max() - estimatedValues.Min();
129      double targetValuesRange = targetValues.Max() - targetValues.Min();
130      double interestingValuesRange = Math.Min(Math.Max(targetValuesRange, 1.0), Math.Max(estimatedValuesRange, 1.0));
131      double digits = (int)Math.Log10(interestingValuesRange) - 3;
132      double zoomInterval = Math.Max(Math.Pow(10, digits), 10E-5);
133      this.chart.ChartAreas[0].CursorX.Interval = zoomInterval;
134      this.chart.ChartAreas[0].CursorY.Interval = zoomInterval;
[7990]135
136      this.chart.ChartAreas[0].AxisX.ScaleView.SmallScrollSize = zoomInterval;
137      this.chart.ChartAreas[0].AxisY.ScaleView.SmallScrollSize = zoomInterval;
138
139      this.chart.ChartAreas[0].AxisX.ScaleView.SmallScrollMinSizeType = DateTimeIntervalType.Number;
140      this.chart.ChartAreas[0].AxisX.ScaleView.SmallScrollMinSize = zoomInterval;
141      this.chart.ChartAreas[0].AxisY.ScaleView.SmallScrollMinSizeType = DateTimeIntervalType.Number;
142      this.chart.ChartAreas[0].AxisY.ScaleView.SmallScrollMinSize = zoomInterval;
143
144      if (digits < 0) {
145        this.chart.ChartAreas[0].AxisX.LabelStyle.Format = "F" + (int)Math.Abs(digits);
146        this.chart.ChartAreas[0].AxisY.LabelStyle.Format = "F" + (int)Math.Abs(digits);
147      } else {
148        this.chart.ChartAreas[0].AxisX.LabelStyle.Format = "F0";
149        this.chart.ChartAreas[0].AxisY.LabelStyle.Format = "F0";
150      }
[3707]151    }
152
153
[3408]154    private void UpdateSeries() {
[3462]155      if (InvokeRequired) Invoke((Action)UpdateSeries);
156      else {
[5663]157        string targetVariableName = Content.ProblemData.TargetVariable;
[12509]158        var dataset = Content.ProblemData.Dataset;
[3933]159        if (this.chart.Series[ALL_SERIES].Points.Count > 0)
[14405]160          this.chart.Series[ALL_SERIES].Points.DataBindXY(dataset.GetDoubleValues(targetVariableName).ToArray(), "",
161            Content.EstimatedValues.ToArray(), "");
[3462]162        if (this.chart.Series[TRAINING_SERIES].Points.Count > 0)
[14405]163          this.chart.Series[TRAINING_SERIES].Points.DataBindXY(dataset.GetDoubleValues(targetVariableName, Content.ProblemData.TrainingIndices).ToArray(), "",
164            Content.EstimatedTrainingValues.ToArray(), "");
[3462]165        if (this.chart.Series[TEST_SERIES].Points.Count > 0)
[14405]166          this.chart.Series[TEST_SERIES].Points.DataBindXY(dataset.GetDoubleValues(targetVariableName, Content.ProblemData.TestIndices).ToArray(), "",
167            Content.EstimatedTestValues.ToArray(), "");
[15810]168        double max = Content.EstimatedTrainingValues
169          .Concat(Content.EstimatedTestValues
170          .Concat(Content.EstimatedValues
171          .Concat(dataset.GetDoubleValues(targetVariableName))))
172          .Where(v => !double.IsNaN(v) && !double.IsInfinity(v)).Max();
173        double min = Content.EstimatedTrainingValues
174          .Concat(Content.EstimatedTestValues
175          .Concat(Content.EstimatedValues
176          .Concat(dataset.GetDoubleValues(targetVariableName))))
177          .Where(v => !double.IsNaN(v) && !double.IsInfinity(v)).Min();
[3408]178
[13764]179        double axisMin, axisMax, axisInterval;
[14008]180        ChartUtil.CalculateOptimalAxisInterval(min, max, out axisMin, out axisMax, out axisInterval);
[14405]181        this.chart.ChartAreas[0].AxisY.Title = "Estimated " + targetVariableName;
182        this.chart.ChartAreas[0].AxisY.Maximum = axisMax;
183        this.chart.ChartAreas[0].AxisY.Minimum = axisMin;
184        this.chart.ChartAreas[0].AxisY.Interval = axisInterval;
185        this.chart.ChartAreas[0].AxisX.Title = targetVariableName;
[13764]186        this.chart.ChartAreas[0].AxisX.Maximum = axisMax;
187        this.chart.ChartAreas[0].AxisX.Minimum = axisMin;
188        this.chart.ChartAreas[0].AxisX.Interval = axisInterval;
[3408]189
[3707]190        UpdateCursorInterval();
[3462]191      }
[3408]192    }
193
194    private void ClearChart() {
195      this.chart.Series[ALL_SERIES].Points.Clear();
196      this.chart.Series[TRAINING_SERIES].Points.Clear();
[3710]197      this.chart.Series[TEST_SERIES].Points.Clear();
[3408]198    }
199
200    private void ToggleSeriesData(Series series) {
201      if (series.Points.Count > 0) {  //checks if series is shown
202        if (this.chart.Series.Any(s => s != series && s.Points.Count > 0)) {
[3442]203          series.Points.Clear();
[3408]204        }
[3442]205      } else if (Content != null) {
[5663]206        string targetVariableName = Content.ProblemData.TargetVariable;
[3442]207
[6982]208        double[] predictedValues = null;
209        double[] targetValues = null;
[3408]210        switch (series.Name) {
211          case ALL_SERIES:
[4468]212            predictedValues = Content.EstimatedValues.ToArray();
[6740]213            targetValues = Content.ProblemData.Dataset.GetDoubleValues(targetVariableName).ToArray();
[3408]214            break;
215          case TRAINING_SERIES:
[4468]216            predictedValues = Content.EstimatedTrainingValues.ToArray();
[8139]217            targetValues = Content.ProblemData.Dataset.GetDoubleValues(targetVariableName, Content.ProblemData.TrainingIndices).ToArray();
[3408]218            break;
219          case TEST_SERIES:
[4468]220            predictedValues = Content.EstimatedTestValues.ToArray();
[8139]221            targetValues = Content.ProblemData.Dataset.GetDoubleValues(targetVariableName, Content.ProblemData.TestIndices).ToArray();
[3408]222            break;
223        }
[6982]224        if (predictedValues.Length == targetValues.Length)
[14405]225          series.Points.DataBindXY(targetValues, "", predictedValues, "");
[3408]226        this.chart.Legends[series.Legend].ForeColor = Color.Black;
[3707]227        UpdateCursorInterval();
[3408]228      }
229    }
230
231    private void chart_MouseDown(object sender, MouseEventArgs e) {
232      HitTestResult result = chart.HitTest(e.X, e.Y);
233      if (result.ChartElementType == ChartElementType.LegendItem) {
234        this.ToggleSeriesData(result.Series);
235      }
236    }
237
238    private void chart_MouseMove(object sender, MouseEventArgs e) {
239      HitTestResult result = chart.HitTest(e.X, e.Y);
240      if (result.ChartElementType == ChartElementType.LegendItem)
241        this.Cursor = Cursors.Hand;
242      else
243        this.Cursor = Cursors.Default;
244    }
245
246    private void chart_AxisViewChanged(object sender, System.Windows.Forms.DataVisualization.Charting.ViewEventArgs e) {
247      this.chart.ChartAreas[0].AxisX.ScaleView.Size = e.NewSize;
248      this.chart.ChartAreas[0].AxisY.ScaleView.Size = e.NewSize;
249    }
250
251    private void chart_CustomizeLegend(object sender, CustomizeLegendEventArgs e) {
252      e.LegendItems[0].Cells[1].ForeColor = this.chart.Series[ALL_SERIES].Points.Count == 0 ? Color.Gray : Color.Black;
253      e.LegendItems[1].Cells[1].ForeColor = this.chart.Series[TRAINING_SERIES].Points.Count == 0 ? Color.Gray : Color.Black;
[3442]254      e.LegendItems[2].Cells[1].ForeColor = this.chart.Series[TEST_SERIES].Points.Count == 0 ? Color.Gray : Color.Black;
[3408]255    }
[6679]256
257    private void chart_PostPaint(object sender, ChartPaintEventArgs e) {
258      var chartArea = e.ChartElement as ChartArea;
259      if (chartArea != null) {
260        ChartGraphics chartGraphics = e.ChartGraphics;
261        using (Pen p = new Pen(Color.DarkGray)) {
262          double xmin = chartArea.AxisX.ScaleView.ViewMinimum;
263          double xmax = chartArea.AxisX.ScaleView.ViewMaximum;
264          double ymin = chartArea.AxisY.ScaleView.ViewMinimum;
265          double ymax = chartArea.AxisY.ScaleView.ViewMaximum;
266
267          if (xmin > ymax || ymin > xmax) return;
268
269          PointF start = PointF.Empty;
270          start.X = (float)chartGraphics.GetPositionFromAxis(chartArea.Name, chartArea.AxisX.AxisName, Math.Max(xmin, ymin));
271          start.Y = (float)chartGraphics.GetPositionFromAxis(chartArea.Name, chartArea.AxisY.AxisName, Math.Max(xmin, ymin));
272          PointF end = PointF.Empty;
273          end.X = (float)chartGraphics.GetPositionFromAxis(chartArea.Name, chartArea.AxisX.AxisName, Math.Min(xmax, ymax));
274          end.Y = (float)chartGraphics.GetPositionFromAxis(chartArea.Name, chartArea.AxisY.AxisName, Math.Min(xmax, ymax));
275
276          chartGraphics.Graphics.DrawLine(p, chartGraphics.GetAbsolutePoint(start), chartGraphics.GetAbsolutePoint(end));
277        }
278      }
279    }
[3408]280  }
281}
Note: See TracBrowser for help on using the repository browser.