[4417] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[5445] | 3 | * Copyright (C) 2002-2011 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[4417] | 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 {
|
---|
[5664] | 33 | [View("Discriminant function classification solution view")]
|
---|
| 34 | [Content(typeof(IDiscriminantFunctionClassificationSolution), true)]
|
---|
| 35 | public sealed partial class DiscriminantFunctionClassificationSolutionView : AsynchronousContentView {
|
---|
[4417] | 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 |
|
---|
[5664] | 42 | public new IDiscriminantFunctionClassificationSolution Content {
|
---|
| 43 | get { return (IDiscriminantFunctionClassificationSolution)base.Content; }
|
---|
[4417] | 44 | set { base.Content = value; }
|
---|
| 45 | }
|
---|
| 46 |
|
---|
| 47 | private Dictionary<double, Series> classValueSeriesMapping;
|
---|
| 48 | private Random random;
|
---|
| 49 | private bool updateInProgress;
|
---|
| 50 |
|
---|
[5664] | 51 | public DiscriminantFunctionClassificationSolutionView()
|
---|
[4417] | 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();
|
---|
[5664] | 87 | Content.ModelChanged += new EventHandler(Content_ModelChanged);
|
---|
[4417] | 88 | Content.ProblemDataChanged += new EventHandler(Content_ProblemDataChanged);
|
---|
| 89 | }
|
---|
| 90 | protected override void DeregisterContentEvents() {
|
---|
| 91 | base.DeregisterContentEvents();
|
---|
[5664] | 92 | Content.ModelChanged -= new EventHandler(Content_ModelChanged);
|
---|
[4417] | 93 | Content.ProblemDataChanged -= new EventHandler(Content_ProblemDataChanged);
|
---|
| 94 | }
|
---|
| 95 |
|
---|
| 96 | private void Content_ProblemDataChanged(object sender, EventArgs e) {
|
---|
| 97 | UpdateChart();
|
---|
| 98 | }
|
---|
[5664] | 99 | private void Content_ModelChanged(object sender, EventArgs e) {
|
---|
[5736] | 100 | Content.Model.ThresholdsChanged += new EventHandler(Model_ThresholdsChanged);
|
---|
[4417] | 101 | UpdateChart();
|
---|
| 102 | }
|
---|
[5736] | 103 | private void Model_ThresholdsChanged(object sender, EventArgs e) {
|
---|
[4417] | 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();
|
---|
[5664] | 119 | IEnumerator<double> classValueEnumerator = Content.ProblemData.ClassValues.OrderBy(x => x).GetEnumerator();
|
---|
[4417] | 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) {
|
---|
[4469] | 136 | List<double> estimatedValues = Content.EstimatedValues.ToList();
|
---|
| 137 | foreach (int row in Content.ProblemData.TrainingIndizes) {
|
---|
| 138 | double estimatedValue = estimatedValues[row];
|
---|
[5664] | 139 | double targetValue = Content.ProblemData.Dataset[Content.ProblemData.TargetVariable, row];
|
---|
[4469] | 140 | if (targetValue.IsAlmost((double)series.Tag)) {
|
---|
[4417] | 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 |
|
---|
[4469] | 150 | foreach (int row in Content.ProblemData.TestIndizes) {
|
---|
| 151 | double estimatedValue = estimatedValues[row];
|
---|
[5664] | 152 | double targetValue = Content.ProblemData.Dataset[Content.ProblemData.TargetVariable, row];
|
---|
[4417] | 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 | }
|
---|
[4469] | 162 |
|
---|
[4417] | 163 | UpdateCursorInterval();
|
---|
| 164 | }
|
---|
| 165 |
|
---|
| 166 | private void AddThresholds() {
|
---|
| 167 | chart.Annotations.Clear();
|
---|
| 168 | int classIndex = 1;
|
---|
[5717] | 169 | foreach (double threshold in Content.Model.Thresholds) {
|
---|
[4417] | 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;
|
---|
[5717] | 236 | double[] thresholds = Content.Model.Thresholds.ToArray();
|
---|
[4417] | 237 | thresholds[classIndex] = e.NewLocationY;
|
---|
[5736] | 238 | Content.Model.SetThresholdsAndClassValues(thresholds, Content.Model.ClassValues);
|
---|
[4417] | 239 | }
|
---|
| 240 |
|
---|
| 241 | private void UpdateCursorInterval() {
|
---|
| 242 | Series series = chart.Series[0];
|
---|
| 243 | double[] xValues = (from point in series.Points
|
---|
| 244 | where !point.IsEmpty
|
---|
| 245 | select point.XValue)
|
---|
| 246 | .DefaultIfEmpty(1.0)
|
---|
| 247 | .ToArray();
|
---|
| 248 | double[] yValues = (from point in series.Points
|
---|
| 249 | where !point.IsEmpty
|
---|
| 250 | select point.YValues[0])
|
---|
| 251 | .DefaultIfEmpty(1.0)
|
---|
| 252 | .ToArray();
|
---|
| 253 |
|
---|
| 254 | double xRange = xValues.Max() - xValues.Min();
|
---|
| 255 | double yRange = yValues.Max() - yValues.Min();
|
---|
| 256 | if (xRange.IsAlmost(0.0)) xRange = 1.0;
|
---|
| 257 | if (yRange.IsAlmost(0.0)) yRange = 1.0;
|
---|
| 258 | double xDigits = (int)Math.Log10(xRange) - 3;
|
---|
| 259 | double yDigits = (int)Math.Log10(yRange) - 3;
|
---|
| 260 | double xZoomInterval = Math.Pow(10, xDigits);
|
---|
| 261 | double yZoomInterval = Math.Pow(10, yDigits);
|
---|
| 262 | this.chart.ChartAreas[0].CursorX.Interval = xZoomInterval;
|
---|
| 263 | this.chart.ChartAreas[0].CursorY.Interval = yZoomInterval;
|
---|
| 264 | }
|
---|
| 265 | }
|
---|
| 266 | }
|
---|