Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/HeuristicLab.Problems.DataAnalysis.Views/3.4/Regression/RegressionSolutionVariableImpactsView.cs @ 17717

Last change on this file since 17717 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
RevLine 
[14348]1#region License Information
2/* HeuristicLab
[17180]3 * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[14348]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
[15665]21
[14348]22using System;
[15626]23using System.Collections.Generic;
[14348]24using System.Linq;
[15797]25using System.Threading;
26using System.Threading.Tasks;
[17426]27using System.Windows.Forms;
[15637]28using HeuristicLab.Common;
[14348]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 {
[15673]36    private enum SortingCriteria {
37      ImpactValue,
38      Occurrence,
39      VariableName
40    }
[16422]41    private CancellationTokenSource cancellationToken = new CancellationTokenSource();
[15998]42    private List<Tuple<string, double>> rawVariableImpacts = new List<Tuple<string, double>>();
[15626]43
[14348]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();
[15665]54
[15727]55      //Set the default values
[14348]56      this.dataPartitionComboBox.SelectedIndex = 0;
[16021]57      this.replacementComboBox.SelectedIndex = 3;
[14826]58      this.factorVarReplComboBox.SelectedIndex = 0;
[15998]59      this.sortByComboBox.SelectedItem = SortingCriteria.ImpactValue;
[14348]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();
[16422]81      rawVariableImpacts.Clear();
82
[14348]83      if (Content == null) {
[16422]84        variableImpactsArrayView.Content = null;
[14348]85      } else {
[15752]86        UpdateVariableImpact();
[14348]87      }
88    }
[17276]89
[17426]90    protected override void OnVisibleChanged(EventArgs e) {
91      base.OnVisibleChanged(e);
92      if (!this.Visible) {
93        cancellationToken.Cancel();
[17276]94      }
[15752]95    }
96
[17426]97    protected override void OnClosed(FormClosedEventArgs e) {
98      base.OnClosed(e);
99      cancellationToken.Cancel();
100    }
101
[15665]102    private void dataPartitionComboBox_SelectedIndexChanged(object sender, EventArgs e) {
[16422]103      rawVariableImpacts.Clear();
[15752]104      UpdateVariableImpact();
[15665]105    }
106    private void replacementComboBox_SelectedIndexChanged(object sender, EventArgs e) {
[16422]107      rawVariableImpacts.Clear();
[15752]108      UpdateVariableImpact();
[15665]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;
[15998]113      ascendingCheckBox.Checked = (SortingCriteria)sortByComboBox.SelectedItem != SortingCriteria.ImpactValue;
[15665]114      ascendingCheckBox.CheckedChanged += ascendingCheckBox_CheckedChanged;
115
[15998]116      UpdateOrdering();
[15665]117    }
118    private void ascendingCheckBox_CheckedChanged(object sender, EventArgs e) {
[15998]119      UpdateOrdering();
[15665]120    }
[15727]121
[15797]122    private async void UpdateVariableImpact() {
[15727]123      //Check if the selection is valid
[15673]124      if (Content == null) { return; }
125      if (replacementComboBox.SelectedIndex < 0) { return; }
126      if (dataPartitionComboBox.SelectedIndex < 0) { return; }
127      if (factorVarReplComboBox.SelectedIndex < 0) { return; }
128
[15727]129      //Prepare arguments
[15673]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
[16422]134      variableImpactsArrayView.Caption = Content.Name + " Variable Impacts";
[16430]135      var progress = Progress.Show(this, "Calculating variable impacts for " + Content.Name);
[15797]136      cancellationToken = new CancellationTokenSource();
[16422]137
[15797]138      try {
139        var problemData = Content.ProblemData;
140        var inputvariables = new HashSet<string>(problemData.AllowedInputVariables.Union(Content.Model.VariablesUsedForPrediction));
[16422]141        //Remember the original ordering of the variables
[16015]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();
[15797]146
[17426]147        var impacts = await Task.Run(() => CalculateVariableImpacts(originalVariableOrdering, Content.Model, problemData, Content.EstimatedValues, dataPartition, replMethod, factorReplMethod, cancellationToken.Token, progress));
[16422]148
149        rawVariableImpacts.AddRange(impacts);
[15998]150        UpdateOrdering();
[17426]151      } catch (OperationCanceledException) {
[17276]152      } finally {
[17426]153        Progress.Hide(this);
[16422]154      }
[14348]155    }
[16422]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);
[14348]170
[16422]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) {
[17426]178        token.ThrowIfCancellationRequested();
[16422]179        progress.ProgressValue = (double)++i / count;
[16430]180        progress.Message = string.Format("Calculating impact for variable {0} ({1} of {2})", variableName, i, count);
[16422]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
[15626]193    /// <summary>
[16422]194    /// Updates the <see cref="variableImpactsArrayView"/> according to the selected ordering <see cref="ascendingCheckBox"/> of the selected Column <see cref="sortByComboBox"/>
[15626]195    /// The default is "Descending" by "VariableImpact" (as in previous versions)
196    /// </summary>
[15998]197    private void UpdateOrdering() {
[15665]198      //Check if valid sortingCriteria is selected and data exists
[15673]199      if (sortByComboBox.SelectedIndex == -1) { return; }
200      if (rawVariableImpacts == null) { return; }
201      if (!rawVariableImpacts.Any()) { return; }
[15665]202
[15673]203      var selectedItem = (SortingCriteria)sortByComboBox.SelectedItem;
[15626]204      bool ascending = ascendingCheckBox.Checked;
205
[15998]206      IEnumerable<Tuple<string, double>> orderedEntries = null;
[15626]207
[15665]208      //Sort accordingly
209      switch (selectedItem) {
[15673]210        case SortingCriteria.ImpactValue:
[15998]211          orderedEntries = rawVariableImpacts.OrderBy(v => v.Item2);
[15665]212          break;
[15673]213        case SortingCriteria.Occurrence:
214          orderedEntries = rawVariableImpacts;
[15665]215          break;
[15673]216        case SortingCriteria.VariableName:
[15998]217          orderedEntries = rawVariableImpacts.OrderBy(v => v.Item1, new NaturalStringComparer());
[15665]218          break;
219        default:
[15673]220          throw new NotImplementedException("Ordering for selected SortingCriteria not implemented");
[15626]221      }
222
[15673]223      if (!ascending) { orderedEntries = orderedEntries.Reverse(); }
224
[15665]225      //Write the data back
[15998]226      var impactArray = new DoubleArray(orderedEntries.Select(i => i.Item2).ToArray()) {
227        ElementNames = orderedEntries.Select(i => i.Item1)
[15665]228      };
[15727]229
[15752]230      //Could be, if the View was closed
[16422]231      if (!variableImpactsArrayView.IsDisposed) {
232        variableImpactsArrayView.Content = (DoubleArray)impactArray.AsReadOnly();
[15727]233      }
[15626]234    }
[14348]235  }
236}
Note: See TracBrowser for help on using the repository browser.