Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
12/04/14 10:59:47 (10 years ago)
Author:
ascheibe
Message:

#2031 implemented review comments for correlations view

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/StatisticalTesting/HeuristicLab.Analysis.Statistics/3.3/CorrelationView.cs

    r11379 r11644  
    3434  [Content(typeof(RunCollection), false)]
    3535  public sealed partial class CorrelationView : ItemView {
     36    private const string PearsonName = "Pearson product-moment correlation coefficient";
     37    private const string SpearmanName = "Spearman's rank correlation coefficient";
     38
    3639    private enum ResultParameterType {
    3740      Result,
     
    3942    }
    4043
     44    private bool suppressUpdates = false;
     45
    4146    public new RunCollection Content {
    4247      get { return (RunCollection)base.Content; }
     
    4853      set { /*not needed because results are always readonly */}
    4954    }
    50 
    51     private Dictionary<string, ResultParameterType> resultsParameters;
    52     private bool suppressUpdates = false;
    5355
    5456    public CorrelationView() {
     
    5658      stringConvertibleMatrixView.Minimum = -1.0;
    5759      stringConvertibleMatrixView.Maximum = 1.0;
     60
     61      methodComboBox.Items.Add(PearsonName);
     62      methodComboBox.Items.Add(SpearmanName);
     63      methodComboBox.SelectedIndex = 0;
    5864    }
    5965
    6066    protected override void OnContentChanged() {
    6167      base.OnContentChanged();
    62       resultComboBox.Items.Clear();
    6368
    6469      if (Content != null) {
    65         UpdateResultComboBox();
     70        RebuildCorrelationTable();
    6671      }
    6772      UpdateCaption();
     
    174179    private void UpdateUI() {
    175180      if (!suppressUpdates) {
    176         UpdateResultComboBox();
    177181        RebuildCorrelationTable();
    178       }
    179     }
    180 
    181     private void UpdateResultComboBox() {
    182       resultsParameters = GetRowNames();
    183       string selectedResult = (string)this.resultComboBox.SelectedItem;
    184       resultComboBox.Items.Clear();
    185       resultComboBox.Items.AddRange(resultsParameters.Keys.ToArray());
    186 
    187       if (selectedResult != null && resultComboBox.Items.Contains(selectedResult)) {
    188         resultComboBox.SelectedItem = selectedResult;
    189       } else if (resultComboBox.Items.Count > 0) {
    190         resultComboBox.SelectedItem = resultComboBox.Items[0];
    191182      }
    192183    }
     
    197188                     from result in run.Results
    198189                     where result.Value is DoubleValue || result.Value is IntValue
    199                      select result.Key).Distinct().ToList();
     190                     select result.Key).Distinct().OrderBy(x => x).ToList();
    200191
    201192      return results;
     
    207198                        from parameter in run.Parameters
    208199                        where parameter.Value is DoubleValue || parameter.Value is IntValue
    209                         select parameter.Key).Distinct().ToList();
     200                        select parameter.Key).Distinct().OrderBy(x => x).ToList();
    210201
    211202      return parameters;
     
    256247    }
    257248
     249    private List<double> GetValuesFromResultsParameters(IEnumerable<IRun> runs, string name, ResultParameterType type) {
     250      if (type == ResultParameterType.Parameter) {
     251        return GetDoublesFromParameters(runs.Where(x => x.Parameters.ContainsKey(name)).ToList(), name);
     252      } else if (type == ResultParameterType.Result) {
     253        return GetDoublesFromResults(runs.Where(x => x.Results.ContainsKey(name)).ToList(), name);
     254      } else {
     255        return null;
     256      }
     257    }
     258
    258259    private void RebuildCorrelationTable() {
    259       string resultName = (string)resultComboBox.SelectedItem;
    260 
    261       var columnNames = new string[2];
    262       columnNames[0] = "Pearson product-moment correlation coefficient";
    263       columnNames[1] = "Spearman's rank correlation coefficient";
    264 
    265       var runs = Content.Where(x => x.Results.ContainsKey(resultName) && x.Visible);
     260      Dictionary<string, ResultParameterType> resultsParameters = GetRowNames();
     261      string methodName = (string)methodComboBox.SelectedItem;
     262      var columnNames = resultsParameters.Keys.ToArray();
     263
     264      var runs = Content.Where(x => x.Visible);
    266265
    267266      DoubleMatrix dt = new DoubleMatrix(resultsParameters.Count(), columnNames.Count());
    268       dt.RowNames = resultsParameters.Keys.ToArray();
     267      dt.RowNames = columnNames;
    269268      dt.ColumnNames = columnNames;
    270269
    271       int j = 0;
    272       foreach (var rowName in resultsParameters) {
    273         var resultVals = GetDoublesFromResults(runs.Where(x => x.Results.ContainsKey(resultName)).Where(x => x.Results[resultName] is DoubleValue || x.Results[resultName] is IntValue).ToList(), resultName);
    274 
    275         List<double> resultRowVals;
    276         if (rowName.Value == ResultParameterType.Result) {
    277           resultRowVals = GetDoublesFromResults(runs.Where(x => x.Results.ContainsKey(rowName.Key)).Where(x => x.Results[rowName.Key] is DoubleValue || x.Results[rowName.Key] is IntValue).ToList(), rowName.Key);
    278         } else {
    279           resultRowVals = GetDoublesFromParameters(runs.Where(x => x.Parameters.ContainsKey(rowName.Key)).Where(x => x.Parameters[rowName.Key] is DoubleValue || x.Parameters[rowName.Key] is IntValue).ToList(), rowName.Key);
     270      int i = 0;
     271      foreach (var res in resultsParameters) {
     272        var rowValues =
     273          GetValuesFromResultsParameters(runs, res.Key, res.Value)
     274            .Where(x => !double.IsNaN(x) && !double.IsNegativeInfinity(x) && !double.IsPositiveInfinity(x));
     275
     276        int j = 0;
     277        foreach (var cres in resultsParameters) {
     278          var columnValues = GetValuesFromResultsParameters(runs, cres.Key, cres.Value)
     279                .Where(x => !double.IsNaN(x) && !double.IsNegativeInfinity(x) && !double.IsPositiveInfinity(x));
     280
     281          if (!rowValues.Any() || !columnValues.Any() || i == j || rowValues.Count() != columnValues.Count()) {
     282            dt[i, j] = double.NaN;
     283          } else {
     284            if (methodName == PearsonName) {
     285              dt[i, j] = alglib.pearsoncorr2(rowValues.ToArray(), columnValues.ToArray());
     286            } else {
     287              dt[i, j] = alglib.spearmancorr2(rowValues.ToArray(), columnValues.ToArray());
     288            }
     289          }
     290          j++;
    280291        }
    281 
    282         resultVals = resultVals.Where(x => !double.IsNaN(x) && !double.IsNegativeInfinity(x) && !double.IsPositiveInfinity(x)).ToList();
    283         resultRowVals = resultRowVals.Where(x => !double.IsNaN(x) && !double.IsNegativeInfinity(x) && !double.IsPositiveInfinity(x)).ToList();
    284 
    285         if (resultRowVals.Count == 0 || resultVals.Count == 0 || resultRowVals.Count != resultVals.Count) {
    286           dt[j, 0] = double.NaN;
    287           dt[j++, 1] = double.NaN;
    288         } else {
    289           dt[j, 0] = alglib.pearsoncorr2(resultVals.ToArray(), resultRowVals.ToArray());
    290           dt[j++, 1] = alglib.spearmancorr2(resultVals.ToArray(), resultRowVals.ToArray());
    291         }
     292        i++;
    292293      }
    293294
     
    296297    }
    297298
    298     private void resultComboBox_SelectedIndexChanged(object sender, EventArgs e) {
    299       RebuildCorrelationTable();
     299    private void methodComboBox_SelectedIndexChanged(object sender, EventArgs e) {
     300      if (Content != null) {
     301        RebuildCorrelationTable();
     302      }
    300303    }
    301304  }
Note: See TracChangeset for help on using the changeset viewer.