Changeset 15797
- Timestamp:
- 02/21/18 12:28:30 (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/HeuristicLab.Problems.DataAnalysis.Views/3.4/Regression/RegressionSolutionVariableImpactsView.cs
r15796 r15797 22 22 using System; 23 23 using System.Collections.Generic; 24 using System.ComponentModel;25 24 using System.Linq; 25 using System.Threading; 26 using System.Threading.Tasks; 26 27 using HeuristicLab.Common; 27 28 using HeuristicLab.Data; … … 32 33 [Content(typeof(IRegressionSolution))] 33 34 public partial class RegressionSolutionVariableImpactsView : DataAnalysisSolutionEvaluationView { 34 private class BackgroundworkerArguments { 35 internal MainForm.WindowsForms.MainForm mainForm; 36 internal RegressionSolutionVariableImpactsCalculator.ReplacementMethodEnum replMethod; 37 internal RegressionSolutionVariableImpactsCalculator.FactorReplacementMethodEnum factorReplMethod; 38 internal RegressionSolutionVariableImpactsCalculator.DataPartitionEnum dataPartition; 39 } 35 private CancellationTokenSource cancellationToken = new CancellationTokenSource(); 40 36 private enum SortingCriteria { 41 37 ImpactValue, … … 45 41 private IProgress progress; 46 42 private Dictionary<string, double> rawVariableImpacts = new Dictionary<string, double>(); 47 private BackgroundWorker worker = new BackgroundWorker();48 43 49 44 public new IRegressionSolution Content { … … 66 61 this.replacementComboBox.SelectedIndex = 0; 67 62 this.factorVarReplComboBox.SelectedIndex = 0; 68 69 //Worker magic70 worker.WorkerSupportsCancellation = true;71 worker.WorkerReportsProgress = true;72 worker.DoWork += Worker_DoWork;73 worker.RunWorkerCompleted += Worker_RunWorkerCompleted;74 worker.ProgressChanged += Worker_ProgressChanged;75 }76 77 78 private void Worker_DoWork(object sender, DoWorkEventArgs e) {79 var args = e.Argument as BackgroundworkerArguments;80 81 //Remember the original ordering of the variables82 var impacts = RegressionSolutionVariableImpactsCalculator.CalculateImpacts(Content, args.dataPartition, args.replMethod, args.factorReplMethod,83 (i) => {84 var worker = (sender as BackgroundWorker);85 worker.ReportProgress(0, i);86 return worker.CancellationPending;87 });88 89 if ((sender as BackgroundWorker).CancellationPending) { return; }90 var problemData = Content.ProblemData;91 var inputvariables = new HashSet<string>(problemData.AllowedInputVariables.Union(Content.Model.VariablesUsedForPrediction));92 var originalVariableOrdering = problemData.Dataset.VariableNames.Where(v => inputvariables.Contains(v)).Where(problemData.Dataset.VariableHasType<double>).ToList();93 94 rawVariableImpacts.Clear();95 originalVariableOrdering.ForEach(v => rawVariableImpacts.Add(v, impacts.First(vv => vv.Item1 == v).Item2));96 }97 private void Worker_ProgressChanged(object sender, ProgressChangedEventArgs e) {98 progress.ProgressValue = (double)e.UserState;99 }100 private void Worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) {101 if (e.Error != null) { throw e.Error; }102 if (e.Error == null && !e.Cancelled) { UpdateDataOrdering(); }103 ((MainForm.WindowsForms.MainForm)MainFormManager.MainForm).RemoveOperationProgressFromView(this);104 63 } 105 64 … … 134 93 135 94 private void RegressionSolutionVariableImpactsView_VisibleChanged(object sender, EventArgs e) { 136 if (!worker.IsBusy) { return; } 137 worker.CancelAsync(); 95 if (!cancellationToken.IsCancellationRequested) { 96 cancellationToken.Cancel(); 97 } 138 98 } 139 99 … … 173 133 174 134 175 private void UpdateVariableImpact() {135 private async void UpdateVariableImpact() { 176 136 //Check if the selection is valid 177 137 if (Content == null) { return; } … … 190 150 progress.ProgressValue = 0; 191 151 192 if (!worker.IsBusy) { 193 worker.RunWorkerAsync(new BackgroundworkerArguments() { 194 mainForm = mainForm, 195 dataPartition = dataPartition, 196 factorReplMethod = factorReplMethod, 197 replMethod = replMethod 198 }); 152 cancellationToken = new CancellationTokenSource(); 153 //Remember the original ordering of the variables 154 try { 155 var impacts = await Task.Run(() => RegressionSolutionVariableImpactsCalculator.CalculateImpacts(Content, dataPartition, replMethod, factorReplMethod, 156 (i) => { 157 progress.ProgressValue = i; 158 return cancellationToken.Token.IsCancellationRequested; 159 }), cancellationToken.Token); 160 161 if (cancellationToken.Token.IsCancellationRequested) { return; } 162 var problemData = Content.ProblemData; 163 var inputvariables = new HashSet<string>(problemData.AllowedInputVariables.Union(Content.Model.VariablesUsedForPrediction)); 164 var originalVariableOrdering = problemData.Dataset.VariableNames.Where(v => inputvariables.Contains(v)).Where(problemData.Dataset.VariableHasType<double>).ToList(); 165 166 rawVariableImpacts.Clear(); 167 originalVariableOrdering.ForEach(v => rawVariableImpacts.Add(v, impacts.First(vv => vv.Item1 == v).Item2)); 168 UpdateDataOrdering(); 169 } finally { 170 ((MainForm.WindowsForms.MainForm)MainFormManager.MainForm).RemoveOperationProgressFromView(this); 199 171 } 200 172 }
Note: See TracChangeset
for help on using the changeset viewer.