[3408] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[14186] | 3 | * Copyright (C) 2002-2016 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;
|
---|
[6679] | 22 | using System.Collections.Generic;
|
---|
[3408] | 23 | using System.Drawing;
|
---|
| 24 | using System.Linq;
|
---|
| 25 | using System.Windows.Forms;
|
---|
[4068] | 26 | using System.Windows.Forms.DataVisualization.Charting;
|
---|
[3408] | 27 | using HeuristicLab.MainForm;
|
---|
[14161] | 28 | using HeuristicLab.Visualization.ChartControlsExtensions;
|
---|
[3408] | 29 |
|
---|
[3442] | 30 | namespace HeuristicLab.Problems.DataAnalysis.Views {
|
---|
[5975] | 31 | [View("Line Chart")]
|
---|
[5663] | 32 | [Content(typeof(IRegressionSolution))]
|
---|
[6642] | 33 | public partial class RegressionSolutionLineChartView : DataAnalysisSolutionEvaluationView {
|
---|
[6238] | 34 | private const string TARGETVARIABLE_SERIES_NAME = "Target Variable";
|
---|
| 35 | private const string ESTIMATEDVALUES_TRAINING_SERIES_NAME = "Estimated Values (training)";
|
---|
| 36 | private const string ESTIMATEDVALUES_TEST_SERIES_NAME = "Estimated Values (test)";
|
---|
[6679] | 37 | private const string ESTIMATEDVALUES_ALL_SERIES_NAME = "Estimated Values (all samples)";
|
---|
[3442] | 38 |
|
---|
[5663] | 39 | public new IRegressionSolution Content {
|
---|
| 40 | get { return (IRegressionSolution)base.Content; }
|
---|
[3916] | 41 | set { base.Content = value; }
|
---|
[3442] | 42 | }
|
---|
| 43 |
|
---|
[5663] | 44 | public RegressionSolutionLineChartView()
|
---|
[3408] | 45 | : base() {
|
---|
| 46 | InitializeComponent();
|
---|
| 47 | //configure axis
|
---|
[4651] | 48 | this.chart.CustomizeAllChartAreas();
|
---|
[3408] | 49 | this.chart.ChartAreas[0].CursorX.IsUserSelectionEnabled = true;
|
---|
| 50 | this.chart.ChartAreas[0].AxisX.ScaleView.Zoomable = true;
|
---|
[6238] | 51 | this.chart.ChartAreas[0].AxisX.IsStartedFromZero = true;
|
---|
[3707] | 52 | this.chart.ChartAreas[0].CursorX.Interval = 1;
|
---|
[3408] | 53 |
|
---|
| 54 | this.chart.ChartAreas[0].CursorY.IsUserSelectionEnabled = true;
|
---|
| 55 | this.chart.ChartAreas[0].AxisY.ScaleView.Zoomable = true;
|
---|
[3442] | 56 | this.chart.ChartAreas[0].CursorY.Interval = 0;
|
---|
[3408] | 57 | }
|
---|
| 58 |
|
---|
[3462] | 59 | private void RedrawChart() {
|
---|
[3442] | 60 | this.chart.Series.Clear();
|
---|
[4011] | 61 | if (Content != null) {
|
---|
[6238] | 62 | this.chart.ChartAreas[0].AxisX.Minimum = 0;
|
---|
| 63 | this.chart.ChartAreas[0].AxisX.Maximum = Content.ProblemData.Dataset.Rows - 1;
|
---|
| 64 |
|
---|
[4011] | 65 | this.chart.Series.Add(TARGETVARIABLE_SERIES_NAME);
|
---|
[14309] | 66 | this.chart.Series[TARGETVARIABLE_SERIES_NAME].LegendText = TARGETVARIABLE_SERIES_NAME;
|
---|
[4011] | 67 | this.chart.Series[TARGETVARIABLE_SERIES_NAME].ChartType = SeriesChartType.FastLine;
|
---|
[6238] | 68 | this.chart.Series[TARGETVARIABLE_SERIES_NAME].Points.DataBindXY(Enumerable.Range(0, Content.ProblemData.Dataset.Rows).ToArray(),
|
---|
[6740] | 69 | Content.ProblemData.Dataset.GetDoubleValues(Content.ProblemData.TargetVariable).ToArray());
|
---|
[7327] | 70 | // training series
|
---|
[6238] | 71 | this.chart.Series.Add(ESTIMATEDVALUES_TRAINING_SERIES_NAME);
|
---|
| 72 | this.chart.Series[ESTIMATEDVALUES_TRAINING_SERIES_NAME].LegendText = ESTIMATEDVALUES_TRAINING_SERIES_NAME;
|
---|
| 73 | this.chart.Series[ESTIMATEDVALUES_TRAINING_SERIES_NAME].ChartType = SeriesChartType.FastLine;
|
---|
[7333] | 74 | this.chart.Series[ESTIMATEDVALUES_TRAINING_SERIES_NAME].EmptyPointStyle.Color = this.chart.Series[ESTIMATEDVALUES_TRAINING_SERIES_NAME].Color;
|
---|
[8139] | 75 | this.chart.Series[ESTIMATEDVALUES_TRAINING_SERIES_NAME].Points.DataBindXY(Content.ProblemData.TrainingIndices.ToArray(), Content.EstimatedTrainingValues.ToArray());
|
---|
[7406] | 76 | this.InsertEmptyPoints(this.chart.Series[ESTIMATEDVALUES_TRAINING_SERIES_NAME]);
|
---|
[6238] | 77 | this.chart.Series[ESTIMATEDVALUES_TRAINING_SERIES_NAME].Tag = Content;
|
---|
[7327] | 78 | // test series
|
---|
[6238] | 79 | this.chart.Series.Add(ESTIMATEDVALUES_TEST_SERIES_NAME);
|
---|
| 80 | this.chart.Series[ESTIMATEDVALUES_TEST_SERIES_NAME].LegendText = ESTIMATEDVALUES_TEST_SERIES_NAME;
|
---|
| 81 | this.chart.Series[ESTIMATEDVALUES_TEST_SERIES_NAME].ChartType = SeriesChartType.FastLine;
|
---|
[8139] | 82 | this.chart.Series[ESTIMATEDVALUES_TEST_SERIES_NAME].Points.DataBindXY(Content.ProblemData.TestIndices.ToArray(), Content.EstimatedTestValues.ToArray());
|
---|
[7406] | 83 | this.InsertEmptyPoints(this.chart.Series[ESTIMATEDVALUES_TEST_SERIES_NAME]);
|
---|
[6238] | 84 | this.chart.Series[ESTIMATEDVALUES_TEST_SERIES_NAME].Tag = Content;
|
---|
[7327] | 85 | // series of remaining points
|
---|
[8139] | 86 | int[] allIndices = Enumerable.Range(0, Content.ProblemData.Dataset.Rows).Except(Content.ProblemData.TrainingIndices).Except(Content.ProblemData.TestIndices).ToArray();
|
---|
[6679] | 87 | var estimatedValues = Content.EstimatedValues.ToArray();
|
---|
[8139] | 88 | List<double> allEstimatedValues = allIndices.Select(index => estimatedValues[index]).ToList();
|
---|
[6679] | 89 | this.chart.Series.Add(ESTIMATEDVALUES_ALL_SERIES_NAME);
|
---|
| 90 | this.chart.Series[ESTIMATEDVALUES_ALL_SERIES_NAME].LegendText = ESTIMATEDVALUES_ALL_SERIES_NAME;
|
---|
| 91 | this.chart.Series[ESTIMATEDVALUES_ALL_SERIES_NAME].ChartType = SeriesChartType.FastLine;
|
---|
[8485] | 92 | if (allEstimatedValues.Count > 0) {
|
---|
| 93 | this.chart.Series[ESTIMATEDVALUES_ALL_SERIES_NAME].Points.DataBindXY(allIndices, allEstimatedValues);
|
---|
| 94 | this.InsertEmptyPoints(this.chart.Series[ESTIMATEDVALUES_ALL_SERIES_NAME]);
|
---|
| 95 | }
|
---|
[6679] | 96 | this.chart.Series[ESTIMATEDVALUES_ALL_SERIES_NAME].Tag = Content;
|
---|
| 97 | this.ToggleSeriesData(this.chart.Series[ESTIMATEDVALUES_ALL_SERIES_NAME]);
|
---|
| 98 |
|
---|
[14309] | 99 | // set the y-axis
|
---|
[14161] | 100 | var axisY = this.chart.ChartAreas[0].AxisY;
|
---|
[14309] | 101 | axisY.Title = Content.ProblemData.TargetVariable;
|
---|
[14161] | 102 | double min = double.MaxValue, max = double.MinValue;
|
---|
| 103 | foreach (var point in chart.Series.SelectMany(x => x.Points)) {
|
---|
| 104 | if (!point.YValues.Any() || double.IsInfinity(point.YValues[0]) || double.IsNaN(point.YValues[0]))
|
---|
| 105 | continue;
|
---|
| 106 | var y = point.YValues[0];
|
---|
| 107 | if (y < min)
|
---|
| 108 | min = y;
|
---|
| 109 | if (y > max)
|
---|
| 110 | max = y;
|
---|
| 111 | }
|
---|
| 112 |
|
---|
| 113 | double axisMin, axisMax, axisInterval;
|
---|
| 114 | ChartUtil.CalculateOptimalAxisInterval(min, max, out axisMin, out axisMax, out axisInterval);
|
---|
| 115 | axisY.Minimum = axisMin;
|
---|
| 116 | axisY.Maximum = axisMax;
|
---|
| 117 | axisY.Interval = axisInterval;
|
---|
| 118 |
|
---|
[4011] | 119 | UpdateCursorInterval();
|
---|
[6238] | 120 | this.UpdateStripLines();
|
---|
[4011] | 121 | }
|
---|
[3408] | 122 | }
|
---|
| 123 |
|
---|
[7406] | 124 | private void InsertEmptyPoints(Series series) {
|
---|
| 125 | int i = 0;
|
---|
| 126 | while (i < series.Points.Count - 1) {
|
---|
| 127 | if (series.Points[i].IsEmpty) {
|
---|
| 128 | ++i;
|
---|
| 129 | continue;
|
---|
| 130 | }
|
---|
| 131 |
|
---|
| 132 | var p1 = series.Points[i];
|
---|
| 133 | var p2 = series.Points[i + 1];
|
---|
| 134 | // check for consecutive indices
|
---|
| 135 | if ((int)p2.XValue - (int)p1.XValue != 1) {
|
---|
| 136 | // insert an empty point between p1 and p2 so that the line will be invisible (transparent)
|
---|
| 137 | var p = new DataPoint((int)((p1.XValue + p2.XValue) / 2), 0.0) { IsEmpty = true };
|
---|
| 138 | series.Points.Insert(i + 1, p);
|
---|
| 139 | }
|
---|
| 140 | ++i;
|
---|
| 141 | }
|
---|
| 142 | }
|
---|
| 143 |
|
---|
[3707] | 144 | private void UpdateCursorInterval() {
|
---|
[6238] | 145 | var estimatedValues = this.chart.Series[ESTIMATEDVALUES_TRAINING_SERIES_NAME].Points.Select(x => x.YValues[0]).DefaultIfEmpty(1.0);
|
---|
[3707] | 146 | var targetValues = this.chart.Series[TARGETVARIABLE_SERIES_NAME].Points.Select(x => x.YValues[0]).DefaultIfEmpty(1.0);
|
---|
| 147 | double estimatedValuesRange = estimatedValues.Max() - estimatedValues.Min();
|
---|
| 148 | double targetValuesRange = targetValues.Max() - targetValues.Min();
|
---|
| 149 | double interestingValuesRange = Math.Min(Math.Max(targetValuesRange, 1.0), Math.Max(estimatedValuesRange, 1.0));
|
---|
| 150 | double digits = (int)Math.Log10(interestingValuesRange) - 3;
|
---|
| 151 | double yZoomInterval = Math.Max(Math.Pow(10, digits), 10E-5);
|
---|
| 152 | this.chart.ChartAreas[0].CursorY.Interval = yZoomInterval;
|
---|
| 153 | }
|
---|
| 154 |
|
---|
[3442] | 155 | #region events
|
---|
| 156 | protected override void RegisterContentEvents() {
|
---|
| 157 | base.RegisterContentEvents();
|
---|
[5663] | 158 | Content.ModelChanged += new EventHandler(Content_ModelChanged);
|
---|
[3442] | 159 | Content.ProblemDataChanged += new EventHandler(Content_ProblemDataChanged);
|
---|
[3408] | 160 | }
|
---|
[3442] | 161 | protected override void DeregisterContentEvents() {
|
---|
| 162 | base.DeregisterContentEvents();
|
---|
[5663] | 163 | Content.ModelChanged -= new EventHandler(Content_ModelChanged);
|
---|
[3442] | 164 | Content.ProblemDataChanged -= new EventHandler(Content_ProblemDataChanged);
|
---|
[3408] | 165 | }
|
---|
| 166 |
|
---|
[6679] | 167 | protected override void OnContentChanged() {
|
---|
| 168 | base.OnContentChanged();
|
---|
| 169 | RedrawChart();
|
---|
| 170 | }
|
---|
[3916] | 171 | private void Content_ProblemDataChanged(object sender, EventArgs e) {
|
---|
[3462] | 172 | RedrawChart();
|
---|
[3408] | 173 | }
|
---|
[5663] | 174 | private void Content_ModelChanged(object sender, EventArgs e) {
|
---|
[3462] | 175 | RedrawChart();
|
---|
[3442] | 176 | }
|
---|
| 177 |
|
---|
[5006] | 178 |
|
---|
[6679] | 179 |
|
---|
[5006] | 180 | private void Chart_MouseDoubleClick(object sender, MouseEventArgs e) {
|
---|
| 181 | HitTestResult result = chart.HitTest(e.X, e.Y);
|
---|
| 182 | if (result.ChartArea != null && (result.ChartElementType == ChartElementType.PlottingArea ||
|
---|
| 183 | result.ChartElementType == ChartElementType.Gridlines) ||
|
---|
| 184 | result.ChartElementType == ChartElementType.StripLines) {
|
---|
| 185 | foreach (var axis in result.ChartArea.Axes)
|
---|
| 186 | axis.ScaleView.ZoomReset(int.MaxValue);
|
---|
| 187 | }
|
---|
| 188 | }
|
---|
[3442] | 189 | #endregion
|
---|
[3408] | 190 |
|
---|
| 191 | private void UpdateStripLines() {
|
---|
| 192 | this.chart.ChartAreas[0].AxisX.StripLines.Clear();
|
---|
[6238] | 193 |
|
---|
| 194 | int[] attr = new int[Content.ProblemData.Dataset.Rows + 1]; // add a virtual last row that is again empty to simplify loop further down
|
---|
[8139] | 195 | foreach (var row in Content.ProblemData.TrainingIndices) {
|
---|
[6238] | 196 | attr[row] += 1;
|
---|
| 197 | }
|
---|
[8139] | 198 | foreach (var row in Content.ProblemData.TestIndices) {
|
---|
[6238] | 199 | attr[row] += 2;
|
---|
| 200 | }
|
---|
| 201 | int start = 0;
|
---|
| 202 | int curAttr = attr[start];
|
---|
| 203 | for (int row = 0; row < attr.Length; row++) {
|
---|
| 204 | if (attr[row] != curAttr) {
|
---|
| 205 | switch (curAttr) {
|
---|
| 206 | case 0: break;
|
---|
| 207 | case 1:
|
---|
| 208 | this.CreateAndAddStripLine("Training", start, row, Color.FromArgb(40, Color.Green), Color.Transparent);
|
---|
| 209 | break;
|
---|
| 210 | case 2:
|
---|
| 211 | this.CreateAndAddStripLine("Test", start, row, Color.FromArgb(40, Color.Red), Color.Transparent);
|
---|
| 212 | break;
|
---|
| 213 | case 3:
|
---|
| 214 | this.CreateAndAddStripLine("Training and Test", start, row, Color.FromArgb(40, Color.Green), Color.FromArgb(40, Color.Red), ChartHatchStyle.WideUpwardDiagonal);
|
---|
| 215 | break;
|
---|
| 216 | default:
|
---|
| 217 | // should not happen
|
---|
| 218 | break;
|
---|
| 219 | }
|
---|
| 220 | curAttr = attr[row];
|
---|
| 221 | start = row;
|
---|
| 222 | }
|
---|
| 223 | }
|
---|
[3408] | 224 | }
|
---|
| 225 |
|
---|
[6238] | 226 | private void CreateAndAddStripLine(string title, int start, int end, Color color, Color secondColor, ChartHatchStyle hatchStyle = ChartHatchStyle.None) {
|
---|
[3408] | 227 | StripLine stripLine = new StripLine();
|
---|
[6238] | 228 | stripLine.BackColor = color;
|
---|
| 229 | stripLine.BackSecondaryColor = secondColor;
|
---|
| 230 | stripLine.BackHatchStyle = hatchStyle;
|
---|
[3408] | 231 | stripLine.Text = title;
|
---|
| 232 | stripLine.Font = new Font("Times New Roman", 12, FontStyle.Bold);
|
---|
[6252] | 233 | // strip range is [start .. end] inclusive, but we evaluate [start..end[ (end is exclusive)
|
---|
| 234 | // the strip should be by one longer (starting at start - 0.5 and ending at end + 0.5)
|
---|
[6520] | 235 | stripLine.StripWidth = end - start;
|
---|
[6252] | 236 | stripLine.IntervalOffset = start - 0.5; // start slightly to the left of the first point to clearly indicate the first point in the partition
|
---|
[3408] | 237 | this.chart.ChartAreas[0].AxisX.StripLines.Add(stripLine);
|
---|
| 238 | }
|
---|
[6679] | 239 |
|
---|
| 240 | private void ToggleSeriesData(Series series) {
|
---|
| 241 | if (series.Points.Count > 0) { //checks if series is shown
|
---|
| 242 | if (this.chart.Series.Any(s => s != series && s.Points.Count > 0)) {
|
---|
[7406] | 243 | ClearPointsQuick(series.Points);
|
---|
[6679] | 244 | }
|
---|
| 245 | } else if (Content != null) {
|
---|
| 246 | string targetVariableName = Content.ProblemData.TargetVariable;
|
---|
| 247 |
|
---|
[8139] | 248 | IEnumerable<int> indices = null;
|
---|
[8485] | 249 | double[] predictedValues = null;
|
---|
[6679] | 250 | switch (series.Name) {
|
---|
| 251 | case ESTIMATEDVALUES_ALL_SERIES_NAME:
|
---|
[8139] | 252 | indices = Enumerable.Range(0, Content.ProblemData.Dataset.Rows).Except(Content.ProblemData.TrainingIndices).Except(Content.ProblemData.TestIndices).ToArray();
|
---|
[6679] | 253 | var estimatedValues = Content.EstimatedValues.ToArray();
|
---|
[8485] | 254 | predictedValues = indices.Select(index => estimatedValues[index]).ToArray();
|
---|
[6679] | 255 | break;
|
---|
| 256 | case ESTIMATEDVALUES_TRAINING_SERIES_NAME:
|
---|
[8139] | 257 | indices = Content.ProblemData.TrainingIndices.ToArray();
|
---|
[6679] | 258 | predictedValues = Content.EstimatedTrainingValues.ToArray();
|
---|
| 259 | break;
|
---|
| 260 | case ESTIMATEDVALUES_TEST_SERIES_NAME:
|
---|
[8139] | 261 | indices = Content.ProblemData.TestIndices.ToArray();
|
---|
[6679] | 262 | predictedValues = Content.EstimatedTestValues.ToArray();
|
---|
| 263 | break;
|
---|
| 264 | }
|
---|
[8485] | 265 | if (predictedValues.Length > 0) {
|
---|
| 266 | series.Points.DataBindXY(indices, predictedValues);
|
---|
| 267 | this.InsertEmptyPoints(series);
|
---|
| 268 | }
|
---|
[6679] | 269 | chart.Legends[series.Legend].ForeColor = Color.Black;
|
---|
| 270 | UpdateCursorInterval();
|
---|
[6775] | 271 | chart.Refresh();
|
---|
[6679] | 272 | }
|
---|
| 273 | }
|
---|
| 274 |
|
---|
[7406] | 275 | // workaround as per http://stackoverflow.com/questions/5744930/datapointcollection-clear-performance
|
---|
| 276 | private static void ClearPointsQuick(DataPointCollection points) {
|
---|
| 277 | points.SuspendUpdates();
|
---|
| 278 | while (points.Count > 0)
|
---|
| 279 | points.RemoveAt(points.Count - 1);
|
---|
| 280 | points.ResumeUpdates();
|
---|
| 281 | }
|
---|
| 282 |
|
---|
[6679] | 283 | private void chart_MouseMove(object sender, MouseEventArgs e) {
|
---|
| 284 | HitTestResult result = chart.HitTest(e.X, e.Y);
|
---|
| 285 | if (result.ChartElementType == ChartElementType.LegendItem && result.Series.Name != TARGETVARIABLE_SERIES_NAME)
|
---|
| 286 | Cursor = Cursors.Hand;
|
---|
| 287 | else
|
---|
| 288 | Cursor = Cursors.Default;
|
---|
| 289 | }
|
---|
| 290 | private void chart_MouseDown(object sender, MouseEventArgs e) {
|
---|
| 291 | HitTestResult result = chart.HitTest(e.X, e.Y);
|
---|
| 292 | if (result.ChartElementType == ChartElementType.LegendItem && result.Series.Name != TARGETVARIABLE_SERIES_NAME) {
|
---|
| 293 | ToggleSeriesData(result.Series);
|
---|
| 294 | }
|
---|
| 295 | }
|
---|
| 296 |
|
---|
| 297 | private void chart_CustomizeLegend(object sender, CustomizeLegendEventArgs e) {
|
---|
| 298 | if (chart.Series.Count != 4) return;
|
---|
| 299 | e.LegendItems[0].Cells[1].ForeColor = this.chart.Series[TARGETVARIABLE_SERIES_NAME].Points.Count == 0 ? Color.Gray : Color.Black;
|
---|
| 300 | e.LegendItems[1].Cells[1].ForeColor = this.chart.Series[ESTIMATEDVALUES_TRAINING_SERIES_NAME].Points.Count == 0 ? Color.Gray : Color.Black;
|
---|
| 301 | e.LegendItems[2].Cells[1].ForeColor = this.chart.Series[ESTIMATEDVALUES_TEST_SERIES_NAME].Points.Count == 0 ? Color.Gray : Color.Black;
|
---|
| 302 | e.LegendItems[3].Cells[1].ForeColor = this.chart.Series[ESTIMATEDVALUES_ALL_SERIES_NAME].Points.Count == 0 ? Color.Gray : Color.Black;
|
---|
| 303 | }
|
---|
[3408] | 304 | }
|
---|
| 305 | }
|
---|