Changeset 15796
Legend:
- Unmodified
- Added
- Removed
-
trunk/HeuristicLab.Problems.DataAnalysis.Views/3.4/Regression/RegressionSolutionVariableImpactsView.cs
r15752 r15796 22 22 using System; 23 23 using System.Collections.Generic; 24 using System.ComponentModel; 24 25 using System.Linq; 25 using System.Threading;26 using System.Threading.Tasks;27 26 using HeuristicLab.Common; 28 27 using HeuristicLab.Data; … … 33 32 [Content(typeof(IRegressionSolution))] 34 33 public partial class RegressionSolutionVariableImpactsView : DataAnalysisSolutionEvaluationView { 35 #region Nested Types 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 } 36 40 private enum SortingCriteria { 37 41 ImpactValue, … … 39 43 VariableName 40 44 } 41 #endregion 42 43 #region Fields 45 private IProgress progress; 44 46 private Dictionary<string, double> rawVariableImpacts = new Dictionary<string, double>(); 45 private Thread thread; 46 #endregion 47 48 #region Getter/Setter 47 private BackgroundWorker worker = new BackgroundWorker(); 48 49 49 public new IRegressionSolution Content { 50 50 get { return (IRegressionSolution)base.Content; } … … 53 53 } 54 54 } 55 #endregion 56 57 #region Ctor 55 58 56 public RegressionSolutionVariableImpactsView() 59 57 : base() { … … 68 66 this.replacementComboBox.SelectedIndex = 0; 69 67 this.factorVarReplComboBox.SelectedIndex = 0; 70 } 71 #endregion 72 73 #region Events 68 69 //Worker magic 70 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 variables 82 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 } 105 74 106 protected override void RegisterContentEvents() { 75 107 base.RegisterContentEvents(); … … 102 134 103 135 private void RegressionSolutionVariableImpactsView_VisibleChanged(object sender, EventArgs e) { 104 if (thread == null) { return; } 105 106 if (thread.IsAlive) { thread.Abort(); } 107 thread = null; 136 if (!worker.IsBusy) { return; } 137 worker.CancelAsync(); 108 138 } 109 139 … … 142 172 } 143 173 144 #endregion 145 146 #region Helper Methods 174 147 175 private void UpdateVariableImpact() { 148 176 //Check if the selection is valid … … 159 187 160 188 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 = RegressionSolutionVariableImpactsCalculator.CalculateImpacts(Content, dataPartition, replMethod, factorReplMethod); 168 var problemData = Content.ProblemData; 169 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(); 171 172 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()); 189 progress = mainForm.AddOperationProgressToView(this, "Calculating variable impacts for " + Content.Name); 190 progress.ProgressValue = 0; 191 192 if (!worker.IsBusy) { 193 worker.RunWorkerAsync(new BackgroundworkerArguments() { 194 mainForm = mainForm, 195 dataPartition = dataPartition, 196 factorReplMethod = factorReplMethod, 197 replMethod = replMethod 198 }); 199 } 179 200 } 180 201 … … 221 242 } 222 243 } 223 #endregion224 244 } 225 245 } -
trunk/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Regression/RegressionSolutionVariableImpactsCalculator.cs
r15673 r15796 52 52 All 53 53 } 54 54 55 55 private const string ReplacementParameterName = "Replacement Method"; 56 56 private const string DataPartitionParameterName = "DataPartition"; … … 96 96 DataPartitionEnum data = DataPartitionEnum.Training, 97 97 ReplacementMethodEnum replacementMethod = ReplacementMethodEnum.Median, 98 FactorReplacementMethodEnum factorReplacementMethod = FactorReplacementMethodEnum.Best ) {98 FactorReplacementMethodEnum factorReplacementMethod = FactorReplacementMethodEnum.Best, Func<double, bool> progressCallback = null) { 99 99 100 100 var problemData = solution.ProblemData; … … 134 134 var allowedInputVariables = dataset.VariableNames.Where(v => inputvariables.Contains(v)).ToList(); 135 135 136 int curIdx = 1; 137 int count = allowedInputVariables.Where(problemData.Dataset.VariableHasType<double>).Count(); 136 138 // calculate impacts for double variables 137 139 foreach (var inputVariable in allowedInputVariables.Where(problemData.Dataset.VariableHasType<double>)) { 140 //Report the current progress in percent. If the callback returns true, it means the execution shall be stopped 141 if (progressCallback != null) { 142 if (progressCallback((double)curIdx++ / count)) { return null; } 143 } 138 144 var newEstimates = EvaluateModelWithReplacedVariable(solution.Model, inputVariable, modifiableDataset, rows, replacementMethod); 139 145 var newR2 = OnlinePearsonsRCalculator.Calculate(targetValues, newEstimates, out error); … … 180 186 } 181 187 188 182 189 private static IEnumerable<double> EvaluateModelWithReplacedVariable(IRegressionModel model, string variable, ModifiableDataset dataset, IEnumerable<int> rows, ReplacementMethodEnum replacement = ReplacementMethodEnum.Median) { 183 190 var originalValues = dataset.GetReadOnlyDoubleValues(variable).ToList();
Note: See TracChangeset
for help on using the changeset viewer.