[3408] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[12031] | 3 | * Copyright (C) 2002-2015 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.Drawing;
|
---|
| 23 | using System.Linq;
|
---|
| 24 | using System.Windows.Forms;
|
---|
| 25 | using System.Windows.Forms.DataVisualization.Charting;
|
---|
| 26 | using HeuristicLab.MainForm;
|
---|
[3442] | 27 | using HeuristicLab.MainForm.WindowsForms;
|
---|
[3408] | 28 |
|
---|
[3442] | 29 | namespace HeuristicLab.Problems.DataAnalysis.Views {
|
---|
[5975] | 30 | [View("Scatter Plot")]
|
---|
[5663] | 31 | [Content(typeof(IRegressionSolution))]
|
---|
[6642] | 32 | public partial class RegressionSolutionScatterPlotView : DataAnalysisSolutionEvaluationView {
|
---|
[5663] | 33 | private const string ALL_SERIES = "All samples";
|
---|
| 34 | private const string TRAINING_SERIES = "Training samples";
|
---|
| 35 | private const string TEST_SERIES = "Test samples";
|
---|
[3408] | 36 |
|
---|
[5663] | 37 | public new IRegressionSolution Content {
|
---|
| 38 | get { return (IRegressionSolution)base.Content; }
|
---|
[3442] | 39 | set { base.Content = value; }
|
---|
| 40 | }
|
---|
| 41 |
|
---|
[5663] | 42 | public RegressionSolutionScatterPlotView()
|
---|
[3408] | 43 | : base() {
|
---|
| 44 | InitializeComponent();
|
---|
| 45 |
|
---|
| 46 | this.chart.Series.Add(ALL_SERIES);
|
---|
| 47 | this.chart.Series[ALL_SERIES].LegendText = ALL_SERIES;
|
---|
| 48 | this.chart.Series[ALL_SERIES].ChartType = SeriesChartType.FastPoint;
|
---|
| 49 |
|
---|
| 50 | this.chart.Series.Add(TRAINING_SERIES);
|
---|
| 51 | this.chart.Series[TRAINING_SERIES].LegendText = TRAINING_SERIES;
|
---|
| 52 | this.chart.Series[TRAINING_SERIES].ChartType = SeriesChartType.FastPoint;
|
---|
[3867] | 53 | this.chart.Series[TRAINING_SERIES].Points.Add(1.0);
|
---|
[3408] | 54 |
|
---|
| 55 | this.chart.Series.Add(TEST_SERIES);
|
---|
| 56 | this.chart.Series[TEST_SERIES].LegendText = TEST_SERIES;
|
---|
| 57 | this.chart.Series[TEST_SERIES].ChartType = SeriesChartType.FastPoint;
|
---|
| 58 |
|
---|
| 59 | this.chart.TextAntiAliasingQuality = TextAntiAliasingQuality.High;
|
---|
| 60 | this.chart.AxisViewChanged += new EventHandler<System.Windows.Forms.DataVisualization.Charting.ViewEventArgs>(chart_AxisViewChanged);
|
---|
| 61 |
|
---|
[4651] | 62 | //configure axis
|
---|
| 63 | this.chart.CustomizeAllChartAreas();
|
---|
[3408] | 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;
|
---|
[3707] | 67 | this.chart.ChartAreas[0].CursorX.Interval = 1;
|
---|
| 68 | this.chart.ChartAreas[0].CursorY.Interval = 1;
|
---|
[3408] | 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 |
|
---|
[3442] | 76 | protected override void RegisterContentEvents() {
|
---|
| 77 | base.RegisterContentEvents();
|
---|
[5663] | 78 | Content.ModelChanged += new EventHandler(Content_ModelChanged);
|
---|
[3442] | 79 | Content.ProblemDataChanged += new EventHandler(Content_ProblemDataChanged);
|
---|
| 80 | }
|
---|
| 81 | protected override void DeregisterContentEvents() {
|
---|
| 82 | base.DeregisterContentEvents();
|
---|
[5663] | 83 | Content.ModelChanged -= new EventHandler(Content_ModelChanged);
|
---|
[3442] | 84 | Content.ProblemDataChanged -= new EventHandler(Content_ProblemDataChanged);
|
---|
| 85 | }
|
---|
| 86 |
|
---|
| 87 |
|
---|
[3904] | 88 | private void Content_ProblemDataChanged(object sender, EventArgs e) {
|
---|
[3462] | 89 | UpdateChart();
|
---|
[3442] | 90 | }
|
---|
[5663] | 91 | private void Content_ModelChanged(object sender, EventArgs e) {
|
---|
[3462] | 92 | UpdateSeries();
|
---|
[3442] | 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) {
|
---|
[3408] | 104 | this.UpdateSeries();
|
---|
| 105 | if (!this.chart.Series.Any(s => s.Points.Count > 0))
|
---|
[3764] | 106 | this.ClearChart();
|
---|
[3408] | 107 | }
|
---|
| 108 | }
|
---|
| 109 | }
|
---|
| 110 |
|
---|
[3707] | 111 | private void UpdateCursorInterval() {
|
---|
[3710] | 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);
|
---|
[3707] | 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;
|
---|
[7990] | 121 |
|
---|
| 122 | this.chart.ChartAreas[0].AxisX.ScaleView.SmallScrollSize = zoomInterval;
|
---|
| 123 | this.chart.ChartAreas[0].AxisY.ScaleView.SmallScrollSize = zoomInterval;
|
---|
| 124 |
|
---|
| 125 | this.chart.ChartAreas[0].AxisX.ScaleView.SmallScrollMinSizeType = DateTimeIntervalType.Number;
|
---|
| 126 | this.chart.ChartAreas[0].AxisX.ScaleView.SmallScrollMinSize = zoomInterval;
|
---|
| 127 | this.chart.ChartAreas[0].AxisY.ScaleView.SmallScrollMinSizeType = DateTimeIntervalType.Number;
|
---|
| 128 | this.chart.ChartAreas[0].AxisY.ScaleView.SmallScrollMinSize = zoomInterval;
|
---|
| 129 |
|
---|
| 130 | if (digits < 0) {
|
---|
| 131 | this.chart.ChartAreas[0].AxisX.LabelStyle.Format = "F" + (int)Math.Abs(digits);
|
---|
| 132 | this.chart.ChartAreas[0].AxisY.LabelStyle.Format = "F" + (int)Math.Abs(digits);
|
---|
| 133 | } else {
|
---|
| 134 | this.chart.ChartAreas[0].AxisX.LabelStyle.Format = "F0";
|
---|
| 135 | this.chart.ChartAreas[0].AxisY.LabelStyle.Format = "F0";
|
---|
| 136 | }
|
---|
[3707] | 137 | }
|
---|
| 138 |
|
---|
| 139 |
|
---|
[3408] | 140 | private void UpdateSeries() {
|
---|
[3462] | 141 | if (InvokeRequired) Invoke((Action)UpdateSeries);
|
---|
| 142 | else {
|
---|
[5663] | 143 | string targetVariableName = Content.ProblemData.TargetVariable;
|
---|
[11571] | 144 | var dataset = Content.ProblemData.Dataset;
|
---|
[3933] | 145 | if (this.chart.Series[ALL_SERIES].Points.Count > 0)
|
---|
| 146 | this.chart.Series[ALL_SERIES].Points.DataBindXY(Content.EstimatedValues.ToArray(), "",
|
---|
[6740] | 147 | dataset.GetDoubleValues(targetVariableName).ToArray(), "");
|
---|
[3462] | 148 | if (this.chart.Series[TRAINING_SERIES].Points.Count > 0)
|
---|
[3933] | 149 | this.chart.Series[TRAINING_SERIES].Points.DataBindXY(Content.EstimatedTrainingValues.ToArray(), "",
|
---|
[8139] | 150 | dataset.GetDoubleValues(targetVariableName, Content.ProblemData.TrainingIndices).ToArray(), "");
|
---|
[3462] | 151 | if (this.chart.Series[TEST_SERIES].Points.Count > 0)
|
---|
[3933] | 152 | this.chart.Series[TEST_SERIES].Points.DataBindXY(Content.EstimatedTestValues.ToArray(), "",
|
---|
[8139] | 153 | dataset.GetDoubleValues(targetVariableName, Content.ProblemData.TestIndices).ToArray(), "");
|
---|
[3408] | 154 |
|
---|
[6740] | 155 | double max = Content.EstimatedTrainingValues.Concat(Content.EstimatedTestValues.Concat(Content.EstimatedValues.Concat(dataset.GetDoubleValues(targetVariableName)))).Max();
|
---|
| 156 | double min = Content.EstimatedTrainingValues.Concat(Content.EstimatedTestValues.Concat(Content.EstimatedValues.Concat(dataset.GetDoubleValues(targetVariableName)))).Min();
|
---|
[3408] | 157 |
|
---|
[4972] | 158 | max = max + 0.2 * Math.Abs(max);
|
---|
| 159 | min = min - 0.2 * Math.Abs(min);
|
---|
[3408] | 160 |
|
---|
[4972] | 161 | double interestingValuesRange = max - min;
|
---|
| 162 | int digits = Math.Max(0, 3 - (int)Math.Log10(interestingValuesRange));
|
---|
| 163 |
|
---|
| 164 | max = Math.Round(max, digits);
|
---|
| 165 | min = Math.Round(min, digits);
|
---|
| 166 |
|
---|
[3462] | 167 | this.chart.ChartAreas[0].AxisX.Maximum = max;
|
---|
| 168 | this.chart.ChartAreas[0].AxisX.Minimum = min;
|
---|
| 169 | this.chart.ChartAreas[0].AxisY.Maximum = max;
|
---|
| 170 | this.chart.ChartAreas[0].AxisY.Minimum = min;
|
---|
[3707] | 171 | UpdateCursorInterval();
|
---|
[3462] | 172 | }
|
---|
[3408] | 173 | }
|
---|
| 174 |
|
---|
| 175 | private void ClearChart() {
|
---|
| 176 | this.chart.Series[ALL_SERIES].Points.Clear();
|
---|
| 177 | this.chart.Series[TRAINING_SERIES].Points.Clear();
|
---|
[3710] | 178 | this.chart.Series[TEST_SERIES].Points.Clear();
|
---|
[3408] | 179 | }
|
---|
| 180 |
|
---|
| 181 | private void ToggleSeriesData(Series series) {
|
---|
| 182 | if (series.Points.Count > 0) { //checks if series is shown
|
---|
| 183 | if (this.chart.Series.Any(s => s != series && s.Points.Count > 0)) {
|
---|
[3442] | 184 | series.Points.Clear();
|
---|
[3408] | 185 | }
|
---|
[3442] | 186 | } else if (Content != null) {
|
---|
[5663] | 187 | string targetVariableName = Content.ProblemData.TargetVariable;
|
---|
[3442] | 188 |
|
---|
[6982] | 189 | double[] predictedValues = null;
|
---|
| 190 | double[] targetValues = null;
|
---|
[3408] | 191 | switch (series.Name) {
|
---|
| 192 | case ALL_SERIES:
|
---|
[4468] | 193 | predictedValues = Content.EstimatedValues.ToArray();
|
---|
[6740] | 194 | targetValues = Content.ProblemData.Dataset.GetDoubleValues(targetVariableName).ToArray();
|
---|
[3408] | 195 | break;
|
---|
| 196 | case TRAINING_SERIES:
|
---|
[4468] | 197 | predictedValues = Content.EstimatedTrainingValues.ToArray();
|
---|
[8139] | 198 | targetValues = Content.ProblemData.Dataset.GetDoubleValues(targetVariableName, Content.ProblemData.TrainingIndices).ToArray();
|
---|
[3408] | 199 | break;
|
---|
| 200 | case TEST_SERIES:
|
---|
[4468] | 201 | predictedValues = Content.EstimatedTestValues.ToArray();
|
---|
[8139] | 202 | targetValues = Content.ProblemData.Dataset.GetDoubleValues(targetVariableName, Content.ProblemData.TestIndices).ToArray();
|
---|
[3408] | 203 | break;
|
---|
| 204 | }
|
---|
[6982] | 205 | if (predictedValues.Length == targetValues.Length)
|
---|
| 206 | series.Points.DataBindXY(predictedValues, "", targetValues, "");
|
---|
[3408] | 207 | this.chart.Legends[series.Legend].ForeColor = Color.Black;
|
---|
[3707] | 208 | UpdateCursorInterval();
|
---|
[3408] | 209 | }
|
---|
| 210 | }
|
---|
| 211 |
|
---|
| 212 | private void chart_MouseDown(object sender, MouseEventArgs e) {
|
---|
| 213 | HitTestResult result = chart.HitTest(e.X, e.Y);
|
---|
| 214 | if (result.ChartElementType == ChartElementType.LegendItem) {
|
---|
| 215 | this.ToggleSeriesData(result.Series);
|
---|
| 216 | }
|
---|
| 217 | }
|
---|
| 218 |
|
---|
| 219 | private void chart_MouseMove(object sender, MouseEventArgs e) {
|
---|
| 220 | HitTestResult result = chart.HitTest(e.X, e.Y);
|
---|
| 221 | if (result.ChartElementType == ChartElementType.LegendItem)
|
---|
| 222 | this.Cursor = Cursors.Hand;
|
---|
| 223 | else
|
---|
| 224 | this.Cursor = Cursors.Default;
|
---|
| 225 | }
|
---|
| 226 |
|
---|
| 227 | private void chart_AxisViewChanged(object sender, System.Windows.Forms.DataVisualization.Charting.ViewEventArgs e) {
|
---|
| 228 | this.chart.ChartAreas[0].AxisX.ScaleView.Size = e.NewSize;
|
---|
| 229 | this.chart.ChartAreas[0].AxisY.ScaleView.Size = e.NewSize;
|
---|
| 230 | }
|
---|
| 231 |
|
---|
| 232 | private void chart_CustomizeLegend(object sender, CustomizeLegendEventArgs e) {
|
---|
| 233 | e.LegendItems[0].Cells[1].ForeColor = this.chart.Series[ALL_SERIES].Points.Count == 0 ? Color.Gray : Color.Black;
|
---|
| 234 | e.LegendItems[1].Cells[1].ForeColor = this.chart.Series[TRAINING_SERIES].Points.Count == 0 ? Color.Gray : Color.Black;
|
---|
[3442] | 235 | e.LegendItems[2].Cells[1].ForeColor = this.chart.Series[TEST_SERIES].Points.Count == 0 ? Color.Gray : Color.Black;
|
---|
[3408] | 236 | }
|
---|
[6679] | 237 |
|
---|
| 238 | private void chart_PostPaint(object sender, ChartPaintEventArgs e) {
|
---|
| 239 | var chartArea = e.ChartElement as ChartArea;
|
---|
| 240 | if (chartArea != null) {
|
---|
| 241 | ChartGraphics chartGraphics = e.ChartGraphics;
|
---|
| 242 | using (Pen p = new Pen(Color.DarkGray)) {
|
---|
| 243 | double xmin = chartArea.AxisX.ScaleView.ViewMinimum;
|
---|
| 244 | double xmax = chartArea.AxisX.ScaleView.ViewMaximum;
|
---|
| 245 | double ymin = chartArea.AxisY.ScaleView.ViewMinimum;
|
---|
| 246 | double ymax = chartArea.AxisY.ScaleView.ViewMaximum;
|
---|
| 247 |
|
---|
| 248 | if (xmin > ymax || ymin > xmax) return;
|
---|
| 249 |
|
---|
| 250 | PointF start = PointF.Empty;
|
---|
| 251 | start.X = (float)chartGraphics.GetPositionFromAxis(chartArea.Name, chartArea.AxisX.AxisName, Math.Max(xmin, ymin));
|
---|
| 252 | start.Y = (float)chartGraphics.GetPositionFromAxis(chartArea.Name, chartArea.AxisY.AxisName, Math.Max(xmin, ymin));
|
---|
| 253 | PointF end = PointF.Empty;
|
---|
| 254 | end.X = (float)chartGraphics.GetPositionFromAxis(chartArea.Name, chartArea.AxisX.AxisName, Math.Min(xmax, ymax));
|
---|
| 255 | end.Y = (float)chartGraphics.GetPositionFromAxis(chartArea.Name, chartArea.AxisY.AxisName, Math.Min(xmax, ymax));
|
---|
| 256 |
|
---|
| 257 | chartGraphics.Graphics.DrawLine(p, chartGraphics.GetAbsolutePoint(start), chartGraphics.GetAbsolutePoint(end));
|
---|
| 258 | }
|
---|
| 259 | }
|
---|
| 260 | }
|
---|
[3408] | 261 | }
|
---|
| 262 | }
|
---|