Changeset 14248 for branches/symbreg-factors-2650/HeuristicLab.Problems.DataAnalysis.Views/3.4/Regression
- Timestamp:
- 08/09/16 15:18:32 (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/symbreg-factors-2650/HeuristicLab.Problems.DataAnalysis.Views/3.4/Regression/RegressionSolutionTargetResponseGradientView.cs
r14158 r14248 21 21 22 22 using System; 23 using System.Collections; 23 24 using System.Collections.Generic; 24 25 using System.Drawing; … … 34 35 [Content(typeof(IRegressionSolution))] 35 36 public partial class RegressionSolutionTargetResponseGradientView : DataAnalysisSolutionEvaluationView { 36 private readonly Dictionary<string, GradientChart> gradientCharts;37 private readonly Dictionary<string, IGradientChart> gradientCharts; 37 38 private readonly Dictionary<string, DensityChart> densityCharts; 38 39 private readonly Dictionary<string, Panel> groupingPanels; … … 47 48 } 48 49 } 49 private IEnumerable< GradientChart> VisibleGradientCharts {50 private IEnumerable<IGradientChart> VisibleGradientCharts { 50 51 get { return VisibleVariables.Select(v => gradientCharts[v]); } 51 52 } … … 59 60 public RegressionSolutionTargetResponseGradientView() { 60 61 InitializeComponent(); 61 gradientCharts = new Dictionary<string, GradientChart>();62 gradientCharts = new Dictionary<string, IGradientChart>(); 62 63 densityCharts = new Dictionary<string, DensityChart>(); 63 64 groupingPanels = new Dictionary<string, Panel>(); … … 112 113 // create dataset 113 114 var allowedInputVariables = Content.ProblemData.AllowedInputVariables; 114 var variableValues = allowedInputVariables.Select(x => new List<double> { problemData.Dataset.GetDoubleValues(x, problemData.TrainingIndices).Median() }); 115 var sharedFixedVariables = new ModifiableDataset(allowedInputVariables, variableValues); 115 var doubleVariables = allowedInputVariables.Where(problemData.Dataset.VariableHasType<double>); 116 var doubleVariableValues = (IEnumerable<IList>)doubleVariables.Select(x => new List<double> { problemData.Dataset.GetDoubleValues(x, problemData.TrainingIndices).Median() }); 117 118 var factorVariables = allowedInputVariables.Where(problemData.Dataset.VariableHasType<string>); 119 var factorVariableValues = (IEnumerable<IList>)factorVariables.Select(x => new List<string> { 120 problemData.Dataset.GetStringValues(x, problemData.TrainingIndices) 121 .GroupBy(val => val).OrderByDescending(g => g.Count()).First().Key // most frequent value 122 }); 123 124 var sharedFixedVariables = new ModifiableDataset(doubleVariables.Concat(factorVariables), doubleVariableValues.Concat(factorVariableValues)); 125 116 126 117 127 // create controls … … 119 129 densityCharts.Clear(); 120 130 groupingPanels.Clear(); 121 foreach (var variableName in allowedInputVariables) {131 foreach (var variableName in doubleVariables) { 122 132 var gradientChart = CreateGradientChart(variableName, sharedFixedVariables); 123 133 gradientCharts.Add(variableName, gradientChart); … … 147 157 // Initially, the inner plot areas are not initialized for hidden charts (scollpanel, ...) 148 158 // This event handler listens for the paint event once (where everything is already initialized) to do some manual layouting. 149 gradientChart.ChartPostPaint += OnGradientChart OnChartPostPaint;159 gradientChart.ChartPostPaint += OnGradientChartPostPaint; 150 160 151 161 var panel = new Panel() { … … 159 169 groupingPanels.Add(variableName, panel); 160 170 } 161 171 foreach (var variableName in factorVariables) { 172 var gradientChart = CreateFactorGradientChart(variableName, sharedFixedVariables); 173 gradientCharts.Add(variableName, gradientChart); 174 175 var densityChart = new DensityChart() { 176 Anchor = AnchorStyles.Left | AnchorStyles.Top | AnchorStyles.Right, 177 Margin = Padding.Empty, 178 Height = 12, 179 Visible = false, 180 Top = (int)(gradientChart.Height * 0.1), 181 }; 182 densityCharts.Add(variableName, densityChart); 183 gradientChart.ZoomChanged += (o, e) => { 184 var gradient = (FactorGradientChart)o; 185 var density = densityCharts[gradient.FreeVariable]; 186 density.Visible = densityComboBox.SelectedIndex != 0 && !gradient.IsZoomed; 187 if (density.Visible) 188 UpdateDensityChart(density, gradient.FreeVariable); 189 }; 190 gradientChart.SizeChanged += (o, e) => { 191 var gradient = (FactorGradientChart)o; 192 var density = densityCharts[gradient.FreeVariable]; 193 density.Top = (int)(gradient.Height * 0.1); 194 }; 195 196 // Initially, the inner plot areas are not initialized for hidden charts (scollpanel, ...) 197 // This event handler listens for the paint event once (where everything is already initialized) to do some manual layouting. 198 gradientChart.ChartPostPaint += OnFactorGradientChartPostPaint; 199 200 var panel = new Panel() { 201 Dock = DockStyle.Fill, 202 Margin = Padding.Empty, 203 BackColor = Color.White 204 }; 205 206 panel.Controls.Add(densityChart); 207 panel.Controls.Add(gradientChart); 208 groupingPanels.Add(variableName, panel); 209 } 162 210 // update variable list 163 211 variableListView.ItemChecked -= variableListView_ItemChecked; … … 173 221 } 174 222 175 private void OnGradientChart OnChartPostPaint(object o, EventArgs e) {223 private void OnGradientChartPostPaint(object o, EventArgs e) { 176 224 var gradient = (GradientChart)o; 177 225 var density = densityCharts[gradient.FreeVariable]; … … 186 234 // removed after succesful layouting due to performance reasons 187 235 if (gcPlotPosition.Width != 0) 188 gradient.ChartPostPaint -= OnGradientChartOnChartPostPaint; 236 gradient.ChartPostPaint -= OnGradientChartPostPaint; 237 } 238 239 private void OnFactorGradientChartPostPaint(object o, EventArgs e) { 240 var gradient = (FactorGradientChart)o; 241 var density = densityCharts[gradient.FreeVariable]; 242 243 density.Width = gradient.Width; 244 245 var gcPlotPosition = gradient.InnerPlotPosition; 246 density.Left = (int)(gcPlotPosition.X / 100.0 * gradient.Width); 247 density.Width = (int)(gcPlotPosition.Width / 100.0 * gradient.Width); 248 gradient.UpdateTitlePosition(); 249 250 // removed after succesful layouting due to performance reasons 251 if (gcPlotPosition.Width != 0) 252 gradient.ChartPostPaint -= OnFactorGradientChartPostPaint; 189 253 } 190 254 … … 192 256 foreach (var variable in VisibleVariables) { 193 257 var gradientChart = gradientCharts[variable]; 194 await gradientChart.RecalculateAsync( );258 await gradientChart.RecalculateAsync(false, false); 195 259 } 196 260 gradientChartTableLayout.SuspendLayout(); … … 201 265 gradientChartTableLayout.Refresh(); 202 266 foreach (var variable in VisibleVariables) { 203 var densityChart = densityCharts[variable]; 204 UpdateDensityChart(densityChart, variable); 267 DensityChart densityChart; 268 if (densityCharts.TryGetValue(variable, out densityChart)) { 269 UpdateDensityChart(densityChart, variable); 270 } 205 271 } 206 272 } … … 215 281 }; 216 282 gradientChart.VariableValueChanged += async (o, e) => { 217 var recalculations = VisibleGradientCharts.Except(new[] { (GradientChart)o }).Select(async chart => { 218 await chart.RecalculateAsync(updateOnFinish: false, resetYAxis: false); 219 }).ToList(); 283 var recalculations = VisibleGradientCharts 284 .Except(new[] { (IGradientChart)o }) 285 .Select(async chart => { 286 await chart.RecalculateAsync(updateOnFinish: false, resetYAxis: false); 287 }).ToList(); 220 288 await Task.WhenAll(recalculations); 221 289 … … 228 296 return gradientChart; 229 297 } 230 298 private FactorGradientChart CreateFactorGradientChart(string variableName, ModifiableDataset sharedFixedVariables) { 299 var gradientChart = new FactorGradientChart { 300 Dock = DockStyle.Fill, 301 Margin = Padding.Empty, 302 ShowLegend = false, 303 ShowCursor = true, 304 YAxisTicks = 5, 305 }; 306 gradientChart.VariableValueChanged += async (o, e) => { 307 var recalculations = VisibleGradientCharts 308 .Except(new[] { (FactorGradientChart)o }) 309 .Select(async chart => { 310 await chart.RecalculateAsync(updateOnFinish: false, resetYAxis: false); 311 }).ToList(); 312 await Task.WhenAll(recalculations); 313 314 if (recalculations.All(t => t.IsCompleted)) 315 SetupYAxis(); 316 }; 317 var variableValues = Content.ProblemData.Dataset.GetStringValues(variableName).Distinct().OrderBy(n => n).ToList(); 318 gradientChart.Configure(new[] { Content }, sharedFixedVariables, variableName, variableValues); 319 gradientChart.SolutionAdded += gradientChart_SolutionAdded; 320 gradientChart.SolutionRemoved += gradientChart_SolutionRemoved; 321 return gradientChart; 322 } 231 323 private void SetupYAxis() { 232 324 double axisMin, axisMax; … … 319 411 if (item.Checked) { 320 412 tl.Controls.Add(chartsPanel); 321 await gradientChart.RecalculateAsync( );413 await gradientChart.RecalculateAsync(false, false); 322 414 } else { 323 415 tl.Controls.Remove(chartsPanel); … … 383 475 indices = GetDensityIndices(densityComboBox.SelectedIndex).ToList(); 384 476 } 385 var data = Content.ProblemData.Dataset.GetDoubleValues(variable, indices).ToList(); 386 var gradientChart = gradientCharts[variable]; 387 var min = gradientChart.FixedXAxisMin; 388 var max = gradientChart.FixedXAxisMax; 389 var buckets = gradientChart.DrawingSteps; 390 if (min.HasValue && max.HasValue) { 391 densityChart.UpdateChart(data, min.Value, max.Value, buckets); 392 densityChart.Width = gradientChart.Width; 393 394 var gcPlotPosition = gradientChart.InnerPlotPosition; 395 densityChart.Left = (int)(gcPlotPosition.X / 100.0 * gradientChart.Width); 396 densityChart.Width = (int)(gcPlotPosition.Width / 100.0 * gradientChart.Width); 397 398 densityChart.Visible = true; 399 } 400 401 gradientChart.UpdateTitlePosition(); 477 if (Content.ProblemData.Dataset.VariableHasType<double>(variable)) { 478 var data = Content.ProblemData.Dataset.GetDoubleValues(variable, indices).ToList(); 479 var gradientChart = gradientCharts[variable] as GradientChart; 480 if (gradientChart != null) { 481 var min = gradientChart.FixedXAxisMin; 482 var max = gradientChart.FixedXAxisMax; 483 var buckets = gradientChart.DrawingSteps; 484 if (min.HasValue && max.HasValue) { 485 densityChart.UpdateChart(data, min.Value, max.Value, buckets); 486 densityChart.Width = gradientChart.Width; 487 488 var gcPlotPosition = gradientChart.InnerPlotPosition; 489 densityChart.Left = (int)(gcPlotPosition.X / 100.0 * gradientChart.Width); 490 densityChart.Width = (int)(gcPlotPosition.Width / 100.0 * gradientChart.Width); 491 492 densityChart.Visible = true; 493 } 494 gradientChart.UpdateTitlePosition(); 495 } 496 } else if (Content.ProblemData.Dataset.VariableHasType<string>(variable)) { 497 var data = Content.ProblemData.Dataset.GetStringValues(variable).ToList(); 498 var gradientChart = gradientCharts[variable] as FactorGradientChart; 499 if (gradientChart != null) { 500 densityChart.UpdateChart(data); 501 densityChart.Width = gradientChart.Width; 502 503 var gcPlotPosition = gradientChart.InnerPlotPosition; 504 densityChart.Left = (int)(gcPlotPosition.X / 100.0 * gradientChart.Width); 505 densityChart.Width = (int)(gcPlotPosition.Width / 100.0 * gradientChart.Width); 506 507 densityChart.Visible = true; 508 509 gradientChart.UpdateTitlePosition(); 510 } 511 } 402 512 } 403 513 … … 422 532 var densityChart = densityCharts[variable]; 423 533 // recalculate and refresh 424 await gradientChart.RecalculateAsync( );534 await gradientChart.RecalculateAsync(false, false); 425 535 gradientChart.Refresh(); 426 536 UpdateDensityChart(densityChart, variable);
Note: See TracChangeset
for help on using the changeset viewer.