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