- Timestamp:
- 05/09/16 12:28:09 (9 years ago)
- Location:
- branches/HeuristicLab.RegressionSolutionGradientView/HeuristicLab.Problems.DataAnalysis.Views/3.4
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/HeuristicLab.RegressionSolutionGradientView/HeuristicLab.Problems.DataAnalysis.Views/3.4/GradientChart.cs
r13836 r13837 25 25 using System.Globalization; 26 26 using System.Linq; 27 using System.Threading.Tasks; 27 28 using System.Windows.Forms; 28 29 using System.Windows.Forms.DataVisualization.Charting; … … 34 35 public partial class GradientChart : UserControl { 35 36 private ModifiableDataset sharedFixedVariables; // used for syncronising variable values between charts 36 private ModifiableDataset internalDataset; // used to cache values and speed up calculations37 private ModifiableDataset internalDataset; // holds the x values for each point drawn 37 38 38 39 public bool ShowLegend { … … 56 57 public int XAxisTicks { 57 58 get { return xAxisTicks; } 58 set { if (xAxisTicks != value) { xAxisTicks = value; UpdateChart (); } }59 set { if (xAxisTicks != value) { xAxisTicks = value; UpdateChartAsync(); } } 59 60 } 60 61 private int yAxisTicks = 5; 61 62 public int YXAxisTicks { 62 63 get { return yAxisTicks; } 63 set { if (yAxisTicks != value) { yAxisTicks = value; UpdateChart (); } }64 set { if (yAxisTicks != value) { yAxisTicks = value; UpdateChartAsync(); } } 64 65 } 65 66 … … 67 68 public double TrainingMin { 68 69 get { return trainingMin; } 69 set { if (!value.IsAlmost(trainingMin)) { trainingMin = value; UpdateChart (); } }70 set { if (!value.IsAlmost(trainingMin)) { trainingMin = value; UpdateChartAsync(); } } 70 71 } 71 72 private double trainingMax = double.MaxValue; 72 73 public double TrainingMax { 73 74 get { return trainingMax; } 74 set { if (!value.IsAlmost(trainingMax)) { trainingMax = value; UpdateChart (); } }75 set { if (!value.IsAlmost(trainingMax)) { trainingMax = value; UpdateChartAsync(); } } 75 76 } 76 77 … … 78 79 public int DrawingSteps { 79 80 get { return drawingSteps; } 80 set { if (value != drawingSteps) { drawingSteps = value; UpdateChart (); } }81 set { if (value != drawingSteps) { drawingSteps = value; UpdateChartAsync(); } } 81 82 } 82 83 … … 91 92 freeVariable = value; 92 93 RecalculateInternalDataset(); 93 UpdateChart(); 94 } 95 } 96 97 private bool updateChartAutomatically = false; 98 public bool UpdateChartAutomatically { 99 get { return updateChartAutomatically; } 100 set { updateChartAutomatically = value; if (updateChartAutomatically) UpdateChart(); } 94 UpdateChartAsync(); 95 } 101 96 } 102 97 … … 139 134 this.sharedFixedVariables.ItemChanged += sharedFixedVariables_ItemChanged; 140 135 141 trainingMin = solutions.Select(s => s.ProblemData.Dataset.GetDoubleValues(freeVariable, s.ProblemData.TrainingIndices).Min()).Max(); 142 trainingMax = solutions.Select(s => s.ProblemData.Dataset.GetDoubleValues(freeVariable, s.ProblemData.TrainingIndices).Max()).Min(); 143 136 RecalculateTrainingLimits(); 144 137 RecalculateInternalDataset(); 145 138 } 146 139 147 140 private void sharedFixedVariables_ItemChanged(object o, EventArgs<int, int> e) { 148 var sender = (ModifiableDataset)o;141 if (o != sharedFixedVariables) return; 149 142 var variables = sharedFixedVariables.DoubleVariables.ToList(); 150 143 var rowIndex = e.Value; … … 153 146 var variableName = variables[columnIndex]; 154 147 if (variableName == FreeVariable) return; 155 var v = s ender.GetDoubleValue(variableName, rowIndex);148 var v = sharedFixedVariables.GetDoubleValue(variableName, rowIndex); 156 149 var values = new List<double>(Enumerable.Repeat(v, DrawingSteps)); 157 150 internalDataset.ReplaceVariable(variableName, values); 158 159 if (UpdateChartAutomatically)160 UpdateChart();161 151 } 162 152 … … 180 170 } 181 171 182 public void UpdateChart() { 172 private void RecalculateTrainingLimits() { 173 trainingMin = solutions.Select(s => s.ProblemData.Dataset.GetDoubleValues(freeVariable, s.ProblemData.TrainingIndices).Min()).Max(); 174 trainingMax = solutions.Select(s => s.ProblemData.Dataset.GetDoubleValues(freeVariable, s.ProblemData.TrainingIndices).Max()).Min(); 175 } 176 177 public async Task UpdateChartAsync() { 183 178 // throw exceptions? 184 179 if (sharedFixedVariables == null || solutions == null || !solutions.Any()) … … 203 198 for (int i = 0; i < solutions.Count; ++i) { 204 199 var solution = solutions[i]; 205 Series confidenceIntervalPlotSeries; 206 var meanSeries = CreateSeries(solution, out confidenceIntervalPlotSeries); 207 meanSeries.Name = Solutions.First().ProblemData.TargetVariable + " " + i; 208 seriesDict.Add(meanSeries, confidenceIntervalPlotSeries); 209 if (confidenceIntervalPlotSeries != null) 210 confidenceIntervalPlotSeries.Name = "95% Conf. Interval " + meanSeries.Name; 200 var series = await CreateSeriesAsync(solution); 201 series.Item1.Tag = i; // for sorting 202 var meanSeries = series.Item1; 203 var confidenceIntervalSeries = series.Item2; 204 meanSeries.Name = solution.ProblemData.TargetVariable + " " + i; 205 seriesDict.Add(meanSeries, confidenceIntervalSeries); 206 if (confidenceIntervalSeries != null) 207 confidenceIntervalSeries.Name = "95% Conf. Interval " + meanSeries.Name; 211 208 } 212 209 … … 214 211 chart.Series.Clear(); 215 212 // Add mean series for applying palette colors 216 foreach (var series in seriesDict.Keys ) {213 foreach (var series in seriesDict.Keys.OrderBy(s => (int)s.Tag)) { 217 214 series.LegendText = series.Name; 218 215 chart.Series.Add(series); … … 222 219 chart.Palette = ChartColorPalette.None; 223 220 224 foreach (var series in seriesDict ) {221 foreach (var series in seriesDict.OrderBy(s => (int)s.Key.Tag)) { 225 222 if (series.Value == null) continue; 226 223 int idx = chart.Series.IndexOf(series.Key); … … 232 229 233 230 234 // //calculate Y-axis interval231 // calculate Y-axis interval 235 232 //double ymin = 0, ymax = 0; 236 //foreach (var v in chart.Series[0].Points.Select(x => x.YValues[0])) { 237 // if (ymin > v) ymin = v; 238 // if (ymax < v) ymax = v; 233 //foreach (var vs in chart.Series.SelectMany(series => series.Points.Select(s => s.YValues))) { 234 // for (int index = 0; index < vs.Length; index++) { 235 // var v = vs[index]; 236 // if (ymin > v) ymin = v; 237 // if (ymax < v) ymax = v; 238 // } 239 239 //} 240 240 //ChartUtil.CalculateAxisInterval(ymin, ymax, YXAxisTicks, out axisMin, out axisMax, out axisInterval); … … 243 243 //axis.Maximum = axisMax; 244 244 //axis.Interval = axisInterval; 245 //chart.ChartAreas[0].RecalculateAxesScale(); 245 246 246 247 // set axis title … … 250 251 } 251 252 252 private Series CreateSeries(IRegressionSolution solution, out Series confidenceIntervalPlotSeries) { 253 var series = new Series { 254 ChartType = SeriesChartType.Line 255 }; 256 257 var xvalues = internalDataset.GetDoubleValues(FreeVariable).ToList(); 258 var yvalues = solution.Model.GetEstimatedValues(internalDataset, Enumerable.Range(0, internalDataset.Rows)).ToList(); 259 series.Points.DataBindXY(xvalues, yvalues); 260 261 var confidenceBoundSolution = solution as IConfidenceBoundRegressionSolution; 262 if (confidenceBoundSolution != null) { 263 var variances = confidenceBoundSolution.Model.GetEstimatedVariances(internalDataset, Enumerable.Range(0, internalDataset.Rows)).ToList(); 264 265 var lower = yvalues.Zip(variances, (m, s2) => m - 1.96 * Math.Sqrt(s2)).ToList(); 266 var upper = yvalues.Zip(variances, (m, s2) => m + 1.96 * Math.Sqrt(s2)).ToList(); 267 268 confidenceIntervalPlotSeries = new Series { 269 ChartType = SeriesChartType.Range, 270 YValuesPerPoint = 2 271 }; 272 confidenceIntervalPlotSeries.Points.DataBindXY(xvalues, lower, upper); 273 } else { 274 confidenceIntervalPlotSeries = null; 275 } 276 277 return series; 253 private Task<Tuple<Series, Series>> CreateSeriesAsync(IRegressionSolution solution) { 254 return Task.Run(() => { 255 var xvalues = internalDataset.GetDoubleValues(FreeVariable).ToList(); 256 var yvalues = solution.Model.GetEstimatedValues(internalDataset, Enumerable.Range(0, internalDataset.Rows)).ToList(); 257 258 var series = new Series { ChartType = SeriesChartType.Line }; 259 series.Points.DataBindXY(xvalues, yvalues); 260 261 var confidenceBoundSolution = solution as IConfidenceBoundRegressionSolution; 262 Series confidenceIntervalSeries = null; 263 if (confidenceBoundSolution != null) { 264 var variances = confidenceBoundSolution.Model.GetEstimatedVariances(internalDataset, Enumerable.Range(0, internalDataset.Rows)).ToList(); 265 266 var lower = yvalues.Zip(variances, (m, s2) => m - 1.96 * Math.Sqrt(s2)).ToList(); 267 var upper = yvalues.Zip(variances, (m, s2) => m + 1.96 * Math.Sqrt(s2)).ToList(); 268 269 confidenceIntervalSeries = new Series { ChartType = SeriesChartType.Range, YValuesPerPoint = 2 }; 270 confidenceIntervalSeries.Points.DataBindXY(xvalues, lower, upper); 271 } 272 273 return Tuple.Create(series, confidenceIntervalSeries); 274 }); 278 275 } 279 276 … … 283 280 if (solutions.Contains(solution)) return; 284 281 solutions.Add(solution); 285 UpdateChart(); 282 RecalculateTrainingLimits(); 283 UpdateChartAsync(); 286 284 } 287 285 public void RemoveSolution(IRegressionSolution solution) { 288 bool removed = solutions.Remove(solution);289 if (removed)290 UpdateChart();286 if (!solutions.Remove(solution)) return; 287 RecalculateTrainingLimits(); 288 UpdateChartAsync(); 291 289 } 292 290 -
branches/HeuristicLab.RegressionSolutionGradientView/HeuristicLab.Problems.DataAnalysis.Views/3.4/RegressionSolutionGradientView.Designer.cs
r13831 r13837 78 78 this.gradientChart.Dock = System.Windows.Forms.DockStyle.Fill; 79 79 this.gradientChart.DrawingSteps = 1000; 80 this.gradientChart.UpdateChartAutomatically = true;81 80 this.gradientChart.Location = new System.Drawing.Point(0, 0); 82 81 this.gradientChart.Name = "gradientChart"; -
branches/HeuristicLab.RegressionSolutionGradientView/HeuristicLab.Problems.DataAnalysis.Views/3.4/RegressionSolutionGradientView.cs
r13836 r13837 32 32 namespace HeuristicLab.Algorithms.DataAnalysis.Views { 33 33 [View("Gradient View")] 34 [Content(typeof(I ConfidenceBoundRegressionSolution))]34 [Content(typeof(IRegressionSolution))] 35 35 public partial class RegressionSolutionGradientView : DataAnalysisSolutionEvaluationView { 36 36 private const int DrawingSteps = 1000; … … 44 44 } 45 45 46 public new I ConfidenceBoundRegressionSolution Content {47 get { return (I ConfidenceBoundRegressionSolution)base.Content; }46 public new IRegressionSolution Content { 47 get { return (IRegressionSolution)base.Content; } 48 48 set { base.Content = value; } 49 49 } … … 55 55 trackbars = new ObservableList<DensityTrackbar>(); 56 56 trackbars.ItemsAdded += (sender, args) => { 57 ForEach(args.Items.Select(i => i.Value),RegisterEvents);57 args.Items.Select(i => i.Value).ForEach(RegisterEvents); 58 58 }; 59 59 trackbars.ItemsRemoved += (sender, args) => { 60 ForEach(args.Items.Select(i => i.Value),DeregisterEvents);60 args.Items.Select(i => i.Value).ForEach(DeregisterEvents); 61 61 }; 62 62 trackbars.CollectionReset += (sender, args) => { 63 ForEach(args.OldItems.Select(i => i.Value),DeregisterEvents);64 ForEach(args.Items.Select(i => i.Value),RegisterEvents);63 args.OldItems.Select(i => i.Value).ForEach(DeregisterEvents); 64 args.Items.Select(i => i.Value).ForEach(RegisterEvents); 65 65 }; 66 66 … … 87 87 sharedFixedVariables = new ModifiableDataset(variableNames, newTrackbars.Select(tb => new List<double>(1) { (double)tb.Value })); 88 88 gradientChart.Configure(new[] { Content }, sharedFixedVariables, variableNames.First(), DrawingSteps); 89 gradientChart.UpdateChart ();89 gradientChart.UpdateChartAsync(); 90 90 91 91 // Add to table and observable lists … … 149 149 } 150 150 151 private void trackbar_ValueChanged(object sender, EventArgs e) {151 private async void trackbar_ValueChanged(object sender, EventArgs e) { 152 152 var trackBar = sender as DensityTrackbar; 153 153 if (trackBar == null) return; 154 154 sharedFixedVariables.SetVariableValue((double)trackBar.Value, variableNames[trackbars.IndexOf(trackBar)], 0); 155 gradientChart.UpdateChart();155 await gradientChart.UpdateChartAsync(); 156 156 } 157 157 … … 183 183 #endregion 184 184 185 public static void ForEach<T>(IEnumerable<T> source, Action<T> action) { 185 186 } 187 188 internal static class Extensions { 189 public static void ForEach<T>(this IEnumerable<T> source, Action<T> action) { 186 190 foreach (T item in source) 187 191 action(item); -
branches/HeuristicLab.RegressionSolutionGradientView/HeuristicLab.Problems.DataAnalysis.Views/3.4/RegressionSolutionTargetResponseGradientView.cs
r13831 r13837 73 73 ShowYAxisLabel = true, 74 74 }; 75 gradientChart.VariableValueChanged += (o, e) => {75 gradientChart.VariableValueChanged += async (o, e) => { 76 76 foreach (var chart in gradientChartTableLayout.Controls.Cast<GradientChart>()) { 77 77 if (chart == (GradientChart)o) continue; 78 chart.UpdateChart();78 await chart.UpdateChartAsync(); 79 79 } 80 80 }; … … 99 99 } 100 100 101 private void variableListView_ItemChecked(object sender, ItemCheckedEventArgs e) {101 private async void variableListView_ItemChecked(object sender, ItemCheckedEventArgs e) { 102 102 if (charts == null) return; 103 103 var item = e.Item; … … 111 111 if (item.Checked) { 112 112 tl.Controls.Add(chart); 113 chart.UpdateChart();113 await chart.UpdateChartAsync(); 114 114 } else { 115 115 tl.Controls.Remove(chart);
Note: See TracChangeset
for help on using the changeset viewer.