[14348] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[15583] | 3 | * Copyright (C) 2002-2018 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
|
---|
| 21 | using System;
|
---|
[15626] | 22 | using System.Collections.Generic;
|
---|
[14348] | 23 | using System.Linq;
|
---|
| 24 | using System.Threading.Tasks;
|
---|
| 25 | using HeuristicLab.Data;
|
---|
| 26 | using HeuristicLab.MainForm;
|
---|
| 27 | using HeuristicLab.Problems.DataAnalysis.Symbolic.Regression;
|
---|
| 28 |
|
---|
| 29 | namespace HeuristicLab.Problems.DataAnalysis.Views {
|
---|
| 30 | [View("Variable Impacts")]
|
---|
| 31 | [Content(typeof(IRegressionSolution))]
|
---|
| 32 | public partial class RegressionSolutionVariableImpactsView : DataAnalysisSolutionEvaluationView {
|
---|
| 33 |
|
---|
[15626] | 34 | private const int ORDER_BY_VARIABLE = 0;
|
---|
| 35 | private const int ORDER_BY_IMPACT = 1;
|
---|
| 36 |
|
---|
| 37 | private Dictionary<string, double> rawVariableImpacts = new Dictionary<string, double>();
|
---|
| 38 |
|
---|
[14348] | 39 | public new IRegressionSolution Content {
|
---|
| 40 | get { return (IRegressionSolution)base.Content; }
|
---|
| 41 | set {
|
---|
| 42 | base.Content = value;
|
---|
| 43 | }
|
---|
| 44 | }
|
---|
| 45 |
|
---|
| 46 | public RegressionSolutionVariableImpactsView()
|
---|
| 47 | : base() {
|
---|
| 48 | InitializeComponent();
|
---|
| 49 | this.dataPartitionComboBox.SelectedIndex = 0;
|
---|
| 50 | this.replacementComboBox.SelectedIndex = 0;
|
---|
[14826] | 51 | this.factorVarReplComboBox.SelectedIndex = 0;
|
---|
[14348] | 52 | }
|
---|
| 53 |
|
---|
| 54 | #region events
|
---|
| 55 | protected override void RegisterContentEvents() {
|
---|
| 56 | base.RegisterContentEvents();
|
---|
| 57 | Content.ModelChanged += new EventHandler(Content_ModelChanged);
|
---|
| 58 | Content.ProblemDataChanged += new EventHandler(Content_ProblemDataChanged);
|
---|
| 59 | }
|
---|
| 60 |
|
---|
| 61 | protected override void DeregisterContentEvents() {
|
---|
| 62 | base.DeregisterContentEvents();
|
---|
| 63 | Content.ModelChanged -= new EventHandler(Content_ModelChanged);
|
---|
| 64 | Content.ProblemDataChanged -= new EventHandler(Content_ProblemDataChanged);
|
---|
| 65 | }
|
---|
| 66 |
|
---|
| 67 | protected virtual void Content_ProblemDataChanged(object sender, EventArgs e) {
|
---|
| 68 | OnContentChanged();
|
---|
| 69 | }
|
---|
| 70 |
|
---|
| 71 | protected virtual void Content_ModelChanged(object sender, EventArgs e) {
|
---|
| 72 | OnContentChanged();
|
---|
| 73 | }
|
---|
| 74 |
|
---|
| 75 | protected override void OnContentChanged() {
|
---|
| 76 | base.OnContentChanged();
|
---|
| 77 | if (Content == null) {
|
---|
| 78 | variableImactsArrayView.Content = null;
|
---|
| 79 | } else {
|
---|
| 80 | UpdateVariableImpacts();
|
---|
| 81 | }
|
---|
| 82 | }
|
---|
| 83 |
|
---|
| 84 | private void UpdateVariableImpacts() {
|
---|
[14826] | 85 | if (Content == null || replacementComboBox.SelectedIndex < 0
|
---|
| 86 | || factorVarReplComboBox.SelectedIndex < 0
|
---|
| 87 | || dataPartitionComboBox.SelectedIndex < 0) return;
|
---|
[14348] | 88 | var mainForm = (MainForm.WindowsForms.MainForm)MainFormManager.MainForm;
|
---|
| 89 | variableImactsArrayView.Caption = Content.Name + " Variable Impacts";
|
---|
| 90 | var replMethod =
|
---|
[14826] | 91 | (RegressionSolutionVariableImpactsCalculator.ReplacementMethodEnum)
|
---|
| 92 | replacementComboBox.Items[replacementComboBox.SelectedIndex];
|
---|
| 93 | var factorReplMethod =
|
---|
| 94 | (RegressionSolutionVariableImpactsCalculator.FactorReplacementMethodEnum)
|
---|
| 95 | factorVarReplComboBox.Items[factorVarReplComboBox.SelectedIndex];
|
---|
[14348] | 96 | var dataPartition =
|
---|
| 97 | (RegressionSolutionVariableImpactsCalculator.DataPartitionEnum)dataPartitionComboBox.SelectedItem;
|
---|
| 98 |
|
---|
| 99 | Task.Factory.StartNew(() => {
|
---|
| 100 | try {
|
---|
| 101 | mainForm.AddOperationProgressToView(this, "Calculating variable impacts for " + Content.Name);
|
---|
| 102 |
|
---|
[14826] | 103 | var impacts = RegressionSolutionVariableImpactsCalculator.CalculateImpacts(Content, dataPartition, replMethod, factorReplMethod);
|
---|
[14348] | 104 | var impactArray = new DoubleArray(impacts.Select(i => i.Item2).ToArray());
|
---|
| 105 | impactArray.ElementNames = impacts.Select(i => i.Item1);
|
---|
| 106 | variableImactsArrayView.Content = (DoubleArray)impactArray.AsReadOnly();
|
---|
[15626] | 107 |
|
---|
| 108 | //Remember the original ordering of the variables
|
---|
| 109 | var problemData = Content.ProblemData;
|
---|
| 110 | var inputvariables = new HashSet<string>(problemData.AllowedInputVariables.Union(Content.Model.VariablesUsedForPrediction));
|
---|
| 111 | var originalVariableOrdering = problemData.Dataset.VariableNames.Where(v => inputvariables.Contains(v)).Where(problemData.Dataset.VariableHasType<double>).ToList();
|
---|
| 112 | rawVariableImpacts.Clear();
|
---|
| 113 | originalVariableOrdering.ForEach(v => rawVariableImpacts.Add(v, impacts.First(vv => vv.Item1 == v).Item2));
|
---|
| 114 |
|
---|
[14348] | 115 | } finally {
|
---|
| 116 | mainForm.RemoveOperationProgressFromView(this);
|
---|
| 117 | }
|
---|
| 118 | });
|
---|
| 119 | }
|
---|
| 120 |
|
---|
| 121 | private void dataPartitionComboBox_SelectedIndexChanged(object sender, EventArgs e) {
|
---|
[15626] | 122 | ResetOrdering();
|
---|
[14348] | 123 | UpdateVariableImpacts();
|
---|
| 124 | }
|
---|
| 125 |
|
---|
| 126 | private void replacementComboBox_SelectedIndexChanged(object sender, EventArgs e) {
|
---|
[15626] | 127 | ResetOrdering();
|
---|
[14348] | 128 | UpdateVariableImpacts();
|
---|
| 129 | }
|
---|
[15626] | 130 |
|
---|
| 131 | private void sortByComboBox_SelectedIndexChanged(object sender, EventArgs e) {
|
---|
| 132 | UpdateDataOrdering();
|
---|
| 133 | }
|
---|
| 134 |
|
---|
| 135 | private void ascendingCheckBox_CheckedChanged(object sender, EventArgs e) {
|
---|
| 136 | UpdateDataOrdering();
|
---|
| 137 | }
|
---|
| 138 | #endregion
|
---|
| 139 |
|
---|
| 140 | #region Helper Methods
|
---|
| 141 | /// <summary>
|
---|
| 142 | /// Updates the <see cref="variableImactsArrayView"/> according to the selected ordering <see cref="ascendingCheckBox"/> of the selected Column <see cref="sortByComboBox"/>
|
---|
| 143 | /// The default is "Descending" by "VariableImpact" (as in previous versions)
|
---|
| 144 | /// </summary>
|
---|
| 145 | private void UpdateDataOrdering() {
|
---|
| 146 | int orderIdx = sortByComboBox.SelectedIndex;
|
---|
| 147 | bool ascending = ascendingCheckBox.Checked;
|
---|
| 148 |
|
---|
| 149 | //Check if valid ordering is selected AND at if any VariableImpact exists
|
---|
| 150 | if (orderIdx > -1 && rawVariableImpacts != null && rawVariableImpacts.Any()) {
|
---|
| 151 | IEnumerable<KeyValuePair<string, double>> orderedEntries = null;
|
---|
| 152 |
|
---|
| 153 | //Sort accordingly
|
---|
| 154 | if (orderIdx == ORDER_BY_VARIABLE) {
|
---|
| 155 | orderedEntries = ascending ? rawVariableImpacts : rawVariableImpacts.Reverse();
|
---|
| 156 | } else if (orderIdx == ORDER_BY_IMPACT) {
|
---|
| 157 | orderedEntries = ascending ? rawVariableImpacts.OrderBy(v => v.Value) : rawVariableImpacts.OrderByDescending(v => v.Value);
|
---|
| 158 | }
|
---|
| 159 |
|
---|
| 160 | //Write the data back
|
---|
| 161 | var impactArray = new DoubleArray(orderedEntries.Select(i => i.Value).ToArray());
|
---|
| 162 | impactArray.ElementNames = orderedEntries.Select(i => i.Key);
|
---|
| 163 | variableImactsArrayView.Content = (DoubleArray)impactArray.AsReadOnly();
|
---|
| 164 | }
|
---|
| 165 | }
|
---|
| 166 |
|
---|
| 167 | /// <summary>
|
---|
| 168 | /// Resets the ordering to the default behaviour (descending by variableImpact), meaning <see cref="ascendingCheckBox"/> and <see cref="sortByComboBox"/> will be updated
|
---|
| 169 | /// Note: this will NOT update the UI
|
---|
| 170 | /// </summary>
|
---|
| 171 | private void ResetOrdering() {
|
---|
| 172 | //The events shouldn't fire everytime we reset the data
|
---|
| 173 | ascendingCheckBox.CheckedChanged -= ascendingCheckBox_CheckedChanged;
|
---|
| 174 | sortByComboBox.SelectedIndexChanged -= sortByComboBox_SelectedIndexChanged;
|
---|
| 175 |
|
---|
| 176 | ascendingCheckBox.Checked = false;
|
---|
| 177 | sortByComboBox.SelectedIndex = ORDER_BY_IMPACT;
|
---|
| 178 |
|
---|
| 179 | ascendingCheckBox.CheckedChanged += ascendingCheckBox_CheckedChanged;
|
---|
| 180 | sortByComboBox.SelectedIndexChanged += sortByComboBox_SelectedIndexChanged;
|
---|
| 181 | }
|
---|
| 182 | #endregion
|
---|
[14348] | 183 | }
|
---|
| 184 | }
|
---|