Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
07/11/15 13:37:32 (9 years ago)
Author:
ascheibe
Message:

#2270 and #2354: merged r12173, r12458, r12077, r12599, r12613, r12112, r12116, r12117, r12131, r12631, r12672, r12684, r12690, r12692 into stable

Location:
stable
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • stable

  • stable/HeuristicLab.Analysis.Statistics.Views/3.3/ChartAnalysisView.cs

    r12009 r12725  
    2323using System.Collections.Generic;
    2424using System.Linq;
     25using System.Threading;
    2526using System.Threading.Tasks;
    2627using System.Windows.Forms;
     
    5253    private bool valuesAdded = false;
    5354    private bool suppressUpdates = false;
     55    private SemaphoreSlim sem = new SemaphoreSlim(1, 1);
    5456
    5557    public ChartAnalysisView() {
     
    108110
    109111    void Content_RowsChanged(object sender, EventArgs e) {
    110       RebuildDataTableAsync();
     112      if (suppressUpdates) return;
     113      if (InvokeRequired) Invoke((Action<object, EventArgs>)Content_RowsChanged, sender, e);
     114      else {
     115        RebuildDataTableAsync();
     116      }
    111117    }
    112118
    113119    void Content_ColumnsChanged(object sender, EventArgs e) {
    114       RebuildDataTableAsync();
     120      if (suppressUpdates) return;
     121      if (InvokeRequired) Invoke((Action<object, EventArgs>)Content_ColumnsChanged, sender, e);
     122      else {
     123        UpdateDataTableComboBox();
     124        RebuildDataTableAsync();
     125      }
    115126    }
    116127
    117128    private void Content_CollectionReset(object sender, CollectionItemsChangedEventArgs<IRun> e) {
    118       UpdateComboboxes();
    119       RebuildDataTableAsync();
     129      if (suppressUpdates) return;
     130      if (InvokeRequired) Invoke((Action<object, CollectionItemsChangedEventArgs<IRun>>)Content_CollectionReset, sender, e);
     131      else {
     132        UpdateComboboxes();
     133        RebuildDataTableAsync();
     134      }
    120135    }
    121136
    122137    private void Content_UpdateOfRunsInProgressChanged(object sender, EventArgs e) {
    123       suppressUpdates = Content.UpdateOfRunsInProgress;
    124 
    125       if (!suppressUpdates && !valuesAdded) {
    126         RebuildDataTableAsync();
    127       }
    128       if (valuesAdded) {
    129         valuesAdded = false;
     138      if (InvokeRequired) Invoke((Action<object, EventArgs>)Content_UpdateOfRunsInProgressChanged, sender, e);
     139      else {
     140        suppressUpdates = Content.UpdateOfRunsInProgress;
     141
     142        if (!suppressUpdates && !valuesAdded) {
     143          UpdateDataTableComboBox();
     144          RebuildDataTableAsync();
     145        }
     146        if (valuesAdded) {
     147          valuesAdded = false;
     148        }
    130149      }
    131150    }
     
    149168
    150169    private void dataRowComboBox_SelectedIndexChanged(object sender, EventArgs e) {
     170      if (suppressUpdates) return;
    151171      RebuildDataTableAsync();
    152172    }
     
    246266
    247267    private void RebuildDataTableAsync() {
    248       progress = MainFormManager.GetMainForm<MainForm.WindowsForms.MainForm>().AddOperationProgressToView(this, "Calculating values...");
    249 
    250268      string resultName = (string)dataTableComboBox.SelectedItem;
     269      if (string.IsNullOrEmpty(resultName)) return;
     270
    251271      string rowName = (string)dataRowComboBox.SelectedItem;
    252272
    253       var task = Task.Factory.StartNew(() => RebuildDataTable(resultName, rowName));
     273      var task = Task.Factory.StartNew(() => {
     274        sem.Wait();
     275        progress = MainFormManager.GetMainForm<MainForm.WindowsForms.MainForm>().AddOperationProgressToView(this, "Calculating values...");
     276        RebuildDataTable(resultName, rowName);
     277      });
    254278
    255279      task.ContinueWith((t) => {
    256280        MainFormManager.GetMainForm<MainForm.WindowsForms.MainForm>().RemoveOperationProgressFromView(this);
    257281        ErrorHandling.ShowErrorDialog("An error occured while calculating values. ", t.Exception);
     282        sem.Release();
    258283      }, TaskContinuationOptions.OnlyOnFaulted);
    259284
    260285      task.ContinueWith((t) => {
    261286        MainFormManager.GetMainForm<MainForm.WindowsForms.MainForm>().RemoveOperationProgressFromView(this);
     287        sem.Release();
    262288      }, TaskContinuationOptions.OnlyOnRanToCompletion);
    263289    }
     
    289315        double percentile25 = values.Percentile(0.25);
    290316        double percentile75 = values.Percentile(0.75);
    291         double lowerAvg = values.OrderBy(x => x).Take((int)(values.Count() * 0.25)).Average();
    292         double upperAvg = values.OrderByDescending(x => x).Take((int)(values.Count() * 0.25)).Average();
    293         double firstAvg = values.Take((int)(values.Count() * 0.25)).Average();
    294         double lastAvg = values.Skip((int)(values.Count() * 0.75)).Average();
     317        double lowerAvg = values.Count() > 4 ? values.OrderBy(x => x).Take((int)(values.Count() * 0.25)).Average() : double.NaN;
     318        double upperAvg = values.Count() > 4 ? values.OrderByDescending(x => x).Take((int)(values.Count() * 0.25)).Average() : double.NaN;
     319        double firstAvg = values.Count() > 4 ? values.Take((int)(values.Count() * 0.25)).Average() : double.NaN;
     320        double lastAvg = values.Count() > 4 ? values.Skip((int)(values.Count() * 0.75)).Average() : double.NaN;
    295321        double slope, intercept, r;
    296322        llsFitting.Calculate(values, out slope, out intercept);
  • stable/HeuristicLab.Analysis.Statistics.Views/3.3/CorrelationView.cs

    r12199 r12725  
    2323using System.Collections.Generic;
    2424using System.Linq;
     25using HeuristicLab.Collections;
    2526using HeuristicLab.Core.Views;
    2627using HeuristicLab.Data;
     
    8182      Content.ColumnsChanged += Content_ColumnsChanged;
    8283      Content.RowsChanged += Content_RowsChanged;
    83       Content.CollectionReset += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_CollectionReset);
     84      Content.CollectionReset += new CollectionItemsChangedEventHandler<IRun>(Content_CollectionReset);
    8485      Content.UpdateOfRunsInProgressChanged += Content_UpdateOfRunsInProgressChanged;
    8586    }
     
    8990      Content.ColumnsChanged -= Content_ColumnsChanged;
    9091      Content.RowsChanged -= Content_RowsChanged;
    91       Content.CollectionReset -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_CollectionReset);
     92      Content.CollectionReset -= new CollectionItemsChangedEventHandler<IRun>(Content_CollectionReset);
    9293      Content.UpdateOfRunsInProgressChanged -= Content_UpdateOfRunsInProgressChanged;
    9394    }
    9495
    9596    void Content_RowsChanged(object sender, EventArgs e) {
    96       UpdateUI();
     97      if (suppressUpdates) return;
     98      if (InvokeRequired) Invoke((Action<object, EventArgs>)Content_RowsChanged, sender, e);
     99      else {
     100        UpdateUI();
     101      }
    97102    }
    98103
    99104    void Content_ColumnsChanged(object sender, EventArgs e) {
    100       UpdateUI();
    101     }
    102 
    103     private void Content_CollectionReset(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<IRun> e) {
    104       UpdateUI();
     105      if (suppressUpdates) return;
     106      if (InvokeRequired) Invoke((Action<object, EventArgs>)Content_ColumnsChanged, sender, e);
     107      else {
     108        UpdateUI();
     109      }
     110    }
     111
     112    private void Content_CollectionReset(object sender, CollectionItemsChangedEventArgs<IRun> e) {
     113      if (suppressUpdates) return;
     114      if (InvokeRequired) Invoke((Action<object, CollectionItemsChangedEventArgs<IRun>>)Content_CollectionReset, sender, e);
     115      else {
     116        UpdateUI();
     117      }
    105118    }
    106119
    107120    private void Content_UpdateOfRunsInProgressChanged(object sender, EventArgs e) {
    108       suppressUpdates = Content.UpdateOfRunsInProgress;
    109       UpdateUI();
     121      if (InvokeRequired) Invoke((Action<object, EventArgs>)Content_UpdateOfRunsInProgressChanged, sender, e);
     122      else {
     123        suppressUpdates = Content.UpdateOfRunsInProgress;
     124        UpdateUI();
     125      }
    110126    }
    111127    #endregion
    112128
    113129    private void UpdateUI() {
    114       if (!suppressUpdates) {
    115         RebuildCorrelationTable();
    116       }
     130      RebuildCorrelationTable();
    117131    }
    118132
  • stable/HeuristicLab.Analysis.Statistics.Views/3.3/SampleSizeInfluenceView.cs

    r12009 r12725  
    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 {
     
    4143    private const string BoxPlotSeriesName = "BoxPlotSeries";
    4244    private const string BoxPlotChartAreaName = "BoxPlotChartArea";
    43     private const string delimitor = ";";
    44 
    45     private bool suppressUpdates = false;
     45    private const string delimiter = ";";
     46
     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.Reset += new EventHandler(Content_Reset);
    75       Content.ColumnNamesChanged += new EventHandler(Content_ColumnNamesChanged);
    76       Content.ItemsAdded += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_ItemsAdded);
    77       Content.ItemsRemoved += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_ItemsRemoved);
    78       Content.CollectionReset += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_CollectionReset);
    79       Content.UpdateOfRunsInProgressChanged += new EventHandler(Content_UpdateOfRunsInProgressChanged);
    80       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;
    8182      RegisterRunEvents(Content);
    8283    }
    8384    protected override void DeregisterContentEvents() {
    8485      base.DeregisterContentEvents();
    85       Content.Reset -= new EventHandler(Content_Reset);
    86       Content.ColumnNamesChanged -= new EventHandler(Content_ColumnNamesChanged);
    87       Content.ItemsAdded -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_ItemsAdded);
    88       Content.ItemsRemoved -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_ItemsRemoved);
    89       Content.CollectionReset -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_CollectionReset);
    90       Content.UpdateOfRunsInProgressChanged -= new EventHandler(Content_UpdateOfRunsInProgressChanged);
    91       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;
    9292      DeregisterRunEvents(Content);
    9393    }
     
    142142      DeregisterRunEvents(e.OldItems);
    143143      RegisterRunEvents(e.Items);
    144       UpdateAll();
     144      if (!suppressUpdates) UpdateAll();
    145145    }
    146146    private void Content_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IRun> e) {
    147147      DeregisterRunEvents(e.Items);
    148       UpdateComboBoxes();
     148      if (!suppressUpdates) UpdateComboBoxes();
    149149    }
    150150    private void Content_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IRun> e) {
    151151      RegisterRunEvents(e.Items);
    152       UpdateComboBoxes();
     152      if (!suppressUpdates) UpdateComboBoxes();
    153153    }
    154154    private void Content_UpdateOfRunsInProgressChanged(object sender, EventArgs e) {
     
    157157      else {
    158158        suppressUpdates = Content.UpdateOfRunsInProgress;
    159         if (!suppressUpdates) UpdateDataPoints();
    160       }
    161     }
    162 
    163     private void Content_Reset(object sender, EventArgs e) {
    164       if (InvokeRequired)
    165         Invoke(new EventHandler(Content_Reset), sender, e);
    166       else {
    167         this.categoricalMapping.Clear();
    168         UpdateDataPoints();
    169         UpdateAxisLabels();
     159        if (!suppressUpdates) UpdateAll();
    170160      }
    171161    }
     
    174164        Invoke(new EventHandler(Content_ColumnNamesChanged), sender, e);
    175165      else {
    176         UpdateComboBoxes();
     166        if (!suppressUpdates) UpdateComboBoxes();
    177167      }
    178168    }
    179169    private void run_Changed(object sender, EventArgs e) {
    180170      if (InvokeRequired)
    181         this.Invoke(new EventHandler(run_Changed), sender, e);
    182       else if (!suppressUpdates) {
    183         UpdateDataPoints();
    184       }
     171        Invoke(new EventHandler(run_Changed), sender, e);
     172      else if (!suppressUpdates) UpdateDataPoints();
    185173    }
    186174
     
    188176      if (InvokeRequired)
    189177        Invoke(new EventHandler(Content_AlgorithmNameChanged), sender, e);
    190       else UpdateCaption();
     178      else if (!suppressUpdates) UpdateCaption();
    191179    }
    192180    #endregion
     
    199187
    200188    private void UpdateAll() {
    201       this.categoricalMapping.Clear();
     189      categoricalMapping.Clear();
    202190      UpdateComboBoxes();
    203191      UpdateDataPoints();
     
    210198
    211199    private void UpdateSampleSizes(bool forceUpdate = false) {
    212       string selectedYAxis = (string)this.yAxisComboBox.SelectedItem;
     200      string selectedYAxis = (string)yAxisComboBox.SelectedItem;
    213201
    214202      if (selectedYAxis != null && (xAxisTextBox.Text.Trim() == string.Empty || forceUpdate)) {
     
    224212        if (values.Any()) {
    225213          if (hypergeometricCheckBox.Checked) {
    226             xAxisTextBox.Text += ((int)(values.Count() / 16)) + delimitor + " ";
    227             xAxisTextBox.Text += ((int)(values.Count() / 8)) + delimitor + " ";
    228             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;
    229217          } else {
    230             xAxisTextBox.Text += ((int)(values.Count() / 4)) + delimitor + " ";
    231             xAxisTextBox.Text += ((int)(values.Count() / 2)) + delimitor + " ";
    232             xAxisTextBox.Text += ((int)(values.Count() / 4 * 3)) + delimitor + " ";
    233             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();
    234222          }
    235223        }
     
    238226
    239227    private void UpdateComboBoxes() {
    240       string selectedYAxis = (string)this.yAxisComboBox.SelectedItem;
    241       this.xAxisTextBox.Text = string.Empty;
    242       this.yAxisComboBox.Items.Clear();
    243       if (Content != null) {
    244         string[] additionalAxisDimension = Enum.GetNames(typeof(AxisDimension));
    245         UpdateSampleSizes();
    246         this.yAxisComboBox.Items.AddRange(additionalAxisDimension);
    247         this.yAxisComboBox.Items.AddRange(Matrix.ColumnNames.ToArray());
    248 
    249         if (selectedYAxis != null && yAxisComboBox.Items.Contains(selectedYAxis)) {
    250           yAxisComboBox.SelectedItem = selectedYAxis;
    251           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          }
    252244        }
    253245      }
     
    255247
    256248    private void UpdateDataPoints() {
    257       this.chart.Series.Clear();
    258       this.seriesCache.Clear();
     249      chart.Series.Clear();
     250      seriesCache.Clear();
    259251      if (Content != null) {
    260252        var usableRuns = Content.Where(r => r.Visible).ToList();
     
    267259        }
    268260
    269         foreach (Series s in this.seriesCache.Values)
    270           this.chart.Series.Add(s);
     261        foreach (Series s in seriesCache.Values)
     262          chart.Series.Add(s);
    271263
    272264        UpdateStatistics();
    273265        if (seriesCache.Count > 0) {
    274266          Series boxPlotSeries = CreateBoxPlotSeries();
    275           this.chart.Series.Add(boxPlotSeries);
     267          chart.Series.Add(boxPlotSeries);
    276268        }
    277269
     
    328320
    329321      if (!yAxisComboBox.DroppedDown)
    330         this.yAxisValue = (string)yAxisComboBox.SelectedItem;
    331 
    332       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();
    333325      if (yValue.Any(x => !x.HasValue)) return;
    334326
     
    338330
    339331    private List<int> ParseGroupSizesFromText(string groupsText, bool verbose = true) {
    340       string[] gs = groupsText.Split(delimitor.ToCharArray());
     332      string[] gs = groupsText.Split(delimiter.ToCharArray());
    341333      List<int> vals = new List<int>();
    342334
     
    352344          catch (Exception ex) {
    353345            if (verbose) {
    354               ErrorHandling.ShowErrorDialog("Can't parse group sizes. Please only use numbers seperated by a " + delimitor + ". ", ex);
     346              ErrorHandling.ShowErrorDialog("Can't parse group sizes. Please only use numbers seperated by a " + delimiter + ". ", ex);
    355347            }
    356348          }
     
    384376      }
    385377      matrix.ColumnNames = columnNames;
    386       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." };
    387379
    388380      for (int i = 0; i < seriesCache.Count; i++) {
     
    415407      boxPlotSeries["BoxPlotShowUnusualValues"] = "true";
    416408      boxPlotSeries["PointWidth"] = "0.4";
    417       boxPlotSeries.BackGradientStyle = System.Windows.Forms.DataVisualization.Charting.GradientStyle.VerticalCenter;
    418       boxPlotSeries.BackSecondaryColor = System.Drawing.Color.FromArgb(130, 224, 64, 10);
    419       boxPlotSeries.BorderColor = System.Drawing.Color.FromArgb(64, 64, 64);
    420       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);
    421413
    422414      return boxPlotSeries;
     
    429421
    430422      if (!yAxisComboBox.DroppedDown)
    431         this.yAxisValue = (string)yAxisComboBox.SelectedItem;
     423        yAxisValue = (string)yAxisComboBox.SelectedItem;
    432424
    433425      xValue = idx;
    434       yValue = GetValue(run, this.yAxisValue);
     426      yValue = GetValue(run, yAxisValue);
    435427
    436428      if (yValue.HasValue) {
    437         if (!this.seriesCache.ContainsKey(xValue))
     429        if (!seriesCache.ContainsKey(xValue))
    438430          seriesCache[xValue] = new Series(xValue.ToString());
    439431
     
    454446        AxisDimension axisDimension = (AxisDimension)Enum.Parse(typeof(AxisDimension), columnName);
    455447        return GetValue(run, axisDimension);
    456       } else {
    457         int columnIndex = Matrix.ColumnNames.ToList().IndexOf(columnName);
    458         IItem value = Content.GetValue(run, columnIndex);
    459         if (value == null)
    460           return null;
    461 
    462         DoubleValue doubleValue = value as DoubleValue;
    463         IntValue intValue = value as IntValue;
    464         TimeSpanValue timeSpanValue = value as TimeSpanValue;
    465         double? ret = null;
    466         if (doubleValue != null) {
    467           if (!double.IsNaN(doubleValue.Value) && !double.IsInfinity(doubleValue.Value))
    468             ret = doubleValue.Value;
    469         } else if (intValue != null)
    470           ret = intValue.Value;
    471         else if (timeSpanValue != null) {
    472           ret = timeSpanValue.Value.TotalSeconds;
    473         } else
    474           ret = GetCategoricalValue(columnIndex, value.ToString());
    475 
    476         return ret;
    477       }
     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;
    478469    }
    479470    private double GetCategoricalValue(int dimension, string value) {
    480       if (!this.categoricalMapping.ContainsKey(dimension)) {
    481         this.categoricalMapping[dimension] = new Dictionary<object, double>();
     471      if (!categoricalMapping.ContainsKey(dimension)) {
     472        categoricalMapping[dimension] = new Dictionary<object, double>();
    482473        var orderedCategories = Content.Where(r => r.Visible && Content.GetValue(r, dimension) != null).Select(r => Content.GetValue(r, dimension).ToString())
    483474                                          .Distinct().OrderBy(x => x, new NaturalStringComparer());
    484475        int count = 1;
    485476        foreach (var category in orderedCategories) {
    486           this.categoricalMapping[dimension].Add(category, count);
     477          categoricalMapping[dimension].Add(category, count);
    487478          count++;
    488479        }
    489480      }
    490       return this.categoricalMapping[dimension][value];
     481      return categoricalMapping[dimension][value];
    491482    }
    492483    private double GetValue(IRun run, AxisDimension axisDimension) {
     
    498489          }
    499490        default: {
    500             throw new ArgumentException("No handling strategy for " + axisDimension.ToString() + " is defined.");
     491            throw new ArgumentException("No handling strategy for " + axisDimension + " is defined.");
    501492          }
    502493      }
     
    507498    #region GUI events
    508499    private void UpdateNoRunsVisibleLabel() {
    509       if (this.chart.Series.Count > 0) {
     500      if (chart.Series.Count > 0) {
    510501        noRunsLabel.Visible = false;
    511502        showStatisticsCheckBox.Enabled = true;
     
    532523    }
    533524    private void UpdateAxisLabels() {
    534       Axis xAxis = this.chart.ChartAreas[BoxPlotChartAreaName].AxisX;
    535       Axis yAxis = this.chart.ChartAreas[BoxPlotChartAreaName].AxisY;
     525      Axis xAxis = chart.ChartAreas[BoxPlotChartAreaName].AxisX;
     526      Axis yAxis = chart.ChartAreas[BoxPlotChartAreaName].AxisY;
    536527      int axisDimensionCount = Enum.GetNames(typeof(AxisDimension)).Count();
    537528
     
    544535    }
    545536
    546     private void chart_AxisViewChanged(object sender, System.Windows.Forms.DataVisualization.Charting.ViewEventArgs e) {
    547       this.UpdateAxisLabels();
     537    private void chart_AxisViewChanged(object sender, ViewEventArgs e) {
     538      UpdateAxisLabels();
    548539    }
    549540
     
    566557        }
    567558      } else if (dimension > 0 && Content.GetValue(0, dimension) is TimeSpanValue) {
    568         this.chart.ChartAreas[0].RecalculateAxesScale();
    569         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();
    570561        if (correspondingAxis == null)
    571562          correspondingAxis = axis;
    572563        for (double i = correspondingAxis.Minimum; i <= correspondingAxis.Maximum; i += correspondingAxis.LabelStyle.Interval) {
    573564          TimeSpan time = TimeSpan.FromSeconds(i);
    574           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);
    575566          axis.CustomLabels.Add(i - correspondingAxis.LabelStyle.Interval / 2, i + correspondingAxis.LabelStyle.Interval / 2, x);
    576567        }
     
    595586      string newTooltipText = string.Empty;
    596587      string oldTooltipText;
    597       HitTestResult h = this.chart.HitTest(e.X, e.Y);
     588      HitTestResult h = chart.HitTest(e.X, e.Y);
    598589      if (h.ChartElementType == ChartElementType.AxisLabels) {
    599590        newTooltipText = ((CustomLabel)h.Object).ToolTip;
    600591      }
    601592
    602       oldTooltipText = this.tooltip.GetToolTip(chart);
     593      oldTooltipText = tooltip.GetToolTip(chart);
    603594      if (newTooltipText != oldTooltipText)
    604         this.tooltip.SetToolTip(chart, newTooltipText);
     595        tooltip.SetToolTip(chart, newTooltipText);
    605596    }
    606597    #endregion
     
    623614          string newVals = "";
    624615          foreach (int v in values) {
    625             newVals += v + delimitor + " ";
     616            newVals += v + delimiter + " ";
    626617          }
    627618          xAxisTextBox.Text = newVals;
  • stable/HeuristicLab.Analysis.Statistics.Views/3.3/StatisticalTestsView.cs

    r12009 r12725  
    2727using HeuristicLab.Collections;
    2828using HeuristicLab.Common;
     29using HeuristicLab.Common.Resources;
    2930using HeuristicLab.Core.Views;
    3031using HeuristicLab.Data;
     
    4041    private const int requiredSampleSize = 5;
    4142    private double[][] data;
     43    private bool suppressUpdates;
     44    private bool initializing;
    4245
    4346    public double SignificanceLevel {
     
    7679
    7780      if (Content != null) {
    78         UpdateResultComboBox();
    79         UpdateGroupsComboBox();
    80         RebuildDataTable();
     81        UpdateUI();
     82      } else {
     83        ResetUI();
    8184      }
    8285      UpdateCaption();
     86    }
     87
     88    private void UpdateUI() {
     89      initializing = true;
     90      UpdateResultComboBox();
     91      UpdateGroupsComboBox();
     92      RebuildDataTable();
     93      FillCompComboBox();
     94      ResetUI();
     95      CalculateValues();
     96      initializing = false;
    8397    }
    8498
     
    92106      Content.ColumnsChanged += Content_ColumnsChanged;
    93107      Content.RowsChanged += Content_RowsChanged;
    94       Content.CollectionReset += new CollectionItemsChangedEventHandler<IRun>(Content_CollectionReset);
     108      Content.CollectionReset += Content_CollectionReset;
    95109      Content.UpdateOfRunsInProgressChanged += Content_UpdateOfRunsInProgressChanged;
    96110    }
     
    100114      Content.ColumnsChanged -= Content_ColumnsChanged;
    101115      Content.RowsChanged -= Content_RowsChanged;
    102       Content.CollectionReset -= new CollectionItemsChangedEventHandler<IRun>(Content_CollectionReset);
     116      Content.CollectionReset -= Content_CollectionReset;
    103117      Content.UpdateOfRunsInProgressChanged -= Content_UpdateOfRunsInProgressChanged;
    104118    }
    105119
    106120    void Content_RowsChanged(object sender, EventArgs e) {
     121      if (suppressUpdates) return;
     122      if (InvokeRequired) Invoke((Action<object, EventArgs>)Content_RowsChanged, sender, e);
     123      else {
     124        UpdateUI();
     125      }
     126    }
     127
     128    void Content_ColumnsChanged(object sender, EventArgs e) {
     129      if (suppressUpdates) return;
     130      if (InvokeRequired) Invoke((Action<object, EventArgs>)Content_ColumnsChanged, sender, e);
     131      else {
     132        UpdateUI();
     133      }
     134    }
     135
     136    private void Content_CollectionReset(object sender, CollectionItemsChangedEventArgs<IRun> e) {
     137      if (suppressUpdates) return;
     138      if (InvokeRequired) Invoke((Action<object, CollectionItemsChangedEventArgs<IRun>>)Content_CollectionReset, sender, e);
     139      else {
     140        UpdateUI();
     141      }
     142    }
     143
     144    void Content_UpdateOfRunsInProgressChanged(object sender, EventArgs e) {
     145      if (InvokeRequired) Invoke((Action<object, EventArgs>)Content_UpdateOfRunsInProgressChanged, sender, e);
     146      else {
     147        suppressUpdates = Content.UpdateOfRunsInProgress;
     148        if (!suppressUpdates) UpdateUI();
     149      }
     150    }
     151
     152    private void openBoxPlotToolStripMenuItem_Click(object sender, EventArgs e) {
     153      RunCollectionBoxPlotView boxplotView = new RunCollectionBoxPlotView();
     154      boxplotView.Content = Content;
     155      boxplotView.SetXAxis(groupComboBox.SelectedItem.ToString());
     156      boxplotView.SetYAxis(resultComboBox.SelectedItem.ToString());
     157
     158      boxplotView.Show();
     159    }
     160
     161    private void groupCompComboBox_SelectedValueChanged(object sender, EventArgs e) {
     162      if (initializing || suppressUpdates) return;
     163      string curItem = (string)groupCompComboBox.SelectedItem;
     164      CalculatePairwise(curItem);
     165    }
     166
     167    private void resultComboBox_SelectedValueChanged(object sender, EventArgs e) {
     168      if (initializing || suppressUpdates) return;
    107169      RebuildDataTable();
    108     }
    109 
    110     void Content_ColumnsChanged(object sender, EventArgs e) {
     170      ResetUI();
     171      CalculateValues();
     172    }
     173
     174    private void groupComboBox_SelectedValueChanged(object sender, EventArgs e) {
     175      if (initializing || suppressUpdates) return;
    111176      RebuildDataTable();
    112     }
    113 
    114     private void Content_CollectionReset(object sender, CollectionItemsChangedEventArgs<IRun> e) {
    115       RebuildDataTable();
    116     }
    117 
    118     void Content_UpdateOfRunsInProgressChanged(object sender, EventArgs e) {
    119       if (!Content.UpdateOfRunsInProgress) {
    120         RebuildDataTable();
    121       }
     177      FillCompComboBox();
     178      ResetUI();
     179      CalculateValues();
    122180    }
    123181    #endregion
    124182
    125183    private void UpdateGroupsComboBox() {
     184      string selectedItem = (string)groupComboBox.SelectedItem;
     185
    126186      groupComboBox.Items.Clear();
    127 
    128187      var parameters = (from run in Content
    129188                        where run.Visible
     
    154213        }
    155214
    156         if (possibleIndizes.Count > 0) {
     215        if (selectedItem != null && groupComboBox.Items.Contains(selectedItem)) {
     216          groupComboBox.SelectedItem = selectedItem;
     217        } else if (possibleIndizes.Count > 0) {
    157218          groupComboBox.SelectedItem = groupComboBox.Items[possibleIndizes.First()];
    158         } else {
    159           groupComboBox.SelectedItem = groupComboBox.Items[0];
    160219        }
    161220      }
     
    169228
    170229    private void UpdateResultComboBox() {
     230      string selectedItem = (string)resultComboBox.SelectedItem;
     231
    171232      resultComboBox.Items.Clear();
    172233      var results = (from run in Content
     
    177238
    178239      resultComboBox.Items.AddRange(results);
    179       if (resultComboBox.Items.Count > 0) resultComboBox.SelectedItem = resultComboBox.Items[0];
     240
     241      if (selectedItem != null && resultComboBox.Items.Contains(selectedItem)) {
     242        resultComboBox.SelectedItem = selectedItem;
     243      } else if (resultComboBox.Items.Count > 0) {
     244        resultComboBox.SelectedItem = resultComboBox.Items[0];
     245      }
    180246    }
    181247
    182248    private void FillCompComboBox() {
     249      string selectedItem = (string)groupCompComboBox.SelectedItem;
    183250      string parameterName = (string)groupComboBox.SelectedItem;
    184251      if (parameterName != null) {
     
    189256          groupCompComboBox.Items.Clear();
    190257          columnNames.ForEach(x => groupCompComboBox.Items.Add(x));
    191           if (groupCompComboBox.Items.Count > 0) groupCompComboBox.SelectedItem = groupCompComboBox.Items[0];
     258          if (selectedItem != null && groupCompComboBox.Items.Contains(selectedItem)) {
     259            groupCompComboBox.SelectedItem = selectedItem;
     260          } else if (groupCompComboBox.Items.Count > 0) {
     261            groupCompComboBox.SelectedItem = groupCompComboBox.Items[0];
     262          }
    192263        }
    193264      }
     
    259330    }
    260331
    261     private void resultComboBox_SelectedValueChanged(object sender, EventArgs e) {
    262       RebuildDataTable();
    263       ResetUI();
    264       CalculateValues();
    265     }
    266 
    267     private void groupComboBox_SelectedValueChanged(object sender, EventArgs e) {
    268       RebuildDataTable();
    269       FillCompComboBox();
    270       ResetUI();
    271       CalculateValues();
    272     }
    273 
    274332    private bool VerifyDataLength(bool showMessage) {
    275333      if (data == null || data.Length == 0)
     
    277335
    278336      //alglib needs at least 5 samples for computation
    279       if (data.Any(x => x.Length <= requiredSampleSize)) {
     337      if (data.Any(x => x.Length < requiredSampleSize)) {
    280338        if (showMessage)
    281339          MessageBox.Show(this, "You need at least " + requiredSampleSize
     
    291349        return;
    292350
    293       if (data != null) {
    294         MainFormManager.GetMainForm<HeuristicLab.MainForm.WindowsForms.MainForm>()
     351      if (data != null && data.All(x => x != null)) {
     352        MainFormManager.GetMainForm<MainForm.WindowsForms.MainForm>()
    295353          .AddOperationProgressToView(this, "Calculating...");
    296354
     
    305363      CalculatePairwiseTest(groupName);
    306364
    307       MainFormManager.GetMainForm<HeuristicLab.MainForm.WindowsForms.MainForm>().RemoveOperationProgressFromView(this);
     365      MainFormManager.GetMainForm<MainForm.WindowsForms.MainForm>().RemoveOperationProgressFromView(this);
    308366    }
    309367
    310368    private void CalculatePairwise(string groupName) {
     369      if (groupName == null) return;
    311370      if (!VerifyDataLength(false))
    312371        return;
    313372
    314       MainFormManager.GetMainForm<HeuristicLab.MainForm.WindowsForms.MainForm>().AddOperationProgressToView(pairwiseTestGroupBox, "Calculating...");
     373      MainFormManager.GetMainForm<MainForm.WindowsForms.MainForm>().AddOperationProgressToView(pairwiseTestGroupBox, "Calculating...");
    315374      Task.Factory.StartNew(() => CalculatePairwiseAsync(groupName));
    316375    }
     
    319378      CalculatePairwiseTest(groupName);
    320379
    321       MainFormManager.GetMainForm<HeuristicLab.MainForm.WindowsForms.MainForm>().RemoveOperationProgressFromView(pairwiseTestGroupBox);
     380      MainFormManager.GetMainForm<MainForm.WindowsForms.MainForm>().RemoveOperationProgressFromView(pairwiseTestGroupBox);
    322381    }
    323382
    324383    private void CalculateAllGroupsTest() {
    325384      double pval = KruskalWallisTest.Test(data);
    326       pValTextBox.Text = pval.ToString();
    327       if (pval < significanceLevel) {
    328         this.Invoke(new Action(() => {
    329           groupCompLabel.Image = HeuristicLab.Common.Resources.VSImageLibrary.Default;
     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) {
     394          groupCompLabel.Image = VSImageLibrary.Default;
    330395          groupComTextLabel.Text = "There are groups with different distributions";
    331         }));
    332       } else {
    333         this.Invoke(new Action(() => {
    334           groupCompLabel.Image = HeuristicLab.Common.Resources.VSImageLibrary.Warning;
     396        } else {
     397          groupCompLabel.Image = VSImageLibrary.Warning;
    335398          groupComTextLabel.Text = "Groups have an equal distribution";
    336         }));
     399        }
    337400      }
    338401    }
     
    343406      DoubleMatrix pValsMatrix = new DoubleMatrix(1, stringConvertibleMatrixView.Content.Columns);
    344407      pValsMatrix.ColumnNames = stringConvertibleMatrixView.Content.ColumnNames;
    345       pValsMatrix.RowNames = new string[] { "p-Value" };
     408      pValsMatrix.RowNames = new[] { "p-Value" };
    346409
    347410      for (int i = 0; i < data.Length; i++) {
     
    353416      // p-value is below significance level and thus the null hypothesis (data is normally distributed) is rejected
    354417      if (res.Any(x => x < significanceLevel)) {
    355         this.Invoke(new Action(() => {
    356           normalityLabel.Image = HeuristicLab.Common.Resources.VSImageLibrary.Warning;
     418        Invoke(new Action(() => {
     419          normalityLabel.Image = VSImageLibrary.Warning;
    357420          normalityTextLabel.Text = "Some groups may not be normally distributed";
    358421        }));
    359422      } else {
    360         this.Invoke(new Action(() => {
    361           normalityLabel.Image = HeuristicLab.Common.Resources.VSImageLibrary.Default;
     423        Invoke(new Action(() => {
     424          normalityLabel.Image = VSImageLibrary.Default;
    362425          normalityTextLabel.Text = "All sample data is normally distributed";
    363426        }));
    364427      }
    365428
    366       this.Invoke(new Action(() => {
     429      Invoke(new Action(() => {
    367430        normalityStringConvertibleMatrixView.Content = pValsMatrix;
    368431        normalityStringConvertibleMatrixView.DataGridView.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);
     
    372435    private void ShowPairwiseResult(int nrOfEqualDistributions) {
    373436      double ratio = ((double)nrOfEqualDistributions) / (data.Length - 1) * 100.0;
    374       equalDistsTextBox.Text = ratio.ToString() + " %";
     437      equalDistsTextBox.Text = ratio + " %";
    375438
    376439      if (nrOfEqualDistributions == 0) {
    377         this.Invoke(new Action(() => {
    378           pairwiseLabel.Image = HeuristicLab.Common.Resources.VSImageLibrary.Default;
     440        Invoke(new Action(() => {
     441          pairwiseLabel.Image = VSImageLibrary.Default;
    379442          pairwiseTextLabel.Text = "All groups have different distributions";
    380443        }));
    381444      } else {
    382         this.Invoke(new Action(() => {
    383           pairwiseLabel.Image = HeuristicLab.Common.Resources.VSImageLibrary.Warning;
     445        Invoke(new Action(() => {
     446          pairwiseLabel.Image = VSImageLibrary.Warning;
    384447          pairwiseTextLabel.Text = "Some groups have equal distributions";
    385448        }));
     
    394457      double[][] newData = FilterDataForPairwiseTest(colIndex);
    395458
    396       var rowNames = new string[] { "p-Value of Mann-Whitney U", "Adjusted p-Value of Mann-Whitney U",
     459      var rowNames = new[] { "p-Value of Mann-Whitney U", "Adjusted p-Value of Mann-Whitney U",
    397460            "p-Value of T-Test", "Adjusted p-Value of T-Test", "Cohen's d", "Hedges' g" };
    398461
     
    433496      }
    434497
    435       this.Invoke(new Action(() => {
     498      Invoke(new Action(() => {
    436499        pairwiseStringConvertibleMatrixView.Content = pValsMatrix;
    437500        pairwiseStringConvertibleMatrixView.DataGridView.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);
     
    469532      return newData;
    470533    }
    471 
    472     private void openBoxPlotToolStripMenuItem_Click(object sender, EventArgs e) {
    473       RunCollectionBoxPlotView boxplotView = new RunCollectionBoxPlotView();
    474       boxplotView.Content = Content;
    475       boxplotView.SetXAxis(groupComboBox.SelectedItem.ToString());
    476       boxplotView.SetYAxis(resultComboBox.SelectedItem.ToString());
    477 
    478       boxplotView.Show();
    479     }
    480 
    481     private void groupCompComboBox_SelectedValueChanged(object sender, EventArgs e) {
    482       string curItem = (string)groupCompComboBox.SelectedItem;
    483       CalculatePairwise(curItem);
    484     }
    485534  }
    486535}
Note: See TracChangeset for help on using the changeset viewer.