Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
08/01/18 14:01:08 (6 years ago)
Author:
fholzing
Message:

#2904: Streamlined the variableimpactcalculator code on both Regression and Classification. Taken over the regression-code for classification with some minor adaptations.

Location:
branches/2904_CalculateImpacts/HeuristicLab.Problems.DataAnalysis.Views/3.4/Classification
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/2904_CalculateImpacts/HeuristicLab.Problems.DataAnalysis.Views/3.4/Classification/ClassificationSolutionVariableImpactsView.Designer.cs

    r15753 r16036  
    1919 */
    2020#endregion
     21
     22
    2123namespace HeuristicLab.Problems.DataAnalysis.Views {
    2224  partial class ClassificationSolutionVariableImpactsView {
     
    4446    /// </summary>
    4547    private void InitializeComponent() {
    46       this.variableImactsArrayView = new HeuristicLab.Data.Views.StringConvertibleArrayView();
    47       this.dataPartitionComboBox = new System.Windows.Forms.ComboBox();
    48       this.dataPartitionLabel = new System.Windows.Forms.Label();
    49       this.numericVarReplacementLabel = new System.Windows.Forms.Label();
    50       this.replacementComboBox = new System.Windows.Forms.ComboBox();
    51       this.factorVarReplacementLabel = new System.Windows.Forms.Label();
    52       this.factorVarReplComboBox = new System.Windows.Forms.ComboBox();
    5348      this.ascendingCheckBox = new System.Windows.Forms.CheckBox();
    5449      this.sortByLabel = new System.Windows.Forms.Label();
    5550      this.sortByComboBox = new System.Windows.Forms.ComboBox();
    56       this.backgroundWorker = new System.ComponentModel.BackgroundWorker();
     51      this.factorVarReplComboBox = new System.Windows.Forms.ComboBox();
     52      this.factorVarReplacementLabel = new System.Windows.Forms.Label();
     53      this.replacementComboBox = new System.Windows.Forms.ComboBox();
     54      this.numericVarReplacementLabel = new System.Windows.Forms.Label();
     55      this.dataPartitionLabel = new System.Windows.Forms.Label();
     56      this.dataPartitionComboBox = new System.Windows.Forms.ComboBox();
     57      this.variableImactsArrayView = new HeuristicLab.Data.Views.StringConvertibleArrayView();
    5758      this.SuspendLayout();
    5859      //
    59       // variableImactsArrayView
    60       //
    61       this.variableImactsArrayView.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
    62             | System.Windows.Forms.AnchorStyles.Left)
    63             | System.Windows.Forms.AnchorStyles.Right)));
    64       this.variableImactsArrayView.Caption = "StringConvertibleArray View";
    65       this.variableImactsArrayView.Content = null;
    66       this.variableImactsArrayView.Location = new System.Drawing.Point(3, 84);
    67       this.variableImactsArrayView.Name = "variableImactsArrayView";
    68       this.variableImactsArrayView.ReadOnly = true;
    69       this.variableImactsArrayView.Size = new System.Drawing.Size(662, 278);
    70       this.variableImactsArrayView.TabIndex = 2;
    71       //
    72       // dataPartitionComboBox
    73       //
    74       this.dataPartitionComboBox.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
    75       this.dataPartitionComboBox.FormattingEnabled = true;
    76       this.dataPartitionComboBox.Items.AddRange(new object[] {
    77             HeuristicLab.Problems.DataAnalysis.ClassificationSolutionVariableImpactsCalculator.DataPartitionEnum.Training,
    78             HeuristicLab.Problems.DataAnalysis.ClassificationSolutionVariableImpactsCalculator.DataPartitionEnum.Test,
    79             HeuristicLab.Problems.DataAnalysis.ClassificationSolutionVariableImpactsCalculator.DataPartitionEnum.All});
    80       this.dataPartitionComboBox.Location = new System.Drawing.Point(197, 3);
    81       this.dataPartitionComboBox.Name = "dataPartitionComboBox";
    82       this.dataPartitionComboBox.Size = new System.Drawing.Size(121, 21);
    83       this.dataPartitionComboBox.TabIndex = 1;
    84       this.dataPartitionComboBox.SelectedIndexChanged += new System.EventHandler(this.dataPartitionComboBox_SelectedIndexChanged);
    85       //
    86       // dataPartitionLabel
    87       //
    88       this.dataPartitionLabel.AutoSize = true;
    89       this.dataPartitionLabel.Location = new System.Drawing.Point(3, 6);
    90       this.dataPartitionLabel.Name = "dataPartitionLabel";
    91       this.dataPartitionLabel.Size = new System.Drawing.Size(73, 13);
    92       this.dataPartitionLabel.TabIndex = 0;
    93       this.dataPartitionLabel.Text = "Data partition:";
    94       //
    95       // numericVarReplacementLabel
    96       //
    97       this.numericVarReplacementLabel.AutoSize = true;
    98       this.numericVarReplacementLabel.Location = new System.Drawing.Point(3, 33);
    99       this.numericVarReplacementLabel.Name = "numericVarReplacementLabel";
    100       this.numericVarReplacementLabel.Size = new System.Drawing.Size(173, 13);
    101       this.numericVarReplacementLabel.TabIndex = 2;
    102       this.numericVarReplacementLabel.Text = "Replacement for numeric variables:";
     60      // ascendingCheckBox
     61      //
     62      this.ascendingCheckBox.AutoSize = true;
     63      this.ascendingCheckBox.Location = new System.Drawing.Point(534, 6);
     64      this.ascendingCheckBox.Name = "ascendingCheckBox";
     65      this.ascendingCheckBox.Size = new System.Drawing.Size(76, 17);
     66      this.ascendingCheckBox.TabIndex = 7;
     67      this.ascendingCheckBox.Text = "Ascending";
     68      this.ascendingCheckBox.UseVisualStyleBackColor = true;
     69      this.ascendingCheckBox.CheckedChanged += new System.EventHandler(this.ascendingCheckBox_CheckedChanged);
     70      //
     71      // sortByLabel
     72      //
     73      this.sortByLabel.AutoSize = true;
     74      this.sortByLabel.Location = new System.Drawing.Point(324, 6);
     75      this.sortByLabel.Name = "sortByLabel";
     76      this.sortByLabel.Size = new System.Drawing.Size(77, 13);
     77      this.sortByLabel.TabIndex = 4;
     78      this.sortByLabel.Text = "Sorting criteria:";
     79      //
     80      // sortByComboBox
     81      //
     82      this.sortByComboBox.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
     83      this.sortByComboBox.FormattingEnabled = true;
     84      this.sortByComboBox.Items.AddRange(new object[] {
     85            HeuristicLab.Problems.DataAnalysis.Views.ClassificationSolutionVariableImpactsView.SortingCriteria.ImpactValue,
     86            HeuristicLab.Problems.DataAnalysis.Views.ClassificationSolutionVariableImpactsView.SortingCriteria.Occurrence,
     87            HeuristicLab.Problems.DataAnalysis.Views.ClassificationSolutionVariableImpactsView.SortingCriteria.VariableName});
     88      this.sortByComboBox.Location = new System.Drawing.Point(407, 3);
     89      this.sortByComboBox.Name = "sortByComboBox";
     90      this.sortByComboBox.Size = new System.Drawing.Size(121, 21);
     91      this.sortByComboBox.TabIndex = 5;
     92      this.sortByComboBox.SelectedIndexChanged += new System.EventHandler(this.sortByComboBox_SelectedIndexChanged);
     93      //
     94      // factorVarReplComboBox
     95      //
     96      this.factorVarReplComboBox.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
     97      this.factorVarReplComboBox.FormattingEnabled = true;
     98      this.factorVarReplComboBox.Items.AddRange(new object[] {
     99            HeuristicLab.Problems.DataAnalysis.ClassificationSolutionVariableImpactsCalculator.FactorReplacementMethodEnum.Best,
     100            HeuristicLab.Problems.DataAnalysis.ClassificationSolutionVariableImpactsCalculator.FactorReplacementMethodEnum.Mode,
     101            HeuristicLab.Problems.DataAnalysis.ClassificationSolutionVariableImpactsCalculator.FactorReplacementMethodEnum.Shuffle});
     102      this.factorVarReplComboBox.Location = new System.Drawing.Point(197, 57);
     103      this.factorVarReplComboBox.Name = "factorVarReplComboBox";
     104      this.factorVarReplComboBox.Size = new System.Drawing.Size(121, 21);
     105      this.factorVarReplComboBox.TabIndex = 1;
     106      this.factorVarReplComboBox.SelectedIndexChanged += new System.EventHandler(this.replacementComboBox_SelectedIndexChanged);
     107      //
     108      // factorVarReplacementLabel
     109      //
     110      this.factorVarReplacementLabel.AutoSize = true;
     111      this.factorVarReplacementLabel.Location = new System.Drawing.Point(3, 60);
     112      this.factorVarReplacementLabel.Name = "factorVarReplacementLabel";
     113      this.factorVarReplacementLabel.Size = new System.Drawing.Size(188, 13);
     114      this.factorVarReplacementLabel.TabIndex = 0;
     115      this.factorVarReplacementLabel.Text = "Replacement for categorical variables:";
    103116      //
    104117      // replacementComboBox
     
    117130      this.replacementComboBox.SelectedIndexChanged += new System.EventHandler(this.replacementComboBox_SelectedIndexChanged);
    118131      //
    119       // factorVarReplacementLabel
    120       //
    121       this.factorVarReplacementLabel.AutoSize = true;
    122       this.factorVarReplacementLabel.Location = new System.Drawing.Point(3, 60);
    123       this.factorVarReplacementLabel.Name = "factorVarReplacementLabel";
    124       this.factorVarReplacementLabel.Size = new System.Drawing.Size(188, 13);
    125       this.factorVarReplacementLabel.TabIndex = 0;
    126       this.factorVarReplacementLabel.Text = "Replacement for categorical variables:";
    127       //
    128       // factorVarReplComboBox
    129       //
    130       this.factorVarReplComboBox.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
    131       this.factorVarReplComboBox.FormattingEnabled = true;
    132       this.factorVarReplComboBox.Items.AddRange(new object[] {
    133             HeuristicLab.Problems.DataAnalysis.ClassificationSolutionVariableImpactsCalculator.FactorReplacementMethodEnum.Best,
    134             HeuristicLab.Problems.DataAnalysis.ClassificationSolutionVariableImpactsCalculator.FactorReplacementMethodEnum.Mode,
    135             HeuristicLab.Problems.DataAnalysis.ClassificationSolutionVariableImpactsCalculator.FactorReplacementMethodEnum.Shuffle});
    136       this.factorVarReplComboBox.Location = new System.Drawing.Point(197, 57);
    137       this.factorVarReplComboBox.Name = "factorVarReplComboBox";
    138       this.factorVarReplComboBox.Size = new System.Drawing.Size(121, 21);
    139       this.factorVarReplComboBox.TabIndex = 1;
    140       this.factorVarReplComboBox.SelectedIndexChanged += new System.EventHandler(this.replacementComboBox_SelectedIndexChanged);
    141       //
    142       // ascendingCheckBox
    143       //
    144       this.ascendingCheckBox.AutoSize = true;
    145       this.ascendingCheckBox.Location = new System.Drawing.Point(534, 6);
    146       this.ascendingCheckBox.Name = "ascendingCheckBox";
    147       this.ascendingCheckBox.Size = new System.Drawing.Size(76, 17);
    148       this.ascendingCheckBox.TabIndex = 10;
    149       this.ascendingCheckBox.Text = "Ascending";
    150       this.ascendingCheckBox.UseVisualStyleBackColor = true;
    151       this.ascendingCheckBox.CheckedChanged += new System.EventHandler(this.ascendingCheckBox_CheckedChanged);
    152       //
    153       // sortByLabel
    154       //
    155       this.sortByLabel.AutoSize = true;
    156       this.sortByLabel.Location = new System.Drawing.Point(324, 6);
    157       this.sortByLabel.Name = "sortByLabel";
    158       this.sortByLabel.Size = new System.Drawing.Size(77, 13);
    159       this.sortByLabel.TabIndex = 8;
    160       this.sortByLabel.Text = "Sorting criteria:";
    161       //
    162       // sortByComboBox
    163       //
    164       this.sortByComboBox.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
    165       this.sortByComboBox.FormattingEnabled = true;
    166       this.sortByComboBox.Location = new System.Drawing.Point(407, 3);
    167       this.sortByComboBox.Name = "sortByComboBox";
    168       this.sortByComboBox.Size = new System.Drawing.Size(121, 21);
    169       this.sortByComboBox.TabIndex = 9;
    170       this.sortByComboBox.SelectedIndexChanged += new System.EventHandler(this.sortByComboBox_SelectedIndexChanged);
     132      // numericVarReplacementLabel
     133      //
     134      this.numericVarReplacementLabel.AutoSize = true;
     135      this.numericVarReplacementLabel.Location = new System.Drawing.Point(3, 33);
     136      this.numericVarReplacementLabel.Name = "numericVarReplacementLabel";
     137      this.numericVarReplacementLabel.Size = new System.Drawing.Size(173, 13);
     138      this.numericVarReplacementLabel.TabIndex = 2;
     139      this.numericVarReplacementLabel.Text = "Replacement for numeric variables:";
     140      //
     141      // dataPartitionLabel
     142      //
     143      this.dataPartitionLabel.AutoSize = true;
     144      this.dataPartitionLabel.Location = new System.Drawing.Point(3, 6);
     145      this.dataPartitionLabel.Name = "dataPartitionLabel";
     146      this.dataPartitionLabel.Size = new System.Drawing.Size(73, 13);
     147      this.dataPartitionLabel.TabIndex = 0;
     148      this.dataPartitionLabel.Text = "Data partition:";
     149      //
     150      // dataPartitionComboBox
     151      //
     152      this.dataPartitionComboBox.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
     153      this.dataPartitionComboBox.FormattingEnabled = true;
     154      this.dataPartitionComboBox.Items.AddRange(new object[] {
     155            HeuristicLab.Problems.DataAnalysis.ClassificationSolutionVariableImpactsCalculator.DataPartitionEnum.Training,
     156            HeuristicLab.Problems.DataAnalysis.ClassificationSolutionVariableImpactsCalculator.DataPartitionEnum.Test,
     157            HeuristicLab.Problems.DataAnalysis.ClassificationSolutionVariableImpactsCalculator.DataPartitionEnum.All});
     158      this.dataPartitionComboBox.Location = new System.Drawing.Point(197, 3);
     159      this.dataPartitionComboBox.Name = "dataPartitionComboBox";
     160      this.dataPartitionComboBox.Size = new System.Drawing.Size(121, 21);
     161      this.dataPartitionComboBox.TabIndex = 1;
     162      this.dataPartitionComboBox.SelectedIndexChanged += new System.EventHandler(this.dataPartitionComboBox_SelectedIndexChanged);
     163      //
     164      // variableImactsArrayView
     165      //
     166      this.variableImactsArrayView.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
     167            | System.Windows.Forms.AnchorStyles.Left)
     168            | System.Windows.Forms.AnchorStyles.Right)));
     169      this.variableImactsArrayView.Caption = "StringConvertibleArray View";
     170      this.variableImactsArrayView.Content = null;
     171      this.variableImactsArrayView.Location = new System.Drawing.Point(3, 84);
     172      this.variableImactsArrayView.Name = "variableImactsArrayView";
     173      this.variableImactsArrayView.ReadOnly = true;
     174      this.variableImactsArrayView.Size = new System.Drawing.Size(706, 278);
     175      this.variableImactsArrayView.TabIndex = 2;
    171176      //
    172177      // ClassificationSolutionVariableImpactsView
     
    185190      this.Controls.Add(this.variableImactsArrayView);
    186191      this.Name = "ClassificationSolutionVariableImpactsView";
    187       this.Size = new System.Drawing.Size(668, 365);
     192      this.Size = new System.Drawing.Size(712, 365);
    188193      this.VisibleChanged += new System.EventHandler(this.ClassificationSolutionVariableImpactsView_VisibleChanged);
    189194      this.ResumeLayout(false);
     
    201206    private System.Windows.Forms.Label factorVarReplacementLabel;
    202207    private System.Windows.Forms.ComboBox factorVarReplComboBox;
    203     private System.Windows.Forms.CheckBox ascendingCheckBox;
    204208    private System.Windows.Forms.Label sortByLabel;
    205209    private System.Windows.Forms.ComboBox sortByComboBox;
    206     private System.ComponentModel.BackgroundWorker backgroundWorker;
     210    private System.Windows.Forms.CheckBox ascendingCheckBox;
    207211  }
    208212}
  • branches/2904_CalculateImpacts/HeuristicLab.Problems.DataAnalysis.Views/3.4/Classification/ClassificationSolutionVariableImpactsView.cs

    r15753 r16036  
    3333  [Content(typeof(IClassificationSolution))]
    3434  public partial class ClassificationSolutionVariableImpactsView : DataAnalysisSolutionEvaluationView {
    35     #region Nested Types
    3635    private enum SortingCriteria {
    3736      ImpactValue,
     
    3938      VariableName
    4039    }
    41     #endregion
    42 
    43     #region Fields
    44     private Dictionary<string, double> rawVariableImpacts = new Dictionary<string, double>();
    45     private Thread thread;
    46     #endregion
    47 
    48     #region Getter/Setter
     40    private CancellationTokenSource cancellationToken = new CancellationTokenSource();
     41    private List<Tuple<string, double>> rawVariableImpacts = new List<Tuple<string, double>>();
     42
    4943    public new IClassificationSolution Content {
    5044      get { return (IClassificationSolution)base.Content; }
     
    5347      }
    5448    }
    55     #endregion
    56 
    57     #region Ctor
     49
    5850    public ClassificationSolutionVariableImpactsView()
    5951      : base() {
    6052      InitializeComponent();
    6153
    62       //Little workaround. If you fill the ComboBox-Items in the other partial class, the UI-Designer will moan.
    63       this.sortByComboBox.Items.AddRange(Enum.GetValues(typeof(SortingCriteria)).Cast<object>().ToArray());
    64       this.sortByComboBox.SelectedItem = SortingCriteria.ImpactValue;
    65 
    6654      //Set the default values
    6755      this.dataPartitionComboBox.SelectedIndex = 0;
    68       this.replacementComboBox.SelectedIndex = 0;
     56      this.replacementComboBox.SelectedIndex = 3;
    6957      this.factorVarReplComboBox.SelectedIndex = 0;
    70     }
    71     #endregion
    72 
    73     #region Events
     58      this.sortByComboBox.SelectedItem = SortingCriteria.ImpactValue;
     59    }
     60
    7461    protected override void RegisterContentEvents() {
    7562      base.RegisterContentEvents();
     
    7764      Content.ProblemDataChanged += new EventHandler(Content_ProblemDataChanged);
    7865    }
    79 
    8066    protected override void DeregisterContentEvents() {
    8167      base.DeregisterContentEvents();
     
    8773      OnContentChanged();
    8874    }
    89 
    9075    protected virtual void Content_ModelChanged(object sender, EventArgs e) {
    9176      OnContentChanged();
    9277    }
    93 
    9478    protected override void OnContentChanged() {
    9579      base.OnContentChanged();
     
    10084      }
    10185    }
    102 
    10386    private void ClassificationSolutionVariableImpactsView_VisibleChanged(object sender, EventArgs e) {
    104       if (thread == null) { return; }
    105 
    106       if (thread.IsAlive) { thread.Abort(); }
    107       thread = null;
    108     }
    109 
     87      cancellationToken.Cancel();
     88    }
    11089
    11190    private void dataPartitionComboBox_SelectedIndexChanged(object sender, EventArgs e) {
    11291      UpdateVariableImpact();
    11392    }
    114 
    11593    private void replacementComboBox_SelectedIndexChanged(object sender, EventArgs e) {
    11694      UpdateVariableImpact();
    11795    }
    118 
    11996    private void sortByComboBox_SelectedIndexChanged(object sender, EventArgs e) {
    12097      //Update the default ordering (asc,desc), but remove the eventHandler beforehand (otherwise the data would be ordered twice)
    12198      ascendingCheckBox.CheckedChanged -= ascendingCheckBox_CheckedChanged;
    122       switch ((SortingCriteria)sortByComboBox.SelectedItem) {
    123         case SortingCriteria.ImpactValue:
    124           ascendingCheckBox.Checked = false;
    125           break;
    126         case SortingCriteria.Occurrence:
    127           ascendingCheckBox.Checked = true;
    128           break;
    129         case SortingCriteria.VariableName:
    130           ascendingCheckBox.Checked = true;
    131           break;
    132         default:
    133           throw new NotImplementedException("Ordering for selected SortingCriteria not implemented");
    134       }
     99      ascendingCheckBox.Checked = (SortingCriteria)sortByComboBox.SelectedItem != SortingCriteria.ImpactValue;
    135100      ascendingCheckBox.CheckedChanged += ascendingCheckBox_CheckedChanged;
    136101
    137       UpdateDataOrdering();
    138     }
    139 
     102      UpdateOrdering();
     103    }
    140104    private void ascendingCheckBox_CheckedChanged(object sender, EventArgs e) {
    141       UpdateDataOrdering();
    142     }
    143 
    144     #endregion
    145 
    146     #region Helper Methods   
    147     private void UpdateVariableImpact() {
     105      UpdateOrdering();
     106    }
     107
     108    private async void UpdateVariableImpact() {
    148109      //Check if the selection is valid
    149110      if (Content == null) { return; }
     
    152113      if (factorVarReplComboBox.SelectedIndex < 0) { return; }
    153114
     115      IProgress progress;
     116
    154117      //Prepare arguments
    155118      var mainForm = (MainForm.WindowsForms.MainForm)MainFormManager.MainForm;
     
    159122
    160123      variableImactsArrayView.Caption = Content.Name + " Variable Impacts";
    161 
    162       mainForm.AddOperationProgressToView(this, "Calculating variable impacts for " + Content.Name);
    163 
    164       Task.Factory.StartNew(() => {
    165         thread = Thread.CurrentThread;
    166         //Remember the original ordering of the variables
    167         var impacts = ClassificationSolutionVariableImpactsCalculator.CalculateImpacts(Content, dataPartition, replMethod, factorReplMethod);
     124      progress = mainForm.AddOperationProgressToView(this, "Calculating variable impacts for " + Content.Name);
     125      progress.ProgressValue = 0;
     126
     127      cancellationToken = new CancellationTokenSource();
     128
     129      try {
    168130        var problemData = Content.ProblemData;
    169131        var inputvariables = new HashSet<string>(problemData.AllowedInputVariables.Union(Content.Model.VariablesUsedForPrediction));
    170         var originalVariableOrdering = problemData.Dataset.VariableNames.Where(v => inputvariables.Contains(v)).Where(problemData.Dataset.VariableHasType<double>).ToList();
     132        //Remember the original ordering of the variables
     133        var originalVariableOrdering = problemData.Dataset.VariableNames
     134          .Where(v => inputvariables.Contains(v))
     135          .Where(v => problemData.Dataset.VariableHasType<double>(v) || problemData.Dataset.VariableHasType<string>(v))
     136          .ToList();
     137
     138        List<Tuple<string, double>> impacts = null;
     139
     140        await Task.Run(() => { impacts = CalculateVariableImpacts(originalVariableOrdering, (IClassificationModel)Content.Model.Clone(), problemData, Content.EstimatedClassValues, dataPartition, replMethod, factorReplMethod, cancellationToken.Token, progress); });
     141        if (impacts == null) { return; }
    171142
    172143        rawVariableImpacts.Clear();
    173         originalVariableOrdering.ForEach(v => rawVariableImpacts.Add(v, impacts.First(vv => vv.Item1 == v).Item2));
    174       }).ContinueWith((o) => {
    175         UpdateDataOrdering();
    176         mainForm.RemoveOperationProgressFromView(this);
    177         thread = null;
    178       }, TaskScheduler.FromCurrentSynchronizationContext());
     144        originalVariableOrdering.ForEach(v => rawVariableImpacts.Add(new Tuple<string, double>(v, impacts.First(vv => vv.Item1 == v).Item2)));
     145        UpdateOrdering();
     146      }
     147      finally {
     148        ((MainForm.WindowsForms.MainForm)MainFormManager.MainForm).RemoveOperationProgressFromView(this);
     149      }
     150    }
     151
     152    private List<Tuple<string, double>> CalculateVariableImpacts(List<string> originalVariableOrdering,
     153      IClassificationModel model,
     154      IClassificationProblemData problemData,
     155      IEnumerable<double> estimatedValues,
     156      ClassificationSolutionVariableImpactsCalculator.DataPartitionEnum dataPartition,
     157      ClassificationSolutionVariableImpactsCalculator.ReplacementMethodEnum replMethod,
     158      ClassificationSolutionVariableImpactsCalculator.FactorReplacementMethodEnum factorReplMethod,
     159      CancellationToken token,
     160      IProgress progress) {
     161      List<Tuple<string, double>> impacts = new List<Tuple<string, double>>();
     162      int count = originalVariableOrdering.Count;
     163      int i = 0;
     164      var modifiableDataset = ((Dataset)(problemData.Dataset).Clone()).ToModifiable();
     165      IEnumerable<int> rows = ClassificationSolutionVariableImpactsCalculator.GetPartitionRows(dataPartition, problemData);
     166
     167      //Calculate original quality-values (via calculator, default is R²)
     168      OnlineCalculatorError error;
     169      IEnumerable<double> targetValuesPartition = problemData.Dataset.GetDoubleValues(problemData.TargetVariable, rows);
     170      IEnumerable<double> estimatedValuesPartition = rows.Select(v => estimatedValues.ElementAt(v));
     171      var originalCalculatorValue = ClassificationSolutionVariableImpactsCalculator.CalculateVariableImpact(targetValuesPartition, estimatedValuesPartition, out error);
     172      if (error != OnlineCalculatorError.None) throw new InvalidOperationException("Error during calculation.");
     173
     174      foreach (var variableName in originalVariableOrdering) {
     175        if (cancellationToken.Token.IsCancellationRequested) { return null; }
     176        progress.ProgressValue = (double)++i / count;
     177        progress.Status = string.Format("Calculating impact for variable {0} ({1} of {2})", variableName, i, count);
     178
     179        double impact = ClassificationSolutionVariableImpactsCalculator.CalculateImpact(variableName, model, modifiableDataset, rows, targetValuesPartition, originalCalculatorValue, replMethod, factorReplMethod);
     180        impacts.Add(new Tuple<string, double>(variableName, impact));
     181      }
     182
     183      return impacts;
    179184    }
    180185
     
    183188    /// The default is "Descending" by "VariableImpact" (as in previous versions)
    184189    /// </summary>
    185     private void UpdateDataOrdering() {
     190    private void UpdateOrdering() {
    186191      //Check if valid sortingCriteria is selected and data exists
    187192      if (sortByComboBox.SelectedIndex == -1) { return; }
     
    192197      bool ascending = ascendingCheckBox.Checked;
    193198
    194       IEnumerable<KeyValuePair<string, double>> orderedEntries = null;
     199      IEnumerable<Tuple<string, double>> orderedEntries = null;
    195200
    196201      //Sort accordingly
    197202      switch (selectedItem) {
    198203        case SortingCriteria.ImpactValue:
    199           orderedEntries = rawVariableImpacts.OrderBy(v => v.Value);
     204          orderedEntries = rawVariableImpacts.OrderBy(v => v.Item2);
    200205          break;
    201206        case SortingCriteria.Occurrence:
     
    203208          break;
    204209        case SortingCriteria.VariableName:
    205           orderedEntries = rawVariableImpacts.OrderBy(v => v.Key, new NaturalStringComparer());
     210          orderedEntries = rawVariableImpacts.OrderBy(v => v.Item1, new NaturalStringComparer());
    206211          break;
    207212        default:
     
    212217
    213218      //Write the data back
    214       var impactArray = new DoubleArray(orderedEntries.Select(i => i.Value).ToArray()) {
    215         ElementNames = orderedEntries.Select(i => i.Key)
     219      var impactArray = new DoubleArray(orderedEntries.Select(i => i.Item2).ToArray()) {
     220        ElementNames = orderedEntries.Select(i => i.Item1)
    216221      };
    217222
     
    221226      }
    222227    }
    223     #endregion 
    224228  }
    225229}
Note: See TracChangeset for help on using the changeset viewer.