Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3073_IA_constraint_splitting/HeuristicLab.Problems.DataAnalysis.Views/3.4/Regression/RegressionSolutionVariableImpactsView.cs @ 17591

Last change on this file since 17591 was 17426, checked in by pfleck, 5 years ago

#2973

  • Certain events of the TargetControl are now forwarded to the ProgressView in order to correctly dispose and hide the ProgressView.
  • Simplified hiding the ProgressView within the Regression/ClassificationSolutionVariableImpactsView.
File size: 10.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Collections.Generic;
24using System.Linq;
25using System.Threading;
26using System.Threading.Tasks;
27using System.Windows.Forms;
28using HeuristicLab.Common;
29using HeuristicLab.Data;
30using HeuristicLab.MainForm;
31
32namespace HeuristicLab.Problems.DataAnalysis.Views {
33  [View("Variable Impacts")]
34  [Content(typeof(IRegressionSolution))]
35  public partial class RegressionSolutionVariableImpactsView : DataAnalysisSolutionEvaluationView {
36    private enum SortingCriteria {
37      ImpactValue,
38      Occurrence,
39      VariableName
40    }
41    private CancellationTokenSource cancellationToken = new CancellationTokenSource();
42    private List<Tuple<string, double>> rawVariableImpacts = new List<Tuple<string, double>>();
43
44    public new IRegressionSolution Content {
45      get { return (IRegressionSolution)base.Content; }
46      set {
47        base.Content = value;
48      }
49    }
50
51    public RegressionSolutionVariableImpactsView()
52      : base() {
53      InitializeComponent();
54
55      //Set the default values
56      this.dataPartitionComboBox.SelectedIndex = 0;
57      this.replacementComboBox.SelectedIndex = 3;
58      this.factorVarReplComboBox.SelectedIndex = 0;
59      this.sortByComboBox.SelectedItem = SortingCriteria.ImpactValue;
60    }
61
62    protected override void RegisterContentEvents() {
63      base.RegisterContentEvents();
64      Content.ModelChanged += new EventHandler(Content_ModelChanged);
65      Content.ProblemDataChanged += new EventHandler(Content_ProblemDataChanged);
66    }
67    protected override void DeregisterContentEvents() {
68      base.DeregisterContentEvents();
69      Content.ModelChanged -= new EventHandler(Content_ModelChanged);
70      Content.ProblemDataChanged -= new EventHandler(Content_ProblemDataChanged);
71    }
72
73    protected virtual void Content_ProblemDataChanged(object sender, EventArgs e) {
74      OnContentChanged();
75    }
76    protected virtual void Content_ModelChanged(object sender, EventArgs e) {
77      OnContentChanged();
78    }
79    protected override void OnContentChanged() {
80      base.OnContentChanged();
81      rawVariableImpacts.Clear();
82
83      if (Content == null) {
84        variableImpactsArrayView.Content = null;
85      } else {
86        UpdateVariableImpact();
87      }
88    }
89
90    protected override void OnVisibleChanged(EventArgs e) {
91      base.OnVisibleChanged(e);
92      if (!this.Visible) {
93        cancellationToken.Cancel();
94      }
95    }
96
97    protected override void OnClosed(FormClosedEventArgs e) {
98      base.OnClosed(e);
99      cancellationToken.Cancel();
100    }
101
102    private void dataPartitionComboBox_SelectedIndexChanged(object sender, EventArgs e) {
103      rawVariableImpacts.Clear();
104      UpdateVariableImpact();
105    }
106    private void replacementComboBox_SelectedIndexChanged(object sender, EventArgs e) {
107      rawVariableImpacts.Clear();
108      UpdateVariableImpact();
109    }
110    private void sortByComboBox_SelectedIndexChanged(object sender, EventArgs e) {
111      //Update the default ordering (asc,desc), but remove the eventHandler beforehand (otherwise the data would be ordered twice)
112      ascendingCheckBox.CheckedChanged -= ascendingCheckBox_CheckedChanged;
113      ascendingCheckBox.Checked = (SortingCriteria)sortByComboBox.SelectedItem != SortingCriteria.ImpactValue;
114      ascendingCheckBox.CheckedChanged += ascendingCheckBox_CheckedChanged;
115
116      UpdateOrdering();
117    }
118    private void ascendingCheckBox_CheckedChanged(object sender, EventArgs e) {
119      UpdateOrdering();
120    }
121
122    private async void UpdateVariableImpact() {
123      //Check if the selection is valid
124      if (Content == null) { return; }
125      if (replacementComboBox.SelectedIndex < 0) { return; }
126      if (dataPartitionComboBox.SelectedIndex < 0) { return; }
127      if (factorVarReplComboBox.SelectedIndex < 0) { return; }
128
129      //Prepare arguments
130      var replMethod = (RegressionSolutionVariableImpactsCalculator.ReplacementMethodEnum)replacementComboBox.Items[replacementComboBox.SelectedIndex];
131      var factorReplMethod = (RegressionSolutionVariableImpactsCalculator.FactorReplacementMethodEnum)factorVarReplComboBox.Items[factorVarReplComboBox.SelectedIndex];
132      var dataPartition = (RegressionSolutionVariableImpactsCalculator.DataPartitionEnum)dataPartitionComboBox.SelectedItem;
133
134      variableImpactsArrayView.Caption = Content.Name + " Variable Impacts";
135      var progress = Progress.Show(this, "Calculating variable impacts for " + Content.Name);
136      cancellationToken = new CancellationTokenSource();
137
138      try {
139        var problemData = Content.ProblemData;
140        var inputvariables = new HashSet<string>(problemData.AllowedInputVariables.Union(Content.Model.VariablesUsedForPrediction));
141        //Remember the original ordering of the variables
142        var originalVariableOrdering = problemData.Dataset.VariableNames
143          .Where(v => inputvariables.Contains(v))
144          .Where(v => problemData.Dataset.VariableHasType<double>(v) || problemData.Dataset.VariableHasType<string>(v))
145          .ToList();
146
147        var impacts = await Task.Run(() => CalculateVariableImpacts(originalVariableOrdering, Content.Model, problemData, Content.EstimatedValues, dataPartition, replMethod, factorReplMethod, cancellationToken.Token, progress));
148
149        rawVariableImpacts.AddRange(impacts);
150        UpdateOrdering();
151      } catch (OperationCanceledException) {
152      } finally {
153        Progress.Hide(this);
154      }
155    }
156    private List<Tuple<string, double>> CalculateVariableImpacts(List<string> originalVariableOrdering,
157      IRegressionModel model,
158      IRegressionProblemData problemData,
159      IEnumerable<double> estimatedValues,
160      RegressionSolutionVariableImpactsCalculator.DataPartitionEnum dataPartition,
161      RegressionSolutionVariableImpactsCalculator.ReplacementMethodEnum replMethod,
162      RegressionSolutionVariableImpactsCalculator.FactorReplacementMethodEnum factorReplMethod,
163      CancellationToken token,
164      IProgress progress) {
165      List<Tuple<string, double>> impacts = new List<Tuple<string, double>>();
166      int count = originalVariableOrdering.Count;
167      int i = 0;
168      var modifiableDataset = ((Dataset)(problemData.Dataset).Clone()).ToModifiable();
169      IEnumerable<int> rows = RegressionSolutionVariableImpactsCalculator.GetPartitionRows(dataPartition, problemData);
170
171      //Calculate original quality-values (via calculator, default is R²)
172      IEnumerable<double> targetValuesPartition = problemData.Dataset.GetDoubleValues(problemData.TargetVariable, rows);
173      IEnumerable<double> estimatedValuesPartition = Content.GetEstimatedValues(rows);
174
175      var originalCalculatorValue = RegressionSolutionVariableImpactsCalculator.CalculateQuality(targetValuesPartition, estimatedValuesPartition);
176
177      foreach (var variableName in originalVariableOrdering) {
178        token.ThrowIfCancellationRequested();
179        progress.ProgressValue = (double)++i / count;
180        progress.Message = string.Format("Calculating impact for variable {0} ({1} of {2})", variableName, i, count);
181
182        double impact = 0;
183        //If the variable isn't used for prediction, it has zero impact.
184        if (model.VariablesUsedForPrediction.Contains(variableName)) {
185          impact = RegressionSolutionVariableImpactsCalculator.CalculateImpact(variableName, model, problemData, modifiableDataset, rows, replMethod, factorReplMethod, targetValuesPartition, originalCalculatorValue);
186        }
187        impacts.Add(new Tuple<string, double>(variableName, impact));
188      }
189
190      return impacts;
191    }
192
193    /// <summary>
194    /// Updates the <see cref="variableImpactsArrayView"/> according to the selected ordering <see cref="ascendingCheckBox"/> of the selected Column <see cref="sortByComboBox"/>
195    /// The default is "Descending" by "VariableImpact" (as in previous versions)
196    /// </summary>
197    private void UpdateOrdering() {
198      //Check if valid sortingCriteria is selected and data exists
199      if (sortByComboBox.SelectedIndex == -1) { return; }
200      if (rawVariableImpacts == null) { return; }
201      if (!rawVariableImpacts.Any()) { return; }
202
203      var selectedItem = (SortingCriteria)sortByComboBox.SelectedItem;
204      bool ascending = ascendingCheckBox.Checked;
205
206      IEnumerable<Tuple<string, double>> orderedEntries = null;
207
208      //Sort accordingly
209      switch (selectedItem) {
210        case SortingCriteria.ImpactValue:
211          orderedEntries = rawVariableImpacts.OrderBy(v => v.Item2);
212          break;
213        case SortingCriteria.Occurrence:
214          orderedEntries = rawVariableImpacts;
215          break;
216        case SortingCriteria.VariableName:
217          orderedEntries = rawVariableImpacts.OrderBy(v => v.Item1, new NaturalStringComparer());
218          break;
219        default:
220          throw new NotImplementedException("Ordering for selected SortingCriteria not implemented");
221      }
222
223      if (!ascending) { orderedEntries = orderedEntries.Reverse(); }
224
225      //Write the data back
226      var impactArray = new DoubleArray(orderedEntries.Select(i => i.Item2).ToArray()) {
227        ElementNames = orderedEntries.Select(i => i.Item1)
228      };
229
230      //Could be, if the View was closed
231      if (!variableImpactsArrayView.IsDisposed) {
232        variableImpactsArrayView.Content = (DoubleArray)impactArray.AsReadOnly();
233      }
234    }
235  }
236}
Note: See TracBrowser for help on using the repository browser.