Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
12/12/11 22:03:15 (12 years ago)
Author:
gkronber
Message:

#1635: fixed the problems highlighted by the review of mkommend.

  • cleaned up code
  • added a check if there is a mix of normal and CV runs
  • added a check if all runs contain a variable impact result
  • added a check if all runs contain variable impacts for the same input variables
  • added code to hide and show the folds combobox and its label
  • changed the column names to show the same names when all folds are selected and when only specific folds are selected.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic.Views/3.4/RunCollectionVariableImpactView.cs

    r6783 r7179  
    2222using System;
    2323using System.Collections.Generic;
     24using System.Drawing;
    2425using System.Linq;
    2526using System.Windows.Forms;
     
    4950    protected override void RegisterContentEvents() {
    5051      base.RegisterContentEvents();
    51       Content.UpdateOfRunsInProgressChanged += new EventHandler(Content_UpdateOfRunsInProgressChanged);
    52       Content.ItemsAdded += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_ItemsAdded);
    53       Content.ItemsRemoved += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_ItemsRemoved);
    54       Content.CollectionReset += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_CollectionReset);
     52      Content.UpdateOfRunsInProgressChanged += Content_UpdateOfRunsInProgressChanged;
     53      Content.ItemsAdded += Content_ItemsAdded;
     54      Content.ItemsRemoved += Content_ItemsRemoved;
     55      Content.CollectionReset += Content_CollectionReset;
    5556      RegisterRunEvents(Content);
    5657    }
    5758    protected override void DeregisterContentEvents() {
    5859      base.RegisterContentEvents();
    59       Content.UpdateOfRunsInProgressChanged -= new EventHandler(Content_UpdateOfRunsInProgressChanged);
    60       Content.ItemsAdded -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_ItemsAdded);
    61       Content.ItemsRemoved -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_ItemsRemoved);
    62       Content.CollectionReset -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_CollectionReset);
     60      Content.UpdateOfRunsInProgressChanged -= Content_UpdateOfRunsInProgressChanged;
     61      Content.ItemsAdded -= Content_ItemsAdded;
     62      Content.ItemsRemoved -= Content_ItemsRemoved;
     63      Content.CollectionReset -= Content_CollectionReset;
    6364      DeregisterRunEvents(Content);
    6465    }
    6566    private void RegisterRunEvents(IEnumerable<IRun> runs) {
    6667      foreach (IRun run in runs)
    67         run.Changed += new EventHandler(Run_Changed);
     68        run.Changed += Run_Changed;
    6869    }
    6970    private void DeregisterRunEvents(IEnumerable<IRun> runs) {
    7071      foreach (IRun run in runs)
    71         run.Changed -= new EventHandler(Run_Changed);
     72        run.Changed -= Run_Changed;
    7273    }
    7374    private void Content_ItemsAdded(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<IRun> e) {
     
    99100    private void comboBox_SelectedValueChanged(object sender, EventArgs e) {
    100101      if (comboBox.SelectedItem != null) {
    101         var cvRuns = from r in Content
    102                      where r.Visible
    103                      where r.Parameters.ContainsKey(numberOfFoldsParameterName)
    104                      select r;
     102        var visibleRuns = from run in Content where run.Visible select run;
    105103        if (comboBox.SelectedIndex == 0) {
    106           var selectedFolds = cvRuns
    107             .SelectMany(r => (RunCollection)r.Results[crossValidationFoldsResultName])
    108             .Where(r => r.Results.ContainsKey(variableImpactResultName));
    109           matrixView.Content = CalculateVariableImpactMatrix(selectedFolds.ToArray());
     104          var selectedFolds = from r in visibleRuns
     105                              let foldCollection = (RunCollection)r.Results[crossValidationFoldsResultName]
     106                              from run in foldCollection
     107                              let name = (r.Name + " " + run.Name)
     108                              select new { run, name };
     109          matrixView.Content = CalculateVariableImpactMatrix(selectedFolds.Select(x => x.run).ToArray(), selectedFolds.Select(x => x.name).ToArray());
    110110        } else {
    111           var selectedFolds = from r in cvRuns
     111          var selectedFolds = from r in visibleRuns
    112112                              let foldCollection = (RunCollection)r.Results[crossValidationFoldsResultName]
    113113                              let run = foldCollection.ElementAt(comboBox.SelectedIndex - 1)
    114                               where run.Results.ContainsKey(variableImpactResultName)
    115                               select new { run, r.Name };
    116           matrixView.Content = CalculateVariableImpactMatrix(selectedFolds.Select(x => x.run).ToArray(), selectedFolds.Select(x => x.Name).ToArray());
     114                              let name = (r.Name + " " + run.Name)
     115                              select new { run, name };
     116          matrixView.Content = CalculateVariableImpactMatrix(selectedFolds.Select(x => x.run).ToArray(), selectedFolds.Select(x => x.name).ToArray());
    117117        }
    118118      }
     
    121121
    122122    private void UpdateData() {
    123       if (Content != null) {
    124         comboBox.Items.Clear();
    125         comboBox.Enabled = false;
    126         var visibleRuns = Content.Where(r => r.Visible).ToArray();
    127         var representativeCvRun =
    128           visibleRuns.Where(r => r.Parameters.ContainsKey(numberOfFoldsParameterName)).FirstOrDefault();
    129         if (representativeCvRun != null) {
    130           // make sure all runs have the same number of folds
    131           int nFolds = ((IntValue)representativeCvRun.Parameters[numberOfFoldsParameterName]).Value;
    132           var cvRuns = visibleRuns.Where(r => r.Parameters.ContainsKey(numberOfFoldsParameterName));
    133           if (cvRuns.All(r => ((IntValue)r.Parameters[numberOfFoldsParameterName]).Value == nFolds)) {
     123      if (InvokeRequired) {
     124        Invoke((Action)UpdateData);
     125      } else {
     126        if (Content != null) {
     127          comboBox.Items.Clear();
     128          comboBox.Enabled = false;
     129          comboBox.Visible = false;
     130          foldsLabel.Visible = false;
     131          variableImpactsGroupBox.Dock = DockStyle.Fill;
     132          var visibleRuns = Content.Where(r => r.Visible).ToArray();
     133          if (visibleRuns.Length == 0) {
     134            DisplayMessage("Run collection is empty.");
     135          } else if (visibleRuns.All(r => r.Parameters.ContainsKey(numberOfFoldsParameterName))) {
     136            // check if all runs are comparable (CV or normal runs)
     137            CheckAndUpdateCvRuns();
     138          } else if (visibleRuns.All(r => !r.Parameters.ContainsKey(numberOfFoldsParameterName))) {
     139            CheckAndUpdateNormalRuns();
     140          } else {
     141            // there is a mix of CV and normal runs => show an error message
     142            DisplayMessage("The run collection contains a mixture of normal runs and cross-validation runs. Variable impact calculation does not work in this case.");
     143          }
     144        }
     145      }
     146    }
     147
     148    private void CheckAndUpdateCvRuns() {
     149      var visibleRuns = from run in Content where run.Visible select run;
     150      var representativeRun = visibleRuns.First();
     151      // make sure all runs have the same number of folds
     152      int nFolds = ((IntValue)representativeRun.Parameters[numberOfFoldsParameterName]).Value;
     153      if (visibleRuns.All(r => ((IntValue)r.Parameters[numberOfFoldsParameterName]).Value == nFolds)) {
     154        var allFoldResults = visibleRuns.SelectMany(run => (RunCollection)run.Results[crossValidationFoldsResultName]);
     155
     156        // make sure each fold contains variable impacts
     157        if (!allFoldResults.All(r => r.Results.ContainsKey(variableImpactResultName))) {
     158          DisplayMessage("At least one of the runs does not contain a variable impact result.");
     159        } else {
     160          // make sure each of the runs has the same input variables
     161          var allVariableNames = from run in allFoldResults
     162                                 let varImpacts = (DoubleMatrix)run.Results[variableImpactResultName]
     163                                 select varImpacts.RowNames;
     164          var groupedVariableNames = allVariableNames
     165            .SelectMany(x => x)
     166            .GroupBy(x => x);
     167
     168          if (groupedVariableNames.Any(g => g.Count() != allFoldResults.Count())) {
     169            DisplayMessage("At least one of the runs has a different input variable set than the rest.");
     170          } else {
    134171            // populate combobox
    135172            comboBox.Items.Add("Overall");
     
    139176            comboBox.SelectedIndex = 0;
    140177            comboBox.Enabled = true;
    141           } else {
    142             matrixView.Content = null;
    143           }
     178            comboBox.Visible = true;
     179            foldsLabel.Visible = true;
     180            variableImpactsGroupBox.Controls.Clear();
     181            variableImpactsGroupBox.Dock = DockStyle.None;
     182            variableImpactsGroupBox.Anchor = AnchorStyles.Left | AnchorStyles.Top | AnchorStyles.Right |
     183                                             AnchorStyles.Bottom;
     184            variableImpactsGroupBox.Height = this.Height - comboBox.Height - 12;
     185            variableImpactsGroupBox.Width = this.Width;
     186            matrixView.Dock = DockStyle.Fill;
     187            variableImpactsGroupBox.Controls.Add(matrixView);
     188          }
     189        }
     190      } else {
     191        DisplayMessage("At least on of the cross-validation runs has a different number of folds than the rest.");
     192      }
     193    }
     194
     195    private void CheckAndUpdateNormalRuns() {
     196      // make sure all runs contain variable impact results
     197      var visibleRuns = from run in Content where run.Visible select run;
     198
     199      if (!visibleRuns.All(r => r.Results.ContainsKey(variableImpactResultName))) {
     200        DisplayMessage("At least one of the runs does not contain a variable impact result.");
     201      } else {
     202        // make sure each of the runs has the same input variables
     203        var allVariableNames = from run in visibleRuns
     204                               let varImpacts = (DoubleMatrix)run.Results[variableImpactResultName]
     205                               select varImpacts.RowNames;
     206        var groupedVariableNames = allVariableNames
     207          .SelectMany(x => x)
     208          .GroupBy(x => x);
     209
     210        if (groupedVariableNames.Any(g => g.Count() != visibleRuns.Count())) {
     211          DisplayMessage("At least one of the runs has a different input variable set than the rest.");
    144212        } else {
    145           var runsWithVariables = visibleRuns.Where(r => r.Results.ContainsKey(variableImpactResultName)).ToArray();
    146           matrixView.Content = CalculateVariableImpactMatrix(runsWithVariables);
    147         }
    148       }
    149     }
    150 
    151     private IStringConvertibleMatrix CalculateVariableImpactMatrix(IRun[] runs) {
    152       return CalculateVariableImpactMatrix(runs, runs.Select(r => r.Name).ToArray());
     213          if (!variableImpactsGroupBox.Controls.Contains(matrixView)) {
     214            variableImpactsGroupBox.Controls.Clear();
     215            matrixView.Dock = DockStyle.Fill;
     216            variableImpactsGroupBox.Controls.Add(matrixView);
     217          }
     218          matrixView.Content = CalculateVariableImpactMatrix(visibleRuns.ToArray(), visibleRuns.Select(r => r.Name).ToArray());
     219        }
     220      }
    153221    }
    154222
     
    280348    }
    281349
     350    private void DisplayMessage(string message) {
     351      variableImpactsGroupBox.Controls.Remove(matrixView);
     352      var label = new Label { TextAlign = ContentAlignment.MiddleCenter, Text = message, Dock = DockStyle.Fill };
     353      variableImpactsGroupBox.Controls.Add(label);
     354    }
    282355  }
    283356}
Note: See TracChangeset for help on using the changeset viewer.