Changeset 12690


Ignore:
Timestamp:
07/08/15 15:42:47 (6 years ago)
Author:
ascheibe
Message:

#2270 fixed cross-threading issues in run collection views

Location:
trunk/sources/HeuristicLab.Analysis.Statistics.Views/3.3
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Analysis.Statistics.Views/3.3/SampleSizeInfluenceView.cs

    r12599 r12690  
    2222using System;
    2323using System.Collections.Generic;
     24using System.Drawing;
    2425using System.Linq;
    2526using System.Windows.Forms;
     
    3334using HeuristicLab.Optimization;
    3435using HeuristicLab.PluginInfrastructure;
     36using ViewEventArgs = System.Windows.Forms.DataVisualization.Charting.ViewEventArgs;
    3537
    3638namespace HeuristicLab.Analysis.Statistics.Views {
     
    4345    private const string delimiter = ";";
    4446
    45     private bool suppressUpdates = false;
     47    private bool suppressUpdates;
    4648    private string yAxisValue;
    4749    private Dictionary<int, Dictionary<object, double>> categoricalMapping;
     
    6668    }
    6769    public IStringConvertibleMatrix Matrix {
    68       get { return this.Content; }
     70      get { return Content; }
    6971    }
    7072
     
    7274    protected override void RegisterContentEvents() {
    7375      base.RegisterContentEvents();
    74       Content.ColumnNamesChanged += new EventHandler(Content_ColumnNamesChanged);
    75       Content.ItemsAdded += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_ItemsAdded);
    76       Content.ItemsRemoved += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_ItemsRemoved);
    77       Content.CollectionReset += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_CollectionReset);
    78       Content.UpdateOfRunsInProgressChanged += new EventHandler(Content_UpdateOfRunsInProgressChanged);
    79       Content.OptimizerNameChanged += new EventHandler(Content_AlgorithmNameChanged);
     76      Content.ColumnNamesChanged += Content_ColumnNamesChanged;
     77      Content.ItemsAdded += Content_ItemsAdded;
     78      Content.ItemsRemoved += Content_ItemsRemoved;
     79      Content.CollectionReset += Content_CollectionReset;
     80      Content.UpdateOfRunsInProgressChanged += Content_UpdateOfRunsInProgressChanged;
     81      Content.OptimizerNameChanged += Content_AlgorithmNameChanged;
    8082      RegisterRunEvents(Content);
    8183    }
    8284    protected override void DeregisterContentEvents() {
    8385      base.DeregisterContentEvents();
    84       Content.ColumnNamesChanged -= new EventHandler(Content_ColumnNamesChanged);
    85       Content.ItemsAdded -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_ItemsAdded);
    86       Content.ItemsRemoved -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_ItemsRemoved);
    87       Content.CollectionReset -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_CollectionReset);
    88       Content.UpdateOfRunsInProgressChanged -= new EventHandler(Content_UpdateOfRunsInProgressChanged);
    89       Content.OptimizerNameChanged -= new EventHandler(Content_AlgorithmNameChanged);
     86      Content.ColumnNamesChanged -= Content_ColumnNamesChanged;
     87      Content.ItemsAdded -= Content_ItemsAdded;
     88      Content.ItemsRemoved -= Content_ItemsRemoved;
     89      Content.CollectionReset -= Content_CollectionReset;
     90      Content.UpdateOfRunsInProgressChanged -= Content_UpdateOfRunsInProgressChanged;
     91      Content.OptimizerNameChanged -= Content_AlgorithmNameChanged;
    9092      DeregisterRunEvents(Content);
    9193    }
     
    167169    private void run_Changed(object sender, EventArgs e) {
    168170      if (InvokeRequired)
    169         this.Invoke(new EventHandler(run_Changed), sender, e);
     171        Invoke(new EventHandler(run_Changed), sender, e);
    170172      else if (!suppressUpdates) UpdateDataPoints();
    171173    }
     
    185187
    186188    private void UpdateAll() {
    187       this.categoricalMapping.Clear();
     189      categoricalMapping.Clear();
    188190      UpdateComboBoxes();
    189191      UpdateDataPoints();
     
    196198
    197199    private void UpdateSampleSizes(bool forceUpdate = false) {
    198       string selectedYAxis = (string)this.yAxisComboBox.SelectedItem;
     200      string selectedYAxis = (string)yAxisComboBox.SelectedItem;
    199201
    200202      if (selectedYAxis != null && (xAxisTextBox.Text.Trim() == string.Empty || forceUpdate)) {
     
    210212        if (values.Any()) {
    211213          if (hypergeometricCheckBox.Checked) {
    212             xAxisTextBox.Text += ((int)(values.Count() / 16)) + delimiter + " ";
    213             xAxisTextBox.Text += ((int)(values.Count() / 8)) + delimiter + " ";
    214             xAxisTextBox.Text += (int)(values.Count() / 4);
     214            xAxisTextBox.Text += values.Count() / 16 + delimiter + " ";
     215            xAxisTextBox.Text += values.Count() / 8 + delimiter + " ";
     216            xAxisTextBox.Text += values.Count() / 4;
    215217          } else {
    216             xAxisTextBox.Text += ((int)(values.Count() / 4)) + delimiter + " ";
    217             xAxisTextBox.Text += ((int)(values.Count() / 2)) + delimiter + " ";
    218             xAxisTextBox.Text += ((int)(values.Count() / 4 * 3)) + delimiter + " ";
    219             xAxisTextBox.Text += (int)(values.Count());
     218            xAxisTextBox.Text += values.Count() / 4 + delimiter + " ";
     219            xAxisTextBox.Text += values.Count() / 2 + delimiter + " ";
     220            xAxisTextBox.Text += values.Count() / 4 * 3 + delimiter + " ";
     221            xAxisTextBox.Text += values.Count();
    220222          }
    221223        }
     
    224226
    225227    private void UpdateComboBoxes() {
    226       string selectedYAxis = (string)this.yAxisComboBox.SelectedItem;
    227       this.xAxisTextBox.Text = string.Empty;
    228       this.yAxisComboBox.Items.Clear();
    229       if (Content != null) {
    230         string[] additionalAxisDimension = Enum.GetNames(typeof(AxisDimension));
    231         UpdateSampleSizes();
    232         this.yAxisComboBox.Items.AddRange(additionalAxisDimension);
    233         this.yAxisComboBox.Items.AddRange(Matrix.ColumnNames.ToArray());
    234 
    235         if (selectedYAxis != null && yAxisComboBox.Items.Contains(selectedYAxis)) {
    236           yAxisComboBox.SelectedItem = selectedYAxis;
    237           UpdateDataPoints();
     228      if (InvokeRequired) {
     229        Invoke((Action)UpdateComboBoxes);
     230      } else {
     231        string selectedYAxis = (string)yAxisComboBox.SelectedItem;
     232        xAxisTextBox.Text = string.Empty;
     233        yAxisComboBox.Items.Clear();
     234        if (Content != null) {
     235          string[] additionalAxisDimension = Enum.GetNames(typeof(AxisDimension));
     236          UpdateSampleSizes();
     237          yAxisComboBox.Items.AddRange(additionalAxisDimension);
     238          yAxisComboBox.Items.AddRange(Matrix.ColumnNames.ToArray());
     239
     240          if (selectedYAxis != null && yAxisComboBox.Items.Contains(selectedYAxis)) {
     241            yAxisComboBox.SelectedItem = selectedYAxis;
     242            UpdateDataPoints();
     243          }
    238244        }
    239245      }
     
    241247
    242248    private void UpdateDataPoints() {
    243       this.chart.Series.Clear();
    244       this.seriesCache.Clear();
     249      chart.Series.Clear();
     250      seriesCache.Clear();
    245251      if (Content != null) {
    246252        var usableRuns = Content.Where(r => r.Visible).ToList();
     
    253259        }
    254260
    255         foreach (Series s in this.seriesCache.Values)
    256           this.chart.Series.Add(s);
     261        foreach (Series s in seriesCache.Values)
     262          chart.Series.Add(s);
    257263
    258264        UpdateStatistics();
    259265        if (seriesCache.Count > 0) {
    260266          Series boxPlotSeries = CreateBoxPlotSeries();
    261           this.chart.Series.Add(boxPlotSeries);
     267          chart.Series.Add(boxPlotSeries);
    262268        }
    263269
     
    314320
    315321      if (!yAxisComboBox.DroppedDown)
    316         this.yAxisValue = (string)yAxisComboBox.SelectedItem;
    317 
    318       List<double?> yValue = usableRuns.Select(x => GetValue(x, this.yAxisValue)).ToList();
     322        yAxisValue = (string)yAxisComboBox.SelectedItem;
     323
     324      List<double?> yValue = usableRuns.Select(x => GetValue(x, yAxisValue)).ToList();
    319325      if (yValue.Any(x => !x.HasValue)) return;
    320326
     
    335341            v = int.Parse(ns);
    336342            vals.Add(v);
    337           } catch (Exception ex) {
     343          }
     344          catch (Exception ex) {
    338345            if (verbose) {
    339346              ErrorHandling.ShowErrorDialog("Can't parse group sizes. Please only use numbers seperated by a " + delimiter + ". ", ex);
     
    369376      }
    370377      matrix.ColumnNames = columnNames;
    371       matrix.RowNames = new string[] { "Count", "Minimum", "Maximum", "Average", "Median", "Standard Deviation", "Variance", "25th Percentile", "75th Percentile", "Lower Confidence Int.", "Upper Confidence Int." };
     378      matrix.RowNames = new[] { "Count", "Minimum", "Maximum", "Average", "Median", "Standard Deviation", "Variance", "25th Percentile", "75th Percentile", "Lower Confidence Int.", "Upper Confidence Int." };
    372379
    373380      for (int i = 0; i < seriesCache.Count; i++) {
     
    400407      boxPlotSeries["BoxPlotShowUnusualValues"] = "true";
    401408      boxPlotSeries["PointWidth"] = "0.4";
    402       boxPlotSeries.BackGradientStyle = System.Windows.Forms.DataVisualization.Charting.GradientStyle.VerticalCenter;
    403       boxPlotSeries.BackSecondaryColor = System.Drawing.Color.FromArgb(130, 224, 64, 10);
    404       boxPlotSeries.BorderColor = System.Drawing.Color.FromArgb(64, 64, 64);
    405       boxPlotSeries.Color = System.Drawing.Color.FromArgb(224, 64, 10);
     409      boxPlotSeries.BackGradientStyle = GradientStyle.VerticalCenter;
     410      boxPlotSeries.BackSecondaryColor = Color.FromArgb(130, 224, 64, 10);
     411      boxPlotSeries.BorderColor = Color.FromArgb(64, 64, 64);
     412      boxPlotSeries.Color = Color.FromArgb(224, 64, 10);
    406413
    407414      return boxPlotSeries;
     
    414421
    415422      if (!yAxisComboBox.DroppedDown)
    416         this.yAxisValue = (string)yAxisComboBox.SelectedItem;
     423        yAxisValue = (string)yAxisComboBox.SelectedItem;
    417424
    418425      xValue = idx;
    419       yValue = GetValue(run, this.yAxisValue);
     426      yValue = GetValue(run, yAxisValue);
    420427
    421428      if (yValue.HasValue) {
    422         if (!this.seriesCache.ContainsKey(xValue))
     429        if (!seriesCache.ContainsKey(xValue))
    423430          seriesCache[xValue] = new Series(xValue.ToString());
    424431
     
    439446        AxisDimension axisDimension = (AxisDimension)Enum.Parse(typeof(AxisDimension), columnName);
    440447        return GetValue(run, axisDimension);
    441       } else {
    442         int columnIndex = Matrix.ColumnNames.ToList().IndexOf(columnName);
    443         IItem value = Content.GetValue(run, columnIndex);
    444         if (value == null)
    445           return null;
    446 
    447         DoubleValue doubleValue = value as DoubleValue;
    448         IntValue intValue = value as IntValue;
    449         TimeSpanValue timeSpanValue = value as TimeSpanValue;
    450         double? ret = null;
    451         if (doubleValue != null) {
    452           if (!double.IsNaN(doubleValue.Value) && !double.IsInfinity(doubleValue.Value))
    453             ret = doubleValue.Value;
    454         } else if (intValue != null)
    455           ret = intValue.Value;
    456         else if (timeSpanValue != null) {
    457           ret = timeSpanValue.Value.TotalSeconds;
    458         } else
    459           ret = GetCategoricalValue(columnIndex, value.ToString());
    460 
    461         return ret;
    462       }
     448      }
     449      int columnIndex = Matrix.ColumnNames.ToList().IndexOf(columnName);
     450      IItem value = Content.GetValue(run, columnIndex);
     451      if (value == null)
     452        return null;
     453
     454      DoubleValue doubleValue = value as DoubleValue;
     455      IntValue intValue = value as IntValue;
     456      TimeSpanValue timeSpanValue = value as TimeSpanValue;
     457      double? ret = null;
     458      if (doubleValue != null) {
     459        if (!double.IsNaN(doubleValue.Value) && !double.IsInfinity(doubleValue.Value))
     460          ret = doubleValue.Value;
     461      } else if (intValue != null)
     462        ret = intValue.Value;
     463      else if (timeSpanValue != null) {
     464        ret = timeSpanValue.Value.TotalSeconds;
     465      } else
     466        ret = GetCategoricalValue(columnIndex, value.ToString());
     467
     468      return ret;
    463469    }
    464470    private double GetCategoricalValue(int dimension, string value) {
    465       if (!this.categoricalMapping.ContainsKey(dimension)) {
    466         this.categoricalMapping[dimension] = new Dictionary<object, double>();
     471      if (!categoricalMapping.ContainsKey(dimension)) {
     472        categoricalMapping[dimension] = new Dictionary<object, double>();
    467473        var orderedCategories = Content.Where(r => r.Visible && Content.GetValue(r, dimension) != null).Select(r => Content.GetValue(r, dimension).ToString())
    468474                                          .Distinct().OrderBy(x => x, new NaturalStringComparer());
    469475        int count = 1;
    470476        foreach (var category in orderedCategories) {
    471           this.categoricalMapping[dimension].Add(category, count);
     477          categoricalMapping[dimension].Add(category, count);
    472478          count++;
    473479        }
    474480      }
    475       return this.categoricalMapping[dimension][value];
     481      return categoricalMapping[dimension][value];
    476482    }
    477483    private double GetValue(IRun run, AxisDimension axisDimension) {
     
    483489          }
    484490        default: {
    485             throw new ArgumentException("No handling strategy for " + axisDimension.ToString() + " is defined.");
     491            throw new ArgumentException("No handling strategy for " + axisDimension + " is defined.");
    486492          }
    487493      }
     
    492498    #region GUI events
    493499    private void UpdateNoRunsVisibleLabel() {
    494       if (this.chart.Series.Count > 0) {
     500      if (chart.Series.Count > 0) {
    495501        noRunsLabel.Visible = false;
    496502        showStatisticsCheckBox.Enabled = true;
     
    517523    }
    518524    private void UpdateAxisLabels() {
    519       Axis xAxis = this.chart.ChartAreas[BoxPlotChartAreaName].AxisX;
    520       Axis yAxis = this.chart.ChartAreas[BoxPlotChartAreaName].AxisY;
     525      Axis xAxis = chart.ChartAreas[BoxPlotChartAreaName].AxisX;
     526      Axis yAxis = chart.ChartAreas[BoxPlotChartAreaName].AxisY;
    521527      int axisDimensionCount = Enum.GetNames(typeof(AxisDimension)).Count();
    522528
     
    529535    }
    530536
    531     private void chart_AxisViewChanged(object sender, System.Windows.Forms.DataVisualization.Charting.ViewEventArgs e) {
    532       this.UpdateAxisLabels();
     537    private void chart_AxisViewChanged(object sender, ViewEventArgs e) {
     538      UpdateAxisLabels();
    533539    }
    534540
     
    551557        }
    552558      } else if (dimension > 0 && Content.GetValue(0, dimension) is TimeSpanValue) {
    553         this.chart.ChartAreas[0].RecalculateAxesScale();
    554         Axis correspondingAxis = this.chart.ChartAreas[0].Axes.Where(x => x.Name == axis.Name).SingleOrDefault();
     559        chart.ChartAreas[0].RecalculateAxesScale();
     560        Axis correspondingAxis = chart.ChartAreas[0].Axes.Where(x => x.Name == axis.Name).SingleOrDefault();
    555561        if (correspondingAxis == null)
    556562          correspondingAxis = axis;
    557563        for (double i = correspondingAxis.Minimum; i <= correspondingAxis.Maximum; i += correspondingAxis.LabelStyle.Interval) {
    558564          TimeSpan time = TimeSpan.FromSeconds(i);
    559           string x = string.Format("{0:00}:{1:00}:{2:00}", (int)time.Hours, time.Minutes, time.Seconds);
     565          string x = string.Format("{0:00}:{1:00}:{2:00}", time.Hours, time.Minutes, time.Seconds);
    560566          axis.CustomLabels.Add(i - correspondingAxis.LabelStyle.Interval / 2, i + correspondingAxis.LabelStyle.Interval / 2, x);
    561567        }
     
    580586      string newTooltipText = string.Empty;
    581587      string oldTooltipText;
    582       HitTestResult h = this.chart.HitTest(e.X, e.Y);
     588      HitTestResult h = chart.HitTest(e.X, e.Y);
    583589      if (h.ChartElementType == ChartElementType.AxisLabels) {
    584590        newTooltipText = ((CustomLabel)h.Object).ToolTip;
    585591      }
    586592
    587       oldTooltipText = this.tooltip.GetToolTip(chart);
     593      oldTooltipText = tooltip.GetToolTip(chart);
    588594      if (newTooltipText != oldTooltipText)
    589         this.tooltip.SetToolTip(chart, newTooltipText);
     595        tooltip.SetToolTip(chart, newTooltipText);
    590596    }
    591597    #endregion
  • trunk/sources/HeuristicLab.Analysis.Statistics.Views/3.3/StatisticalTestsView.cs

    r12684 r12690  
    383383    private void CalculateAllGroupsTest() {
    384384      double pval = KruskalWallisTest.Test(data);
    385       pValTextBox.Text = pval.ToString();
    386       if (pval < significanceLevel) {
    387         Invoke(new Action(() => {
     385      DisplayAllGroupsTextResults(pval);
     386    }
     387
     388    private void DisplayAllGroupsTextResults(double pval) {
     389      if (InvokeRequired) {
     390        Invoke((Action<double>)DisplayAllGroupsTextResults, pval);
     391      } else {
     392        pValTextBox.Text = pval.ToString();
     393        if (pval < significanceLevel) {
    388394          groupCompLabel.Image = VSImageLibrary.Default;
    389395          groupComTextLabel.Text = "There are groups with different distributions";
    390         }));
    391       } else {
    392         Invoke(new Action(() => {
     396        } else {
    393397          groupCompLabel.Image = VSImageLibrary.Warning;
    394398          groupComTextLabel.Text = "Groups have an equal distribution";
    395         }));
     399        }
    396400      }
    397401    }
Note: See TracChangeset for help on using the changeset viewer.