[7186] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[7485] | 3 | * Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[7186] | 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;
|
---|
[7255] | 24 | using System.Drawing;
|
---|
[7186] | 25 | using System.Linq;
|
---|
[7255] | 26 | using System.Windows.Forms;
|
---|
[7186] | 27 | using System.Windows.Forms.DataVisualization.Charting;
|
---|
| 28 | using HeuristicLab.MainForm;
|
---|
[7255] | 29 | using HeuristicLab.MainForm.WindowsForms;
|
---|
[7186] | 30 |
|
---|
[7255] | 31 | namespace HeuristicLab.Problems.DataAnalysis.Views {
|
---|
[7186] | 32 | [View("Residual Histogram")]
|
---|
| 33 | [Content(typeof(IRegressionSolution))]
|
---|
| 34 | public partial class RegressionSolutionResidualHistogram : DataAnalysisSolutionEvaluationView {
|
---|
[7485] | 35 |
|
---|
| 36 | #region variables
|
---|
[7255] | 37 | protected const string ALL_SAMPLES = "All samples";
|
---|
| 38 | protected const string TRAINING_SAMPLES = "Training samples";
|
---|
| 39 | protected const string TEST_SAMPLES = "Test samples";
|
---|
[7485] | 40 | /// <summary>
|
---|
| 41 | /// used to reduce code duplication
|
---|
| 42 | /// </summary>
|
---|
[7255] | 43 | protected static string[] ALL_SERIES = new string[] { ALL_SAMPLES, TRAINING_SAMPLES, TEST_SAMPLES };
|
---|
[7485] | 44 | /// <summary>
|
---|
| 45 | /// approximate amount of bins
|
---|
| 46 | /// </summary>
|
---|
[7255] | 47 | protected const double bins = 25;
|
---|
[7485] | 48 | /// <summary>
|
---|
| 49 | /// keeps for all series a list for every bin with the position of the bin, the relative frequency of the
|
---|
| 50 | /// residuals and the beginning and the end of the interval of the bin
|
---|
| 51 | /// </summary>
|
---|
[7255] | 52 | protected Dictionary<string, List<List<double>>> relativeFrequencies;
|
---|
[7485] | 53 | #endregion
|
---|
[7255] | 54 |
|
---|
[7186] | 55 | public new IRegressionSolution Content {
|
---|
| 56 | get { return (IRegressionSolution)base.Content; }
|
---|
| 57 | set { base.Content = value; }
|
---|
| 58 | }
|
---|
| 59 |
|
---|
[7255] | 60 | public RegressionSolutionResidualHistogram()
|
---|
| 61 | : base() {
|
---|
[7186] | 62 | InitializeComponent();
|
---|
[7255] | 63 | relativeFrequencies = new Dictionary<string, List<List<double>>>();
|
---|
| 64 | foreach (string series in ALL_SERIES) {
|
---|
[7485] | 65 | chart.Series.Add(series);
|
---|
| 66 | chart.Series[series].LegendText = series;
|
---|
| 67 | chart.Series[series].ChartType = SeriesChartType.Column;
|
---|
| 68 | chart.Series[series]["PointWidth"] = "0.9";
|
---|
| 69 | chart.Series[series].BorderWidth = 1;
|
---|
| 70 | chart.Series[series].BorderDashStyle = ChartDashStyle.Solid;
|
---|
| 71 | chart.Series[series].BorderColor = Color.Black;
|
---|
| 72 | chart.Series[series].ToolTip = series + " Y = #VALY from #CUSTOMPROPERTY(from) to #CUSTOMPROPERTY(to)";
|
---|
[7255] | 73 | relativeFrequencies[series] = new List<List<double>>();
|
---|
| 74 | }
|
---|
[7186] | 75 | //configure axis
|
---|
[7485] | 76 | chart.CustomizeAllChartAreas();
|
---|
| 77 | chart.ChartAreas[0].AxisX.Title = "Residuals";
|
---|
| 78 | chart.ChartAreas[0].CursorX.IsUserSelectionEnabled = true;
|
---|
| 79 | chart.ChartAreas[0].AxisX.ScaleView.Zoomable = true;
|
---|
| 80 | chart.ChartAreas[0].CursorX.Interval = 1;
|
---|
| 81 | chart.ChartAreas[0].CursorY.Interval = 1;
|
---|
| 82 | chart.ChartAreas[0].AxisY.Title = "Relative Frequency";
|
---|
| 83 | chart.ChartAreas[0].CursorY.IsUserSelectionEnabled = true;
|
---|
| 84 | chart.ChartAreas[0].AxisY.ScaleView.Zoomable = true;
|
---|
| 85 | chart.ChartAreas[0].AxisY.IsStartedFromZero = true;
|
---|
[7186] | 86 | }
|
---|
| 87 |
|
---|
| 88 | private void RedrawChart() {
|
---|
[7255] | 89 | foreach (string series in ALL_SERIES) {
|
---|
[7485] | 90 | chart.Series[series].Points.Clear();
|
---|
| 91 | relativeFrequencies[series].Clear();
|
---|
[7255] | 92 | }
|
---|
[7186] | 93 | if (Content != null) {
|
---|
[7255] | 94 | Dictionary<string, List<double>> residuals = CalculateResiduals();
|
---|
[7503] | 95 | double realMax = Math.Max(Math.Abs(residuals[ALL_SAMPLES].Min()), Math.Abs(residuals[ALL_SAMPLES].Max()));
|
---|
| 96 | double roundedMax = HumanRoundMax(realMax);
|
---|
| 97 | double intervalWidth = (roundedMax * 2.0) / bins;
|
---|
[7255] | 98 | intervalWidth = HumanRoundMax(intervalWidth);
|
---|
[7503] | 99 | // sets roundedMax to a value, so that zero will be in the middle of the x axis
|
---|
| 100 | double help = realMax / intervalWidth;
|
---|
| 101 | help = help % 1 < 0.5 ? (int)help : (int)help + 1;
|
---|
| 102 | roundedMax = help * intervalWidth;
|
---|
[7186] | 103 |
|
---|
[7255] | 104 | foreach (string series in ALL_SERIES) {
|
---|
[7503] | 105 | CalculateFrequencies(residuals[series], series, roundedMax, intervalWidth);
|
---|
[7485] | 106 | if (!series.Equals(ALL_SAMPLES))
|
---|
| 107 | ShowValues(chart.Series[series], relativeFrequencies[series]);
|
---|
[7186] | 108 | }
|
---|
| 109 |
|
---|
[7255] | 110 | ChartArea chartArea = chart.ChartAreas[0];
|
---|
[7503] | 111 | chartArea.AxisX.Minimum = -roundedMax - intervalWidth;
|
---|
| 112 | chartArea.AxisX.Maximum = roundedMax + intervalWidth;
|
---|
| 113 | // get the highest frequency of a residual of any series
|
---|
| 114 | chartArea.AxisY.Maximum = (from series in relativeFrequencies.Values
|
---|
| 115 | select (from residual in series
|
---|
| 116 | select residual.ElementAt(1)).Max()).Max();
|
---|
| 117 | if (chartArea.AxisY.Maximum < 0.1) {
|
---|
| 118 | chartArea.AxisY.Interval = 0.01;
|
---|
| 119 | chartArea.AxisY.Maximum = Math.Ceiling(chartArea.AxisY.Maximum * 100) / 100;
|
---|
| 120 | } else {
|
---|
| 121 | chartArea.AxisY.Interval = 0.1;
|
---|
| 122 | chartArea.AxisY.Maximum = Math.Ceiling(chartArea.AxisY.Maximum * 10) / 10;
|
---|
| 123 | }
|
---|
[7255] | 124 | chartArea.AxisX.Interval = intervalWidth;
|
---|
[7503] | 125 | int curBins = (int)Math.Round((roundedMax * 2) / intervalWidth);
|
---|
[7255] | 126 | //shifts the x axis label so that zero is in the middle
|
---|
| 127 | if (curBins % 2 == 0)
|
---|
| 128 | chartArea.AxisX.IntervalOffset = intervalWidth;
|
---|
| 129 | else
|
---|
| 130 | chartArea.AxisX.IntervalOffset = intervalWidth / 2;
|
---|
| 131 | }
|
---|
| 132 | }
|
---|
| 133 |
|
---|
| 134 | private Dictionary<string, List<double>> CalculateResiduals() {
|
---|
| 135 | Dictionary<string, List<double>> residuals = new Dictionary<string, List<double>>();
|
---|
| 136 |
|
---|
| 137 | foreach (string series in ALL_SERIES) {
|
---|
| 138 | residuals[series] = new List<double>();
|
---|
| 139 | }
|
---|
| 140 | IRegressionProblemData problemdata = Content.ProblemData;
|
---|
| 141 | List<double> targetValues = problemdata.Dataset.GetDoubleValues(Content.ProblemData.TargetVariable).ToList();
|
---|
| 142 | List<double> estimatedValues = Content.EstimatedValues.ToList();
|
---|
| 143 |
|
---|
| 144 | for (int i = 0; i < Content.ProblemData.Dataset.Rows; i++) {
|
---|
| 145 | double residual = estimatedValues[i] - targetValues[i];
|
---|
| 146 | residuals[ALL_SAMPLES].Add(residual);
|
---|
| 147 | if (i >= problemdata.TrainingPartition.Start && i < problemdata.TrainingPartition.End)
|
---|
| 148 | residuals[TRAINING_SAMPLES].Add(residual);
|
---|
| 149 | if (i >= problemdata.TestPartition.Start && i < problemdata.TestPartition.End)
|
---|
| 150 | residuals[TEST_SAMPLES].Add(residual);
|
---|
| 151 | }
|
---|
| 152 | return residuals;
|
---|
| 153 | }
|
---|
| 154 |
|
---|
| 155 | private void CalculateFrequencies(List<double> residualValues, string series, double max, double intervalWidth) {
|
---|
| 156 | double intervalCenter = intervalWidth / 2.0;
|
---|
| 157 | double sampleCount = residualValues.Count();
|
---|
| 158 | double current = -max;
|
---|
| 159 |
|
---|
| 160 | for (int i = 0; i <= bins; i++) {
|
---|
| 161 | IEnumerable<double> help = residualValues.Where(x => x >= (current - intervalCenter) && x < (current + intervalCenter));
|
---|
| 162 | relativeFrequencies[series].Add(new List<double>() { current, help.Count() / sampleCount, current - intervalCenter, current + intervalCenter });
|
---|
| 163 | current += intervalWidth;
|
---|
| 164 | }
|
---|
| 165 | }
|
---|
| 166 |
|
---|
| 167 | private double HumanRoundMax(double max) {
|
---|
| 168 | double base10;
|
---|
| 169 | if (max > 0) base10 = Math.Pow(10.0, Math.Floor(Math.Log10(max)));
|
---|
| 170 | else base10 = Math.Pow(10.0, Math.Ceiling(Math.Log10(-max)));
|
---|
| 171 | double rounding = (max > 0) ? base10 : -base10;
|
---|
| 172 | while (rounding < max) rounding += base10;
|
---|
| 173 | return rounding;
|
---|
| 174 | }
|
---|
| 175 |
|
---|
[7186] | 176 | #region events
|
---|
| 177 | protected override void RegisterContentEvents() {
|
---|
| 178 | base.RegisterContentEvents();
|
---|
| 179 | Content.ModelChanged += new EventHandler(Content_ModelChanged);
|
---|
| 180 | Content.ProblemDataChanged += new EventHandler(Content_ProblemDataChanged);
|
---|
| 181 | }
|
---|
| 182 | protected override void DeregisterContentEvents() {
|
---|
| 183 | base.DeregisterContentEvents();
|
---|
| 184 | Content.ModelChanged -= new EventHandler(Content_ModelChanged);
|
---|
| 185 | Content.ProblemDataChanged -= new EventHandler(Content_ProblemDataChanged);
|
---|
| 186 | }
|
---|
| 187 |
|
---|
| 188 | protected override void OnContentChanged() {
|
---|
| 189 | base.OnContentChanged();
|
---|
| 190 | RedrawChart();
|
---|
| 191 | }
|
---|
| 192 | private void Content_ProblemDataChanged(object sender, EventArgs e) {
|
---|
| 193 | RedrawChart();
|
---|
| 194 | }
|
---|
| 195 | private void Content_ModelChanged(object sender, EventArgs e) {
|
---|
| 196 | RedrawChart();
|
---|
| 197 | }
|
---|
[7255] | 198 | private void chart_MouseDown(object sender, MouseEventArgs e) {
|
---|
| 199 | HitTestResult result = chart.HitTest(e.X, e.Y);
|
---|
| 200 | if (result.ChartElementType == ChartElementType.LegendItem) {
|
---|
| 201 | ToggleSeriesData(result.Series);
|
---|
| 202 | }
|
---|
| 203 | }
|
---|
| 204 | private void chart_MouseMove(object sender, MouseEventArgs e) {
|
---|
| 205 | HitTestResult result = chart.HitTest(e.X, e.Y);
|
---|
| 206 | if (result.ChartElementType == ChartElementType.LegendItem)
|
---|
| 207 | Cursor = Cursors.Hand;
|
---|
| 208 | else
|
---|
| 209 | Cursor = Cursors.Default;
|
---|
| 210 | }
|
---|
| 211 | private void chart_CustomizeLegend(object sender, CustomizeLegendEventArgs e) {
|
---|
| 212 | if (chart.Series.Count != 3) return;
|
---|
[7485] | 213 | e.LegendItems[0].Cells[1].ForeColor = chart.Series[ALL_SAMPLES].Points.Count == 0 ? Color.Gray : Color.Black;
|
---|
| 214 | e.LegendItems[1].Cells[1].ForeColor = chart.Series[TRAINING_SAMPLES].Points.Count == 0 ? Color.Gray : Color.Black;
|
---|
| 215 | e.LegendItems[2].Cells[1].ForeColor = chart.Series[TEST_SAMPLES].Points.Count == 0 ? Color.Gray : Color.Black;
|
---|
[7255] | 216 | }
|
---|
[7186] | 217 | #endregion
|
---|
[7485] | 218 |
|
---|
| 219 | private void ToggleSeriesData(Series series) {
|
---|
| 220 | if (series.Points.Count > 0) { //checks if series is shown
|
---|
| 221 | if (chart.Series.Any(s => s != series && s.Points.Count > 0)) {
|
---|
| 222 | series.Points.Clear();
|
---|
| 223 | }
|
---|
| 224 | } else if (Content != null) {
|
---|
| 225 | ShowValues(series, relativeFrequencies[series.Name]);
|
---|
| 226 | chart.Legends[series.Legend].ForeColor = Color.Black;
|
---|
| 227 | chart.Refresh();
|
---|
| 228 | }
|
---|
| 229 | }
|
---|
| 230 | private void ShowValues(Series series, List<List<double>> relativeSeriesFrequencies) {
|
---|
| 231 | DataPointCollection seriesPoints = series.Points;
|
---|
| 232 |
|
---|
| 233 | foreach (var valueList in relativeSeriesFrequencies) {
|
---|
| 234 | seriesPoints.AddXY(valueList[0], valueList[1]);
|
---|
| 235 | seriesPoints[seriesPoints.Count - 1]["from"] = valueList[2].ToString();
|
---|
| 236 | seriesPoints[seriesPoints.Count - 1]["to"] = valueList[3].ToString();
|
---|
| 237 | }
|
---|
| 238 | }
|
---|
[7186] | 239 | }
|
---|
| 240 | }
|
---|