Changeset 16428 for branches/2845_EnhancedProgress/HeuristicLab.Problems.DataAnalysis.Views/3.4/Regression/RegressionSolutionVariableImpactsView.cs
- Timestamp:
- 12/21/18 10:31:49 (5 years ago)
- Location:
- branches/2845_EnhancedProgress
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/2845_EnhancedProgress
- Property svn:mergeinfo changed
-
branches/2845_EnhancedProgress/HeuristicLab.Problems.DataAnalysis.Views
- Property svn:mergeinfo changed
-
branches/2845_EnhancedProgress/HeuristicLab.Problems.DataAnalysis.Views/3.4/Regression/RegressionSolutionVariableImpactsView.cs
r16312 r16428 33 33 [Content(typeof(IRegressionSolution))] 34 34 public partial class RegressionSolutionVariableImpactsView : DataAnalysisSolutionEvaluationView { 35 private CancellationTokenSource cancellationToken = new CancellationTokenSource();36 35 private enum SortingCriteria { 37 36 ImpactValue, … … 39 38 VariableName 40 39 } 40 private CancellationTokenSource cancellationToken = new CancellationTokenSource(); 41 41 private List<Tuple<string, double>> rawVariableImpacts = new List<Tuple<string, double>>(); 42 42 … … 64 64 Content.ProblemDataChanged += new EventHandler(Content_ProblemDataChanged); 65 65 } 66 67 66 protected override void DeregisterContentEvents() { 68 67 base.DeregisterContentEvents(); … … 74 73 OnContentChanged(); 75 74 } 76 77 75 protected virtual void Content_ModelChanged(object sender, EventArgs e) { 78 76 OnContentChanged(); 79 77 } 80 81 78 protected override void OnContentChanged() { 82 79 base.OnContentChanged(); 80 rawVariableImpacts.Clear(); 81 83 82 if (Content == null) { 84 variableIm actsArrayView.Content = null;83 variableImpactsArrayView.Content = null; 85 84 } else { 86 85 UpdateVariableImpact(); 87 86 } 88 87 } 89 90 88 private void RegressionSolutionVariableImpactsView_VisibleChanged(object sender, EventArgs e) { 91 89 cancellationToken.Cancel(); 92 90 } 93 91 94 95 92 private void dataPartitionComboBox_SelectedIndexChanged(object sender, EventArgs e) { 93 rawVariableImpacts.Clear(); 96 94 UpdateVariableImpact(); 97 95 } 98 99 96 private void replacementComboBox_SelectedIndexChanged(object sender, EventArgs e) { 97 rawVariableImpacts.Clear(); 100 98 UpdateVariableImpact(); 101 99 } 102 103 100 private void sortByComboBox_SelectedIndexChanged(object sender, EventArgs e) { 104 101 //Update the default ordering (asc,desc), but remove the eventHandler beforehand (otherwise the data would be ordered twice) … … 109 106 UpdateOrdering(); 110 107 } 111 112 108 private void ascendingCheckBox_CheckedChanged(object sender, EventArgs e) { 113 109 UpdateOrdering(); 114 110 } 115 116 111 117 112 private async void UpdateVariableImpact() { … … 127 122 var dataPartition = (RegressionSolutionVariableImpactsCalculator.DataPartitionEnum)dataPartitionComboBox.SelectedItem; 128 123 129 variableIm actsArrayView.Caption = Content.Name + " Variable Impacts";124 variableImpactsArrayView.Caption = Content.Name + " Variable Impacts"; 130 125 var progress = Progress.Show(this, "Calculating variable impacts for " + Content.Name); 131 126 cancellationToken = new CancellationTokenSource(); 132 //Remember the original ordering of the variables 127 133 128 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; }142 129 var problemData = Content.ProblemData; 143 130 var inputvariables = new HashSet<string>(problemData.AllowedInputVariables.Union(Content.Model.VariablesUsedForPrediction)); 131 //Remember the original ordering of the variables 144 132 var originalVariableOrdering = problemData.Dataset.VariableNames 145 133 .Where(v => inputvariables.Contains(v)) … … 147 135 .ToList(); 148 136 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); 151 142 UpdateOrdering(); 152 } finally { 143 } 144 finally { 153 145 Progress.Hide(this); 154 146 } 155 147 } 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 } 156 184 157 185 /// <summary> 158 /// Updates the <see cref="variableIm actsArrayView"/> 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"/> 159 187 /// The default is "Descending" by "VariableImpact" (as in previous versions) 160 188 /// </summary> … … 193 221 194 222 //Could be, if the View was closed 195 if (!variableIm actsArrayView.IsDisposed) {196 variableIm actsArrayView.Content = (DoubleArray)impactArray.AsReadOnly();223 if (!variableImpactsArrayView.IsDisposed) { 224 variableImpactsArrayView.Content = (DoubleArray)impactArray.AsReadOnly(); 197 225 } 198 226 }
Note: See TracChangeset
for help on using the changeset viewer.