Changeset 13843
- Timestamp:
- 05/11/16 16:38:23 (9 years ago)
- Location:
- branches/HeuristicLab.RegressionSolutionGradientView/HeuristicLab.Problems.DataAnalysis.Views/3.4
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/HeuristicLab.RegressionSolutionGradientView/HeuristicLab.Problems.DataAnalysis.Views/3.4/GradientChart.cs
r13842 r13843 65 65 public int XAxisTicks { 66 66 get { return xAxisTicks; } 67 set { xAxisTicks = value; } 67 set { 68 if (value != xAxisTicks) { 69 xAxisTicks = value; 70 SetupAxis(chart.ChartAreas[0].AxisX, trainingMin, trainingMax, XAxisTicks, FixedXAxisMin, FixedXAxisMax); 71 RecalculateInternalDataset(); 72 } 73 } 68 74 } 69 75 private double? fixedXAxisMin; … … 73 79 if ((value.HasValue && fixedXAxisMin.HasValue && !value.Value.IsAlmost(fixedXAxisMin.Value)) || (value.HasValue != fixedXAxisMin.HasValue)) { 74 80 fixedXAxisMin = value; 81 SetupAxis(chart.ChartAreas[0].AxisX, trainingMin, trainingMax, XAxisTicks, FixedXAxisMin, FixedXAxisMax); 75 82 RecalculateInternalDataset(); 76 83 } … … 83 90 if ((value.HasValue && fixedXAxisMax.HasValue && !value.Value.IsAlmost(fixedXAxisMax.Value)) || (value.HasValue != fixedXAxisMax.HasValue)) { 84 91 fixedXAxisMax = value; 92 SetupAxis(chart.ChartAreas[0].AxisX, trainingMin, trainingMax, XAxisTicks, FixedXAxisMin, FixedXAxisMax); 85 93 RecalculateInternalDataset(); 86 94 } … … 91 99 public int YAxisTicks { 92 100 get { return yAxisTicks; } 93 set { yAxisTicks = value; } 101 set { 102 if (value != yAxisTicks) { 103 yAxisTicks = value; 104 SetupAxis(chart.ChartAreas[0].AxisY, yMin, yMax, YAxisTicks, FixedYAxisMin, FixedYAxisMax); 105 RecalculateInternalDataset(); 106 } 107 } 94 108 } 95 109 private double? fixedYAxisMin; … … 99 113 if ((value.HasValue && fixedYAxisMin.HasValue && !value.Value.IsAlmost(fixedYAxisMin.Value)) || (value.HasValue != fixedYAxisMin.HasValue)) { 100 114 fixedYAxisMin = value; 115 SetupAxis(chart.ChartAreas[0].AxisY, yMin, yMax, YAxisTicks, FixedYAxisMin, FixedYAxisMax); 101 116 } 102 117 } … … 108 123 if ((value.HasValue && fixedYAxisMax.HasValue && !value.Value.IsAlmost(fixedYAxisMax.Value)) || (value.HasValue != fixedYAxisMax.HasValue)) { 109 124 fixedYAxisMax = value; 110 } 111 } 112 } 113 114 private double trainingMin = double.MinValue; 115 public double TrainingMin { 116 get { return trainingMin; } 117 set { trainingMin = value; } 118 } 119 private double trainingMax = double.MaxValue; 120 public double TrainingMax { 121 get { return trainingMax; } 122 set { trainingMax = value; } 123 } 125 SetupAxis(chart.ChartAreas[0].AxisY, yMin, yMax, YAxisTicks, FixedYAxisMin, FixedYAxisMax); 126 } 127 } 128 } 129 130 private double trainingMin = 1; 131 private double trainingMax = -1; 124 132 125 133 private int drawingSteps = 1000; … … 146 154 RecalculateInternalDataset(); 147 155 } 156 } 157 158 private double yMin; 159 public double YMin { 160 get { return yMin; } 161 } 162 private double yMax; 163 public double YMax { 164 get { return yMax; } 148 165 } 149 166 … … 173 190 } 174 191 private void GradientChart_Disposed(object sender, EventArgs e) { 175 if (cancelCurrentRecalculateSource != null) { 176 if (cancelCurrentRecalculateSource.IsCancellationRequested) 177 cancelCurrentRecalculateSource.Cancel(); 178 } 192 if (cancelCurrentRecalculateSource != null) 193 cancelCurrentRecalculateSource.Cancel(); 179 194 } 180 195 … … 212 227 } 213 228 214 public async Task RecalculateAsync( ) {229 public async Task RecalculateAsync(bool updateOnFinish = true, bool resetYAxis = true) { 215 230 if (IsDisposed 216 231 || sharedFixedVariables == null || !solutions.Any() || string.IsNullOrEmpty(freeVariable) … … 230 245 VerticalLineAnnotation.X = defaultValue; 231 246 chart.ChartAreas[0].AxisX.Title = FreeVariable + " : " + defaultValue.ToString("N3", CultureInfo.CurrentCulture); 232 SetupAxis(chart.ChartAreas[0].AxisX, trainingMin, trainingMax, XAxisTicks, fixedXAxisMin, fixedXAxisMax);233 247 234 248 // Update series 235 249 var cancellationToken = cancelCurrentRecalculateSource.Token; 236 250 try { 237 await UpdateSeriesData(cancellationToken); 238 chart.Update(); 239 251 var limits = await UpdateAllSeriesDataAsync(cancellationToken); 252 //cancellationToken.ThrowIfCancellationRequested(); 253 254 yMin = limits.Lower; 255 yMax = limits.Upper; 240 256 // Set y-axis 241 double ymin = 0, ymax = 0; 242 foreach (var vs in chart.Series.SelectMany(series => series.Points.Select(s => s.YValues))) { 243 for (int i = 0; i < vs.Length; i++) { 244 var v = vs[i]; 245 if (ymin > v) ymin = v; 246 if (ymax < v) ymax = v; 247 } 248 } 249 SetupAxis(chart.ChartAreas[0].AxisY, ymin, ymax, YAxisTicks, FixedYAxisMin, FixedYAxisMax); 250 chart.ChartAreas[0].RecalculateAxesScale(); 257 if (resetYAxis) 258 SetupAxis(chart.ChartAreas[0].AxisY, yMin, yMax, YAxisTicks, FixedYAxisMin, FixedYAxisMax); 251 259 252 260 UpdateOutOfTrainingRangeStripLines(); 253 261 254 262 statusLabel.Visible = false; 255 Update(); // immediately show 263 if (updateOnFinish) 264 Update(); 256 265 } 257 266 catch (OperationCanceledException) { } … … 262 271 } 263 272 264 private staticvoid SetupAxis(Axis axis, double minValue, double maxValue, int ticks, double? fixedAxisMin, double? fixedAxisMax) {273 private void SetupAxis(Axis axis, double minValue, double maxValue, int ticks, double? fixedAxisMin, double? fixedAxisMax) { 265 274 double axisMin, axisMax, axisInterval; 266 275 ChartUtil.CalculateAxisInterval(minValue, maxValue, ticks, out axisMin, out axisMax, out axisInterval); 267 276 axis.Minimum = fixedAxisMin ?? axisMin; 268 277 axis.Maximum = fixedAxisMax ?? axisMax; 269 axis.Interval = (axisMax - axisMin) / ticks; 278 axis.Interval = (axis.Maximum - axis.Minimum) / ticks; 279 280 chart.ChartAreas[0].RecalculateAxesScale(); 270 281 } 271 282 … … 283 294 284 295 private void RecalculateInternalDataset() { 296 if (sharedFixedVariables == null) 297 return; 298 285 299 // we expand the range in order to get nice tick intervals on the x axis 286 300 double xmin, xmax, xinterval; … … 351 365 } 352 366 353 private Task UpdateSeriesData(CancellationToken cancellationToken) { 367 private async Task<DoubleLimit> UpdateAllSeriesDataAsync(CancellationToken cancellationToken) { 368 var updateTasks = solutions.Select(solution => UpdateSeriesDataAsync(solution, cancellationToken)); 369 370 double min = double.MaxValue, max = double.MinValue; 371 foreach (var update in updateTasks) { 372 var limit = await update; 373 if (limit.Lower < min) min = limit.Lower; 374 if (limit.Upper > max) max = limit.Upper; 375 } 376 377 return new DoubleLimit(min, max); 378 } 379 380 private Task<DoubleLimit> UpdateSeriesDataAsync(IRegressionSolution solution, CancellationToken cancellationToken) { 354 381 return Task.Run(() => { 355 Parallel.ForEach(solutions, new ParallelOptions { CancellationToken = cancellationToken }, solution => { 356 var xvalues = internalDataset.GetDoubleValues(FreeVariable).ToList(); 357 var yvalues = solution.Model.GetEstimatedValues(internalDataset, Enumerable.Range(0, internalDataset.Rows)).ToList(); 358 359 var series = seriesCache[solution]; 360 for (int i = 0; i < xvalues.Count; i++) 361 series.Points[i].SetValueXY(xvalues[i], yvalues[i]); 362 363 var confidenceBoundSolution = solution as IConfidenceBoundRegressionSolution; 364 if (confidenceBoundSolution != null) { 365 var confidenceIntervalSeries = ciSeriesCache[solution]; 366 367 cancellationToken.ThrowIfCancellationRequested(); 368 var variances = 369 confidenceBoundSolution.Model.GetEstimatedVariances(internalDataset, 370 Enumerable.Range(0, internalDataset.Rows)).ToList(); 371 for (int i = 0; i < xvalues.Count; i++) { 372 var lower = yvalues[i] - 1.96 * Math.Sqrt(variances[i]); 373 var upper = yvalues[i] + 1.96 * Math.Sqrt(variances[i]); 374 confidenceIntervalSeries.Points[i].SetValueXY(xvalues[i], lower, upper); 375 } 382 var xvalues = internalDataset.GetDoubleValues(FreeVariable).ToList(); 383 var yvalues = solution.Model.GetEstimatedValues(internalDataset, Enumerable.Range(0, internalDataset.Rows)).ToList(); 384 385 double min = double.MaxValue, max = double.MinValue; 386 387 var series = seriesCache[solution]; 388 for (int i = 0; i < xvalues.Count; i++) { 389 series.Points[i].SetValueXY(xvalues[i], yvalues[i]); 390 if (yvalues[i] < min) min = yvalues[i]; 391 if (yvalues[i] > max) max = yvalues[i]; 392 } 393 394 var confidenceBoundSolution = solution as IConfidenceBoundRegressionSolution; 395 if (confidenceBoundSolution != null) { 396 var confidenceIntervalSeries = ciSeriesCache[solution]; 397 398 cancellationToken.ThrowIfCancellationRequested(); 399 var variances = 400 confidenceBoundSolution.Model.GetEstimatedVariances(internalDataset, 401 Enumerable.Range(0, internalDataset.Rows)).ToList(); 402 for (int i = 0; i < xvalues.Count; i++) { 403 var lower = yvalues[i] - 1.96 * Math.Sqrt(variances[i]); 404 var upper = yvalues[i] + 1.96 * Math.Sqrt(variances[i]); 405 confidenceIntervalSeries.Points[i].SetValueXY(xvalues[i], lower, upper); 406 if (lower < min) min = lower; 407 if (upper > max) max = upper; 376 408 } 377 cancellationToken.ThrowIfCancellationRequested(); 378 }); 409 } 410 411 cancellationToken.ThrowIfCancellationRequested(); 412 return new DoubleLimit(min, max); 379 413 }, cancellationToken); 380 414 } 381 415 382 416 private void ResizeAllSeriesData() { 417 if (internalDataset == null) 418 return; 419 383 420 var xvalues = internalDataset.GetDoubleValues(FreeVariable).ToList(); 384 421 foreach (var solution in solutions) -
branches/HeuristicLab.RegressionSolutionGradientView/HeuristicLab.Problems.DataAnalysis.Views/3.4/RegressionSolutionGradientView.Designer.cs
r13842 r13843 82 82 this.gradientChart.ShowCursor = false; 83 83 this.gradientChart.ShowLegend = true; 84 this.gradientChart.ShowXAxisLabel = true;84 this.gradientChart.ShowXAxisLabel = false; 85 85 this.gradientChart.ShowYAxisLabel = true; 86 86 this.gradientChart.Size = new System.Drawing.Size(715, 376); 87 87 this.gradientChart.TabIndex = 0; 88 this.gradientChart.TrainingMax = 1.7976931348623157E+308D;89 this.gradientChart.TrainingMin = -1.7976931348623157E+308D;90 88 this.gradientChart.XAxisTicks = 10; 91 89 this.gradientChart.YAxisTicks = 5; -
branches/HeuristicLab.RegressionSolutionGradientView/HeuristicLab.Problems.DataAnalysis.Views/3.4/RegressionSolutionTargetResponseGradientView.cs
r13842 r13843 23 23 using System.Collections.Generic; 24 24 using System.Linq; 25 using System.Threading.Tasks; 25 26 using System.Windows.Forms; 26 27 using HeuristicLab.Common; 27 28 using HeuristicLab.MainForm; 29 using HeuristicLab.Visualization.ChartControlsExtensions; 28 30 29 31 namespace HeuristicLab.Problems.DataAnalysis.Views { … … 33 35 private readonly Dictionary<string, GradientChart> charts; 34 36 private const int Points = 200; 37 38 private IEnumerable<GradientChart> VisibleCharts { 39 get { return gradientChartTableLayout.Controls.OfType<GradientChart>(); } 40 } 35 41 36 42 public RegressionSolutionTargetResponseGradientView() { … … 72 78 ShowXAxisLabel = true, 73 79 ShowYAxisLabel = true, 80 YAxisTicks = 5, 74 81 }; 75 gradientChart.VariableValueChanged += (o, e) => { 76 foreach (var chart in gradientChartTableLayout.Controls.Cast<GradientChart>()) { 77 if (chart == (GradientChart)o) continue; 78 chart.RecalculateAsync(); 79 } 82 gradientChart.VariableValueChanged += async (o, e) => { 83 var recalculations = VisibleCharts.Except(new[] { (GradientChart)o }).Select(async chart => { 84 await chart.RecalculateAsync(updateOnFinish: false, resetYAxis: false); 85 }).ToList(); 86 await Task.WhenAll(recalculations); 87 88 if (recalculations.All(t => t.IsCompleted)) 89 SyncYAxis(); 80 90 }; 81 91 gradientChart.Configure(new[] { Content }, sharedFixedVariables, variableName, Points); … … 86 96 variableListView.Items.AddRange(variableNames.Select(x => new ListViewItem(x, 0)).ToArray()); 87 97 } 98 99 private void SyncYAxis() { 100 double min = double.MaxValue, max = double.MinValue; 101 foreach (var chart in VisibleCharts) { 102 if (chart.YMin < min) min = chart.YMin; 103 if (chart.YMax > max) max = chart.YMax; 104 } 105 double axisMin, axisMax, axisInterval; 106 ChartUtil.CalculateAxisInterval(min, max, 5, out axisMin, out axisMax, out axisInterval); 107 108 foreach (var chart in VisibleCharts) { 109 chart.FixedYAxisMin = axisMin; 110 chart.FixedYAxisMax = axisMax; 111 chart.Update(); 112 } 113 } 114 88 115 // sort chart controls so that they always appear in the same order as in the list view 89 116 // the gradient chart layout should be suspended before calling this method … … 115 142 tl.Controls.Remove(chart); 116 143 } 144 SyncYAxis(); 145 117 146 var count = tl.Controls.Count; 118 147 SortControls();
Note: See TracChangeset
for help on using the changeset viewer.