Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2965_CancelablePersistence/HeuristicLab.Problems.DataAnalysis.Views/3.4/Regression/RegressionSolutionVariableImpactsView.cs @ 16811

Last change on this file since 16811 was 16433, checked in by pfleck, 6 years ago

#2965 Merged recent trunk changes.
Enabled the prepared hooks that allows to cancel the save file using the recently introduced cancelable progressbars (in FileManager).

File size: 10.0 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2018 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 HeuristicLab.Common;
28using HeuristicLab.Data;
29using HeuristicLab.MainForm;
30
31namespace HeuristicLab.Problems.DataAnalysis.Views {
32  [View("Variable Impacts")]
33  [Content(typeof(IRegressionSolution))]
34  public partial class RegressionSolutionVariableImpactsView : DataAnalysisSolutionEvaluationView {
35    private enum SortingCriteria {
36      ImpactValue,
37      Occurrence,
38      VariableName
39    }
40    private CancellationTokenSource cancellationToken = new CancellationTokenSource();
41    private List<Tuple<string, double>> rawVariableImpacts = new List<Tuple<string, double>>();
42
43    public new IRegressionSolution Content {
44      get { return (IRegressionSolution)base.Content; }
45      set {
46        base.Content = value;
47      }
48    }
49
50    public RegressionSolutionVariableImpactsView()
51      : base() {
52      InitializeComponent();
53
54      //Set the default values
55      this.dataPartitionComboBox.SelectedIndex = 0;
56      this.replacementComboBox.SelectedIndex = 3;
57      this.factorVarReplComboBox.SelectedIndex = 0;
58      this.sortByComboBox.SelectedItem = SortingCriteria.ImpactValue;
59    }
60
61    protected override void RegisterContentEvents() {
62      base.RegisterContentEvents();
63      Content.ModelChanged += new EventHandler(Content_ModelChanged);
64      Content.ProblemDataChanged += new EventHandler(Content_ProblemDataChanged);
65    }
66    protected override void DeregisterContentEvents() {
67      base.DeregisterContentEvents();
68      Content.ModelChanged -= new EventHandler(Content_ModelChanged);
69      Content.ProblemDataChanged -= new EventHandler(Content_ProblemDataChanged);
70    }
71
72    protected virtual void Content_ProblemDataChanged(object sender, EventArgs e) {
73      OnContentChanged();
74    }
75    protected virtual void Content_ModelChanged(object sender, EventArgs e) {
76      OnContentChanged();
77    }
78    protected override void OnContentChanged() {
79      base.OnContentChanged();
80      rawVariableImpacts.Clear();
81
82      if (Content == null) {
83        variableImpactsArrayView.Content = null;
84      } else {
85        UpdateVariableImpact();
86      }
87    }
88    private void RegressionSolutionVariableImpactsView_VisibleChanged(object sender, EventArgs e) {
89      cancellationToken.Cancel();
90    }
91
92    private void dataPartitionComboBox_SelectedIndexChanged(object sender, EventArgs e) {
93      rawVariableImpacts.Clear();
94      UpdateVariableImpact();
95    }
96    private void replacementComboBox_SelectedIndexChanged(object sender, EventArgs e) {
97      rawVariableImpacts.Clear();
98      UpdateVariableImpact();
99    }
100    private void sortByComboBox_SelectedIndexChanged(object sender, EventArgs e) {
101      //Update the default ordering (asc,desc), but remove the eventHandler beforehand (otherwise the data would be ordered twice)
102      ascendingCheckBox.CheckedChanged -= ascendingCheckBox_CheckedChanged;
103      ascendingCheckBox.Checked = (SortingCriteria)sortByComboBox.SelectedItem != SortingCriteria.ImpactValue;
104      ascendingCheckBox.CheckedChanged += ascendingCheckBox_CheckedChanged;
105
106      UpdateOrdering();
107    }
108    private void ascendingCheckBox_CheckedChanged(object sender, EventArgs e) {
109      UpdateOrdering();
110    }
111
112    private async void UpdateVariableImpact() {
113      //Check if the selection is valid
114      if (Content == null) { return; }
115      if (replacementComboBox.SelectedIndex < 0) { return; }
116      if (dataPartitionComboBox.SelectedIndex < 0) { return; }
117      if (factorVarReplComboBox.SelectedIndex < 0) { return; }
118
119      //Prepare arguments
120      var replMethod = (RegressionSolutionVariableImpactsCalculator.ReplacementMethodEnum)replacementComboBox.Items[replacementComboBox.SelectedIndex];
121      var factorReplMethod = (RegressionSolutionVariableImpactsCalculator.FactorReplacementMethodEnum)factorVarReplComboBox.Items[factorVarReplComboBox.SelectedIndex];
122      var dataPartition = (RegressionSolutionVariableImpactsCalculator.DataPartitionEnum)dataPartitionComboBox.SelectedItem;
123
124      variableImpactsArrayView.Caption = Content.Name + " Variable Impacts";
125      var progress = Progress.Show(this, "Calculating variable impacts for " + Content.Name);
126      cancellationToken = new CancellationTokenSource();
127
128      try {
129        var problemData = Content.ProblemData;
130        var inputvariables = new HashSet<string>(problemData.AllowedInputVariables.Union(Content.Model.VariablesUsedForPrediction));
131        //Remember the original ordering of the variables
132        var originalVariableOrdering = problemData.Dataset.VariableNames
133          .Where(v => inputvariables.Contains(v))
134          .Where(v => problemData.Dataset.VariableHasType<double>(v) || problemData.Dataset.VariableHasType<string>(v))
135          .ToList();
136
137        List<Tuple<string, double>> impacts = null;
138        await Task.Run(() => { impacts = CalculateVariableImpacts(originalVariableOrdering, Content.Model, problemData, Content.EstimatedValues, dataPartition, replMethod, factorReplMethod, cancellationToken.Token, progress); });
139        if (impacts == null) { return; }
140
141        rawVariableImpacts.AddRange(impacts);
142        UpdateOrdering();
143      }
144      finally {
145        Progress.Hide(this);
146      }
147    }
148    private List<Tuple<string, double>> CalculateVariableImpacts(List<string> originalVariableOrdering,
149      IRegressionModel model,
150      IRegressionProblemData problemData,
151      IEnumerable<double> estimatedValues,
152      RegressionSolutionVariableImpactsCalculator.DataPartitionEnum dataPartition,
153      RegressionSolutionVariableImpactsCalculator.ReplacementMethodEnum replMethod,
154      RegressionSolutionVariableImpactsCalculator.FactorReplacementMethodEnum factorReplMethod,
155      CancellationToken token,
156      IProgress progress) {
157      List<Tuple<string, double>> impacts = new List<Tuple<string, double>>();
158      int count = originalVariableOrdering.Count;
159      int i = 0;
160      var modifiableDataset = ((Dataset)(problemData.Dataset).Clone()).ToModifiable();
161      IEnumerable<int> rows = RegressionSolutionVariableImpactsCalculator.GetPartitionRows(dataPartition, problemData);
162
163      //Calculate original quality-values (via calculator, default is R²)
164      IEnumerable<double> targetValuesPartition = problemData.Dataset.GetDoubleValues(problemData.TargetVariable, rows);
165      IEnumerable<double> estimatedValuesPartition = Content.GetEstimatedValues(rows);
166
167      var originalCalculatorValue = RegressionSolutionVariableImpactsCalculator.CalculateQuality(targetValuesPartition, estimatedValuesPartition);
168
169      foreach (var variableName in originalVariableOrdering) {
170        if (cancellationToken.Token.IsCancellationRequested) { return null; }
171        progress.ProgressValue = (double)++i / count;
172        progress.Message = string.Format("Calculating impact for variable {0} ({1} of {2})", variableName, i, count);
173
174        double impact = 0;
175        //If the variable isn't used for prediction, it has zero impact.
176        if (model.VariablesUsedForPrediction.Contains(variableName)) {
177          impact = RegressionSolutionVariableImpactsCalculator.CalculateImpact(variableName, model, problemData, modifiableDataset, rows, replMethod, factorReplMethod, targetValuesPartition, originalCalculatorValue);
178        }
179        impacts.Add(new Tuple<string, double>(variableName, impact));
180      }
181
182      return impacts;
183    }
184
185    /// <summary>
186    /// Updates the <see cref="variableImpactsArrayView"/> according to the selected ordering <see cref="ascendingCheckBox"/> of the selected Column <see cref="sortByComboBox"/>
187    /// The default is "Descending" by "VariableImpact" (as in previous versions)
188    /// </summary>
189    private void UpdateOrdering() {
190      //Check if valid sortingCriteria is selected and data exists
191      if (sortByComboBox.SelectedIndex == -1) { return; }
192      if (rawVariableImpacts == null) { return; }
193      if (!rawVariableImpacts.Any()) { return; }
194
195      var selectedItem = (SortingCriteria)sortByComboBox.SelectedItem;
196      bool ascending = ascendingCheckBox.Checked;
197
198      IEnumerable<Tuple<string, double>> orderedEntries = null;
199
200      //Sort accordingly
201      switch (selectedItem) {
202        case SortingCriteria.ImpactValue:
203          orderedEntries = rawVariableImpacts.OrderBy(v => v.Item2);
204          break;
205        case SortingCriteria.Occurrence:
206          orderedEntries = rawVariableImpacts;
207          break;
208        case SortingCriteria.VariableName:
209          orderedEntries = rawVariableImpacts.OrderBy(v => v.Item1, new NaturalStringComparer());
210          break;
211        default:
212          throw new NotImplementedException("Ordering for selected SortingCriteria not implemented");
213      }
214
215      if (!ascending) { orderedEntries = orderedEntries.Reverse(); }
216
217      //Write the data back
218      var impactArray = new DoubleArray(orderedEntries.Select(i => i.Item2).ToArray()) {
219        ElementNames = orderedEntries.Select(i => i.Item1)
220      };
221
222      //Could be, if the View was closed
223      if (!variableImpactsArrayView.IsDisposed) {
224        variableImpactsArrayView.Content = (DoubleArray)impactArray.AsReadOnly();
225      }
226    }
227  }
228}
Note: See TracBrowser for help on using the repository browser.