Changeset 13829
- Timestamp:
- 05/03/16 21:20:55 (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/HeuristicLab.RegressionSolutionGradientView/HeuristicLab.Problems.DataAnalysis.Views/3.4/GradientChart.cs
r13825 r13829 32 32 namespace HeuristicLab.Problems.DataAnalysis.Views { 33 33 public partial class GradientChart : EnhancedChart { 34 private ModifiableDataset dataset; 34 private ModifiableDataset sharedDataset; // used for syncronising variable values between charts 35 private ModifiableDataset internalDataset; // used to cache values and speed up calculations 35 36 36 37 public bool ShowLegend { get; set; } … … 191 192 this.problemData = pd; 192 193 this.variable = variable; 193 this. dataset = dataset;194 this.sharedDataset = dataset; 194 195 this.min = min; 195 196 this.max = max; 196 197 this.points = points; 198 199 // add an event such that whenever a value is changed in the shared dataset, 200 // this change is reflected in the internal dataset (where the value becomes a whole column) 201 var variables = sharedDataset.DoubleVariables.ToList(); 202 sharedDataset.ItemChanged += (o, e) => { 203 var rowIndex = e.Value; 204 var columnIndex = e.Value2; 205 var ds = (ModifiableDataset)o; 206 var variableName = variables[columnIndex]; 207 if (variableName == Variable) return; 208 var v = ds.GetDoubleValue(variableName, rowIndex); 209 var values = new List<double>(Enumerable.Repeat(v, Points)); 210 internalDataset.ReplaceVariable(variableName, values); 211 }; 212 213 // configure internal dataset. we also expand the range in order to get nice tick intervals on the x axis 214 const int tics = 5; 215 double xmin, xmax, xinterval; 216 ChartUtil.CalculateAxisInterval(min, max, tics, out xmin, out xmax, out xinterval); 217 var step = (xmax - xmin) / points; 218 var xvalues = new List<double>(); 219 for (int i = 0; i < points; ++i) { xvalues.Add(xmin + i * step); } 220 internalDataset = new ModifiableDataset(variables, variables.Select(x => x == Variable ? xvalues : new List<double>(Enumerable.Repeat(sharedDataset.GetDoubleValue(x, 0), xvalues.Count)))); 197 221 } 198 222 199 223 public void UpdateChart() { 200 224 // throw exceptions? 201 if ( dataset == null || solutionList == null || !solutionList.Any())225 if (sharedDataset == null || solutionList == null || !solutionList.Any()) 202 226 return; 203 227 if (min.IsAlmost(max) || min > max || points == 0) 204 228 return; 205 229 Series.Clear(); 206 207 var defaultValue = dataset.GetDoubleValue(variable, 0);208 230 var vla = VerticalLineAnnotation; 231 Annotations.Clear(); 232 var defaultValue = sharedDataset.GetDoubleValue(variable, 0); 233 vla.Visible = ShowCursor; 234 Annotations.Add(vla); 209 235 vla.X = defaultValue; 210 vla.Visible = ShowCursor;211 Annotations.Clear();212 Annotations.Add(vla);213 236 214 237 double axisMin, axisMax, axisInterval; … … 262 285 } 263 286 } 264 dataset = new ModifiableDataset(variables, variableValues); 265 } 266 267 private double GetEstimatedValue(IRegressionSolution solution, double x) { 268 var v = dataset.GetDoubleValue(Variable, 0); 269 dataset.SetVariableValue(x, Variable, 0); 270 var y = solution.Model.GetEstimatedValues(dataset, new[] { 0 }).Single(); 271 dataset.SetVariableValue(v, Variable, 0); 272 return y; 287 sharedDataset = new ModifiableDataset(variables, variableValues); 273 288 } 274 289 275 290 private Series PlotSeries(IRegressionSolution solution) { 276 var v = dataset.GetDoubleValue(variable, 0);291 var v = sharedDataset.GetDoubleValue(variable, 0); 277 292 var series = new Series { ChartType = SeriesChartType.Point }; 278 279 var step = (max - min) / points; 280 var axisX = ChartAreas[0].AxisX; 281 if (ShowXAxisLabel) { 282 axisX.Title = Variable + " : " + v.ToString("N3", CultureInfo.CurrentCulture); 283 } 284 var axisY = ChartAreas[0].AxisY; 285 if (ShowYAxisLabel) { axisY.Title = Target; } 286 double y; 287 // lefthand section outside of the training range 288 for (double x = axisX.Minimum; x < min; x += step) { 289 y = GetEstimatedValue(solution, x); 290 series.Points.Add(new DataPoint(x, y) { MarkerSize = 2, MarkerColor = Color.Orange }); 291 } 292 // points in the trainig range 293 for (double x = min; x < max; x += step) { 294 y = GetEstimatedValue(solution, x); 295 series.Points.Add(new DataPoint(x, y) { MarkerSize = 2 }); 296 } 297 // righthand section outside of the training range 298 for (double x = max; x < axisX.Maximum; x += step) { 299 y = GetEstimatedValue(solution, x); 300 series.Points.Add(new DataPoint(x, y) { MarkerSize = 2, MarkerColor = Color.Orange }); 301 } 302 293 // get values from series 294 var xvalues = internalDataset.GetReadOnlyDoubleValues(Variable); 295 var yvalues = solution.Model.GetEstimatedValues(internalDataset, Enumerable.Range(0, internalDataset.Rows)); 296 int i = 0; 297 foreach (var y in yvalues) { 298 var x = xvalues[i++]; 299 series.Points.Add(new DataPoint(x, y) { MarkerSize = 2, MarkerColor = Color.DodgerBlue }); 300 } 303 301 if (ShowCursor) { 304 y = GetEstimatedValue(solution, v);302 var y = solution.Model.GetEstimatedValues(sharedDataset, new[] { 0 }).Single(); 305 303 series.Points.Add(new DataPoint(v, y) { MarkerSize = 5, MarkerColor = Color.Red }); 306 304 } … … 308 306 series.IsVisibleInLegend = true; 309 307 } 310 311 308 return series; 312 309 } … … 355 352 var annotation = VerticalLineAnnotation; 356 353 var x = annotation.X; 357 dataset.SetVariableValue(x, Variable, 0);354 sharedDataset.SetVariableValue(x, Variable, 0); 358 355 for (int i = 0; i < solutionList.Count; ++i) { 359 var y = GetEstimatedValue(solutionList[i], x);356 var y = solutionList[i].Model.GetEstimatedValues(sharedDataset, new[] { 0 }).Single(); 360 357 var s = Series[i]; 361 358 var n = s.Points.Count;
Note: See TracChangeset
for help on using the changeset viewer.