Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.DataAnalysis.Views/3.3/ScatterPlotView.cs @ 4468

Last change on this file since 4468 was 4468, checked in by mkommend, 14 years ago

Preparation for cross validation - removed the test samples from the trainining samples and added ValidationPercentage parameter (ticket #1199).

File size: 9.5 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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.MainForm;
28using HeuristicLab.MainForm.WindowsForms;
29
30namespace HeuristicLab.Problems.DataAnalysis.Views {
31  [View("Scatter Plot View")]
32  [Content(typeof(DataAnalysisSolution), true)]
33  public partial class ScatterPlotView : AsynchronousContentView {
34    private const string ALL_SERIES = "All Samples";
35    private const string TRAINING_SERIES = "Training Samples";
36    private const string TEST_SERIES = "Test Samples";
37
38    public new DataAnalysisSolution Content {
39      get { return (DataAnalysisSolution)base.Content; }
40      set { base.Content = value; }
41    }
42
43    public ScatterPlotView()
44      : base() {
45      InitializeComponent();
46
47      this.chart.Series.Add(ALL_SERIES);
48      this.chart.Series[ALL_SERIES].LegendText = ALL_SERIES;
49      this.chart.Series[ALL_SERIES].ChartType = SeriesChartType.FastPoint;
50
51      this.chart.Series.Add(TRAINING_SERIES);
52      this.chart.Series[TRAINING_SERIES].LegendText = TRAINING_SERIES;
53      this.chart.Series[TRAINING_SERIES].ChartType = SeriesChartType.FastPoint;
54      this.chart.Series[TRAINING_SERIES].Points.Add(1.0);
55
56      this.chart.Series.Add(TEST_SERIES);
57      this.chart.Series[TEST_SERIES].LegendText = TEST_SERIES;
58      this.chart.Series[TEST_SERIES].ChartType = SeriesChartType.FastPoint;
59
60      this.chart.TextAntiAliasingQuality = TextAntiAliasingQuality.High;
61      this.chart.AxisViewChanged += new EventHandler<System.Windows.Forms.DataVisualization.Charting.ViewEventArgs>(chart_AxisViewChanged);
62
63      //configure axis                 
64      this.chart.ChartAreas[0].AxisX.Title = "Estimated Values";
65      this.chart.ChartAreas[0].CursorX.IsUserSelectionEnabled = true;
66      this.chart.ChartAreas[0].AxisX.ScaleView.Zoomable = true;
67      this.chart.ChartAreas[0].CursorX.Interval = 1;
68      this.chart.ChartAreas[0].CursorY.Interval = 1;
69
70      this.chart.ChartAreas[0].AxisY.Title = "Target Values";
71      this.chart.ChartAreas[0].CursorY.IsUserSelectionEnabled = true;
72      this.chart.ChartAreas[0].AxisY.ScaleView.Zoomable = true;
73      this.chart.ChartAreas[0].AxisY.IsStartedFromZero = true;
74    }
75
76    protected override void RegisterContentEvents() {
77      base.RegisterContentEvents();
78      Content.EstimatedValuesChanged += new EventHandler(Content_EstimatedValuesChanged);
79      Content.ProblemDataChanged += new EventHandler(Content_ProblemDataChanged);
80    }
81    protected override void DeregisterContentEvents() {
82      base.DeregisterContentEvents();
83      Content.EstimatedValuesChanged -= new EventHandler(Content_EstimatedValuesChanged);
84      Content.ProblemDataChanged -= new EventHandler(Content_ProblemDataChanged);
85    }
86
87
88    private void Content_ProblemDataChanged(object sender, EventArgs e) {
89      UpdateChart();
90    }
91    private void Content_EstimatedValuesChanged(object sender, EventArgs e) {
92      UpdateSeries();
93    }
94
95    protected override void OnContentChanged() {
96      base.OnContentChanged();
97      UpdateChart();
98    }
99
100    private void UpdateChart() {
101      if (InvokeRequired) Invoke((Action)UpdateChart);
102      else {
103        if (Content != null) {
104          this.UpdateSeries();
105          if (!this.chart.Series.Any(s => s.Points.Count > 0))
106            this.ClearChart();
107        }
108      }
109    }
110
111    private void UpdateCursorInterval() {
112      var estimatedValues = this.chart.Series[ALL_SERIES].Points.Select(x => x.XValue).DefaultIfEmpty(1.0);
113      var targetValues = this.chart.Series[ALL_SERIES].Points.Select(x => x.YValues[0]).DefaultIfEmpty(1.0);
114      double estimatedValuesRange = estimatedValues.Max() - estimatedValues.Min();
115      double targetValuesRange = targetValues.Max() - targetValues.Min();
116      double interestingValuesRange = Math.Min(Math.Max(targetValuesRange, 1.0), Math.Max(estimatedValuesRange, 1.0));
117      double digits = (int)Math.Log10(interestingValuesRange) - 3;
118      double zoomInterval = Math.Max(Math.Pow(10, digits), 10E-5);
119      this.chart.ChartAreas[0].CursorX.Interval = zoomInterval;
120      this.chart.ChartAreas[0].CursorY.Interval = zoomInterval;
121    }
122
123
124    private void UpdateSeries() {
125      if (InvokeRequired) Invoke((Action)UpdateSeries);
126      else {
127        string targetVariableName = Content.ProblemData.TargetVariable.Value;
128        Dataset dataset = Content.ProblemData.Dataset;
129        if (this.chart.Series[ALL_SERIES].Points.Count > 0)
130          this.chart.Series[ALL_SERIES].Points.DataBindXY(Content.EstimatedValues.ToArray(), "",
131            dataset.GetVariableValues(targetVariableName), "");
132        if (this.chart.Series[TRAINING_SERIES].Points.Count > 0)
133          this.chart.Series[TRAINING_SERIES].Points.DataBindXY(Content.EstimatedTrainingValues.ToArray(), "",
134            dataset.GetEnumeratedVariableValues(targetVariableName, Content.ProblemData.TrainingIndizes).ToArray(), "");
135        if (this.chart.Series[TEST_SERIES].Points.Count > 0)
136          this.chart.Series[TEST_SERIES].Points.DataBindXY(Content.EstimatedTestValues.ToArray(), "",
137           dataset.GetEnumeratedVariableValues(targetVariableName, Content.ProblemData.TestIndizes).ToArray(), "");
138
139        double max = Math.Max(Content.EstimatedValues.Max(), dataset.GetVariableValues(targetVariableName).Max());
140        double min = Math.Min(Content.EstimatedValues.Min(), dataset.GetVariableValues(targetVariableName).Min());
141
142        max = Math.Ceiling(max) * 1.2;
143        min = Math.Floor(min) * 0.8;
144
145        this.chart.ChartAreas[0].AxisX.Maximum = max;
146        this.chart.ChartAreas[0].AxisX.Minimum = min;
147        this.chart.ChartAreas[0].AxisY.Maximum = max;
148        this.chart.ChartAreas[0].AxisY.Minimum = min;
149        UpdateCursorInterval();
150      }
151    }
152
153    private void ClearChart() {
154      this.chart.Series[ALL_SERIES].Points.Clear();
155      this.chart.Series[TRAINING_SERIES].Points.Clear();
156      this.chart.Series[TEST_SERIES].Points.Clear();
157    }
158
159    private void ToggleSeriesData(Series series) {
160      if (series.Points.Count > 0) {  //checks if series is shown
161        if (this.chart.Series.Any(s => s != series && s.Points.Count > 0)) {
162          series.Points.Clear();
163        }
164      } else if (Content != null) {
165        string targetVariableName = Content.ProblemData.TargetVariable.Value;
166
167        IEnumerable<double> predictedValues = null;
168        IEnumerable<double> targetValues = null;
169        switch (series.Name) {
170          case ALL_SERIES:
171            predictedValues = Content.EstimatedValues.ToArray();
172            targetValues = Content.ProblemData.Dataset.GetVariableValues(targetVariableName);
173            break;
174          case TRAINING_SERIES:
175            predictedValues = Content.EstimatedTrainingValues.ToArray();
176            targetValues = Content.ProblemData.Dataset.GetEnumeratedVariableValues(targetVariableName, Content.ProblemData.TrainingIndizes).ToArray();
177            break;
178          case TEST_SERIES:
179            predictedValues = Content.EstimatedTestValues.ToArray();
180            targetValues = Content.ProblemData.Dataset.GetEnumeratedVariableValues(targetVariableName, Content.ProblemData.TestIndizes).ToArray();
181            break;
182        }
183        series.Points.DataBindXY(predictedValues, "", targetValues, "");
184        this.chart.Legends[series.Legend].ForeColor = Color.Black;
185        UpdateCursorInterval();
186      }
187    }
188
189    private void chart_MouseDown(object sender, MouseEventArgs e) {
190      HitTestResult result = chart.HitTest(e.X, e.Y);
191      if (result.ChartElementType == ChartElementType.LegendItem) {
192        this.ToggleSeriesData(result.Series);
193      }
194    }
195
196    private void chart_MouseMove(object sender, MouseEventArgs e) {
197      HitTestResult result = chart.HitTest(e.X, e.Y);
198      if (result.ChartElementType == ChartElementType.LegendItem)
199        this.Cursor = Cursors.Hand;
200      else
201        this.Cursor = Cursors.Default;
202    }
203
204    private void chart_AxisViewChanged(object sender, System.Windows.Forms.DataVisualization.Charting.ViewEventArgs e) {
205      this.chart.ChartAreas[0].AxisX.ScaleView.Size = e.NewSize;
206      this.chart.ChartAreas[0].AxisY.ScaleView.Size = e.NewSize;
207    }
208
209    private void chart_CustomizeLegend(object sender, CustomizeLegendEventArgs e) {
210      e.LegendItems[0].Cells[1].ForeColor = this.chart.Series[ALL_SERIES].Points.Count == 0 ? Color.Gray : Color.Black;
211      e.LegendItems[1].Cells[1].ForeColor = this.chart.Series[TRAINING_SERIES].Points.Count == 0 ? Color.Gray : Color.Black;
212      e.LegendItems[2].Cells[1].ForeColor = this.chart.Series[TEST_SERIES].Points.Count == 0 ? Color.Gray : Color.Black;
213    }
214  }
215}
Note: See TracBrowser for help on using the repository browser.