Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Problems.DataAnalysis.Views/3.4/Classification/ClassificationSolutionVariableImpactsView.cs @ 17399

Last change on this file since 17399 was 17181, checked in by swagner, 5 years ago

#2875: Merged r17180 from trunk to stable

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