Changeset 16057 for branches/2839_HiveProjectManagement/HeuristicLab.Problems.DataAnalysis.Views/3.4/Regression/RegressionSolutionVariableImpactsView.cs
- Timestamp:
- 08/06/18 18:15:29 (6 years ago)
- Location:
- branches/2839_HiveProjectManagement
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/2839_HiveProjectManagement
- Property svn:mergeinfo changed
-
branches/2839_HiveProjectManagement/HeuristicLab.Problems.DataAnalysis.Views
- Property svn:mergeinfo changed
-
branches/2839_HiveProjectManagement/HeuristicLab.Problems.DataAnalysis.Views/3.4/Regression/RegressionSolutionVariableImpactsView.cs
r14826 r16057 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; 26 using HeuristicLab.Problems.DataAnalysis.Symbolic.Regression;27 30 28 31 namespace HeuristicLab.Problems.DataAnalysis.Views { … … 30 33 [Content(typeof(IRegressionSolution))] 31 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>>(); 32 42 33 43 public new IRegressionSolution Content { … … 41 51 : base() { 42 52 InitializeComponent(); 53 54 //Set the default values 43 55 this.dataPartitionComboBox.SelectedIndex = 0; 44 this.replacementComboBox.SelectedIndex = 0;56 this.replacementComboBox.SelectedIndex = 3; 45 57 this.factorVarReplComboBox.SelectedIndex = 0; 46 }47 48 #region events 58 this.sortByComboBox.SelectedItem = SortingCriteria.ImpactValue; 59 } 60 49 61 protected override void RegisterContentEvents() { 50 62 base.RegisterContentEvents(); … … 72 84 variableImactsArrayView.Content = null; 73 85 } else { 74 UpdateVariableImpacts(); 75 } 76 } 77 78 private void UpdateVariableImpacts() { 79 if (Content == null || replacementComboBox.SelectedIndex < 0 80 || factorVarReplComboBox.SelectedIndex < 0 81 || 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 IProgress progress; 119 120 //Check if the selection is valid 121 if (Content == null) { return; } 122 if (replacementComboBox.SelectedIndex < 0) { return; } 123 if (dataPartitionComboBox.SelectedIndex < 0) { return; } 124 if (factorVarReplComboBox.SelectedIndex < 0) { return; } 125 126 //Prepare arguments 82 127 var mainForm = (MainForm.WindowsForms.MainForm)MainFormManager.MainForm; 128 var replMethod = (RegressionSolutionVariableImpactsCalculator.ReplacementMethodEnum)replacementComboBox.Items[replacementComboBox.SelectedIndex]; 129 var factorReplMethod = (RegressionSolutionVariableImpactsCalculator.FactorReplacementMethodEnum)factorVarReplComboBox.Items[factorVarReplComboBox.SelectedIndex]; 130 var dataPartition = (RegressionSolutionVariableImpactsCalculator.DataPartitionEnum)dataPartitionComboBox.SelectedItem; 131 83 132 variableImactsArrayView.Caption = Content.Name + " Variable Impacts"; 84 var replMethod = 85 (RegressionSolutionVariableImpactsCalculator.ReplacementMethodEnum) 86 replacementComboBox.Items[replacementComboBox.SelectedIndex]; 87 var factorReplMethod = 88 (RegressionSolutionVariableImpactsCalculator.FactorReplacementMethodEnum) 89 factorVarReplComboBox.Items[factorVarReplComboBox.SelectedIndex]; 90 var dataPartition = 91 (RegressionSolutionVariableImpactsCalculator.DataPartitionEnum)dataPartitionComboBox.SelectedItem; 92 93 Task.Factory.StartNew(() => { 94 try { 95 mainForm.AddOperationProgressToView(this, "Calculating variable impacts for " + Content.Name); 96 97 var impacts = RegressionSolutionVariableImpactsCalculator.CalculateImpacts(Content, dataPartition, replMethod, factorReplMethod); 98 var impactArray = new DoubleArray(impacts.Select(i => i.Item2).ToArray()); 99 impactArray.ElementNames = impacts.Select(i => i.Item1); 100 variableImactsArrayView.Content = (DoubleArray)impactArray.AsReadOnly(); 101 } finally { 102 mainForm.RemoveOperationProgressFromView(this); 103 } 104 }); 105 } 106 107 #endregion 108 109 private void dataPartitionComboBox_SelectedIndexChanged(object sender, EventArgs e) { 110 UpdateVariableImpacts(); 111 } 112 113 private void replacementComboBox_SelectedIndexChanged(object sender, EventArgs e) { 114 UpdateVariableImpacts(); 133 progress = mainForm.AddOperationProgressToView(this, "Calculating variable impacts for " + Content.Name); 134 progress.ProgressValue = 0; 135 136 cancellationToken = new CancellationTokenSource(); 137 //Remember the original ordering of the variables 138 try { 139 var impacts = await Task.Run(() => RegressionSolutionVariableImpactsCalculator.CalculateImpacts(Content, dataPartition, replMethod, factorReplMethod, 140 (i, s) => { 141 progress.ProgressValue = i; 142 progress.Status = s; 143 return cancellationToken.Token.IsCancellationRequested; 144 }), cancellationToken.Token); 145 146 if (cancellationToken.Token.IsCancellationRequested) { return; } 147 var problemData = Content.ProblemData; 148 var inputvariables = new HashSet<string>(problemData.AllowedInputVariables.Union(Content.Model.VariablesUsedForPrediction)); 149 var originalVariableOrdering = problemData.Dataset.VariableNames 150 .Where(v => inputvariables.Contains(v)) 151 .Where(v => problemData.Dataset.VariableHasType<double>(v) || problemData.Dataset.VariableHasType<string>(v)) 152 .ToList(); 153 154 rawVariableImpacts.Clear(); 155 originalVariableOrdering.ForEach(v => rawVariableImpacts.Add(new Tuple<string, double>(v, impacts.First(vv => vv.Item1 == v).Item2))); 156 UpdateOrdering(); 157 } finally { 158 ((MainForm.WindowsForms.MainForm)MainFormManager.MainForm).RemoveOperationProgressFromView(this); 159 } 160 } 161 162 /// <summary> 163 /// Updates the <see cref="variableImactsArrayView"/> according to the selected ordering <see cref="ascendingCheckBox"/> of the selected Column <see cref="sortByComboBox"/> 164 /// The default is "Descending" by "VariableImpact" (as in previous versions) 165 /// </summary> 166 private void UpdateOrdering() { 167 //Check if valid sortingCriteria is selected and data exists 168 if (sortByComboBox.SelectedIndex == -1) { return; } 169 if (rawVariableImpacts == null) { return; } 170 if (!rawVariableImpacts.Any()) { return; } 171 172 var selectedItem = (SortingCriteria)sortByComboBox.SelectedItem; 173 bool ascending = ascendingCheckBox.Checked; 174 175 IEnumerable<Tuple<string, double>> orderedEntries = null; 176 177 //Sort accordingly 178 switch (selectedItem) { 179 case SortingCriteria.ImpactValue: 180 orderedEntries = rawVariableImpacts.OrderBy(v => v.Item2); 181 break; 182 case SortingCriteria.Occurrence: 183 orderedEntries = rawVariableImpacts; 184 break; 185 case SortingCriteria.VariableName: 186 orderedEntries = rawVariableImpacts.OrderBy(v => v.Item1, new NaturalStringComparer()); 187 break; 188 default: 189 throw new NotImplementedException("Ordering for selected SortingCriteria not implemented"); 190 } 191 192 if (!ascending) { orderedEntries = orderedEntries.Reverse(); } 193 194 //Write the data back 195 var impactArray = new DoubleArray(orderedEntries.Select(i => i.Item2).ToArray()) { 196 ElementNames = orderedEntries.Select(i => i.Item1) 197 }; 198 199 //Could be, if the View was closed 200 if (!variableImactsArrayView.IsDisposed) { 201 variableImactsArrayView.Content = (DoubleArray)impactArray.AsReadOnly(); 202 } 115 203 } 116 204 }
Note: See TracChangeset
for help on using the changeset viewer.