Free cookie consent management tool by TermsFeed Policy Generator

Changeset 13837


Ignore:
Timestamp:
05/09/16 12:28:09 (9 years ago)
Author:
pfleck
Message:

#2597:

  • Reverted UpdateAutomatically property.
  • Made UpdateChart method async to avoid blocking the GUI thread.
  • Changed Content type of RegressionSolutionGradientView to IRegressionSolution.
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  
    2525using System.Globalization;
    2626using System.Linq;
     27using System.Threading.Tasks;
    2728using System.Windows.Forms;
    2829using System.Windows.Forms.DataVisualization.Charting;
     
    3435  public partial class GradientChart : UserControl {
    3536    private ModifiableDataset sharedFixedVariables; // used for syncronising variable values between charts
    36     private ModifiableDataset internalDataset; // used to cache values and speed up calculations
     37    private ModifiableDataset internalDataset; // holds the x values for each point drawn
    3738
    3839    public bool ShowLegend {
     
    5657    public int XAxisTicks {
    5758      get { return xAxisTicks; }
    58       set { if (xAxisTicks != value) { xAxisTicks = value; UpdateChart(); } }
     59      set { if (xAxisTicks != value) { xAxisTicks = value; UpdateChartAsync(); } }
    5960    }
    6061    private int yAxisTicks = 5;
    6162    public int YXAxisTicks {
    6263      get { return yAxisTicks; }
    63       set { if (yAxisTicks != value) { yAxisTicks = value; UpdateChart(); } }
     64      set { if (yAxisTicks != value) { yAxisTicks = value; UpdateChartAsync(); } }
    6465    }
    6566
     
    6768    public double TrainingMin {
    6869      get { return trainingMin; }
    69       set { if (!value.IsAlmost(trainingMin)) { trainingMin = value; UpdateChart(); } }
     70      set { if (!value.IsAlmost(trainingMin)) { trainingMin = value; UpdateChartAsync(); } }
    7071    }
    7172    private double trainingMax = double.MaxValue;
    7273    public double TrainingMax {
    7374      get { return trainingMax; }
    74       set { if (!value.IsAlmost(trainingMax)) { trainingMax = value; UpdateChart(); } }
     75      set { if (!value.IsAlmost(trainingMax)) { trainingMax = value; UpdateChartAsync(); } }
    7576    }
    7677
     
    7879    public int DrawingSteps {
    7980      get { return drawingSteps; }
    80       set { if (value != drawingSteps) { drawingSteps = value; UpdateChart(); } }
     81      set { if (value != drawingSteps) { drawingSteps = value; UpdateChartAsync(); } }
    8182    }
    8283
     
    9192        freeVariable = value;
    9293        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      }
    10196    }
    10297
     
    139134      this.sharedFixedVariables.ItemChanged += sharedFixedVariables_ItemChanged;
    140135
    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();
    144137      RecalculateInternalDataset();
    145138    }
    146139
    147140    private void sharedFixedVariables_ItemChanged(object o, EventArgs<int, int> e) {
    148       var sender = (ModifiableDataset)o;
     141      if (o != sharedFixedVariables) return;
    149142      var variables = sharedFixedVariables.DoubleVariables.ToList();
    150143      var rowIndex = e.Value;
     
    153146      var variableName = variables[columnIndex];
    154147      if (variableName == FreeVariable) return;
    155       var v = sender.GetDoubleValue(variableName, rowIndex);
     148      var v = sharedFixedVariables.GetDoubleValue(variableName, rowIndex);
    156149      var values = new List<double>(Enumerable.Repeat(v, DrawingSteps));
    157150      internalDataset.ReplaceVariable(variableName, values);
    158 
    159       if (UpdateChartAutomatically)
    160         UpdateChart();
    161151    }
    162152
     
    180170    }
    181171
    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() {
    183178      // throw exceptions?
    184179      if (sharedFixedVariables == null || solutions == null || !solutions.Any())
     
    203198      for (int i = 0; i < solutions.Count; ++i) {
    204199        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;
    211208      }
    212209
     
    214211      chart.Series.Clear();
    215212      // 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)) {
    217214        series.LegendText = series.Name;
    218215        chart.Series.Add(series);
     
    222219      chart.Palette = ChartColorPalette.None;
    223220
    224       foreach (var series in seriesDict) {
     221      foreach (var series in seriesDict.OrderBy(s => (int)s.Key.Tag)) {
    225222        if (series.Value == null) continue;
    226223        int idx = chart.Series.IndexOf(series.Key);
     
    232229
    233230
    234       //// calculate Y-axis interval
     231      // calculate Y-axis interval
    235232      //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      //  }
    239239      //}
    240240      //ChartUtil.CalculateAxisInterval(ymin, ymax, YXAxisTicks, out axisMin, out axisMax, out axisInterval);
     
    243243      //axis.Maximum = axisMax;
    244244      //axis.Interval = axisInterval;
     245      //chart.ChartAreas[0].RecalculateAxesScale();
    245246
    246247      // set axis title
     
    250251    }
    251252
    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      });
    278275    }
    279276
     
    283280      if (solutions.Contains(solution)) return;
    284281      solutions.Add(solution);
    285       UpdateChart();
     282      RecalculateTrainingLimits();
     283      UpdateChartAsync();
    286284    }
    287285    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();
    291289    }
    292290
  • branches/HeuristicLab.RegressionSolutionGradientView/HeuristicLab.Problems.DataAnalysis.Views/3.4/RegressionSolutionGradientView.Designer.cs

    r13831 r13837  
    7878      this.gradientChart.Dock = System.Windows.Forms.DockStyle.Fill;
    7979      this.gradientChart.DrawingSteps = 1000;
    80       this.gradientChart.UpdateChartAutomatically = true;
    8180      this.gradientChart.Location = new System.Drawing.Point(0, 0);
    8281      this.gradientChart.Name = "gradientChart";
  • branches/HeuristicLab.RegressionSolutionGradientView/HeuristicLab.Problems.DataAnalysis.Views/3.4/RegressionSolutionGradientView.cs

    r13836 r13837  
    3232namespace HeuristicLab.Algorithms.DataAnalysis.Views {
    3333  [View("Gradient View")]
    34   [Content(typeof(IConfidenceBoundRegressionSolution))]
     34  [Content(typeof(IRegressionSolution))]
    3535  public partial class RegressionSolutionGradientView : DataAnalysisSolutionEvaluationView {
    3636    private const int DrawingSteps = 1000;
     
    4444    }
    4545
    46     public new IConfidenceBoundRegressionSolution Content {
    47       get { return (IConfidenceBoundRegressionSolution)base.Content; }
     46    public new IRegressionSolution Content {
     47      get { return (IRegressionSolution)base.Content; }
    4848      set { base.Content = value; }
    4949    }
     
    5555      trackbars = new ObservableList<DensityTrackbar>();
    5656      trackbars.ItemsAdded += (sender, args) => {
    57         ForEach(args.Items.Select(i => i.Value), RegisterEvents);
     57        args.Items.Select(i => i.Value).ForEach(RegisterEvents);
    5858      };
    5959      trackbars.ItemsRemoved += (sender, args) => {
    60         ForEach(args.Items.Select(i => i.Value), DeregisterEvents);
     60        args.Items.Select(i => i.Value).ForEach(DeregisterEvents);
    6161      };
    6262      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);
    6565      };
    6666
     
    8787      sharedFixedVariables = new ModifiableDataset(variableNames, newTrackbars.Select(tb => new List<double>(1) { (double)tb.Value }));
    8888      gradientChart.Configure(new[] { Content }, sharedFixedVariables, variableNames.First(), DrawingSteps);
    89       gradientChart.UpdateChart();
     89      gradientChart.UpdateChartAsync();
    9090
    9191      // Add to table and observable lists     
     
    149149    }
    150150
    151     private void trackbar_ValueChanged(object sender, EventArgs e) {
     151    private async void trackbar_ValueChanged(object sender, EventArgs e) {
    152152      var trackBar = sender as DensityTrackbar;
    153153      if (trackBar == null) return;
    154154      sharedFixedVariables.SetVariableValue((double)trackBar.Value, variableNames[trackbars.IndexOf(trackBar)], 0);
    155       gradientChart.UpdateChart();
     155      await gradientChart.UpdateChartAsync();
    156156    }
    157157
     
    183183    #endregion
    184184
    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) {
    186190      foreach (T item in source)
    187191        action(item);
  • branches/HeuristicLab.RegressionSolutionGradientView/HeuristicLab.Problems.DataAnalysis.Views/3.4/RegressionSolutionTargetResponseGradientView.cs

    r13831 r13837  
    7373          ShowYAxisLabel = true,
    7474        };
    75         gradientChart.VariableValueChanged += (o, e) => {
     75        gradientChart.VariableValueChanged += async (o, e) => {
    7676          foreach (var chart in gradientChartTableLayout.Controls.Cast<GradientChart>()) {
    7777            if (chart == (GradientChart)o) continue;
    78             chart.UpdateChart();
     78            await chart.UpdateChartAsync();
    7979          }
    8080        };
     
    9999    }
    100100
    101     private void variableListView_ItemChecked(object sender, ItemCheckedEventArgs e) {
     101    private async void variableListView_ItemChecked(object sender, ItemCheckedEventArgs e) {
    102102      if (charts == null) return;
    103103      var item = e.Item;
     
    111111      if (item.Checked) {
    112112        tl.Controls.Add(chart);
    113         chart.UpdateChart();
     113        await chart.UpdateChartAsync();
    114114      } else {
    115115        tl.Controls.Remove(chart);
Note: See TracChangeset for help on using the changeset viewer.