Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
09/14/18 11:47:37 (6 years ago)
Author:
abeham
Message:

#2817: updated to trunk r16140

Location:
branches/2817-BinPackingSpeedup
Files:
15 edited

Legend:

Unmodified
Added
Removed
  • branches/2817-BinPackingSpeedup

  • branches/2817-BinPackingSpeedup/HeuristicLab.Problems.DataAnalysis.Views

  • branches/2817-BinPackingSpeedup/HeuristicLab.Problems.DataAnalysis.Views/3.4/Classification/ClassificationSolutionVariableImpactsView.Designer.cs

    r16140 r16141  
    5454      this.sortByLabel = new System.Windows.Forms.Label();
    5555      this.sortByComboBox = new System.Windows.Forms.ComboBox();
     56      this.backgroundWorker = new System.ComponentModel.BackgroundWorker();
    5657      this.SuspendLayout();
    5758      //
     
    185186      this.Name = "ClassificationSolutionVariableImpactsView";
    186187      this.Size = new System.Drawing.Size(668, 365);
     188      this.VisibleChanged += new System.EventHandler(this.ClassificationSolutionVariableImpactsView_VisibleChanged);
    187189      this.ResumeLayout(false);
    188190      this.PerformLayout();
     
    202204    private System.Windows.Forms.Label sortByLabel;
    203205    private System.Windows.Forms.ComboBox sortByComboBox;
     206    private System.ComponentModel.BackgroundWorker backgroundWorker;
    204207  }
    205208}
  • branches/2817-BinPackingSpeedup/HeuristicLab.Problems.DataAnalysis.Views/3.4/Classification/ClassificationSolutionVariableImpactsView.cs

    r16140 r16141  
    2323using System.Collections.Generic;
    2424using System.Linq;
     25using System.Threading;
    2526using System.Threading.Tasks;
    2627using HeuristicLab.Common;
     
    3233  [Content(typeof(IClassificationSolution))]
    3334  public partial class ClassificationSolutionVariableImpactsView : DataAnalysisSolutionEvaluationView {
     35    #region Nested Types
    3436    private enum SortingCriteria {
    3537      ImpactValue,
     
    3739      VariableName
    3840    }
    39 
     41    #endregion
     42
     43    #region Fields
    4044    private Dictionary<string, double> rawVariableImpacts = new Dictionary<string, double>();
    41 
     45    private Thread thread;
     46    #endregion
     47
     48    #region Getter/Setter
    4249    public new IClassificationSolution Content {
    4350      get { return (IClassificationSolution)base.Content; }
     
    4653      }
    4754    }
    48 
     55    #endregion
     56
     57    #region Ctor
    4958    public ClassificationSolutionVariableImpactsView()
    5059      : base() {
     
    5564      this.sortByComboBox.SelectedItem = SortingCriteria.ImpactValue;
    5665
     66      //Set the default values
    5767      this.dataPartitionComboBox.SelectedIndex = 0;
    5868      this.replacementComboBox.SelectedIndex = 0;
    5969      this.factorVarReplComboBox.SelectedIndex = 0;
    6070    }
    61 
    62     #region events
     71    #endregion
     72
     73    #region Events
    6374    protected override void RegisterContentEvents() {
    6475      base.RegisterContentEvents();
     
    8697        variableImactsArrayView.Content = null;
    8798      } else {
    88         UpdateVariableImpacts();
    89       }
     99        UpdateVariableImpact();
     100      }
     101    }
     102
     103    private void ClassificationSolutionVariableImpactsView_VisibleChanged(object sender, EventArgs e) {
     104      if (thread == null) { return; }
     105
     106      if (thread.IsAlive) { thread.Abort(); }
     107      thread = null;
    90108    }
    91109
    92110
    93111    private void dataPartitionComboBox_SelectedIndexChanged(object sender, EventArgs e) {
    94       UpdateVariableImpacts();
     112      UpdateVariableImpact();
    95113    }
    96114
    97115    private void replacementComboBox_SelectedIndexChanged(object sender, EventArgs e) {
    98       UpdateVariableImpacts();
     116      UpdateVariableImpact();
    99117    }
    100118
     
    123141      UpdateDataOrdering();
    124142    }
    125     #endregion
    126 
    127     #region Helper Methods
    128     private void UpdateVariableImpacts() {
     143
     144    #endregion
     145
     146    #region Helper Methods   
     147    private void UpdateVariableImpact() {
     148      //Check if the selection is valid
    129149      if (Content == null) { return; }
    130150      if (replacementComboBox.SelectedIndex < 0) { return; }
     
    132152      if (factorVarReplComboBox.SelectedIndex < 0) { return; }
    133153
    134       variableImactsArrayView.Caption = Content.Name + " Variable Impacts";
    135 
     154      //Prepare arguments
    136155      var mainForm = (MainForm.WindowsForms.MainForm)MainFormManager.MainForm;
    137156      var replMethod = (ClassificationSolutionVariableImpactsCalculator.ReplacementMethodEnum)replacementComboBox.Items[replacementComboBox.SelectedIndex];
     
    139158      var dataPartition = (ClassificationSolutionVariableImpactsCalculator.DataPartitionEnum)dataPartitionComboBox.SelectedItem;
    140159
     160      variableImactsArrayView.Caption = Content.Name + " Variable Impacts";
     161
     162      mainForm.AddOperationProgressToView(this, "Calculating variable impacts for " + Content.Name);
     163
    141164      Task.Factory.StartNew(() => {
    142         try {
    143           mainForm.AddOperationProgressToView(this, "Calculating variable impacts for " + Content.Name);
    144 
    145           //Remember the original ordering of the variables
    146           var impacts = ClassificationSolutionVariableImpactsCalculator.CalculateImpacts(Content, dataPartition, replMethod, factorReplMethod);
    147           var problemData = Content.ProblemData;
    148           var inputvariables = new HashSet<string>(problemData.AllowedInputVariables.Union(Content.Model.VariablesUsedForPrediction));
    149           var originalVariableOrdering = problemData.Dataset.VariableNames.Where(v => inputvariables.Contains(v)).Where(problemData.Dataset.VariableHasType<double>).ToList();
    150           rawVariableImpacts.Clear();
    151           originalVariableOrdering.ForEach(v => rawVariableImpacts.Add(v, impacts.First(vv => vv.Item1 == v).Item2));
    152           UpdateDataOrdering();
    153         } finally {
    154           mainForm.RemoveOperationProgressFromView(this);
    155         }
    156       });
     165        thread = Thread.CurrentThread;
     166        //Remember the original ordering of the variables
     167        var impacts = ClassificationSolutionVariableImpactsCalculator.CalculateImpacts(Content, dataPartition, replMethod, factorReplMethod);
     168        var problemData = Content.ProblemData;
     169        var inputvariables = new HashSet<string>(problemData.AllowedInputVariables.Union(Content.Model.VariablesUsedForPrediction));
     170        var originalVariableOrdering = problemData.Dataset.VariableNames.Where(v => inputvariables.Contains(v)).Where(problemData.Dataset.VariableHasType<double>).ToList();
     171
     172        rawVariableImpacts.Clear();
     173        originalVariableOrdering.ForEach(v => rawVariableImpacts.Add(v, impacts.First(vv => vv.Item1 == v).Item2));
     174      }).ContinueWith((o) => {
     175        UpdateDataOrdering();
     176        mainForm.RemoveOperationProgressFromView(this);
     177        thread = null;
     178      }, TaskScheduler.FromCurrentSynchronizationContext());
    157179    }
    158180
     
    193215        ElementNames = orderedEntries.Select(i => i.Key)
    194216      };
    195       variableImactsArrayView.Content = (DoubleArray)impactArray.AsReadOnly();
    196     }
    197     #endregion   
     217
     218      //Could be, if the View was closed
     219      if (!variableImactsArrayView.IsDisposed) {
     220        variableImactsArrayView.Content = (DoubleArray)impactArray.AsReadOnly();
     221      }
     222    }
     223    #endregion 
    198224  }
    199225}
  • branches/2817-BinPackingSpeedup/HeuristicLab.Problems.DataAnalysis.Views/3.4/Controls/FactorPartialDependencePlot.cs

    r16140 r16141  
    143143      // Configure axis
    144144      chart.CustomizeAllChartAreas();
    145       chart.ChartAreas[0].CursorX.IsUserSelectionEnabled = true;
    146       chart.ChartAreas[0].AxisX.ScaleView.Zoomable = true;
    147       chart.ChartAreas[0].CursorX.Interval = 0;
    148 
    149       chart.ChartAreas[0].CursorY.IsUserSelectionEnabled = true;
    150       chart.ChartAreas[0].AxisY.ScaleView.Zoomable = true;
    151       chart.ChartAreas[0].CursorY.Interval = 0;
     145      chart.ChartAreas[0].CursorX.IsUserSelectionEnabled = false;
     146      chart.ChartAreas[0].CursorY.IsUserSelectionEnabled = false;
     147
     148      chart.ChartAreas[0].Axes.ToList().ForEach(x => { x.ScaleView.Zoomable = false; });
    152149
    153150      Disposed += Control_Disposed;
  • branches/2817-BinPackingSpeedup/HeuristicLab.Problems.DataAnalysis.Views/3.4/Controls/PartialDependencePlot.cs

    r16140 r16141  
    215215      // Configure axis
    216216      chart.CustomizeAllChartAreas();
    217       chart.ChartAreas[0].CursorX.IsUserSelectionEnabled = true;
    218       chart.ChartAreas[0].AxisX.ScaleView.Zoomable = true;
    219       chart.ChartAreas[0].CursorX.Interval = 0;
    220 
    221       chart.ChartAreas[0].CursorY.IsUserSelectionEnabled = true;
    222       chart.ChartAreas[0].AxisY.ScaleView.Zoomable = true;
    223       chart.ChartAreas[0].CursorY.Interval = 0;
     217      chart.ChartAreas[0].CursorX.IsUserSelectionEnabled = false;
     218      chart.ChartAreas[0].CursorY.IsUserSelectionEnabled = false;
     219
     220      chart.ChartAreas[0].Axes.ToList().ForEach(x => { x.ScaleView.Zoomable = false; });
    224221
    225222      configToolStripMenuItem = new ToolStripMenuItem("Configuration");
     
    351348
    352349    private void RecalculateTrainingLimits(bool initializeAxisRanges) {
    353       trainingMin = solutions.Select(s => s.ProblemData.Dataset.GetDoubleValues(freeVariable, s.ProblemData.TrainingIndices).Min()).Max();
    354       trainingMax = solutions.Select(s => s.ProblemData.Dataset.GetDoubleValues(freeVariable, s.ProblemData.TrainingIndices).Max()).Min();
     350      trainingMin = solutions.Select(s => s.ProblemData.Dataset.GetDoubleValues(freeVariable, s.ProblemData.TrainingIndices).Where(x => !double.IsNaN(x)).Min()).Max();
     351      trainingMax = solutions.Select(s => s.ProblemData.Dataset.GetDoubleValues(freeVariable, s.ProblemData.TrainingIndices).Where(x => !double.IsNaN(x)).Max()).Min();
    355352
    356353      if (initializeAxisRanges) {
     
    702699    private void config_Click(object sender, EventArgs e) {
    703700      configurationDialog.ShowDialog(this);
     701      OnZoomChanged(this, EventArgs.Empty);
    704702    }
    705703
  • branches/2817-BinPackingSpeedup/HeuristicLab.Problems.DataAnalysis.Views/3.4/Regression/RegressionSolutionErrorCharacteristicsCurveView.cs

    r16140 r16141  
    249249    protected virtual List<double> GetResiduals(IEnumerable<double> originalValues, IEnumerable<double> estimatedValues) {
    250250      switch (residualComboBox.SelectedItem.ToString()) {
    251         case "Absolute error": return originalValues.Zip(estimatedValues, (x, y) => Math.Abs(x - y)).ToList();
    252         case "Squared error": return originalValues.Zip(estimatedValues, (x, y) => (x - y) * (x - y)).ToList();
    253         case "Relative error": return originalValues.Zip(estimatedValues, (x, y) => x.IsAlmost(0.0) ? -1 : Math.Abs((x - y) / x))
    254           .Where(x => x > 0) // remove entries where the original value is 0
    255           .ToList();
     251        case "Absolute error": return originalValues.Zip(estimatedValues, (x, y) => Math.Abs(x - y))
     252            .Where(r => !double.IsNaN(r) && !double.IsInfinity(r)).ToList();
     253        case "Squared error": return originalValues.Zip(estimatedValues, (x, y) => (x - y) * (x - y))
     254            .Where(r => !double.IsNaN(r) && !double.IsInfinity(r)).ToList();
     255        case "Relative error":
     256          return originalValues.Zip(estimatedValues, (x, y) => x.IsAlmost(0.0) ? -1 : Math.Abs((x - y) / x))
     257            .Where(r => r > 0 && !double.IsNaN(r) && !double.IsInfinity(r)) // remove entries where the original value is 0
     258            .ToList();
    256259        default: throw new NotSupportedException();
    257260      }
  • branches/2817-BinPackingSpeedup/HeuristicLab.Problems.DataAnalysis.Views/3.4/Regression/RegressionSolutionLineChartViewBase.cs

    r16140 r16141  
    2020#endregion
    2121using System;
    22 using System.Collections.Generic;
    2322using System.Drawing;
    2423using System.Linq;
     
    7271        this.chart.Series[TARGETVARIABLE_SERIES_NAME].LegendText = TARGETVARIABLE_SERIES_NAME;
    7372        this.chart.Series[TARGETVARIABLE_SERIES_NAME].ChartType = SeriesChartType.FastLine;
    74         this.chart.Series[TARGETVARIABLE_SERIES_NAME].Points.DataBindXY(Enumerable.Range(0, Content.ProblemData.Dataset.Rows).ToArray(),
    75           Content.ProblemData.Dataset.GetDoubleValues(Content.ProblemData.TargetVariable).ToArray());
     73
     74        var rows = Enumerable.Range(0, Content.ProblemData.Dataset.Rows).ToArray();
     75        var targetValues = Content.ProblemData.Dataset.GetDoubleValues(Content.ProblemData.TargetVariable);
     76
     77
     78        this.chart.Series[TARGETVARIABLE_SERIES_NAME].Points.DataBindXY(rows.ToArray(), targetValues.Select(v => double.IsInfinity(v) ? double.NaN : v).ToArray());
    7679        // training series
    7780        this.chart.Series.Add(ESTIMATEDVALUES_TRAINING_SERIES_NAME);
     
    161164      var targetValues = this.chart.Series[TARGETVARIABLE_SERIES_NAME].Points.Select(x => x.YValues[0]).DefaultIfEmpty(1.0);
    162165      double estimatedValuesRange = estimatedValues.Max() - estimatedValues.Min();
    163       double targetValuesRange = targetValues.Max() - targetValues.Min();
     166      double targetValuesRange = targetValues.Where(v => !double.IsInfinity(v) && !double.IsNaN(v)).Max() -
     167                                 targetValues.Where(v => !double.IsInfinity(v) && !double.IsNaN(v)).Min();
    164168      double interestingValuesRange = Math.Min(Math.Max(targetValuesRange, 1.0), Math.Max(estimatedValuesRange, 1.0));
    165169      double digits = (int)Math.Log10(interestingValuesRange) - 3;
  • branches/2817-BinPackingSpeedup/HeuristicLab.Problems.DataAnalysis.Views/3.4/Regression/RegressionSolutionResidualAnalysisView.cs

    r16140 r16141  
    8989      var runs = new RunCollection();
    9090      // determine relevant variables (at least two different values)
    91       var doubleVars = ds.DoubleVariables.Where(vn => ds.GetDoubleValues(vn).Max() > ds.GetDoubleValues(vn).Min()).ToArray();
     91      var doubleVars = ds.DoubleVariables.Where(vn => ds.GetDoubleValues(vn).Distinct().Skip(1).Any()).ToArray();
    9292      var stringVars = ds.StringVariables.Where(vn => ds.GetStringValues(vn).Distinct().Skip(1).Any()).ToArray();
    9393      var dateTimeVars = ds.DateTimeVariables.Where(vn => ds.GetDateTimeValues(vn).Distinct().Skip(1).Any()).ToArray();
  • branches/2817-BinPackingSpeedup/HeuristicLab.Problems.DataAnalysis.Views/3.4/Regression/RegressionSolutionResidualHistogram.cs

    r16140 r16141  
    126126
    127127      for (int i = 0; i < solution.ProblemData.Dataset.Rows; i++) {
     128        if (double.IsNaN(estimatedValues[i]) || double.IsInfinity(estimatedValues[i])) continue;
     129        if (double.IsNaN(targetValues[i]) || double.IsInfinity(targetValues[i])) continue;
    128130        double residual = estimatedValues[i] - targetValues[i];
    129131        residuals.Add(residual);
  • branches/2817-BinPackingSpeedup/HeuristicLab.Problems.DataAnalysis.Views/3.4/Regression/RegressionSolutionResidualsLineChartView.cs

    r16140 r16141  
    3838      var target = problemData.Dataset.GetDoubleValues(problemData.TargetVariable, idx).ToArray();
    3939      for (int i = 0; i < idx.Length; i++) {
    40         x[i] = target[i] - x[i];
     40        if (!double.IsInfinity(target[i]) && !double.IsNaN(target[i]) &&
     41            !double.IsInfinity(x[i]) && !double.IsNaN(x[i])) {
     42          x[i] = target[i] - x[i];
     43        } else {
     44          x[i] = 0.0;
     45        }
    4146      }
    4247    }
  • branches/2817-BinPackingSpeedup/HeuristicLab.Problems.DataAnalysis.Views/3.4/Regression/RegressionSolutionScatterPlotView.cs

    r16140 r16141  
    166166          this.chart.Series[TEST_SERIES].Points.DataBindXY(dataset.GetDoubleValues(targetVariableName, Content.ProblemData.TestIndices).ToArray(), "",
    167167            Content.EstimatedTestValues.ToArray(), "");
    168         double max = Content.EstimatedTrainingValues.Concat(Content.EstimatedTestValues.Concat(Content.EstimatedValues.Concat(dataset.GetDoubleValues(targetVariableName)))).Max();
    169         double min = Content.EstimatedTrainingValues.Concat(Content.EstimatedTestValues.Concat(Content.EstimatedValues.Concat(dataset.GetDoubleValues(targetVariableName)))).Min();
     168        double max = Content.EstimatedTrainingValues
     169          .Concat(Content.EstimatedTestValues
     170          .Concat(Content.EstimatedValues
     171          .Concat(dataset.GetDoubleValues(targetVariableName))))
     172          .Where(v => !double.IsNaN(v) && !double.IsInfinity(v)).Max();
     173        double min = Content.EstimatedTrainingValues
     174          .Concat(Content.EstimatedTestValues
     175          .Concat(Content.EstimatedValues
     176          .Concat(dataset.GetDoubleValues(targetVariableName))))
     177          .Where(v => !double.IsNaN(v) && !double.IsInfinity(v)).Min();
    170178
    171179        double axisMin, axisMax, axisInterval;
  • branches/2817-BinPackingSpeedup/HeuristicLab.Problems.DataAnalysis.Views/3.4/Regression/RegressionSolutionVariableImpactsView.Designer.cs

    r16140 r16141  
    1919 */
    2020#endregion
     21
    2122
    2223namespace HeuristicLab.Problems.DataAnalysis.Views {
     
    8182      this.sortByComboBox.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
    8283      this.sortByComboBox.FormattingEnabled = true;
     84      this.sortByComboBox.Items.AddRange(new object[] {
     85            HeuristicLab.Problems.DataAnalysis.Views.RegressionSolutionVariableImpactsView.SortingCriteria.ImpactValue,
     86            HeuristicLab.Problems.DataAnalysis.Views.RegressionSolutionVariableImpactsView.SortingCriteria.Occurrence,
     87            HeuristicLab.Problems.DataAnalysis.Views.RegressionSolutionVariableImpactsView.SortingCriteria.VariableName});
    8388      this.sortByComboBox.Location = new System.Drawing.Point(407, 3);
    8489      this.sortByComboBox.Name = "sortByComboBox";
     
    159164      // variableImactsArrayView
    160165      //
    161       this.variableImactsArrayView.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
    162             | System.Windows.Forms.AnchorStyles.Left) 
     166      this.variableImactsArrayView.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
     167            | System.Windows.Forms.AnchorStyles.Left)
    163168            | System.Windows.Forms.AnchorStyles.Right)));
    164169      this.variableImactsArrayView.Caption = "StringConvertibleArray View";
     
    186191      this.Name = "RegressionSolutionVariableImpactsView";
    187192      this.Size = new System.Drawing.Size(712, 365);
     193      this.VisibleChanged += new System.EventHandler(this.RegressionSolutionVariableImpactsView_VisibleChanged);
    188194      this.ResumeLayout(false);
    189195      this.PerformLayout();
  • branches/2817-BinPackingSpeedup/HeuristicLab.Problems.DataAnalysis.Views/3.4/Regression/RegressionSolutionVariableImpactsView.cs

    r16140 r16141  
    2323using System.Collections.Generic;
    2424using System.Linq;
     25using System.Threading;
    2526using System.Threading.Tasks;
    2627using HeuristicLab.Common;
     
    3233  [Content(typeof(IRegressionSolution))]
    3334  public partial class RegressionSolutionVariableImpactsView : DataAnalysisSolutionEvaluationView {
     35    private CancellationTokenSource cancellationToken = new CancellationTokenSource();
    3436    private enum SortingCriteria {
    3537      ImpactValue,
     
    3739      VariableName
    3840    }
    39 
    40     private Dictionary<string, double> rawVariableImpacts = new Dictionary<string, double>();
     41    private List<Tuple<string, double>> rawVariableImpacts = new List<Tuple<string, double>>();
    4142
    4243    public new IRegressionSolution Content {
     
    5152      InitializeComponent();
    5253
    53       //Little workaround. If you fill the ComboBox-Items in the other partial class, the UI-Designer will moan.
    54       this.sortByComboBox.Items.AddRange(Enum.GetValues(typeof(SortingCriteria)).Cast<object>().ToArray());
     54      //Set the default values
     55      this.dataPartitionComboBox.SelectedIndex = 0;
     56      this.replacementComboBox.SelectedIndex = 3;
     57      this.factorVarReplComboBox.SelectedIndex = 0;
    5558      this.sortByComboBox.SelectedItem = SortingCriteria.ImpactValue;
    56 
    57       this.dataPartitionComboBox.SelectedIndex = 0;
    58       this.replacementComboBox.SelectedIndex = 0;
    59       this.factorVarReplComboBox.SelectedIndex = 0;
    60     }
    61 
    62     #region events
     59    }
     60
    6361    protected override void RegisterContentEvents() {
    6462      base.RegisterContentEvents();
     
    8684        variableImactsArrayView.Content = null;
    8785      } else {
    88         UpdateVariableImpacts();
    89       }
     86        UpdateVariableImpact();
     87      }
     88    }
     89
     90    private void RegressionSolutionVariableImpactsView_VisibleChanged(object sender, EventArgs e) {
     91      cancellationToken.Cancel();
    9092    }
    9193
    9294
    9395    private void dataPartitionComboBox_SelectedIndexChanged(object sender, EventArgs e) {
    94       UpdateVariableImpacts();
     96      UpdateVariableImpact();
    9597    }
    9698
    9799    private void replacementComboBox_SelectedIndexChanged(object sender, EventArgs e) {
    98       UpdateVariableImpacts();
     100      UpdateVariableImpact();
    99101    }
    100102
     
    102104      //Update the default ordering (asc,desc), but remove the eventHandler beforehand (otherwise the data would be ordered twice)
    103105      ascendingCheckBox.CheckedChanged -= ascendingCheckBox_CheckedChanged;
    104       switch ((SortingCriteria)sortByComboBox.SelectedItem) {
    105         case SortingCriteria.ImpactValue:
    106           ascendingCheckBox.Checked = false;
    107           break;
    108         case SortingCriteria.Occurrence:
    109           ascendingCheckBox.Checked = true;
    110           break;
    111         case SortingCriteria.VariableName:
    112           ascendingCheckBox.Checked = true;
    113           break;
    114         default:
    115           throw new NotImplementedException("Ordering for selected SortingCriteria not implemented");
    116       }
     106      ascendingCheckBox.Checked = (SortingCriteria)sortByComboBox.SelectedItem != SortingCriteria.ImpactValue;
    117107      ascendingCheckBox.CheckedChanged += ascendingCheckBox_CheckedChanged;
    118108
    119       UpdateDataOrdering();
     109      UpdateOrdering();
    120110    }
    121111
    122112    private void ascendingCheckBox_CheckedChanged(object sender, EventArgs e) {
    123       UpdateDataOrdering();
    124     }
    125     #endregion
    126 
    127     #region Helper Methods         
    128     private void UpdateVariableImpacts() {
     113      UpdateOrdering();
     114    }
     115
     116
     117    private async void UpdateVariableImpact() {
     118      IProgress progress;
     119
     120      //Check if the selection is valid
    129121      if (Content == null) { return; }
    130122      if (replacementComboBox.SelectedIndex < 0) { return; }
     
    132124      if (factorVarReplComboBox.SelectedIndex < 0) { return; }
    133125
    134       variableImactsArrayView.Caption = Content.Name + " Variable Impacts";
    135 
     126      //Prepare arguments
    136127      var mainForm = (MainForm.WindowsForms.MainForm)MainFormManager.MainForm;
    137128      var replMethod = (RegressionSolutionVariableImpactsCalculator.ReplacementMethodEnum)replacementComboBox.Items[replacementComboBox.SelectedIndex];
     
    139130      var dataPartition = (RegressionSolutionVariableImpactsCalculator.DataPartitionEnum)dataPartitionComboBox.SelectedItem;
    140131
    141       Task.Factory.StartNew(() => {
    142         try {
    143           mainForm.AddOperationProgressToView(this, "Calculating variable impacts for " + Content.Name);
    144 
    145           //Remember the original ordering of the variables
    146           var impacts = RegressionSolutionVariableImpactsCalculator.CalculateImpacts(Content, dataPartition, replMethod, factorReplMethod);
    147           var problemData = Content.ProblemData;
    148           var inputvariables = new HashSet<string>(problemData.AllowedInputVariables.Union(Content.Model.VariablesUsedForPrediction));
    149           var originalVariableOrdering = problemData.Dataset.VariableNames.Where(v => inputvariables.Contains(v)).Where(problemData.Dataset.VariableHasType<double>).ToList();
    150           rawVariableImpacts.Clear();
    151           originalVariableOrdering.ForEach(v => rawVariableImpacts.Add(v, impacts.First(vv => vv.Item1 == v).Item2));
    152           UpdateDataOrdering();
    153         } finally {
    154           mainForm.RemoveOperationProgressFromView(this);
    155         }
    156       });
     132      variableImactsArrayView.Caption = Content.Name + " Variable Impacts";
     133      progress = mainForm.AddOperationProgressToView(this, "Calculating variable impacts for " + Content.Name);
     134      progress.ProgressValue = 0;
     135
     136      cancellationToken = new CancellationTokenSource();
     137      //Remember the original ordering of the variables
     138      try {
     139        var impacts = await Task.Run(() => RegressionSolutionVariableImpactsCalculator.CalculateImpacts(Content, dataPartition, replMethod, factorReplMethod,
     140          (i, s) => {
     141            progress.ProgressValue = i;
     142            progress.Status = s;
     143            return cancellationToken.Token.IsCancellationRequested;
     144          }), cancellationToken.Token);
     145
     146        if (cancellationToken.Token.IsCancellationRequested) { return; }
     147        var problemData = Content.ProblemData;
     148        var inputvariables = new HashSet<string>(problemData.AllowedInputVariables.Union(Content.Model.VariablesUsedForPrediction));
     149        var originalVariableOrdering = problemData.Dataset.VariableNames
     150          .Where(v => inputvariables.Contains(v))
     151          .Where(v => problemData.Dataset.VariableHasType<double>(v) || problemData.Dataset.VariableHasType<string>(v))
     152          .ToList();
     153
     154        rawVariableImpacts.Clear();
     155        originalVariableOrdering.ForEach(v => rawVariableImpacts.Add(new Tuple<string, double>(v, impacts.First(vv => vv.Item1 == v).Item2)));
     156        UpdateOrdering();
     157      } finally {
     158        ((MainForm.WindowsForms.MainForm)MainFormManager.MainForm).RemoveOperationProgressFromView(this);
     159      }
    157160    }
    158161
     
    161164    /// The default is "Descending" by "VariableImpact" (as in previous versions)
    162165    /// </summary>
    163     private void UpdateDataOrdering() {
     166    private void UpdateOrdering() {
    164167      //Check if valid sortingCriteria is selected and data exists
    165168      if (sortByComboBox.SelectedIndex == -1) { return; }
     
    170173      bool ascending = ascendingCheckBox.Checked;
    171174
    172       IEnumerable<KeyValuePair<string, double>> orderedEntries = null;
     175      IEnumerable<Tuple<string, double>> orderedEntries = null;
    173176
    174177      //Sort accordingly
    175178      switch (selectedItem) {
    176179        case SortingCriteria.ImpactValue:
    177           orderedEntries = rawVariableImpacts.OrderBy(v => v.Value);
     180          orderedEntries = rawVariableImpacts.OrderBy(v => v.Item2);
    178181          break;
    179182        case SortingCriteria.Occurrence:
     
    181184          break;
    182185        case SortingCriteria.VariableName:
    183           orderedEntries = rawVariableImpacts.OrderBy(v => v.Key, new NaturalStringComparer());
     186          orderedEntries = rawVariableImpacts.OrderBy(v => v.Item1, new NaturalStringComparer());
    184187          break;
    185188        default:
     
    190193
    191194      //Write the data back
    192       var impactArray = new DoubleArray(orderedEntries.Select(i => i.Value).ToArray()) {
    193         ElementNames = orderedEntries.Select(i => i.Key)
     195      var impactArray = new DoubleArray(orderedEntries.Select(i => i.Item2).ToArray()) {
     196        ElementNames = orderedEntries.Select(i => i.Item1)
    194197      };
    195       variableImactsArrayView.Content = (DoubleArray)impactArray.AsReadOnly();
    196     }
    197     #endregion
     198
     199      //Could be, if the View was closed
     200      if (!variableImactsArrayView.IsDisposed) {
     201        variableImactsArrayView.Content = (DoubleArray)impactArray.AsReadOnly();
     202      }
     203    }
    198204  }
    199205}
  • branches/2817-BinPackingSpeedup/HeuristicLab.Problems.DataAnalysis.Views/3.4/TimeSeriesPrognosis/TimeSeriesPrognosisResidualsLineChartView.cs

    r16140 r16141  
    2020#endregion
    2121
    22 using System;
    2322using System.Linq;
    24 using System.Windows.Forms.DataVisualization.Charting;
    2523using HeuristicLab.MainForm;
    2624
Note: See TracChangeset for help on using the changeset viewer.