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