Changeset 15667


Ignore:
Timestamp:
01/30/18 10:28:21 (13 months ago)
Author:
fholzing
Message:

#2884: Added sorting mechanism (see #2871)

Location:
trunk/sources
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Problems.DataAnalysis.Views/3.4/Classification/ClassificationSolutionVariableImpactsView.Designer.cs

    r15638 r15667  
    5151      this.factorVarReplacementLabel = new System.Windows.Forms.Label();
    5252      this.factorVarReplComboBox = new System.Windows.Forms.ComboBox();
     53      this.ascendingCheckBox = new System.Windows.Forms.CheckBox();
     54      this.sortByLabel = new System.Windows.Forms.Label();
     55      this.sortByComboBox = new System.Windows.Forms.ComboBox();
    5356      this.SuspendLayout();
    5457      //
     
    6366      this.variableImactsArrayView.Name = "variableImactsArrayView";
    6467      this.variableImactsArrayView.ReadOnly = true;
    65       this.variableImactsArrayView.Size = new System.Drawing.Size(363, 278);
     68      this.variableImactsArrayView.Size = new System.Drawing.Size(662, 278);
    6669      this.variableImactsArrayView.TabIndex = 2;
    6770      //
    6871      // dataPartitionComboBox
    6972      //
     73      this.dataPartitionComboBox.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
    7074      this.dataPartitionComboBox.FormattingEnabled = true;
    7175      this.dataPartitionComboBox.Items.AddRange(new object[] {
     
    99103      // replacementComboBox
    100104      //
     105      this.replacementComboBox.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
    101106      this.replacementComboBox.FormattingEnabled = true;
    102107      this.replacementComboBox.Items.AddRange(new object[] {
     
    122127      // factorVarReplComboBox
    123128      //
     129      this.factorVarReplComboBox.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
    124130      this.factorVarReplComboBox.FormattingEnabled = true;
    125131      this.factorVarReplComboBox.Items.AddRange(new object[] {
     
    133139      this.factorVarReplComboBox.SelectedIndexChanged += new System.EventHandler(this.replacementComboBox_SelectedIndexChanged);
    134140      //
     141      // ascendingCheckBox
     142      //
     143      this.ascendingCheckBox.AutoSize = true;
     144      this.ascendingCheckBox.Location = new System.Drawing.Point(534, 6);
     145      this.ascendingCheckBox.Name = "ascendingCheckBox";
     146      this.ascendingCheckBox.Size = new System.Drawing.Size(76, 17);
     147      this.ascendingCheckBox.TabIndex = 10;
     148      this.ascendingCheckBox.Text = "Ascending";
     149      this.ascendingCheckBox.UseVisualStyleBackColor = true;
     150      this.ascendingCheckBox.CheckedChanged += new System.EventHandler(this.ascendingCheckBox_CheckedChanged);
     151      //
     152      // sortByLabel
     153      //
     154      this.sortByLabel.AutoSize = true;
     155      this.sortByLabel.Location = new System.Drawing.Point(324, 6);
     156      this.sortByLabel.Name = "sortByLabel";
     157      this.sortByLabel.Size = new System.Drawing.Size(77, 13);
     158      this.sortByLabel.TabIndex = 8;
     159      this.sortByLabel.Text = "Sorting criteria:";
     160      //
     161      // sortByComboBox
     162      //
     163      this.sortByComboBox.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
     164      this.sortByComboBox.FormattingEnabled = true;
     165      this.sortByComboBox.Location = new System.Drawing.Point(407, 3);
     166      this.sortByComboBox.Name = "sortByComboBox";
     167      this.sortByComboBox.Size = new System.Drawing.Size(121, 21);
     168      this.sortByComboBox.TabIndex = 9;
     169      this.sortByComboBox.SelectedIndexChanged += new System.EventHandler(this.sortByComboBox_SelectedIndexChanged);
     170      //
    135171      // ClassificationSolutionVariableImpactsView
    136172      //
    137173      this.AllowDrop = true;
    138174      this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Inherit;
     175      this.Controls.Add(this.ascendingCheckBox);
     176      this.Controls.Add(this.sortByLabel);
     177      this.Controls.Add(this.sortByComboBox);
    139178      this.Controls.Add(this.factorVarReplComboBox);
    140179      this.Controls.Add(this.factorVarReplacementLabel);
     
    145184      this.Controls.Add(this.variableImactsArrayView);
    146185      this.Name = "ClassificationSolutionVariableImpactsView";
    147       this.Size = new System.Drawing.Size(369, 365);
     186      this.Size = new System.Drawing.Size(668, 365);
    148187      this.ResumeLayout(false);
    149188      this.PerformLayout();
     
    160199    private System.Windows.Forms.Label factorVarReplacementLabel;
    161200    private System.Windows.Forms.ComboBox factorVarReplComboBox;
     201    private System.Windows.Forms.CheckBox ascendingCheckBox;
     202    private System.Windows.Forms.Label sortByLabel;
     203    private System.Windows.Forms.ComboBox sortByComboBox;
    162204  }
    163205}
  • trunk/sources/HeuristicLab.Problems.DataAnalysis.Views/3.4/Classification/ClassificationSolutionVariableImpactsView.cs

    r15638 r15667  
    1919 */
    2020#endregion
     21
    2122using System;
     23using System.Collections.Generic;
    2224using System.Linq;
    2325using System.Threading.Tasks;
     26using HeuristicLab.Common;
    2427using HeuristicLab.Data;
    2528using HeuristicLab.MainForm;
     
    2932  [Content(typeof(IClassificationSolution))]
    3033  public partial class ClassificationSolutionVariableImpactsView : DataAnalysisSolutionEvaluationView {
     34    private Dictionary<string, double> rawVariableImpacts = new Dictionary<string, double>();
    3135
    3236    public new IClassificationSolution Content {
     
    4044      : base() {
    4145      InitializeComponent();
     46
     47      //Little workaround. If you fill the ComboBox-Items in the other partial class, the UI-Designer will moan.
     48      this.sortByComboBox.Items.AddRange(Enum.GetValues(typeof(ClassificationSolutionVariableImpactsCalculator.SortingCriteria)).Cast<object>().ToArray());
     49      this.sortByComboBox.SelectedItem = ClassificationSolutionVariableImpactsCalculator.SortingCriteria.ImpactValue;
     50
    4251      this.dataPartitionComboBox.SelectedIndex = 0;
    4352      this.replacementComboBox.SelectedIndex = 0;
     
    7584    }
    7685
     86
     87    private void dataPartitionComboBox_SelectedIndexChanged(object sender, EventArgs e) {
     88      UpdateVariableImpacts();
     89    }
     90
     91    private void replacementComboBox_SelectedIndexChanged(object sender, EventArgs e) {
     92      UpdateVariableImpacts();
     93    }
     94
     95    private void sortByComboBox_SelectedIndexChanged(object sender, EventArgs e) {
     96      //Update the default ordering (asc,desc), but remove the eventHandler beforehand (otherwise the data would be ordered twice)
     97      ascendingCheckBox.CheckedChanged -= ascendingCheckBox_CheckedChanged;
     98      switch ((ClassificationSolutionVariableImpactsCalculator.SortingCriteria)sortByComboBox.SelectedItem) {
     99        case ClassificationSolutionVariableImpactsCalculator.SortingCriteria.ImpactValue:
     100          ascendingCheckBox.Checked = false;
     101          break;
     102        case ClassificationSolutionVariableImpactsCalculator.SortingCriteria.Occurrence:
     103          ascendingCheckBox.Checked = true;
     104          break;
     105        case ClassificationSolutionVariableImpactsCalculator.SortingCriteria.VariableName:
     106          ascendingCheckBox.Checked = true;
     107          break;
     108        default:
     109          throw new Exception("Cannot interpret SortingCriteria");
     110      }
     111      ascendingCheckBox.CheckedChanged += ascendingCheckBox_CheckedChanged;
     112
     113      UpdateDataOrdering();
     114    }
     115
     116    private void ascendingCheckBox_CheckedChanged(object sender, EventArgs e) {
     117      UpdateDataOrdering();
     118    }
     119    #endregion
     120
     121    #region Helper Methods
    77122    private void UpdateVariableImpacts() {
    78123      if (Content == null || replacementComboBox.SelectedIndex < 0
     
    94139          mainForm.AddOperationProgressToView(this, "Calculating variable impacts for " + Content.Name);
    95140
     141          //Remember the original ordering of the variables
    96142          var impacts = ClassificationSolutionVariableImpactsCalculator.CalculateImpacts(Content, dataPartition, replMethod, factorReplMethod);
    97           var impactArray = new DoubleArray(impacts.Select(i => i.Item2).ToArray());
    98           impactArray.ElementNames = impacts.Select(i => i.Item1);
    99           variableImactsArrayView.Content = (DoubleArray)impactArray.AsReadOnly();
     143          var problemData = Content.ProblemData;
     144          var inputvariables = new HashSet<string>(problemData.AllowedInputVariables.Union(Content.Model.VariablesUsedForPrediction));
     145          var originalVariableOrdering = problemData.Dataset.VariableNames.Where(v => inputvariables.Contains(v)).Where(problemData.Dataset.VariableHasType<double>).ToList();
     146          rawVariableImpacts.Clear();
     147          originalVariableOrdering.ForEach(v => rawVariableImpacts.Add(v, impacts.First(vv => vv.Item1 == v).Item2));
    100148        } finally {
    101149          mainForm.RemoveOperationProgressFromView(this);
     150          UpdateDataOrdering();
    102151        }
    103152      });
    104153    }
    105154
    106     #endregion
     155    /// <summary>
     156    /// Updates the <see cref="variableImactsArrayView"/> according to the selected ordering <see cref="ascendingCheckBox"/> of the selected Column <see cref="sortByComboBox"/>
     157    /// The default is "Descending" by "VariableImpact" (as in previous versions)
     158    /// </summary>
     159    private void UpdateDataOrdering() {
     160      //Check if valid sortingCriteria is selected and data exists
     161      if (sortByComboBox.SelectedIndex == -1 || rawVariableImpacts == null || !rawVariableImpacts.Any()) { return; }
    107162
    108     private void dataPartitionComboBox_SelectedIndexChanged(object sender, EventArgs e) {
    109       UpdateVariableImpacts();
     163      var selectedItem = (ClassificationSolutionVariableImpactsCalculator.SortingCriteria)sortByComboBox.SelectedItem;
     164      bool ascending = ascendingCheckBox.Checked;
     165
     166      IEnumerable<KeyValuePair<string, double>> orderedEntries = null;
     167
     168      //Sort accordingly
     169      switch (selectedItem) {
     170        case ClassificationSolutionVariableImpactsCalculator.SortingCriteria.ImpactValue:
     171          orderedEntries = ascending ? rawVariableImpacts.OrderBy(v => v.Value) : rawVariableImpacts.OrderByDescending(v => v.Value);
     172          break;
     173        case ClassificationSolutionVariableImpactsCalculator.SortingCriteria.Occurrence:
     174          orderedEntries = ascending ? rawVariableImpacts : rawVariableImpacts.Reverse();
     175          break;
     176        case ClassificationSolutionVariableImpactsCalculator.SortingCriteria.VariableName:
     177          orderedEntries = ascending ? rawVariableImpacts.OrderBy(v => v.Key, new NaturalStringComparer()) : rawVariableImpacts.OrderByDescending(v => v.Key, new NaturalStringComparer());
     178          break;
     179        default:
     180          throw new Exception("Cannot interpret SortingCriteria");
     181      }
     182
     183      //Write the data back
     184      var impactArray = new DoubleArray(orderedEntries.Select(i => i.Value).ToArray()) {
     185        ElementNames = orderedEntries.Select(i => i.Key)
     186      };
     187      variableImactsArrayView.Content = (DoubleArray)impactArray.AsReadOnly();
    110188    }
    111 
    112     private void replacementComboBox_SelectedIndexChanged(object sender, EventArgs e) {
    113       UpdateVariableImpacts();
    114     }
     189    #endregion   
    115190  }
    116191}
  • trunk/sources/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Classification/ClassificationSolutionVariableImpactsCalculator.cs

    r15638 r15667  
    5151      Test,
    5252      All
     53    }
     54
     55    public enum SortingCriteria {
     56      ImpactValue,
     57      Occurrence,
     58      VariableName
    5359    }
    5460
Note: See TracChangeset for help on using the changeset viewer.