Changeset 16311 for branches/2845_EnhancedProgress/HeuristicLab.Problems.DataAnalysis.Views/3.4/Regression/RegressionSolutionVariableImpactsView.cs
- Timestamp:
- 11/20/18 15:26:57 (6 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
r16308 r16311 1 1 #region License Information 2 2 /* HeuristicLab 3 * Copyright (C) 2002-201 6Heuristic and Evolutionary Algorithms Laboratory (HEAL)3 * Copyright (C) 2002-2018 Heuristic and Evolutionary Algorithms Laboratory (HEAL) 4 4 * 5 5 * This file is part of HeuristicLab. … … 19 19 */ 20 20 #endregion 21 21 22 using System; 23 using System.Collections.Generic; 22 24 using System.Linq; 25 using System.Threading; 23 26 using System.Threading.Tasks; 27 using HeuristicLab.Common; 24 28 using HeuristicLab.Data; 25 29 using HeuristicLab.MainForm; … … 29 33 [Content(typeof(IRegressionSolution))] 30 34 public partial class RegressionSolutionVariableImpactsView : DataAnalysisSolutionEvaluationView { 35 private CancellationTokenSource cancellationToken = new CancellationTokenSource(); 36 private enum SortingCriteria { 37 ImpactValue, 38 Occurrence, 39 VariableName 40 } 41 private List<Tuple<string, double>> rawVariableImpacts = new List<Tuple<string, double>>(); 31 42 32 43 public new IRegressionSolution Content { … … 40 51 : base() { 41 52 InitializeComponent(); 53 54 //Set the default values 42 55 this.dataPartitionComboBox.SelectedIndex = 0; 43 this.replacementComboBox.SelectedIndex = 0;56 this.replacementComboBox.SelectedIndex = 3; 44 57 this.factorVarReplComboBox.SelectedIndex = 0; 45 }46 47 #region events 58 this.sortByComboBox.SelectedItem = SortingCriteria.ImpactValue; 59 } 60 48 61 protected override void RegisterContentEvents() { 49 62 base.RegisterContentEvents(); … … 71 84 variableImactsArrayView.Content = null; 72 85 } else { 73 UpdateVariableImpacts(); 74 } 75 } 76 77 private void UpdateVariableImpacts() { 78 if (Content == null || replacementComboBox.SelectedIndex < 0 79 || factorVarReplComboBox.SelectedIndex < 0 80 || dataPartitionComboBox.SelectedIndex < 0) return; 86 UpdateVariableImpact(); 87 } 88 } 89 90 private void RegressionSolutionVariableImpactsView_VisibleChanged(object sender, EventArgs e) { 91 cancellationToken.Cancel(); 92 } 93 94 95 private void dataPartitionComboBox_SelectedIndexChanged(object sender, EventArgs e) { 96 UpdateVariableImpact(); 97 } 98 99 private void replacementComboBox_SelectedIndexChanged(object sender, EventArgs e) { 100 UpdateVariableImpact(); 101 } 102 103 private void sortByComboBox_SelectedIndexChanged(object sender, EventArgs e) { 104 //Update the default ordering (asc,desc), but remove the eventHandler beforehand (otherwise the data would be ordered twice) 105 ascendingCheckBox.CheckedChanged -= ascendingCheckBox_CheckedChanged; 106 ascendingCheckBox.Checked = (SortingCriteria)sortByComboBox.SelectedItem != SortingCriteria.ImpactValue; 107 ascendingCheckBox.CheckedChanged += ascendingCheckBox_CheckedChanged; 108 109 UpdateOrdering(); 110 } 111 112 private void ascendingCheckBox_CheckedChanged(object sender, EventArgs e) { 113 UpdateOrdering(); 114 } 115 116 117 private async void UpdateVariableImpact() { 118 //Check if the selection is valid 119 if (Content == null) { return; } 120 if (replacementComboBox.SelectedIndex < 0) { return; } 121 if (dataPartitionComboBox.SelectedIndex < 0) { return; } 122 if (factorVarReplComboBox.SelectedIndex < 0) { return; } 123 124 //Prepare arguments 125 var replMethod = (RegressionSolutionVariableImpactsCalculator.ReplacementMethodEnum)replacementComboBox.Items[replacementComboBox.SelectedIndex]; 126 var factorReplMethod = (RegressionSolutionVariableImpactsCalculator.FactorReplacementMethodEnum)factorVarReplComboBox.Items[factorVarReplComboBox.SelectedIndex]; 127 var dataPartition = (RegressionSolutionVariableImpactsCalculator.DataPartitionEnum)dataPartitionComboBox.SelectedItem; 128 81 129 variableImactsArrayView.Caption = Content.Name + " Variable Impacts"; 82 var replMethod = 83 (RegressionSolutionVariableImpactsCalculator.ReplacementMethodEnum) 84 replacementComboBox.Items[replacementComboBox.SelectedIndex]; 85 var factorReplMethod = 86 (RegressionSolutionVariableImpactsCalculator.FactorReplacementMethodEnum) 87 factorVarReplComboBox.Items[factorVarReplComboBox.SelectedIndex]; 88 var dataPartition = 89 (RegressionSolutionVariableImpactsCalculator.DataPartitionEnum)dataPartitionComboBox.SelectedItem; 90 91 Task.Factory.StartNew(() => { 92 try { 93 Progress.ShowMarquee(this, "Calculating variable impacts for " + Content.Name); 94 95 var impacts = RegressionSolutionVariableImpactsCalculator.CalculateImpacts(Content, dataPartition, replMethod, factorReplMethod); 96 var impactArray = new DoubleArray(impacts.Select(i => i.Item2).ToArray()); 97 impactArray.ElementNames = impacts.Select(i => i.Item1); 98 variableImactsArrayView.Content = (DoubleArray)impactArray.AsReadOnly(); 99 } finally { 100 Progress.Hide(this); 101 } 102 }); 103 } 104 105 #endregion 106 107 private void dataPartitionComboBox_SelectedIndexChanged(object sender, EventArgs e) { 108 UpdateVariableImpacts(); 109 } 110 111 private void replacementComboBox_SelectedIndexChanged(object sender, EventArgs e) { 112 UpdateVariableImpacts(); 130 var progress = Progress.ShowMarquee(this, "Calculating variable impacts for " + Content.Name); 131 cancellationToken = new CancellationTokenSource(); 132 //Remember the original ordering of the variables 133 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 var problemData = Content.ProblemData; 143 var inputvariables = new HashSet<string>(problemData.AllowedInputVariables.Union(Content.Model.VariablesUsedForPrediction)); 144 var originalVariableOrdering = problemData.Dataset.VariableNames 145 .Where(v => inputvariables.Contains(v)) 146 .Where(v => problemData.Dataset.VariableHasType<double>(v) || problemData.Dataset.VariableHasType<string>(v)) 147 .ToList(); 148 149 rawVariableImpacts.Clear(); 150 originalVariableOrdering.ForEach(v => rawVariableImpacts.Add(new Tuple<string, double>(v, impacts.First(vv => vv.Item1 == v).Item2))); 151 UpdateOrdering(); 152 } finally { 153 Progress.Hide(this); 154 } 155 } 156 157 /// <summary> 158 /// Updates the <see cref="variableImactsArrayView"/> according to the selected ordering <see cref="ascendingCheckBox"/> of the selected Column <see cref="sortByComboBox"/> 159 /// The default is "Descending" by "VariableImpact" (as in previous versions) 160 /// </summary> 161 private void UpdateOrdering() { 162 //Check if valid sortingCriteria is selected and data exists 163 if (sortByComboBox.SelectedIndex == -1) { return; } 164 if (rawVariableImpacts == null) { return; } 165 if (!rawVariableImpacts.Any()) { return; } 166 167 var selectedItem = (SortingCriteria)sortByComboBox.SelectedItem; 168 bool ascending = ascendingCheckBox.Checked; 169 170 IEnumerable<Tuple<string, double>> orderedEntries = null; 171 172 //Sort accordingly 173 switch (selectedItem) { 174 case SortingCriteria.ImpactValue: 175 orderedEntries = rawVariableImpacts.OrderBy(v => v.Item2); 176 break; 177 case SortingCriteria.Occurrence: 178 orderedEntries = rawVariableImpacts; 179 break; 180 case SortingCriteria.VariableName: 181 orderedEntries = rawVariableImpacts.OrderBy(v => v.Item1, new NaturalStringComparer()); 182 break; 183 default: 184 throw new NotImplementedException("Ordering for selected SortingCriteria not implemented"); 185 } 186 187 if (!ascending) { orderedEntries = orderedEntries.Reverse(); } 188 189 //Write the data back 190 var impactArray = new DoubleArray(orderedEntries.Select(i => i.Item2).ToArray()) { 191 ElementNames = orderedEntries.Select(i => i.Item1) 192 }; 193 194 //Could be, if the View was closed 195 if (!variableImactsArrayView.IsDisposed) { 196 variableImactsArrayView.Content = (DoubleArray)impactArray.AsReadOnly(); 197 } 113 198 } 114 199 }
Note: See TracChangeset
for help on using the changeset viewer.