[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
|
---|
| 21 | using System;
|
---|
| 22 | using System.Collections.Generic;
|
---|
| 23 | using System.Drawing;
|
---|
| 24 | using System.Linq;
|
---|
| 25 | using System.Windows.Forms;
|
---|
| 26 | using System.Windows.Forms.DataVisualization.Charting;
|
---|
| 27 | using HeuristicLab.MainForm;
|
---|
[3442] | 28 | using HeuristicLab.MainForm.WindowsForms;
|
---|
[3408] | 29 |
|
---|
[3442] | 30 | namespace HeuristicLab.Problems.DataAnalysis.Views {
|
---|
[5975] | 31 | [View("Scatter Plot")]
|
---|
[5663] | 32 | [Content(typeof(IRegressionSolution))]
|
---|
[6760] | 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 |
|
---|
[5663] | 38 | public new IRegressionSolution Content {
|
---|
| 39 | get { return (IRegressionSolution)base.Content; }
|
---|
[3442] | 40 | set { base.Content = value; }
|
---|
| 41 | }
|
---|
| 42 |
|
---|
[5663] | 43 | public RegressionSolutionScatterPlotView()
|
---|
[3408] | 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;
|
---|
[3867] | 54 | this.chart.Series[TRAINING_SERIES].Points.Add(1.0);
|
---|
[3408] | 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 |
|
---|
[4651] | 63 | //configure axis
|
---|
| 64 | this.chart.CustomizeAllChartAreas();
|
---|
[3408] | 65 | this.chart.ChartAreas[0].AxisX.Title = "Estimated Values";
|
---|
| 66 | this.chart.ChartAreas[0].CursorX.IsUserSelectionEnabled = true;
|
---|
| 67 | this.chart.ChartAreas[0].AxisX.ScaleView.Zoomable = true;
|
---|
[3707] | 68 | this.chart.ChartAreas[0].CursorX.Interval = 1;
|
---|
| 69 | this.chart.ChartAreas[0].CursorY.Interval = 1;
|
---|
[3408] | 70 |
|
---|
| 71 | this.chart.ChartAreas[0].AxisY.Title = "Target Values";
|
---|
| 72 | this.chart.ChartAreas[0].CursorY.IsUserSelectionEnabled = true;
|
---|
| 73 | this.chart.ChartAreas[0].AxisY.ScaleView.Zoomable = true;
|
---|
| 74 | this.chart.ChartAreas[0].AxisY.IsStartedFromZero = true;
|
---|
| 75 | }
|
---|
| 76 |
|
---|
[3442] | 77 | protected override void RegisterContentEvents() {
|
---|
| 78 | base.RegisterContentEvents();
|
---|
[5663] | 79 | Content.ModelChanged += new EventHandler(Content_ModelChanged);
|
---|
[3442] | 80 | Content.ProblemDataChanged += new EventHandler(Content_ProblemDataChanged);
|
---|
| 81 | }
|
---|
| 82 | protected override void DeregisterContentEvents() {
|
---|
| 83 | base.DeregisterContentEvents();
|
---|
[5663] | 84 | Content.ModelChanged -= new EventHandler(Content_ModelChanged);
|
---|
[3442] | 85 | Content.ProblemDataChanged -= new EventHandler(Content_ProblemDataChanged);
|
---|
| 86 | }
|
---|
| 87 |
|
---|
| 88 |
|
---|
[3904] | 89 | private void Content_ProblemDataChanged(object sender, EventArgs e) {
|
---|
[3462] | 90 | UpdateChart();
|
---|
[3442] | 91 | }
|
---|
[5663] | 92 | private void Content_ModelChanged(object sender, EventArgs e) {
|
---|
[3462] | 93 | UpdateSeries();
|
---|
[3442] | 94 | }
|
---|
| 95 |
|
---|
| 96 | protected override void OnContentChanged() {
|
---|
| 97 | base.OnContentChanged();
|
---|
| 98 | UpdateChart();
|
---|
| 99 | }
|
---|
| 100 |
|
---|
| 101 | private void UpdateChart() {
|
---|
| 102 | if (InvokeRequired) Invoke((Action)UpdateChart);
|
---|
| 103 | else {
|
---|
| 104 | if (Content != null) {
|
---|
[3408] | 105 | this.UpdateSeries();
|
---|
| 106 | if (!this.chart.Series.Any(s => s.Points.Count > 0))
|
---|
[3764] | 107 | this.ClearChart();
|
---|
[3408] | 108 | }
|
---|
| 109 | }
|
---|
| 110 | }
|
---|
| 111 |
|
---|
[3707] | 112 | private void UpdateCursorInterval() {
|
---|
[3710] | 113 | var estimatedValues = this.chart.Series[ALL_SERIES].Points.Select(x => x.XValue).DefaultIfEmpty(1.0);
|
---|
| 114 | var targetValues = this.chart.Series[ALL_SERIES].Points.Select(x => x.YValues[0]).DefaultIfEmpty(1.0);
|
---|
[3707] | 115 | double estimatedValuesRange = estimatedValues.Max() - estimatedValues.Min();
|
---|
| 116 | double targetValuesRange = targetValues.Max() - targetValues.Min();
|
---|
| 117 | double interestingValuesRange = Math.Min(Math.Max(targetValuesRange, 1.0), Math.Max(estimatedValuesRange, 1.0));
|
---|
| 118 | double digits = (int)Math.Log10(interestingValuesRange) - 3;
|
---|
| 119 | double zoomInterval = Math.Max(Math.Pow(10, digits), 10E-5);
|
---|
| 120 | this.chart.ChartAreas[0].CursorX.Interval = zoomInterval;
|
---|
| 121 | this.chart.ChartAreas[0].CursorY.Interval = zoomInterval;
|
---|
| 122 | }
|
---|
| 123 |
|
---|
| 124 |
|
---|
[3408] | 125 | private void UpdateSeries() {
|
---|
[3462] | 126 | if (InvokeRequired) Invoke((Action)UpdateSeries);
|
---|
| 127 | else {
|
---|
[5663] | 128 | string targetVariableName = Content.ProblemData.TargetVariable;
|
---|
[3462] | 129 | Dataset dataset = Content.ProblemData.Dataset;
|
---|
[3933] | 130 | if (this.chart.Series[ALL_SERIES].Points.Count > 0)
|
---|
| 131 | this.chart.Series[ALL_SERIES].Points.DataBindXY(Content.EstimatedValues.ToArray(), "",
|
---|
[6760] | 132 | dataset.GetDoubleValues(targetVariableName).ToArray(), "");
|
---|
[3462] | 133 | if (this.chart.Series[TRAINING_SERIES].Points.Count > 0)
|
---|
[3933] | 134 | this.chart.Series[TRAINING_SERIES].Points.DataBindXY(Content.EstimatedTrainingValues.ToArray(), "",
|
---|
[6760] | 135 | dataset.GetDoubleValues(targetVariableName, Content.ProblemData.TrainingIndizes).ToArray(), "");
|
---|
[3462] | 136 | if (this.chart.Series[TEST_SERIES].Points.Count > 0)
|
---|
[3933] | 137 | this.chart.Series[TEST_SERIES].Points.DataBindXY(Content.EstimatedTestValues.ToArray(), "",
|
---|
[6760] | 138 | dataset.GetDoubleValues(targetVariableName, Content.ProblemData.TestIndizes).ToArray(), "");
|
---|
[3408] | 139 |
|
---|
[6760] | 140 | double max = Content.EstimatedTrainingValues.Concat(Content.EstimatedTestValues.Concat(Content.EstimatedValues.Concat(dataset.GetDoubleValues(targetVariableName)))).Max();
|
---|
| 141 | double min = Content.EstimatedTrainingValues.Concat(Content.EstimatedTestValues.Concat(Content.EstimatedValues.Concat(dataset.GetDoubleValues(targetVariableName)))).Min();
|
---|
[3408] | 142 |
|
---|
[4972] | 143 | max = max + 0.2 * Math.Abs(max);
|
---|
| 144 | min = min - 0.2 * Math.Abs(min);
|
---|
[3408] | 145 |
|
---|
[4972] | 146 | double interestingValuesRange = max - min;
|
---|
| 147 | int digits = Math.Max(0, 3 - (int)Math.Log10(interestingValuesRange));
|
---|
| 148 |
|
---|
| 149 | max = Math.Round(max, digits);
|
---|
| 150 | min = Math.Round(min, digits);
|
---|
| 151 |
|
---|
[3462] | 152 | this.chart.ChartAreas[0].AxisX.Maximum = max;
|
---|
| 153 | this.chart.ChartAreas[0].AxisX.Minimum = min;
|
---|
| 154 | this.chart.ChartAreas[0].AxisY.Maximum = max;
|
---|
| 155 | this.chart.ChartAreas[0].AxisY.Minimum = min;
|
---|
[3707] | 156 | UpdateCursorInterval();
|
---|
[3462] | 157 | }
|
---|
[3408] | 158 | }
|
---|
| 159 |
|
---|
| 160 | private void ClearChart() {
|
---|
| 161 | this.chart.Series[ALL_SERIES].Points.Clear();
|
---|
| 162 | this.chart.Series[TRAINING_SERIES].Points.Clear();
|
---|
[3710] | 163 | this.chart.Series[TEST_SERIES].Points.Clear();
|
---|
[3408] | 164 | }
|
---|
| 165 |
|
---|
| 166 | private void ToggleSeriesData(Series series) {
|
---|
| 167 | if (series.Points.Count > 0) { //checks if series is shown
|
---|
| 168 | if (this.chart.Series.Any(s => s != series && s.Points.Count > 0)) {
|
---|
[3442] | 169 | series.Points.Clear();
|
---|
[3408] | 170 | }
|
---|
[3442] | 171 | } else if (Content != null) {
|
---|
[5663] | 172 | string targetVariableName = Content.ProblemData.TargetVariable;
|
---|
[3442] | 173 |
|
---|
[3408] | 174 | IEnumerable<double> predictedValues = null;
|
---|
| 175 | IEnumerable<double> targetValues = null;
|
---|
| 176 | switch (series.Name) {
|
---|
| 177 | case ALL_SERIES:
|
---|
[4468] | 178 | predictedValues = Content.EstimatedValues.ToArray();
|
---|
[6760] | 179 | targetValues = Content.ProblemData.Dataset.GetDoubleValues(targetVariableName).ToArray();
|
---|
[3408] | 180 | break;
|
---|
| 181 | case TRAINING_SERIES:
|
---|
[4468] | 182 | predictedValues = Content.EstimatedTrainingValues.ToArray();
|
---|
[6760] | 183 | targetValues = Content.ProblemData.Dataset.GetDoubleValues(targetVariableName, Content.ProblemData.TrainingIndizes).ToArray();
|
---|
[3408] | 184 | break;
|
---|
| 185 | case TEST_SERIES:
|
---|
[4468] | 186 | predictedValues = Content.EstimatedTestValues.ToArray();
|
---|
[6760] | 187 | targetValues = Content.ProblemData.Dataset.GetDoubleValues(targetVariableName, Content.ProblemData.TestIndizes).ToArray();
|
---|
[3408] | 188 | break;
|
---|
| 189 | }
|
---|
[3442] | 190 | series.Points.DataBindXY(predictedValues, "", targetValues, "");
|
---|
[3408] | 191 | this.chart.Legends[series.Legend].ForeColor = Color.Black;
|
---|
[3707] | 192 | UpdateCursorInterval();
|
---|
[3408] | 193 | }
|
---|
| 194 | }
|
---|
| 195 |
|
---|
| 196 | private void chart_MouseDown(object sender, MouseEventArgs e) {
|
---|
| 197 | HitTestResult result = chart.HitTest(e.X, e.Y);
|
---|
| 198 | if (result.ChartElementType == ChartElementType.LegendItem) {
|
---|
| 199 | this.ToggleSeriesData(result.Series);
|
---|
| 200 | }
|
---|
| 201 | }
|
---|
| 202 |
|
---|
| 203 | private void chart_MouseMove(object sender, MouseEventArgs e) {
|
---|
| 204 | HitTestResult result = chart.HitTest(e.X, e.Y);
|
---|
| 205 | if (result.ChartElementType == ChartElementType.LegendItem)
|
---|
| 206 | this.Cursor = Cursors.Hand;
|
---|
| 207 | else
|
---|
| 208 | this.Cursor = Cursors.Default;
|
---|
| 209 | }
|
---|
| 210 |
|
---|
| 211 | private void chart_AxisViewChanged(object sender, System.Windows.Forms.DataVisualization.Charting.ViewEventArgs e) {
|
---|
| 212 | this.chart.ChartAreas[0].AxisX.ScaleView.Size = e.NewSize;
|
---|
| 213 | this.chart.ChartAreas[0].AxisY.ScaleView.Size = e.NewSize;
|
---|
| 214 | }
|
---|
| 215 |
|
---|
| 216 | private void chart_CustomizeLegend(object sender, CustomizeLegendEventArgs e) {
|
---|
| 217 | e.LegendItems[0].Cells[1].ForeColor = this.chart.Series[ALL_SERIES].Points.Count == 0 ? Color.Gray : Color.Black;
|
---|
| 218 | e.LegendItems[1].Cells[1].ForeColor = this.chart.Series[TRAINING_SERIES].Points.Count == 0 ? Color.Gray : Color.Black;
|
---|
[3442] | 219 | e.LegendItems[2].Cells[1].ForeColor = this.chart.Series[TEST_SERIES].Points.Count == 0 ? Color.Gray : Color.Black;
|
---|
[3408] | 220 | }
|
---|
[6760] | 221 |
|
---|
| 222 | private void chart_PostPaint(object sender, ChartPaintEventArgs e) {
|
---|
| 223 | var chartArea = e.ChartElement as ChartArea;
|
---|
| 224 | if (chartArea != null) {
|
---|
| 225 | ChartGraphics chartGraphics = e.ChartGraphics;
|
---|
| 226 | using (Pen p = new Pen(Color.DarkGray)) {
|
---|
| 227 | double xmin = chartArea.AxisX.ScaleView.ViewMinimum;
|
---|
| 228 | double xmax = chartArea.AxisX.ScaleView.ViewMaximum;
|
---|
| 229 | double ymin = chartArea.AxisY.ScaleView.ViewMinimum;
|
---|
| 230 | double ymax = chartArea.AxisY.ScaleView.ViewMaximum;
|
---|
| 231 |
|
---|
| 232 | if (xmin > ymax || ymin > xmax) return;
|
---|
| 233 |
|
---|
| 234 | PointF start = PointF.Empty;
|
---|
| 235 | start.X = (float)chartGraphics.GetPositionFromAxis(chartArea.Name, chartArea.AxisX.AxisName, Math.Max(xmin, ymin));
|
---|
| 236 | start.Y = (float)chartGraphics.GetPositionFromAxis(chartArea.Name, chartArea.AxisY.AxisName, Math.Max(xmin, ymin));
|
---|
| 237 | PointF end = PointF.Empty;
|
---|
| 238 | end.X = (float)chartGraphics.GetPositionFromAxis(chartArea.Name, chartArea.AxisX.AxisName, Math.Min(xmax, ymax));
|
---|
| 239 | end.Y = (float)chartGraphics.GetPositionFromAxis(chartArea.Name, chartArea.AxisY.AxisName, Math.Min(xmax, ymax));
|
---|
| 240 |
|
---|
| 241 | chartGraphics.Graphics.DrawLine(p, chartGraphics.GetAbsolutePoint(start), chartGraphics.GetAbsolutePoint(end));
|
---|
| 242 | }
|
---|
| 243 | }
|
---|
| 244 | }
|
---|
[3408] | 245 | }
|
---|
| 246 | }
|
---|