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
|
---|
21 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Drawing;
|
---|
25 | using System.Linq;
|
---|
26 | using System.Windows.Forms;
|
---|
27 | using System.Windows.Forms.DataVisualization.Charting;
|
---|
28 | using HeuristicLab.Common;
|
---|
29 | using HeuristicLab.MainForm;
|
---|
30 | using HeuristicLab.MainForm.WindowsForms;
|
---|
31 |
|
---|
32 | namespace HeuristicLab.Problems.DataAnalysis.Classification.Views {
|
---|
33 | [View("Symbolic Classification View")]
|
---|
34 | [Content(typeof(SymbolicClassificationSolution), true)]
|
---|
35 | public sealed partial class SymbolicClassificationSolutionView : AsynchronousContentView {
|
---|
36 | private const double TrainingAxisValue = 0.0;
|
---|
37 | private const double TestAxisValue = 10.0;
|
---|
38 | private const double TrainingTestBorder = (TestAxisValue - TrainingAxisValue) / 2;
|
---|
39 | private const string TrainingLabelText = "Training Samples";
|
---|
40 | private const string TestLabelText = "Test Samples";
|
---|
41 |
|
---|
42 | public new SymbolicClassificationSolution Content {
|
---|
43 | get { return (SymbolicClassificationSolution)base.Content; }
|
---|
44 | set { base.Content = value; }
|
---|
45 | }
|
---|
46 |
|
---|
47 | private Dictionary<double, Series> classValueSeriesMapping;
|
---|
48 | private Random random;
|
---|
49 | private bool updateInProgress;
|
---|
50 |
|
---|
51 | public SymbolicClassificationSolutionView()
|
---|
52 | : base() {
|
---|
53 | InitializeComponent();
|
---|
54 |
|
---|
55 | classValueSeriesMapping = new Dictionary<double, Series>();
|
---|
56 | random = new Random();
|
---|
57 | updateInProgress = false;
|
---|
58 |
|
---|
59 | this.chart.ChartAreas[0].CursorX.IsUserSelectionEnabled = true;
|
---|
60 | this.chart.ChartAreas[0].AxisX.ScaleView.Zoomable = true;
|
---|
61 | this.chart.ChartAreas[0].AxisX.Minimum = TrainingAxisValue - TrainingTestBorder;
|
---|
62 | this.chart.ChartAreas[0].AxisX.Maximum = TestAxisValue + TrainingTestBorder;
|
---|
63 | AddCustomLabelToAxis(this.chart.ChartAreas[0].AxisX);
|
---|
64 |
|
---|
65 | this.chart.ChartAreas[0].AxisY.Title = "Estimated Values";
|
---|
66 | this.chart.ChartAreas[0].CursorY.IsUserSelectionEnabled = true;
|
---|
67 | this.chart.ChartAreas[0].AxisX.ScaleView.Zoomable = true;
|
---|
68 | }
|
---|
69 |
|
---|
70 | private void AddCustomLabelToAxis(Axis axis) {
|
---|
71 | CustomLabel trainingLabel = new CustomLabel();
|
---|
72 | trainingLabel.Text = TrainingLabelText;
|
---|
73 | trainingLabel.FromPosition = TrainingAxisValue - TrainingTestBorder;
|
---|
74 | trainingLabel.ToPosition = TrainingAxisValue + TrainingTestBorder;
|
---|
75 | axis.CustomLabels.Add(trainingLabel);
|
---|
76 |
|
---|
77 | CustomLabel testLabel = new CustomLabel();
|
---|
78 | testLabel.Text = TestLabelText;
|
---|
79 | testLabel.FromPosition = TestAxisValue - TrainingTestBorder;
|
---|
80 | testLabel.ToPosition = TestAxisValue + TrainingTestBorder;
|
---|
81 | axis.CustomLabels.Add(testLabel);
|
---|
82 | }
|
---|
83 |
|
---|
84 | protected override void RegisterContentEvents() {
|
---|
85 | base.RegisterContentEvents();
|
---|
86 | Content.EstimatedValuesChanged += new EventHandler(Content_EstimatedValuesChanged);
|
---|
87 | Content.ProblemDataChanged += new EventHandler(Content_ProblemDataChanged);
|
---|
88 | Content.ThresholdsChanged += new EventHandler(Content_ThresholdsChanged);
|
---|
89 | }
|
---|
90 | protected override void DeregisterContentEvents() {
|
---|
91 | base.DeregisterContentEvents();
|
---|
92 | Content.EstimatedValuesChanged -= new EventHandler(Content_EstimatedValuesChanged);
|
---|
93 | Content.ProblemDataChanged -= new EventHandler(Content_ProblemDataChanged);
|
---|
94 | Content.ThresholdsChanged -= new EventHandler(Content_ThresholdsChanged);
|
---|
95 | }
|
---|
96 |
|
---|
97 | private void Content_ProblemDataChanged(object sender, EventArgs e) {
|
---|
98 | UpdateChart();
|
---|
99 | }
|
---|
100 | private void Content_EstimatedValuesChanged(object sender, EventArgs e) {
|
---|
101 | UpdateChart();
|
---|
102 | }
|
---|
103 | private void Content_ThresholdsChanged(object sender, EventArgs e) {
|
---|
104 | AddThresholds();
|
---|
105 | }
|
---|
106 | protected override void OnContentChanged() {
|
---|
107 | base.OnContentChanged();
|
---|
108 | UpdateChart();
|
---|
109 | }
|
---|
110 |
|
---|
111 | private void UpdateChart() {
|
---|
112 | if (InvokeRequired) Invoke((Action)UpdateChart);
|
---|
113 | else if (!updateInProgress) {
|
---|
114 | updateInProgress = true;
|
---|
115 | chart.Series.Clear();
|
---|
116 | classValueSeriesMapping.Clear();
|
---|
117 | if (Content != null) {
|
---|
118 | IEnumerator<string> classNameEnumerator = Content.ProblemData.ClassNames.GetEnumerator();
|
---|
119 | IEnumerator<double> classValueEnumerator = Content.ProblemData.SortedClassValues.GetEnumerator();
|
---|
120 | while (classNameEnumerator.MoveNext() && classValueEnumerator.MoveNext()) {
|
---|
121 | Series series = new Series(classNameEnumerator.Current);
|
---|
122 | series.ChartType = SeriesChartType.FastPoint;
|
---|
123 | series.Tag = classValueEnumerator.Current;
|
---|
124 | chart.Series.Add(series);
|
---|
125 | classValueSeriesMapping.Add(classValueEnumerator.Current, series);
|
---|
126 | FillSeriesWithDataPoints(series);
|
---|
127 | }
|
---|
128 | AddThresholds();
|
---|
129 | }
|
---|
130 | chart.ChartAreas[0].RecalculateAxesScale();
|
---|
131 | updateInProgress = false;
|
---|
132 | }
|
---|
133 | }
|
---|
134 |
|
---|
135 | private void FillSeriesWithDataPoints(Series series) {
|
---|
136 | List<double> estimatedValues = Content.EstimatedValues.ToList();
|
---|
137 | foreach (int row in Content.ProblemData.TrainingIndizes) {
|
---|
138 | double estimatedValue = estimatedValues[row];
|
---|
139 | double targetValue = Content.ProblemData.Dataset[Content.ProblemData.TargetVariable.Value, row];
|
---|
140 | if (targetValue.IsAlmost((double)series.Tag)) {
|
---|
141 | double jitterValue = random.NextDouble() * 2.0 - 1.0;
|
---|
142 | DataPoint point = new DataPoint();
|
---|
143 | point.XValue = TrainingAxisValue + 0.01 * jitterValue * JitterTrackBar.Value * (TrainingTestBorder * 0.9);
|
---|
144 | point.YValues[0] = estimatedValue;
|
---|
145 | point.Tag = new KeyValuePair<double, double>(TrainingAxisValue, jitterValue);
|
---|
146 | series.Points.Add(point);
|
---|
147 | }
|
---|
148 | }
|
---|
149 |
|
---|
150 | foreach (int row in Content.ProblemData.TestIndizes) {
|
---|
151 | double estimatedValue = estimatedValues[row];
|
---|
152 | double targetValue = Content.ProblemData.Dataset[Content.ProblemData.TargetVariable.Value, row];
|
---|
153 | if (targetValue == (double)series.Tag) {
|
---|
154 | double jitterValue = random.NextDouble() * 2.0 - 1.0;
|
---|
155 | DataPoint point = new DataPoint();
|
---|
156 | point.XValue = TestAxisValue + 0.01 * jitterValue * JitterTrackBar.Value * (TrainingTestBorder * 0.9);
|
---|
157 | point.YValues[0] = estimatedValue;
|
---|
158 | point.Tag = new KeyValuePair<double, double>(TestAxisValue, jitterValue);
|
---|
159 | series.Points.Add(point);
|
---|
160 | }
|
---|
161 | }
|
---|
162 |
|
---|
163 | UpdateCursorInterval();
|
---|
164 | }
|
---|
165 |
|
---|
166 | private void AddThresholds() {
|
---|
167 | chart.Annotations.Clear();
|
---|
168 | int classIndex = 1;
|
---|
169 | foreach (double threshold in Content.Thresholds) {
|
---|
170 | if (!double.IsInfinity(threshold)) {
|
---|
171 | HorizontalLineAnnotation annotation = new HorizontalLineAnnotation();
|
---|
172 | annotation.AllowMoving = true;
|
---|
173 | annotation.AllowResizing = false;
|
---|
174 | annotation.LineWidth = 2;
|
---|
175 | annotation.LineColor = Color.Red;
|
---|
176 |
|
---|
177 | annotation.IsInfinitive = true;
|
---|
178 | annotation.ClipToChartArea = chart.ChartAreas[0].Name;
|
---|
179 | annotation.Tag = classIndex; //save classIndex as Tag to avoid moving the threshold accross class bounderies
|
---|
180 |
|
---|
181 | annotation.AxisX = chart.ChartAreas[0].AxisX;
|
---|
182 | annotation.AxisY = chart.ChartAreas[0].AxisY;
|
---|
183 | annotation.Y = threshold;
|
---|
184 |
|
---|
185 | chart.Annotations.Add(annotation);
|
---|
186 | classIndex++;
|
---|
187 | }
|
---|
188 | }
|
---|
189 | }
|
---|
190 |
|
---|
191 | private void JitterTrackBar_ValueChanged(object sender, EventArgs e) {
|
---|
192 | foreach (Series series in chart.Series) {
|
---|
193 | foreach (DataPoint point in series.Points) {
|
---|
194 | double value = ((KeyValuePair<double, double>)point.Tag).Key;
|
---|
195 | double jitterValue = ((KeyValuePair<double, double>)point.Tag).Value; ;
|
---|
196 | point.XValue = value + 0.01 * jitterValue * JitterTrackBar.Value * (TrainingTestBorder * 0.9);
|
---|
197 | }
|
---|
198 | }
|
---|
199 | }
|
---|
200 |
|
---|
201 | private void chart_CustomizeLegend(object sender, CustomizeLegendEventArgs e) {
|
---|
202 | foreach (LegendItem legendItem in e.LegendItems) {
|
---|
203 | var series = chart.Series[legendItem.SeriesName];
|
---|
204 | if (series != null) {
|
---|
205 | bool seriesIsInvisible = series.Points.Count == 0;
|
---|
206 | foreach (LegendCell cell in legendItem.Cells)
|
---|
207 | cell.ForeColor = seriesIsInvisible ? Color.Gray : Color.Black;
|
---|
208 | }
|
---|
209 | }
|
---|
210 | }
|
---|
211 |
|
---|
212 | private void chart_MouseMove(object sender, MouseEventArgs e) {
|
---|
213 | HitTestResult result = chart.HitTest(e.X, e.Y);
|
---|
214 | if (result.ChartElementType == ChartElementType.LegendItem)
|
---|
215 | this.Cursor = Cursors.Hand;
|
---|
216 | else
|
---|
217 | this.Cursor = Cursors.Default;
|
---|
218 | }
|
---|
219 |
|
---|
220 | private void ToggleSeries(Series series) {
|
---|
221 | if (series.Points.Count == 0)
|
---|
222 | FillSeriesWithDataPoints(series);
|
---|
223 | else
|
---|
224 | series.Points.Clear();
|
---|
225 | }
|
---|
226 |
|
---|
227 | private void chart_MouseDown(object sender, MouseEventArgs e) {
|
---|
228 | HitTestResult result = chart.HitTest(e.X, e.Y);
|
---|
229 | if (result.ChartElementType == ChartElementType.LegendItem) {
|
---|
230 | if (result.Series != null) ToggleSeries(result.Series);
|
---|
231 | }
|
---|
232 | }
|
---|
233 |
|
---|
234 | private void chart_AnnotationPositionChanging(object sender, AnnotationPositionChangingEventArgs e) {
|
---|
235 | int classIndex = (int)e.Annotation.Tag;
|
---|
236 |
|
---|
237 | double classValue = Content.ProblemData.SortedClassValues.ElementAt(classIndex);
|
---|
238 | if (e.NewLocationY >= classValue)
|
---|
239 | e.NewLocationY = classValue;
|
---|
240 |
|
---|
241 | classValue = Content.ProblemData.SortedClassValues.ElementAt(classIndex - 1);
|
---|
242 | if (e.NewLocationY <= classValue)
|
---|
243 | e.NewLocationY = classValue;
|
---|
244 |
|
---|
245 | double[] thresholds = Content.Thresholds.ToArray();
|
---|
246 | thresholds[classIndex] = e.NewLocationY;
|
---|
247 | Content.Thresholds = thresholds;
|
---|
248 | }
|
---|
249 |
|
---|
250 | private void UpdateCursorInterval() {
|
---|
251 | Series series = chart.Series[0];
|
---|
252 | double[] xValues = (from point in series.Points
|
---|
253 | where !point.IsEmpty
|
---|
254 | select point.XValue)
|
---|
255 | .DefaultIfEmpty(1.0)
|
---|
256 | .ToArray();
|
---|
257 | double[] yValues = (from point in series.Points
|
---|
258 | where !point.IsEmpty
|
---|
259 | select point.YValues[0])
|
---|
260 | .DefaultIfEmpty(1.0)
|
---|
261 | .ToArray();
|
---|
262 |
|
---|
263 | double xRange = xValues.Max() - xValues.Min();
|
---|
264 | double yRange = yValues.Max() - yValues.Min();
|
---|
265 | if (xRange.IsAlmost(0.0)) xRange = 1.0;
|
---|
266 | if (yRange.IsAlmost(0.0)) yRange = 1.0;
|
---|
267 | double xDigits = (int)Math.Log10(xRange) - 3;
|
---|
268 | double yDigits = (int)Math.Log10(yRange) - 3;
|
---|
269 | double xZoomInterval = Math.Pow(10, xDigits);
|
---|
270 | double yZoomInterval = Math.Pow(10, yDigits);
|
---|
271 | this.chart.ChartAreas[0].CursorX.Interval = xZoomInterval;
|
---|
272 | this.chart.ChartAreas[0].CursorY.Interval = yZoomInterval;
|
---|
273 | }
|
---|
274 | }
|
---|
275 | }
|
---|