Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
12/21/18 10:31:49 (5 years ago)
Author:
pfleck
Message:

#2845: Merged recent trunk changes into branch

Location:
branches/2845_EnhancedProgress
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • branches/2845_EnhancedProgress

  • branches/2845_EnhancedProgress/HeuristicLab.Problems.DataAnalysis.Views

  • branches/2845_EnhancedProgress/HeuristicLab.Problems.DataAnalysis.Views/3.4/Regression/RegressionSolutionVariableImpactsView.cs

    r16312 r16428  
    3333  [Content(typeof(IRegressionSolution))]
    3434  public partial class RegressionSolutionVariableImpactsView : DataAnalysisSolutionEvaluationView {
    35     private CancellationTokenSource cancellationToken = new CancellationTokenSource();
    3635    private enum SortingCriteria {
    3736      ImpactValue,
     
    3938      VariableName
    4039    }
     40    private CancellationTokenSource cancellationToken = new CancellationTokenSource();
    4141    private List<Tuple<string, double>> rawVariableImpacts = new List<Tuple<string, double>>();
    4242
     
    6464      Content.ProblemDataChanged += new EventHandler(Content_ProblemDataChanged);
    6565    }
    66 
    6766    protected override void DeregisterContentEvents() {
    6867      base.DeregisterContentEvents();
     
    7473      OnContentChanged();
    7574    }
    76 
    7775    protected virtual void Content_ModelChanged(object sender, EventArgs e) {
    7876      OnContentChanged();
    7977    }
    80 
    8178    protected override void OnContentChanged() {
    8279      base.OnContentChanged();
     80      rawVariableImpacts.Clear();
     81
    8382      if (Content == null) {
    84         variableImactsArrayView.Content = null;
     83        variableImpactsArrayView.Content = null;
    8584      } else {
    8685        UpdateVariableImpact();
    8786      }
    8887    }
    89 
    9088    private void RegressionSolutionVariableImpactsView_VisibleChanged(object sender, EventArgs e) {
    9189      cancellationToken.Cancel();
    9290    }
    9391
    94 
    9592    private void dataPartitionComboBox_SelectedIndexChanged(object sender, EventArgs e) {
     93      rawVariableImpacts.Clear();
    9694      UpdateVariableImpact();
    9795    }
    98 
    9996    private void replacementComboBox_SelectedIndexChanged(object sender, EventArgs e) {
     97      rawVariableImpacts.Clear();
    10098      UpdateVariableImpact();
    10199    }
    102 
    103100    private void sortByComboBox_SelectedIndexChanged(object sender, EventArgs e) {
    104101      //Update the default ordering (asc,desc), but remove the eventHandler beforehand (otherwise the data would be ordered twice)
     
    109106      UpdateOrdering();
    110107    }
    111 
    112108    private void ascendingCheckBox_CheckedChanged(object sender, EventArgs e) {
    113109      UpdateOrdering();
    114110    }
    115 
    116111
    117112    private async void UpdateVariableImpact() {
     
    127122      var dataPartition = (RegressionSolutionVariableImpactsCalculator.DataPartitionEnum)dataPartitionComboBox.SelectedItem;
    128123
    129       variableImactsArrayView.Caption = Content.Name + " Variable Impacts";
     124      variableImpactsArrayView.Caption = Content.Name + " Variable Impacts";
    130125      var progress = Progress.Show(this, "Calculating variable impacts for " + Content.Name);
    131126      cancellationToken = new CancellationTokenSource();
    132       //Remember the original ordering of the variables
     127
    133128      try {
    134         var impacts = await Task.Run(() => RegressionSolutionVariableImpactsCalculator.CalculateImpacts(Content, dataPartition, replMethod, factorReplMethod,
    135           (i, s) => {
    136             progress.ProgressValue = i;
    137             progress.Message = s;
    138             return cancellationToken.Token.IsCancellationRequested;
    139           }), cancellationToken.Token);
    140 
    141         if (cancellationToken.Token.IsCancellationRequested) { return; }
    142129        var problemData = Content.ProblemData;
    143130        var inputvariables = new HashSet<string>(problemData.AllowedInputVariables.Union(Content.Model.VariablesUsedForPrediction));
     131        //Remember the original ordering of the variables
    144132        var originalVariableOrdering = problemData.Dataset.VariableNames
    145133          .Where(v => inputvariables.Contains(v))
     
    147135          .ToList();
    148136
    149         rawVariableImpacts.Clear();
    150         originalVariableOrdering.ForEach(v => rawVariableImpacts.Add(new Tuple<string, double>(v, impacts.First(vv => vv.Item1 == v).Item2)));
     137        List<Tuple<string, double>> impacts = null;
     138        await Task.Run(() => { impacts = CalculateVariableImpacts(originalVariableOrdering, Content.Model, problemData, Content.EstimatedValues, dataPartition, replMethod, factorReplMethod, cancellationToken.Token, progress); });
     139        if (impacts == null) { return; }
     140
     141        rawVariableImpacts.AddRange(impacts);
    151142        UpdateOrdering();
    152       } finally {
     143      }
     144      finally {
    153145        Progress.Hide(this);
    154146      }
    155147    }
     148    private List<Tuple<string, double>> CalculateVariableImpacts(List<string> originalVariableOrdering,
     149      IRegressionModel model,
     150      IRegressionProblemData problemData,
     151      IEnumerable<double> estimatedValues,
     152      RegressionSolutionVariableImpactsCalculator.DataPartitionEnum dataPartition,
     153      RegressionSolutionVariableImpactsCalculator.ReplacementMethodEnum replMethod,
     154      RegressionSolutionVariableImpactsCalculator.FactorReplacementMethodEnum factorReplMethod,
     155      CancellationToken token,
     156      IProgress progress) {
     157      List<Tuple<string, double>> impacts = new List<Tuple<string, double>>();
     158      int count = originalVariableOrdering.Count;
     159      int i = 0;
     160      var modifiableDataset = ((Dataset)(problemData.Dataset).Clone()).ToModifiable();
     161      IEnumerable<int> rows = RegressionSolutionVariableImpactsCalculator.GetPartitionRows(dataPartition, problemData);
     162
     163      //Calculate original quality-values (via calculator, default is R²)
     164      IEnumerable<double> targetValuesPartition = problemData.Dataset.GetDoubleValues(problemData.TargetVariable, rows);
     165      IEnumerable<double> estimatedValuesPartition = Content.GetEstimatedValues(rows);
     166
     167      var originalCalculatorValue = RegressionSolutionVariableImpactsCalculator.CalculateQuality(targetValuesPartition, estimatedValuesPartition);
     168
     169      foreach (var variableName in originalVariableOrdering) {
     170        if (cancellationToken.Token.IsCancellationRequested) { return null; }
     171        progress.ProgressValue = (double)++i / count;
     172        progress.Message = string.Format("Calculating impact for variable {0} ({1} of {2})", variableName, i, count);
     173
     174        double impact = 0;
     175        //If the variable isn't used for prediction, it has zero impact.
     176        if (model.VariablesUsedForPrediction.Contains(variableName)) {
     177          impact = RegressionSolutionVariableImpactsCalculator.CalculateImpact(variableName, model, problemData, modifiableDataset, rows, replMethod, factorReplMethod, targetValuesPartition, originalCalculatorValue);
     178        }
     179        impacts.Add(new Tuple<string, double>(variableName, impact));
     180      }
     181
     182      return impacts;
     183    }
    156184
    157185    /// <summary>
    158     /// Updates the <see cref="variableImactsArrayView"/> according to the selected ordering <see cref="ascendingCheckBox"/> of the selected Column <see cref="sortByComboBox"/>
     186    /// Updates the <see cref="variableImpactsArrayView"/> according to the selected ordering <see cref="ascendingCheckBox"/> of the selected Column <see cref="sortByComboBox"/>
    159187    /// The default is "Descending" by "VariableImpact" (as in previous versions)
    160188    /// </summary>
     
    193221
    194222      //Could be, if the View was closed
    195       if (!variableImactsArrayView.IsDisposed) {
    196         variableImactsArrayView.Content = (DoubleArray)impactArray.AsReadOnly();
     223      if (!variableImpactsArrayView.IsDisposed) {
     224        variableImpactsArrayView.Content = (DoubleArray)impactArray.AsReadOnly();
    197225      }
    198226    }
Note: See TracChangeset for help on using the changeset viewer.