Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
09/16/11 12:00:36 (13 years ago)
Author:
mkommend
Message:

#1479: Integrated trunk changes.

Location:
branches/GP.Grammar.Editor/HeuristicLab.Problems.DataAnalysis.Symbolic.Views/3.4
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • branches/GP.Grammar.Editor/HeuristicLab.Problems.DataAnalysis.Symbolic.Views/3.4/RunCollectionVariableImpactView.Designer.cs

    r5809 r6784  
    4646    private void InitializeComponent() {
    4747      this.matrixView = new HeuristicLab.Data.Views.StringConvertibleMatrixView();
     48      this.comboBox = new System.Windows.Forms.ComboBox();
     49      this.label1 = new System.Windows.Forms.Label();
     50      this.variableImpactsGroupBox = new System.Windows.Forms.GroupBox();
     51      this.variableImpactsGroupBox.SuspendLayout();
    4852      this.SuspendLayout();
    4953      //
     
    5559      this.matrixView.Caption = "StringConvertibleMatrix View";
    5660      this.matrixView.Content = null;
    57       this.matrixView.Location = new System.Drawing.Point(3, 3);
     61      this.matrixView.Location = new System.Drawing.Point(6, 19);
    5862      this.matrixView.Name = "matrixView";
    5963      this.matrixView.ReadOnly = true;
    60       this.matrixView.Size = new System.Drawing.Size(303, 229);
     64      this.matrixView.ShowRowsAndColumnsTextBox = true;
     65      this.matrixView.ShowStatisticalInformation = true;
     66      this.matrixView.Size = new System.Drawing.Size(294, 174);
    6167      this.matrixView.TabIndex = 0;
     68      //
     69      // comboBox
     70      //
     71      this.comboBox.FormattingEnabled = true;
     72      this.comboBox.Location = new System.Drawing.Point(39, 6);
     73      this.comboBox.Name = "comboBox";
     74      this.comboBox.Size = new System.Drawing.Size(68, 21);
     75      this.comboBox.TabIndex = 1;
     76      this.comboBox.SelectedValueChanged += new System.EventHandler(this.comboBox_SelectedValueChanged);
     77      //
     78      // label1
     79      //
     80      this.label1.AutoSize = true;
     81      this.label1.Location = new System.Drawing.Point(3, 9);
     82      this.label1.Name = "label1";
     83      this.label1.Size = new System.Drawing.Size(30, 13);
     84      this.label1.TabIndex = 2;
     85      this.label1.Text = "Fold:";
     86      //
     87      // variableImpactsGroupBox
     88      //
     89      this.variableImpactsGroupBox.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
     90                  | System.Windows.Forms.AnchorStyles.Left)
     91                  | System.Windows.Forms.AnchorStyles.Right)));
     92      this.variableImpactsGroupBox.Controls.Add(this.matrixView);
     93      this.variableImpactsGroupBox.Location = new System.Drawing.Point(0, 33);
     94      this.variableImpactsGroupBox.Name = "variableImpactsGroupBox";
     95      this.variableImpactsGroupBox.Size = new System.Drawing.Size(306, 199);
     96      this.variableImpactsGroupBox.TabIndex = 3;
     97      this.variableImpactsGroupBox.TabStop = false;
     98      this.variableImpactsGroupBox.Text = "Variable impacts:";
    6299      //
    63100      // RunCollectionVariableImpactView
     
    65102      this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
    66103      this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
    67       this.Controls.Add(this.matrixView);
     104      this.Controls.Add(this.variableImpactsGroupBox);
     105      this.Controls.Add(this.label1);
     106      this.Controls.Add(this.comboBox);
    68107      this.Name = "RunCollectionVariableImpactView";
    69108      this.Size = new System.Drawing.Size(309, 235);
     109      this.variableImpactsGroupBox.ResumeLayout(false);
    70110      this.ResumeLayout(false);
     111      this.PerformLayout();
    71112
    72113    }
     
    75116
    76117    private HeuristicLab.Data.Views.StringConvertibleMatrixView matrixView;
     118    private System.Windows.Forms.ComboBox comboBox;
     119    private System.Windows.Forms.Label label1;
     120    private System.Windows.Forms.GroupBox variableImpactsGroupBox;
    77121  }
    78122}
  • branches/GP.Grammar.Editor/HeuristicLab.Problems.DataAnalysis.Symbolic.Views/3.4/RunCollectionVariableImpactView.cs

    r5975 r6784  
    3535  public sealed partial class RunCollectionVariableImpactView : AsynchronousContentView {
    3636    private const string variableImpactResultName = "Variable impacts";
     37    private const string crossValidationFoldsResultName = "CrossValidation Folds";
     38    private const string numberOfFoldsParameterName = "Folds";
    3739    public RunCollectionVariableImpactView() {
    3840      InitializeComponent();
     
    9597    }
    9698
     99    private void comboBox_SelectedValueChanged(object sender, EventArgs e) {
     100      if (comboBox.SelectedItem != null) {
     101        var cvRuns = from r in Content
     102                     where r.Visible
     103                     where r.Parameters.ContainsKey(numberOfFoldsParameterName)
     104                     select r;
     105        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());
     110        } else {
     111          var selectedFolds = from r in cvRuns
     112                              let foldCollection = (RunCollection)r.Results[crossValidationFoldsResultName]
     113                              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());
     117        }
     118      }
     119    }
     120
     121
    97122    private void UpdateData() {
    98       matrixView.Content = CalculateVariableImpactMatrix();
    99     }
    100 
    101     private DoubleMatrix CalculateVariableImpactMatrix() {
     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)) {
     134            // populate combobox
     135            comboBox.Items.Add("Overall");
     136            for (int foldIndex = 0; foldIndex < nFolds; foldIndex++) {
     137              comboBox.Items.Add("Fold " + foldIndex);
     138            }
     139            comboBox.SelectedIndex = 0;
     140            comboBox.Enabled = true;
     141          } else {
     142            matrixView.Content = null;
     143          }
     144        } 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());
     153    }
     154
     155    private DoubleMatrix CalculateVariableImpactMatrix(IRun[] runs, string[] runNames) {
    102156      DoubleMatrix matrix = null;
    103       if (Content != null) {
    104         List<IRun> runsWithVariables = Content.Where(r => r.Visible && r.Results.ContainsKey(variableImpactResultName)).ToList();
    105         IEnumerable<DoubleMatrix> allVariableImpacts = (from run in runsWithVariables
    106                                                         select run.Results[variableImpactResultName]).Cast<DoubleMatrix>();
    107         IEnumerable<string> variableNames = (from variableImpact in allVariableImpacts
    108                                              from variableName in variableImpact.RowNames
    109                                              select variableName)
    110                                             .Distinct();
    111         // filter variableNames: only include names that have at least one non-zero value in a run
    112         List<string> variableNamesList = (from variableName in variableNames
    113                                           where GetVariableImpacts(variableName, allVariableImpacts).Any(x => !x.IsAlmost(0.0))
    114                                           select variableName)
    115                                          .ToList();
    116 
    117         List<string> statictics = new List<string> { "Median Rank", "Mean", "StdDev", "pValue" };
    118         List<string> columnNames = runsWithVariables.Select(r => r.Name).ToList();
    119         columnNames.AddRange(statictics);
    120         int runs = runsWithVariables.Count();
    121 
    122         matrix = new DoubleMatrix(variableNamesList.Count, runs + statictics.Count);
    123         matrix.SortableView = true;
    124         matrix.RowNames = variableNamesList;
    125         matrix.ColumnNames = columnNames;
    126 
    127         for (int i = 0; i < runsWithVariables.Count; i++) {
    128           IRun run = runsWithVariables[i];
    129           DoubleMatrix runVariableImpacts = (DoubleMatrix)run.Results[variableImpactResultName];
    130           for (int j = 0; j < runVariableImpacts.Rows; j++) {
    131             int rowIndex = variableNamesList.FindIndex(s => s == runVariableImpacts.RowNames.ElementAt(j));
    132             if (rowIndex > -1) {
    133               matrix[rowIndex, i] = runVariableImpacts[j, 0];
    134             }
    135           }
    136         }
    137 
    138         List<List<double>> variableImpactsOverRuns = (from variableName in variableNamesList
    139                                                       select GetVariableImpacts(variableName, allVariableImpacts).ToList())
    140                                                      .ToList();
    141         List<List<double>> variableRanks = (from variableName in variableNamesList
    142                                             select GetVariableImpactRanks(variableName, allVariableImpacts).ToList())
    143                                         .ToList();
    144         if (variableImpactsOverRuns.Count() > 0) {
    145           // the variable with the worst median impact value is chosen as the reference variable
    146           // this is problematic if all variables are relevant, however works often in practice
    147           List<double> referenceImpacts = (from impacts in variableImpactsOverRuns
    148                                            let avg = impacts.Median()
    149                                            orderby avg
    150                                            select impacts)
    151                                            .First();
    152           // for all variables
    153           for (int row = 0; row < variableImpactsOverRuns.Count; row++) {
    154             // median rank
    155             matrix[row, runs] = variableRanks[row].Median();
    156             // also show mean and std.dev. of relative variable impacts to indicate the relative difference in impacts of variables
    157             matrix[row, runs + 1] = variableImpactsOverRuns[row].Average();
    158             matrix[row, runs + 2] = variableImpactsOverRuns[row].StandardDeviation();
    159 
    160             double leftTail = 0; double rightTail = 0; double bothTails = 0;
    161             // calc differences of impacts for current variable and reference variable
    162             double[] z = new double[referenceImpacts.Count];
    163             for (int i = 0; i < z.Length; i++) {
    164               z[i] = variableImpactsOverRuns[row][i] - referenceImpacts[i];
    165             }
    166             // wilcoxon signed rank test is used because the impact values of two variables in a single run are not independent
    167             alglib.wsr.wilcoxonsignedranktest(z, z.Length, 0, ref bothTails, ref leftTail, ref rightTail);
    168             matrix[row, runs + 3] = bothTails;
    169           }
    170         }
    171       }
    172       return matrix;
     157      IEnumerable<DoubleMatrix> allVariableImpacts = (from run in runs
     158                                                      select run.Results[variableImpactResultName]).Cast<DoubleMatrix>();
     159      IEnumerable<string> variableNames = (from variableImpact in allVariableImpacts
     160                                           from variableName in variableImpact.RowNames
     161                                           select variableName)
     162                                          .Distinct();
     163      // filter variableNames: only include names that have at least one non-zero value in a run
     164      List<string> variableNamesList = (from variableName in variableNames
     165                                        where GetVariableImpacts(variableName, allVariableImpacts).Any(x => !x.IsAlmost(0.0))
     166                                        select variableName)
     167                                       .ToList();
     168
     169      List<string> statictics = new List<string> { "Median Rank", "Mean", "StdDev", "pValue" };
     170      List<string> columnNames = new List<string>(runNames);
     171      columnNames.AddRange(statictics);
     172      int numberOfRuns = runs.Length;
     173
     174      matrix = new DoubleMatrix(variableNamesList.Count, numberOfRuns + statictics.Count);
     175      matrix.SortableView = true;
     176      matrix.RowNames = variableNamesList;
     177      matrix.ColumnNames = columnNames;
     178
     179      // calculate statistics
     180      List<List<double>> variableImpactsOverRuns = (from variableName in variableNamesList
     181                                                    select GetVariableImpacts(variableName, allVariableImpacts).ToList())
     182                                             .ToList();
     183      List<List<double>> variableRanks = (from variableName in variableNamesList
     184                                          select GetVariableImpactRanks(variableName, allVariableImpacts).ToList())
     185                                      .ToList();
     186      if (variableImpactsOverRuns.Count() > 0) {
     187        // the variable with the worst median impact value is chosen as the reference variable
     188        // this is problematic if all variables are relevant, however works often in practice
     189        List<double> referenceImpacts = (from impacts in variableImpactsOverRuns
     190                                         let avg = impacts.Median()
     191                                         orderby avg
     192                                         select impacts)
     193                                         .First();
     194        // for all variables
     195        for (int row = 0; row < variableImpactsOverRuns.Count; row++) {
     196          // median rank
     197          matrix[row, numberOfRuns] = variableRanks[row].Median();
     198          // also show mean and std.dev. of relative variable impacts to indicate the relative difference in impacts of variables
     199          matrix[row, numberOfRuns + 1] = Math.Round(variableImpactsOverRuns[row].Average(), 3);
     200          matrix[row, numberOfRuns + 2] = Math.Round(variableImpactsOverRuns[row].StandardDeviation(), 3);
     201
     202          double leftTail = 0; double rightTail = 0; double bothTails = 0;
     203          // calc differences of impacts for current variable and reference variable
     204          double[] z = new double[referenceImpacts.Count];
     205          for (int i = 0; i < z.Length; i++) {
     206            z[i] = variableImpactsOverRuns[row][i] - referenceImpacts[i];
     207          }
     208          // wilcoxon signed rank test is used because the impact values of two variables in a single run are not independent
     209          alglib.wsr.wilcoxonsignedranktest(z, z.Length, 0, ref bothTails, ref leftTail, ref rightTail);
     210          matrix[row, numberOfRuns + 3] = Math.Round(bothTails, 4);
     211        }
     212      }
     213
     214      // fill matrix with impacts from runs
     215      for (int i = 0; i < runs.Length; i++) {
     216        IRun run = runs[i];
     217        DoubleMatrix runVariableImpacts = (DoubleMatrix)run.Results[variableImpactResultName];
     218        for (int j = 0; j < runVariableImpacts.Rows; j++) {
     219          int rowIndex = variableNamesList.FindIndex(s => s == runVariableImpacts.RowNames.ElementAt(j));
     220          if (rowIndex > -1) {
     221            matrix[rowIndex, i] = Math.Round(runVariableImpacts[j, 0], 3);
     222          }
     223        }
     224      }
     225      // sort by median
     226      var sortedMatrix = (DoubleMatrix)matrix.Clone();
     227      var sortedIndexes = from i in Enumerable.Range(0, sortedMatrix.Rows)
     228                          orderby matrix[i, numberOfRuns]
     229                          select i;
     230
     231      int targetIndex = 0;
     232      foreach (var sourceIndex in sortedIndexes) {
     233        for (int c = 0; c < matrix.Columns; c++)
     234          sortedMatrix[targetIndex, c] = matrix[sourceIndex, c];
     235        targetIndex++;
     236      }
     237      return sortedMatrix;
    173238    }
    174239
     
    213278      }
    214279    }
     280
    215281  }
    216282}
  • branches/GP.Grammar.Editor/HeuristicLab.Problems.DataAnalysis.Symbolic.Views/3.4/SymbolicDataAnalysisSolutionResponseFunctionView.cs

    r6675 r6784  
    2727using HeuristicLab.Common;
    2828using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
    29 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Views;
     29using HeuristicLab.MainForm;
    3030using HeuristicLab.MainForm.WindowsForms;
    31 using System.Windows.Forms.DataVisualization.Charting;
    32 using HeuristicLab.MainForm;
    3331
    3432namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Views {
     
    8886        select varNode.VariableName)
    8987         .Distinct()
    90          .OrderBy(x=>x)
     88         .OrderBy(x => x)
    9189         .ToList();
    9290
    9391        medianValues.Clear();
    9492        foreach (var variableName in referencedVariables) {
    95           medianValues.Add(variableName, Content.ProblemData.Dataset.GetEnumeratedVariableValues(variableName).Median());
     93          medianValues.Add(variableName, Content.ProblemData.Dataset.GetDoubleValues(variableName).Median());
    9694        }
    9795
     
    107105      foreach (var variableName in variableNames) {
    108106        var variableTrackbar = new VariableTrackbar(variableName,
    109                                                     Content.ProblemData.Dataset.GetEnumeratedVariableValues(variableName));
     107                                                    Content.ProblemData.Dataset.GetDoubleValues(variableName));
    110108        variableTrackbar.Size = new Size(variableTrackbar.Size.Width, flowLayoutPanel.Size.Height - 23);
    111109        variableTrackbar.ValueChanged += TrackBarValueChanged;
     
    132130        .Except(new string[] { freeVariable });
    133131
    134       var freeVariableValues = Content.ProblemData.Dataset.GetEnumeratedVariableValues(freeVariable, Content.ProblemData.TrainingIndizes).ToArray();
     132      var freeVariableValues = Content.ProblemData.Dataset.GetDoubleValues(freeVariable, Content.ProblemData.TrainingIndizes).ToArray();
    135133      var responseValues = Content.Model.Interpreter.GetSymbolicExpressionTreeValues(clonedTree,
    136134                                                                              Content.ProblemData.Dataset,
Note: See TracChangeset for help on using the changeset viewer.