[13780] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
| 3 | * Copyright (C) 2002-2016 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;
|
---|
[13836] | 24 | using System.Drawing;
|
---|
[13817] | 25 | using System.Globalization;
|
---|
[13780] | 26 | using System.Linq;
|
---|
[13840] | 27 | using System.Threading;
|
---|
[13837] | 28 | using System.Threading.Tasks;
|
---|
[13780] | 29 | using System.Windows.Forms;
|
---|
| 30 | using System.Windows.Forms.DataVisualization.Charting;
|
---|
| 31 | using HeuristicLab.Common;
|
---|
[13836] | 32 | using HeuristicLab.MainForm.WindowsForms;
|
---|
[13780] | 33 | using HeuristicLab.Visualization.ChartControlsExtensions;
|
---|
| 34 |
|
---|
| 35 | namespace HeuristicLab.Problems.DataAnalysis.Views {
|
---|
[13831] | 36 | public partial class GradientChart : UserControl {
|
---|
| 37 | private ModifiableDataset sharedFixedVariables; // used for syncronising variable values between charts
|
---|
[13837] | 38 | private ModifiableDataset internalDataset; // holds the x values for each point drawn
|
---|
[13780] | 39 |
|
---|
[13842] | 40 | private CancellationTokenSource cancelCurrentRecalculateSource;
|
---|
[13840] | 41 |
|
---|
[13842] | 42 | private readonly List<IRegressionSolution> solutions;
|
---|
| 43 | private readonly Dictionary<IRegressionSolution, Series> seriesCache;
|
---|
| 44 | private readonly Dictionary<IRegressionSolution, Series> ciSeriesCache;
|
---|
| 45 |
|
---|
[13853] | 46 | private readonly ToolStripMenuItem configToolStripMenuItem;
|
---|
| 47 | private readonly GradientChartConfigurationDialog configurationDialog;
|
---|
| 48 |
|
---|
[13842] | 49 | #region Properties
|
---|
[14014] | 50 | public string XAxisTitle {
|
---|
| 51 | get { return chart.ChartAreas[0].AxisX.Title; }
|
---|
| 52 | set { chart.ChartAreas[0].AxisX.Title = value; }
|
---|
| 53 | }
|
---|
| 54 |
|
---|
| 55 | public string YAxisTitle {
|
---|
| 56 | get { return chart.ChartAreas[0].AxisY.Title; }
|
---|
| 57 | set { chart.ChartAreas[0].AxisY.Title = value; }
|
---|
| 58 | }
|
---|
| 59 |
|
---|
[13831] | 60 | public bool ShowLegend {
|
---|
| 61 | get { return chart.Legends[0].Enabled; }
|
---|
| 62 | set { chart.Legends[0].Enabled = value; }
|
---|
[13780] | 63 | }
|
---|
[13831] | 64 | public bool ShowCursor {
|
---|
| 65 | get { return chart.Annotations[0].Visible; }
|
---|
[13853] | 66 | set {
|
---|
| 67 | chart.Annotations[0].Visible = value;
|
---|
| 68 | if (!value) chart.ChartAreas[0].AxisX.Title = string.Empty;
|
---|
| 69 | }
|
---|
[13831] | 70 | }
|
---|
[13780] | 71 |
|
---|
[13855] | 72 | public bool ShowConfigButton {
|
---|
| 73 | get { return configurationButton.Visible; }
|
---|
| 74 | set { configurationButton.Visible = value; }
|
---|
| 75 | }
|
---|
| 76 |
|
---|
[13831] | 77 | private int xAxisTicks = 5;
|
---|
| 78 | public int XAxisTicks {
|
---|
| 79 | get { return xAxisTicks; }
|
---|
[13843] | 80 | set {
|
---|
| 81 | if (value != xAxisTicks) {
|
---|
| 82 | xAxisTicks = value;
|
---|
| 83 | SetupAxis(chart.ChartAreas[0].AxisX, trainingMin, trainingMax, XAxisTicks, FixedXAxisMin, FixedXAxisMax);
|
---|
| 84 | RecalculateInternalDataset();
|
---|
| 85 | }
|
---|
| 86 | }
|
---|
[13780] | 87 | }
|
---|
[13842] | 88 | private double? fixedXAxisMin;
|
---|
| 89 | public double? FixedXAxisMin {
|
---|
| 90 | get { return fixedXAxisMin; }
|
---|
| 91 | set {
|
---|
| 92 | if ((value.HasValue && fixedXAxisMin.HasValue && !value.Value.IsAlmost(fixedXAxisMin.Value)) || (value.HasValue != fixedXAxisMin.HasValue)) {
|
---|
| 93 | fixedXAxisMin = value;
|
---|
[14006] | 94 | if (trainingMin < trainingMax) {
|
---|
| 95 | SetupAxis(chart.ChartAreas[0].AxisX, trainingMin, trainingMax, XAxisTicks, FixedXAxisMin, FixedXAxisMax);
|
---|
| 96 | RecalculateInternalDataset();
|
---|
| 97 | // set the vertical line position
|
---|
| 98 | if (VerticalLineAnnotation.X <= fixedXAxisMin) {
|
---|
| 99 | var axisX = chart.ChartAreas[0].AxisX;
|
---|
| 100 | var step = (axisX.Maximum - axisX.Minimum) / drawingSteps;
|
---|
| 101 | VerticalLineAnnotation.X = axisX.Minimum + step;
|
---|
| 102 | }
|
---|
| 103 | }
|
---|
[13842] | 104 | }
|
---|
| 105 | }
|
---|
| 106 | }
|
---|
| 107 | private double? fixedXAxisMax;
|
---|
| 108 | public double? FixedXAxisMax {
|
---|
| 109 | get { return fixedXAxisMax; }
|
---|
| 110 | set {
|
---|
| 111 | if ((value.HasValue && fixedXAxisMax.HasValue && !value.Value.IsAlmost(fixedXAxisMax.Value)) || (value.HasValue != fixedXAxisMax.HasValue)) {
|
---|
| 112 | fixedXAxisMax = value;
|
---|
[14006] | 113 | if (trainingMin < trainingMax) {
|
---|
| 114 | SetupAxis(chart.ChartAreas[0].AxisX, trainingMin, trainingMax, XAxisTicks, FixedXAxisMin, FixedXAxisMax);
|
---|
| 115 | RecalculateInternalDataset();
|
---|
| 116 | // set the vertical line position
|
---|
| 117 | if (VerticalLineAnnotation.X >= fixedXAxisMax) {
|
---|
| 118 | var axisX = chart.ChartAreas[0].AxisX;
|
---|
| 119 | var step = (axisX.Maximum - axisX.Minimum) / drawingSteps;
|
---|
| 120 | VerticalLineAnnotation.X = axisX.Maximum - step;
|
---|
| 121 | }
|
---|
| 122 | }
|
---|
[13842] | 123 | }
|
---|
| 124 | }
|
---|
| 125 | }
|
---|
| 126 |
|
---|
[13831] | 127 | private int yAxisTicks = 5;
|
---|
[13842] | 128 | public int YAxisTicks {
|
---|
[13831] | 129 | get { return yAxisTicks; }
|
---|
[13843] | 130 | set {
|
---|
| 131 | if (value != yAxisTicks) {
|
---|
| 132 | yAxisTicks = value;
|
---|
| 133 | SetupAxis(chart.ChartAreas[0].AxisY, yMin, yMax, YAxisTicks, FixedYAxisMin, FixedYAxisMax);
|
---|
| 134 | RecalculateInternalDataset();
|
---|
| 135 | }
|
---|
| 136 | }
|
---|
[13831] | 137 | }
|
---|
[13842] | 138 | private double? fixedYAxisMin;
|
---|
| 139 | public double? FixedYAxisMin {
|
---|
| 140 | get { return fixedYAxisMin; }
|
---|
| 141 | set {
|
---|
| 142 | if ((value.HasValue && fixedYAxisMin.HasValue && !value.Value.IsAlmost(fixedYAxisMin.Value)) || (value.HasValue != fixedYAxisMin.HasValue)) {
|
---|
| 143 | fixedYAxisMin = value;
|
---|
[14021] | 144 | SetupAxis(chart.ChartAreas[0].AxisY, yMin, yMax, YAxisTicks, FixedYAxisMin, FixedYAxisMax);
|
---|
[13842] | 145 | }
|
---|
| 146 | }
|
---|
| 147 | }
|
---|
| 148 | private double? fixedYAxisMax;
|
---|
| 149 | public double? FixedYAxisMax {
|
---|
| 150 | get { return fixedYAxisMax; }
|
---|
| 151 | set {
|
---|
| 152 | if ((value.HasValue && fixedYAxisMax.HasValue && !value.Value.IsAlmost(fixedYAxisMax.Value)) || (value.HasValue != fixedYAxisMax.HasValue)) {
|
---|
| 153 | fixedYAxisMax = value;
|
---|
[14021] | 154 | SetupAxis(chart.ChartAreas[0].AxisY, yMin, yMax, YAxisTicks, FixedYAxisMin, FixedYAxisMax);
|
---|
[13842] | 155 | }
|
---|
| 156 | }
|
---|
| 157 | }
|
---|
[13780] | 158 |
|
---|
[13843] | 159 | private double trainingMin = 1;
|
---|
| 160 | private double trainingMax = -1;
|
---|
[13780] | 161 |
|
---|
[13831] | 162 | private int drawingSteps = 1000;
|
---|
| 163 | public int DrawingSteps {
|
---|
| 164 | get { return drawingSteps; }
|
---|
[13842] | 165 | set {
|
---|
| 166 | if (value != drawingSteps) {
|
---|
| 167 | drawingSteps = value;
|
---|
| 168 | RecalculateInternalDataset();
|
---|
| 169 | ResizeAllSeriesData();
|
---|
| 170 | }
|
---|
| 171 | }
|
---|
[13780] | 172 | }
|
---|
| 173 |
|
---|
[13831] | 174 | private string freeVariable;
|
---|
| 175 | public string FreeVariable {
|
---|
| 176 | get { return freeVariable; }
|
---|
[13780] | 177 | set {
|
---|
[13831] | 178 | if (value == freeVariable) return;
|
---|
| 179 | if (solutions.Any(s => !s.ProblemData.Dataset.DoubleVariables.Contains(value))) {
|
---|
| 180 | throw new ArgumentException("Variable does not exist in the ProblemData of the Solutions.");
|
---|
| 181 | }
|
---|
| 182 | freeVariable = value;
|
---|
| 183 | RecalculateInternalDataset();
|
---|
[13780] | 184 | }
|
---|
| 185 | }
|
---|
| 186 |
|
---|
[13843] | 187 | private double yMin;
|
---|
| 188 | public double YMin {
|
---|
| 189 | get { return yMin; }
|
---|
| 190 | }
|
---|
| 191 | private double yMax;
|
---|
| 192 | public double YMax {
|
---|
| 193 | get { return yMax; }
|
---|
| 194 | }
|
---|
| 195 |
|
---|
[14089] | 196 | public bool IsZoomed {
|
---|
| 197 | get { return chart.ChartAreas[0].AxisX.ScaleView.IsZoomed; }
|
---|
| 198 | }
|
---|
| 199 |
|
---|
[13831] | 200 | private VerticalLineAnnotation VerticalLineAnnotation {
|
---|
| 201 | get { return (VerticalLineAnnotation)chart.Annotations.SingleOrDefault(x => x is VerticalLineAnnotation); }
|
---|
[13780] | 202 | }
|
---|
[13850] | 203 |
|
---|
| 204 | internal ElementPosition InnerPlotPosition {
|
---|
| 205 | get { return chart.ChartAreas[0].InnerPlotPosition; }
|
---|
| 206 | }
|
---|
[13842] | 207 | #endregion
|
---|
[13780] | 208 |
|
---|
| 209 | public GradientChart() {
|
---|
| 210 | InitializeComponent();
|
---|
[13836] | 211 |
|
---|
[13842] | 212 | solutions = new List<IRegressionSolution>();
|
---|
| 213 | seriesCache = new Dictionary<IRegressionSolution, Series>();
|
---|
| 214 | ciSeriesCache = new Dictionary<IRegressionSolution, Series>();
|
---|
| 215 |
|
---|
[13836] | 216 | // Configure axis
|
---|
| 217 | chart.CustomizeAllChartAreas();
|
---|
| 218 | chart.ChartAreas[0].CursorX.IsUserSelectionEnabled = true;
|
---|
| 219 | chart.ChartAreas[0].AxisX.ScaleView.Zoomable = true;
|
---|
| 220 | chart.ChartAreas[0].CursorX.Interval = 0;
|
---|
| 221 |
|
---|
| 222 | chart.ChartAreas[0].CursorY.IsUserSelectionEnabled = true;
|
---|
| 223 | chart.ChartAreas[0].AxisY.ScaleView.Zoomable = true;
|
---|
| 224 | chart.ChartAreas[0].CursorY.Interval = 0;
|
---|
[13840] | 225 |
|
---|
[13853] | 226 | configToolStripMenuItem = new ToolStripMenuItem("Configuration");
|
---|
[13855] | 227 | configToolStripMenuItem.Click += config_Click;
|
---|
[13853] | 228 | chart.ContextMenuStrip.Items.Add(new ToolStripSeparator());
|
---|
| 229 | chart.ContextMenuStrip.Items.Add(configToolStripMenuItem);
|
---|
| 230 | configurationDialog = new GradientChartConfigurationDialog(this);
|
---|
| 231 |
|
---|
[13842] | 232 | Disposed += GradientChart_Disposed;
|
---|
[13780] | 233 | }
|
---|
[13853] | 234 |
|
---|
[13840] | 235 | private void GradientChart_Disposed(object sender, EventArgs e) {
|
---|
[13843] | 236 | if (cancelCurrentRecalculateSource != null)
|
---|
| 237 | cancelCurrentRecalculateSource.Cancel();
|
---|
[13840] | 238 | }
|
---|
| 239 |
|
---|
[13842] | 240 | public void Configure(IEnumerable<IRegressionSolution> solutions, ModifiableDataset sharedFixedVariables, string freeVariable, int drawingSteps, bool initializeAxisRanges = true) {
|
---|
[13831] | 241 | if (!SolutionsCompatible(solutions))
|
---|
| 242 | throw new ArgumentException("Solutions are not compatible with the problem data.");
|
---|
| 243 | this.freeVariable = freeVariable;
|
---|
| 244 | this.drawingSteps = drawingSteps;
|
---|
[13780] | 245 |
|
---|
[13842] | 246 | this.solutions.Clear();
|
---|
| 247 | this.solutions.AddRange(solutions);
|
---|
| 248 |
|
---|
[13831] | 249 | // add an event such that whenever a value is changed in the shared dataset,
|
---|
| 250 | // this change is reflected in the internal dataset (where the value becomes a whole column)
|
---|
| 251 | if (this.sharedFixedVariables != null)
|
---|
| 252 | this.sharedFixedVariables.ItemChanged -= sharedFixedVariables_ItemChanged;
|
---|
| 253 | this.sharedFixedVariables = sharedFixedVariables;
|
---|
| 254 | this.sharedFixedVariables.ItemChanged += sharedFixedVariables_ItemChanged;
|
---|
[13780] | 255 |
|
---|
[13842] | 256 | RecalculateTrainingLimits(initializeAxisRanges);
|
---|
[13831] | 257 | RecalculateInternalDataset();
|
---|
[13842] | 258 |
|
---|
| 259 | chart.Series.Clear();
|
---|
| 260 | seriesCache.Clear();
|
---|
| 261 | ciSeriesCache.Clear();
|
---|
| 262 | foreach (var solution in this.solutions) {
|
---|
| 263 | var series = CreateSeries(solution);
|
---|
| 264 | seriesCache.Add(solution, series.Item1);
|
---|
| 265 | if (series.Item2 != null)
|
---|
| 266 | ciSeriesCache.Add(solution, series.Item2);
|
---|
| 267 | }
|
---|
| 268 |
|
---|
| 269 | ResizeAllSeriesData();
|
---|
| 270 | OrderAndColorSeries();
|
---|
[13780] | 271 | }
|
---|
| 272 |
|
---|
[13843] | 273 | public async Task RecalculateAsync(bool updateOnFinish = true, bool resetYAxis = true) {
|
---|
[13842] | 274 | if (IsDisposed
|
---|
| 275 | || sharedFixedVariables == null || !solutions.Any() || string.IsNullOrEmpty(freeVariable)
|
---|
| 276 | || trainingMin.IsAlmost(trainingMax) || trainingMin > trainingMax || drawingSteps == 0)
|
---|
| 277 | return;
|
---|
[13829] | 278 |
|
---|
[13853] | 279 | calculationPendingTimer.Start();
|
---|
| 280 |
|
---|
[13842] | 281 | Update(); // immediately show label
|
---|
| 282 |
|
---|
| 283 | // cancel previous recalculate call
|
---|
| 284 | if (cancelCurrentRecalculateSource != null)
|
---|
| 285 | cancelCurrentRecalculateSource.Cancel();
|
---|
| 286 | cancelCurrentRecalculateSource = new CancellationTokenSource();
|
---|
| 287 |
|
---|
| 288 | // Set cursor and x-axis
|
---|
[13995] | 289 | // Make sure to allow a small offset to be able to distinguish the vertical line annotation from the axis
|
---|
[13842] | 290 | var defaultValue = sharedFixedVariables.GetDoubleValue(freeVariable, 0);
|
---|
[13995] | 291 | var step = (trainingMax - trainingMin) / drawingSteps;
|
---|
| 292 | var minimum = chart.ChartAreas[0].AxisX.Minimum;
|
---|
| 293 | var maximum = chart.ChartAreas[0].AxisX.Maximum;
|
---|
[14006] | 294 | if (defaultValue <= minimum)
|
---|
[13995] | 295 | VerticalLineAnnotation.X = minimum + step;
|
---|
[14006] | 296 | else if (defaultValue >= maximum)
|
---|
[13995] | 297 | VerticalLineAnnotation.X = maximum - step;
|
---|
| 298 | else
|
---|
| 299 | VerticalLineAnnotation.X = defaultValue;
|
---|
| 300 |
|
---|
[13853] | 301 | if (ShowCursor)
|
---|
| 302 | chart.ChartAreas[0].AxisX.Title = FreeVariable + " : " + defaultValue.ToString("N3", CultureInfo.CurrentCulture);
|
---|
[13842] | 303 |
|
---|
| 304 | // Update series
|
---|
| 305 | var cancellationToken = cancelCurrentRecalculateSource.Token;
|
---|
| 306 | try {
|
---|
[13843] | 307 | var limits = await UpdateAllSeriesDataAsync(cancellationToken);
|
---|
| 308 | //cancellationToken.ThrowIfCancellationRequested();
|
---|
[13842] | 309 |
|
---|
[13843] | 310 | yMin = limits.Lower;
|
---|
| 311 | yMax = limits.Upper;
|
---|
[13842] | 312 | // Set y-axis
|
---|
[13843] | 313 | if (resetYAxis)
|
---|
| 314 | SetupAxis(chart.ChartAreas[0].AxisY, yMin, yMax, YAxisTicks, FixedYAxisMin, FixedYAxisMax);
|
---|
[13842] | 315 |
|
---|
| 316 | UpdateOutOfTrainingRangeStripLines();
|
---|
| 317 |
|
---|
[13853] | 318 | calculationPendingTimer.Stop();
|
---|
| 319 | calculationPendingLabel.Visible = false;
|
---|
[13843] | 320 | if (updateOnFinish)
|
---|
| 321 | Update();
|
---|
[13842] | 322 | }
|
---|
| 323 | catch (OperationCanceledException) { }
|
---|
| 324 | catch (AggregateException ae) {
|
---|
| 325 | if (!ae.InnerExceptions.Any(e => e is OperationCanceledException))
|
---|
| 326 | throw;
|
---|
| 327 | }
|
---|
[13831] | 328 | }
|
---|
| 329 |
|
---|
[13843] | 330 | private void SetupAxis(Axis axis, double minValue, double maxValue, int ticks, double? fixedAxisMin, double? fixedAxisMax) {
|
---|
[13842] | 331 | double axisMin, axisMax, axisInterval;
|
---|
| 332 | ChartUtil.CalculateAxisInterval(minValue, maxValue, ticks, out axisMin, out axisMax, out axisInterval);
|
---|
| 333 | axis.Minimum = fixedAxisMin ?? axisMin;
|
---|
| 334 | axis.Maximum = fixedAxisMax ?? axisMax;
|
---|
[13843] | 335 | axis.Interval = (axis.Maximum - axis.Minimum) / ticks;
|
---|
| 336 |
|
---|
[13845] | 337 | try {
|
---|
| 338 | chart.ChartAreas[0].RecalculateAxesScale();
|
---|
| 339 | }
|
---|
| 340 | catch (InvalidOperationException) {
|
---|
| 341 | // Can occur if eg. axis min == axis max
|
---|
| 342 | }
|
---|
[13842] | 343 | }
|
---|
| 344 |
|
---|
| 345 | private void RecalculateTrainingLimits(bool initializeAxisRanges) {
|
---|
| 346 | trainingMin = solutions.Select(s => s.ProblemData.Dataset.GetDoubleValues(freeVariable, s.ProblemData.TrainingIndices).Min()).Max();
|
---|
| 347 | trainingMax = solutions.Select(s => s.ProblemData.Dataset.GetDoubleValues(freeVariable, s.ProblemData.TrainingIndices).Max()).Min();
|
---|
| 348 |
|
---|
| 349 | if (initializeAxisRanges) {
|
---|
| 350 | double xmin, xmax, xinterval;
|
---|
| 351 | ChartUtil.CalculateAxisInterval(trainingMin, trainingMax, XAxisTicks, out xmin, out xmax, out xinterval);
|
---|
| 352 | FixedXAxisMin = xmin;
|
---|
| 353 | FixedXAxisMax = xmax;
|
---|
| 354 | }
|
---|
| 355 | }
|
---|
| 356 |
|
---|
[13831] | 357 | private void RecalculateInternalDataset() {
|
---|
[13843] | 358 | if (sharedFixedVariables == null)
|
---|
| 359 | return;
|
---|
| 360 |
|
---|
[13831] | 361 | // we expand the range in order to get nice tick intervals on the x axis
|
---|
[13829] | 362 | double xmin, xmax, xinterval;
|
---|
[13831] | 363 | ChartUtil.CalculateAxisInterval(trainingMin, trainingMax, XAxisTicks, out xmin, out xmax, out xinterval);
|
---|
[13842] | 364 |
|
---|
| 365 | if (FixedXAxisMin.HasValue) xmin = FixedXAxisMin.Value;
|
---|
| 366 | if (FixedXAxisMax.HasValue) xmax = FixedXAxisMax.Value;
|
---|
[13831] | 367 | double step = (xmax - xmin) / drawingSteps;
|
---|
| 368 |
|
---|
[13829] | 369 | var xvalues = new List<double>();
|
---|
[13831] | 370 | for (int i = 0; i < drawingSteps; i++)
|
---|
| 371 | xvalues.Add(xmin + i * step);
|
---|
| 372 |
|
---|
| 373 | var variables = sharedFixedVariables.DoubleVariables.ToList();
|
---|
| 374 | internalDataset = new ModifiableDataset(variables,
|
---|
| 375 | variables.Select(x => x == FreeVariable
|
---|
| 376 | ? xvalues
|
---|
| 377 | : Enumerable.Repeat(sharedFixedVariables.GetDoubleValue(x, 0), xvalues.Count).ToList()
|
---|
| 378 | )
|
---|
| 379 | );
|
---|
[13808] | 380 | }
|
---|
| 381 |
|
---|
[13842] | 382 | private Tuple<Series, Series> CreateSeries(IRegressionSolution solution) {
|
---|
| 383 | var series = new Series {
|
---|
| 384 | ChartType = SeriesChartType.Line,
|
---|
| 385 | Name = solution.ProblemData.TargetVariable + " " + solutions.IndexOf(solution)
|
---|
| 386 | };
|
---|
| 387 | series.LegendText = series.Name;
|
---|
[13837] | 388 |
|
---|
[13842] | 389 | var confidenceBoundSolution = solution as IConfidenceBoundRegressionSolution;
|
---|
| 390 | Series confidenceIntervalSeries = null;
|
---|
| 391 | if (confidenceBoundSolution != null) {
|
---|
| 392 | confidenceIntervalSeries = new Series {
|
---|
| 393 | ChartType = SeriesChartType.Range,
|
---|
| 394 | YValuesPerPoint = 2,
|
---|
| 395 | Name = "95% Conf. Interval " + series.Name,
|
---|
| 396 | IsVisibleInLegend = false
|
---|
| 397 | };
|
---|
[13836] | 398 | }
|
---|
[13842] | 399 | return Tuple.Create(series, confidenceIntervalSeries);
|
---|
| 400 | }
|
---|
[13836] | 401 |
|
---|
[13842] | 402 | private void OrderAndColorSeries() {
|
---|
[13836] | 403 | chart.SuspendRepaint();
|
---|
[13842] | 404 |
|
---|
[13836] | 405 | chart.Series.Clear();
|
---|
| 406 | // Add mean series for applying palette colors
|
---|
[13842] | 407 | foreach (var solution in solutions) {
|
---|
| 408 | chart.Series.Add(seriesCache[solution]);
|
---|
[13780] | 409 | }
|
---|
[13842] | 410 |
|
---|
[13836] | 411 | chart.Palette = ChartColorPalette.BrightPastel;
|
---|
| 412 | chart.ApplyPaletteColors();
|
---|
| 413 | chart.Palette = ChartColorPalette.None;
|
---|
| 414 |
|
---|
[13842] | 415 | // Add confidence interval series before its coresponding series for correct z index
|
---|
| 416 | foreach (var solution in solutions) {
|
---|
| 417 | Series ciSeries;
|
---|
| 418 | if (ciSeriesCache.TryGetValue(solution, out ciSeries)) {
|
---|
| 419 | var series = seriesCache[solution];
|
---|
[13995] | 420 | ciSeries.Color = Color.FromArgb(40, series.Color);
|
---|
[13842] | 421 | int idx = chart.Series.IndexOf(seriesCache[solution]);
|
---|
| 422 | chart.Series.Insert(idx, ciSeries);
|
---|
| 423 | }
|
---|
[13836] | 424 | }
|
---|
[13842] | 425 |
|
---|
[13836] | 426 | chart.ResumeRepaint(true);
|
---|
[13842] | 427 | }
|
---|
[13836] | 428 |
|
---|
[13843] | 429 | private async Task<DoubleLimit> UpdateAllSeriesDataAsync(CancellationToken cancellationToken) {
|
---|
| 430 | var updateTasks = solutions.Select(solution => UpdateSeriesDataAsync(solution, cancellationToken));
|
---|
| 431 |
|
---|
| 432 | double min = double.MaxValue, max = double.MinValue;
|
---|
| 433 | foreach (var update in updateTasks) {
|
---|
| 434 | var limit = await update;
|
---|
| 435 | if (limit.Lower < min) min = limit.Lower;
|
---|
| 436 | if (limit.Upper > max) max = limit.Upper;
|
---|
| 437 | }
|
---|
| 438 |
|
---|
| 439 | return new DoubleLimit(min, max);
|
---|
| 440 | }
|
---|
| 441 |
|
---|
| 442 | private Task<DoubleLimit> UpdateSeriesDataAsync(IRegressionSolution solution, CancellationToken cancellationToken) {
|
---|
[13842] | 443 | return Task.Run(() => {
|
---|
[13843] | 444 | var xvalues = internalDataset.GetDoubleValues(FreeVariable).ToList();
|
---|
| 445 | var yvalues = solution.Model.GetEstimatedValues(internalDataset, Enumerable.Range(0, internalDataset.Rows)).ToList();
|
---|
[13836] | 446 |
|
---|
[13843] | 447 | double min = double.MaxValue, max = double.MinValue;
|
---|
[13820] | 448 |
|
---|
[13843] | 449 | var series = seriesCache[solution];
|
---|
| 450 | for (int i = 0; i < xvalues.Count; i++) {
|
---|
| 451 | series.Points[i].SetValueXY(xvalues[i], yvalues[i]);
|
---|
| 452 | if (yvalues[i] < min) min = yvalues[i];
|
---|
| 453 | if (yvalues[i] > max) max = yvalues[i];
|
---|
| 454 | }
|
---|
[13820] | 455 |
|
---|
[13843] | 456 | var confidenceBoundSolution = solution as IConfidenceBoundRegressionSolution;
|
---|
| 457 | if (confidenceBoundSolution != null) {
|
---|
| 458 | var confidenceIntervalSeries = ciSeriesCache[solution];
|
---|
| 459 |
|
---|
| 460 | cancellationToken.ThrowIfCancellationRequested();
|
---|
| 461 | var variances =
|
---|
| 462 | confidenceBoundSolution.Model.GetEstimatedVariances(internalDataset,
|
---|
| 463 | Enumerable.Range(0, internalDataset.Rows)).ToList();
|
---|
| 464 | for (int i = 0; i < xvalues.Count; i++) {
|
---|
| 465 | var lower = yvalues[i] - 1.96 * Math.Sqrt(variances[i]);
|
---|
| 466 | var upper = yvalues[i] + 1.96 * Math.Sqrt(variances[i]);
|
---|
| 467 | confidenceIntervalSeries.Points[i].SetValueXY(xvalues[i], lower, upper);
|
---|
| 468 | if (lower < min) min = lower;
|
---|
| 469 | if (upper > max) max = upper;
|
---|
[13842] | 470 | }
|
---|
[13843] | 471 | }
|
---|
| 472 |
|
---|
| 473 | cancellationToken.ThrowIfCancellationRequested();
|
---|
| 474 | return new DoubleLimit(min, max);
|
---|
[13842] | 475 | }, cancellationToken);
|
---|
| 476 | }
|
---|
[13840] | 477 |
|
---|
[13842] | 478 | private void ResizeAllSeriesData() {
|
---|
[13843] | 479 | if (internalDataset == null)
|
---|
| 480 | return;
|
---|
| 481 |
|
---|
[13842] | 482 | var xvalues = internalDataset.GetDoubleValues(FreeVariable).ToList();
|
---|
| 483 | foreach (var solution in solutions)
|
---|
| 484 | ResizeSeriesData(solution, xvalues);
|
---|
[13780] | 485 | }
|
---|
[13842] | 486 | private void ResizeSeriesData(IRegressionSolution solution, IList<double> xvalues = null) {
|
---|
| 487 | if (xvalues == null)
|
---|
| 488 | xvalues = internalDataset.GetDoubleValues(FreeVariable).ToList();
|
---|
[13780] | 489 |
|
---|
[13842] | 490 | var series = seriesCache[solution];
|
---|
| 491 | series.Points.SuspendUpdates();
|
---|
[13853] | 492 | series.Points.Clear();
|
---|
[13842] | 493 | for (int i = 0; i < xvalues.Count; i++)
|
---|
| 494 | series.Points.Add(new DataPoint(xvalues[i], 0.0));
|
---|
| 495 | series.Points.ResumeUpdates();
|
---|
[13780] | 496 |
|
---|
[13842] | 497 | Series confidenceIntervalSeries;
|
---|
| 498 | if (ciSeriesCache.TryGetValue(solution, out confidenceIntervalSeries)) {
|
---|
| 499 | confidenceIntervalSeries.Points.SuspendUpdates();
|
---|
[13853] | 500 | confidenceIntervalSeries.Points.Clear();
|
---|
[13842] | 501 | for (int i = 0; i < xvalues.Count; i++)
|
---|
| 502 | confidenceIntervalSeries.Points.Add(new DataPoint(xvalues[i], new[] { -1.0, 1.0 }));
|
---|
| 503 | confidenceIntervalSeries.Points.ResumeUpdates();
|
---|
| 504 | }
|
---|
[13780] | 505 | }
|
---|
| 506 |
|
---|
[13842] | 507 | public async Task AddSolutionAsync(IRegressionSolution solution) {
|
---|
[13831] | 508 | if (!SolutionsCompatible(solutions.Concat(new[] { solution })))
|
---|
| 509 | throw new ArgumentException("The solution is not compatible with the problem data.");
|
---|
[13842] | 510 | if (solutions.Contains(solution))
|
---|
| 511 | return;
|
---|
| 512 |
|
---|
[13831] | 513 | solutions.Add(solution);
|
---|
[13842] | 514 | RecalculateTrainingLimits(true);
|
---|
| 515 |
|
---|
| 516 | var series = CreateSeries(solution);
|
---|
| 517 | seriesCache.Add(solution, series.Item1);
|
---|
| 518 | if (series.Item2 != null)
|
---|
| 519 | ciSeriesCache.Add(solution, series.Item2);
|
---|
| 520 |
|
---|
| 521 | ResizeSeriesData(solution);
|
---|
| 522 | OrderAndColorSeries();
|
---|
| 523 |
|
---|
| 524 | await RecalculateAsync();
|
---|
[13995] | 525 | var args = new EventArgs<IRegressionSolution>(solution);
|
---|
| 526 | OnSolutionAdded(this, args);
|
---|
[13780] | 527 | }
|
---|
[13995] | 528 |
|
---|
[13842] | 529 | public async Task RemoveSolutionAsync(IRegressionSolution solution) {
|
---|
| 530 | if (!solutions.Remove(solution))
|
---|
| 531 | return;
|
---|
| 532 |
|
---|
| 533 | RecalculateTrainingLimits(true);
|
---|
| 534 |
|
---|
| 535 | seriesCache.Remove(solution);
|
---|
| 536 | ciSeriesCache.Remove(solution);
|
---|
| 537 |
|
---|
| 538 | await RecalculateAsync();
|
---|
[13995] | 539 | var args = new EventArgs<IRegressionSolution>(solution);
|
---|
| 540 | OnSolutionRemoved(this, args);
|
---|
[13831] | 541 | }
|
---|
[13780] | 542 |
|
---|
[13831] | 543 | private static bool SolutionsCompatible(IEnumerable<IRegressionSolution> solutions) {
|
---|
| 544 | foreach (var solution1 in solutions) {
|
---|
| 545 | var variables1 = solution1.ProblemData.Dataset.DoubleVariables;
|
---|
| 546 | foreach (var solution2 in solutions) {
|
---|
| 547 | if (solution1 == solution2)
|
---|
| 548 | continue;
|
---|
| 549 | var variables2 = solution2.ProblemData.Dataset.DoubleVariables;
|
---|
| 550 | if (!variables1.All(variables2.Contains))
|
---|
| 551 | return false;
|
---|
| 552 | }
|
---|
| 553 | }
|
---|
| 554 | return true;
|
---|
[13780] | 555 | }
|
---|
| 556 |
|
---|
[13842] | 557 | private void UpdateOutOfTrainingRangeStripLines() {
|
---|
[13831] | 558 | var axisX = chart.ChartAreas[0].AxisX;
|
---|
| 559 | var lowerStripLine = axisX.StripLines[0];
|
---|
| 560 | var upperStripLine = axisX.StripLines[1];
|
---|
| 561 |
|
---|
| 562 | lowerStripLine.IntervalOffset = axisX.Minimum;
|
---|
[14006] | 563 | lowerStripLine.StripWidth = Math.Abs(trainingMin - axisX.Minimum);
|
---|
[13831] | 564 |
|
---|
| 565 | upperStripLine.IntervalOffset = trainingMax;
|
---|
[14006] | 566 | upperStripLine.StripWidth = Math.Abs(axisX.Maximum - trainingMax);
|
---|
[13780] | 567 | }
|
---|
| 568 |
|
---|
[13842] | 569 | #region Events
|
---|
[13995] | 570 | public event EventHandler<EventArgs<IRegressionSolution>> SolutionAdded;
|
---|
| 571 | public void OnSolutionAdded(object sender, EventArgs<IRegressionSolution> args) {
|
---|
| 572 | var added = SolutionAdded;
|
---|
| 573 | if (added == null) return;
|
---|
| 574 | added(sender, args);
|
---|
| 575 | }
|
---|
| 576 |
|
---|
| 577 | public event EventHandler<EventArgs<IRegressionSolution>> SolutionRemoved;
|
---|
| 578 | public void OnSolutionRemoved(object sender, EventArgs<IRegressionSolution> args) {
|
---|
| 579 | var removed = SolutionRemoved;
|
---|
| 580 | if (removed == null) return;
|
---|
| 581 | removed(sender, args);
|
---|
| 582 | }
|
---|
| 583 |
|
---|
[13817] | 584 | public event EventHandler VariableValueChanged;
|
---|
| 585 | public void OnVariableValueChanged(object sender, EventArgs args) {
|
---|
| 586 | var changed = VariableValueChanged;
|
---|
| 587 | if (changed == null) return;
|
---|
| 588 | changed(sender, args);
|
---|
| 589 | }
|
---|
| 590 |
|
---|
[14089] | 591 | public event EventHandler ZoomChanged;
|
---|
| 592 | public void OnZoomChanged(object sender, EventArgs args) {
|
---|
| 593 | var changed = ZoomChanged;
|
---|
| 594 | if (changed == null) return;
|
---|
| 595 | changed(sender, args);
|
---|
| 596 | }
|
---|
| 597 |
|
---|
[13842] | 598 | private void sharedFixedVariables_ItemChanged(object o, EventArgs<int, int> e) {
|
---|
| 599 | if (o != sharedFixedVariables) return;
|
---|
| 600 | var variables = sharedFixedVariables.DoubleVariables.ToList();
|
---|
| 601 | var rowIndex = e.Value;
|
---|
| 602 | var columnIndex = e.Value2;
|
---|
[13831] | 603 |
|
---|
[13842] | 604 | var variableName = variables[columnIndex];
|
---|
| 605 | if (variableName == FreeVariable) return;
|
---|
| 606 | var v = sharedFixedVariables.GetDoubleValue(variableName, rowIndex);
|
---|
| 607 | var values = new List<double>(Enumerable.Repeat(v, DrawingSteps));
|
---|
| 608 | internalDataset.ReplaceVariable(variableName, values);
|
---|
[13780] | 609 | }
|
---|
| 610 |
|
---|
[13818] | 611 | private void chart_AnnotationPositionChanging(object sender, AnnotationPositionChangingEventArgs e) {
|
---|
[13840] | 612 | var step = (trainingMax - trainingMin) / drawingSteps;
|
---|
[13846] | 613 | double newLocation = step * (long)Math.Round(e.NewLocationX / step);
|
---|
[13840] | 614 | var axisX = chart.ChartAreas[0].AxisX;
|
---|
[13995] | 615 | if (newLocation >= axisX.Maximum)
|
---|
| 616 | newLocation = axisX.Maximum - step;
|
---|
| 617 | if (newLocation <= axisX.Minimum)
|
---|
| 618 | newLocation = axisX.Minimum + step;
|
---|
[13831] | 619 |
|
---|
[13846] | 620 | e.NewLocationX = newLocation;
|
---|
[13831] | 621 | var annotation = VerticalLineAnnotation;
|
---|
| 622 | var x = annotation.X;
|
---|
| 623 | sharedFixedVariables.SetVariableValue(x, FreeVariable, 0);
|
---|
| 624 |
|
---|
[13853] | 625 | if (ShowCursor) {
|
---|
| 626 | chart.ChartAreas[0].AxisX.Title = FreeVariable + " : " + x.ToString("N3", CultureInfo.CurrentCulture);
|
---|
| 627 | chart.Update();
|
---|
| 628 | }
|
---|
[13831] | 629 |
|
---|
[13853] | 630 | OnVariableValueChanged(this, EventArgs.Empty);
|
---|
[13818] | 631 | }
|
---|
| 632 |
|
---|
[13780] | 633 | private void chart_MouseMove(object sender, MouseEventArgs e) {
|
---|
[13842] | 634 | bool hitCursor = chart.HitTest(e.X, e.Y).ChartElementType == ChartElementType.Annotation;
|
---|
| 635 | chart.Cursor = hitCursor ? Cursors.VSplit : Cursors.Default;
|
---|
[13780] | 636 | }
|
---|
| 637 |
|
---|
[13842] | 638 | private void chart_DragDrop(object sender, DragEventArgs e) {
|
---|
[13780] | 639 | var data = e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat);
|
---|
| 640 | if (data != null) {
|
---|
| 641 | var solution = data as IRegressionSolution;
|
---|
[13842] | 642 | if (!solutions.Contains(solution))
|
---|
| 643 | AddSolutionAsync(solution);
|
---|
[13780] | 644 | }
|
---|
| 645 | }
|
---|
[13842] | 646 | private void chart_DragEnter(object sender, DragEventArgs e) {
|
---|
[13780] | 647 | if (!e.Data.GetDataPresent(HeuristicLab.Common.Constants.DragDropDataFormat)) return;
|
---|
| 648 | e.Effect = DragDropEffects.None;
|
---|
| 649 |
|
---|
| 650 | var data = e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat);
|
---|
| 651 | var regressionSolution = data as IRegressionSolution;
|
---|
| 652 | if (regressionSolution != null) {
|
---|
| 653 | e.Effect = DragDropEffects.Copy;
|
---|
| 654 | }
|
---|
| 655 | }
|
---|
[13853] | 656 |
|
---|
| 657 | private void calculationPendingTimer_Tick(object sender, EventArgs e) {
|
---|
| 658 | calculationPendingLabel.Visible = true;
|
---|
| 659 | Update();
|
---|
| 660 | }
|
---|
| 661 |
|
---|
[13855] | 662 | private void config_Click(object sender, EventArgs e) {
|
---|
[13853] | 663 | configurationDialog.ShowDialog(this);
|
---|
| 664 | }
|
---|
[14089] | 665 |
|
---|
| 666 | private void chart_SelectionRangeChanged(object sender, CursorEventArgs e) {
|
---|
| 667 | OnZoomChanged(this, EventArgs.Empty);
|
---|
| 668 | }
|
---|
[13817] | 669 | #endregion
|
---|
[13780] | 670 | }
|
---|
| 671 | }
|
---|