Changeset 13938


Ignore:
Timestamp:
06/27/16 15:28:26 (5 years ago)
Author:
mkommend
Message:

#2619:

  • Refactored and separated the different feature correlation calculations.
  • Added a checkbox to ignore missing values in the calculation.
Location:
trunk/sources
Files:
2 added
11 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Problems.DataAnalysis.Views/3.4/FeatureCorrelation/AbstractFeatureCorrelationView.cs

    r12151 r13938  
    3737    public const string TESTSAMPLES = "Test Samples";
    3838
    39     public static readonly IList<string> Partitions = new List<string>() { ALLSAMPLES, TRAININGSAMPLES, TESTSAMPLES };
     39    public static readonly IList<string> Partitions = new List<string>() { ALLSAMPLES, TRAININGSAMPLES, TESTSAMPLES }.AsReadOnly();
    4040
    41     protected FeatureCorrelationCalculator fcc;
     41    protected AbstractFeatureCorrelationCalculator CorrelationCalculator { get; set; }
    4242
    4343    public new DataAnalysisProblemData Content {
     
    4949      InitializeComponent();
    5050      dataView.FormatPattern = "0.000";
    51       fcc = new FeatureCorrelationCalculator();
    5251      var calculators = ApplicationManager.Manager.GetInstances<IDependencyCalculator>();
    5352      var calcList = calculators.OrderBy(c => c.Name).Select(c => new { Name = c.Name, Calculator = c }).ToList();
     
    5554      correlationCalcComboBox.DisplayMember = "Name";
    5655      correlationCalcComboBox.DataSource = calcList;
    57       correlationCalcComboBox.SelectedItem = calcList.First(c => c.Calculator.GetType().Equals(typeof(PearsonsRDependenceCalculator)));
     56      correlationCalcComboBox.SelectedItem = calcList.First(c => c.Calculator.GetType() == typeof(PearsonsRDependenceCalculator));
    5857      partitionComboBox.DataSource = Partitions;
    5958      partitionComboBox.SelectedItem = TRAININGSAMPLES;
     
    6160    }
    6261
     62    protected override void OnClosing(FormClosingEventArgs e) {
     63      base.OnClosing(e);
     64      CorrelationCalculator.TryCancelCalculation();
     65    }
     66
    6367    protected override void RegisterContentEvents() {
    6468      base.RegisterContentEvents();
    65       fcc.ProgressCalculation += new FeatureCorrelationCalculator.ProgressCalculationHandler(Content_ProgressCalculation);
    66       fcc.CorrelationCalculationFinished += new FeatureCorrelationCalculator.CorrelationCalculationFinishedHandler(Content_CorrelationCalculationFinished);
     69      CorrelationCalculator.ProgressChanged += FeatureCorrelation_ProgressChanged;
     70      CorrelationCalculator.CorrelationCalculationFinished += FeatureCorrelation_CalculationFinished;
    6771    }
    6872
    6973    protected override void DeregisterContentEvents() {
    70       fcc.CorrelationCalculationFinished -= new FeatureCorrelationCalculator.CorrelationCalculationFinishedHandler(Content_CorrelationCalculationFinished);
    71       fcc.ProgressCalculation -= new FeatureCorrelationCalculator.ProgressCalculationHandler(Content_ProgressCalculation);
     74      CorrelationCalculator.ProgressChanged -= FeatureCorrelation_ProgressChanged;
     75      CorrelationCalculator.CorrelationCalculationFinished -= FeatureCorrelation_CalculationFinished;
    7276      base.DeregisterContentEvents();
    7377    }
     
    7579    protected override void OnContentChanged() {
    7680      base.OnContentChanged();
    77       fcc.TryCancelCalculation();
     81      CorrelationCalculator.TryCancelCalculation();
    7882      if (Content != null) {
    79         fcc.ProblemData = Content;
    8083        CalculateCorrelation();
    8184      } else {
     
    106109
    107110    protected abstract void CalculateCorrelation();
    108     protected abstract void Content_CorrelationCalculationFinished(object sender, FeatureCorrelationCalculator.CorrelationCalculationFinishedArgs e);
     111
     112    protected abstract void FeatureCorrelation_CalculationFinished(object sender,
     113      AbstractFeatureCorrelationCalculator.CorrelationCalculationFinishedArgs e);
    109114
    110115    protected void UpdateDataView(DoubleMatrix correlation) {
     
    121126    }
    122127
    123     protected void Content_ProgressCalculation(object sender, ProgressChangedEventArgs e) {
     128    protected void FeatureCorrelation_ProgressChanged(object sender, ProgressChangedEventArgs e) {
    124129      if (!progressPanel.Visible && e.ProgressPercentage != progressBar.Maximum) {
    125130        progressPanel.Show();
  • trunk/sources/HeuristicLab.Problems.DataAnalysis.Views/3.4/FeatureCorrelation/FeatureCorrelationCalculator.cs

    r12509 r13938  
    2424using System.ComponentModel;
    2525using System.Linq;
     26using System.Threading.Tasks;
    2627using HeuristicLab.PluginInfrastructure;
    2728
    2829namespace HeuristicLab.Problems.DataAnalysis.Views {
    2930  [NonDiscoverableType]
    30   public class FeatureCorrelationCalculator : Object {
     31  public sealed class FeatureCorrelationCalculator : AbstractFeatureCorrelationCalculator {
     32    public FeatureCorrelationCalculator() : base() { }
    3133
    32     private BackgroundWorker bw;
    33     private BackgroundWorkerInfo bwInfo;
     34    public void CalculateElements(IDataAnalysisProblemData problemData, IDependencyCalculator calc, string partition, bool ignoreMissingValues) {
     35      var indices = GetRelevantIndices(problemData, partition);
     36      var info = new BackgroundWorkerInfo {
     37        Dataset = problemData.Dataset, Calculator = calc, Partition = partition, Indices = indices, IgnoreMissingValues = ignoreMissingValues
     38      };
    3439
    35     private IDataAnalysisProblemData problemData;
    36     public IDataAnalysisProblemData ProblemData {
    37       set {
    38         if (bw != null) {
    39           bw.CancelAsync();
    40         }
    41         problemData = value;
    42       }
     40      StartCalculation(info);
    4341    }
    4442
    45     public FeatureCorrelationCalculator()
    46       : base() { }
     43    protected override void BackgroundWorker_DoWork(object sender, DoWorkEventArgs e) {
     44      BackgroundWorker worker = (BackgroundWorker)sender;
     45      BackgroundWorkerInfo bwInfo = (BackgroundWorkerInfo)e.Argument;
    4746
    48     public FeatureCorrelationCalculator(IDataAnalysisProblemData problemData)
    49       : base() {
    50       this.problemData = problemData;
    51     }
    52 
    53     public void CalculateElements(IDependencyCalculator calc, string partition) {
    54       CalculateElements(problemData.Dataset, calc, partition);
    55     }
    56 
    57     // returns true if any calculation takes place
    58     public bool CalculateTimeframeElements(IDependencyCalculator calc, string partition, string variable, int frames, double[,] correlation = null) {
    59       if (correlation == null || correlation.GetLength(1) <= frames) {
    60         CalculateElements(problemData.Dataset, calc, partition, variable, frames, correlation);
    61         return true;
    62       } else {
    63         return false;
    64       }
    65     }
    66 
    67     public void TryCancelCalculation() {
    68       if (bw != null && bw.IsBusy) {
    69         bwInfo = null;
    70         bw.CancelAsync();
    71       }
    72     }
    73 
    74     private void CalculateElements(IDataset dataset, IDependencyCalculator calc, string partition, string variable = null, int frames = 0, double[,] alreadyCalculated = null) {
    75       var indices = GetRelevantIndices(problemData, partition);
    76       bwInfo = new BackgroundWorkerInfo {
    77         Dataset = dataset, Calculator = calc, Partition = partition, Indices = indices,
    78         Variable = variable, Frames = frames, AlreadyCalculated = alreadyCalculated
    79       };
    80       if (bw == null) {
    81         bw = new BackgroundWorker();
    82         bw.WorkerReportsProgress = true;
    83         bw.WorkerSupportsCancellation = true;
    84         bw.DoWork += new DoWorkEventHandler(BwDoWork);
    85         bw.ProgressChanged += new ProgressChangedEventHandler(BwProgressChanged);
    86         bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(BwRunWorkerCompleted);
    87       }
    88       if (bw.IsBusy) {
    89         bw.CancelAsync();
    90       } else {
    91         bw.RunWorkerAsync(bwInfo);
    92       }
    93     }
    94 
    95     private IEnumerable<int> GetRelevantIndices(IDataAnalysisProblemData problemData, string partition) {
    96       IEnumerable<int> var;
    97       if (partition.Equals(AbstractFeatureCorrelationView.TRAININGSAMPLES))
    98         var = problemData.TrainingIndices;
    99       else if (partition.Equals(AbstractFeatureCorrelationView.TESTSAMPLES))
    100         var = problemData.TestIndices;
    101       else var = Enumerable.Range(0, problemData.Dataset.Rows);
    102       return var;
    103     }
    104 
    105     #region backgroundworker
    106     private void BwDoWork(object sender, DoWorkEventArgs e) {
    107       BackgroundWorkerInfo bwInfo = (BackgroundWorkerInfo)e.Argument;
    108       if (bwInfo.Variable == null) {
    109         BwCalculateCorrelation(sender, e);
    110       } else {
    111         BwCalculateTimeframeCorrelation(sender, e);
    112       }
    113     }
    114 
    115     private void BwCalculateCorrelation(object sender, DoWorkEventArgs e) {
    116       BackgroundWorker worker = sender as BackgroundWorker;
    117 
    118       BackgroundWorkerInfo bwInfo = (BackgroundWorkerInfo)e.Argument;
    11947      var dataset = bwInfo.Dataset;
    120       IEnumerable<int> indices = bwInfo.Indices;
     48      var indices = bwInfo.Indices.ToArray();
    12149      IDependencyCalculator calc = bwInfo.Calculator;
    12250
     
    12553      int length = doubleVariableNames.Count;
    12654      double[,] elements = new double[length, length];
    127       double calculations = (Math.Pow(length, 2) + length) / 2;
    12855
    12956      worker.ReportProgress(0);
    13057
    131       for (int i = 0; i < length; i++) {
    132         for (int j = 0; j < i + 1; j++) {
    133           if (worker.CancellationPending) {
    134             worker.ReportProgress(100);
    135             e.Cancel = true;
    136             return;
    137           }
    138           IEnumerable<double> var1 = problemData.Dataset.GetDoubleValues(doubleVariableNames[i], indices);
    139           IEnumerable<double> var2 = problemData.Dataset.GetDoubleValues(doubleVariableNames[j], indices);
     58      for (int counter = 0; counter < length; counter++) {
     59        if (worker.CancellationPending) {
     60          worker.ReportProgress(100);
     61          e.Cancel = true;
     62          return;
     63        }
    14064
    141           elements[i, j] = calc.Calculate(var1, var2, out error);
     65        var i = counter;
     66        Parallel.ForEach(Enumerable.Range(i, length - i), j => {
     67          var var1 = dataset.GetDoubleValues(doubleVariableNames[i], indices);
     68          var var2 = dataset.GetDoubleValues(doubleVariableNames[j], indices);
     69
     70          if (bwInfo.IgnoreMissingValues) {
     71            var filtered = FilterNaNValues(var1, var2);
     72            elements[i, j] = calc.Calculate(filtered, out error);
     73          } else
     74            elements[i, j] = calc.Calculate(var1, var2, out error);
    14275
    14376          if (!error.Equals(OnlineCalculatorError.None)) {
     
    14578          }
    14679          elements[j, i] = elements[i, j];
    147           worker.ReportProgress((int)Math.Round((((Math.Pow(i, 2) + i) / 2 + j + 1.0) / calculations) * 100));
    148         }
     80
     81        });
     82        worker.ReportProgress((int)(((double)counter) / length * 100));
    14983      }
    15084      e.Result = elements;
     
    15286    }
    15387
    154     private void BwCalculateTimeframeCorrelation(object sender, DoWorkEventArgs e) {
    155       BackgroundWorker worker = sender as BackgroundWorker;
    15688
    157       BackgroundWorkerInfo bwInfo = (BackgroundWorkerInfo)e.Argument;
    158       var dataset = bwInfo.Dataset;
    159       IEnumerable<int> indices = bwInfo.Indices;
    160       IDependencyCalculator calc = bwInfo.Calculator;
    161       string variable = bwInfo.Variable;
    162       int frames = bwInfo.Frames;
    163       double[,] alreadyCalculated = bwInfo.AlreadyCalculated;
     89    private static IEnumerable<Tuple<double, double>> FilterNaNValues(IEnumerable<double> first, IEnumerable<double> second) {
     90      var firstEnumerator = first.GetEnumerator();
     91      var secondEnumerator = second.GetEnumerator();
    16492
    165       IList<string> doubleVariableNames = dataset.DoubleVariables.ToList();
    166       OnlineCalculatorError error = OnlineCalculatorError.None;
    167       int length = doubleVariableNames.Count;
    168       double[,] elements = new double[length, frames + 1];
    169       double calculations = (frames + 1) * length;
     93      while (firstEnumerator.MoveNext() & secondEnumerator.MoveNext()) {
     94        var firstValue = firstEnumerator.Current;
     95        var secondValue = secondEnumerator.Current;
    17096
    171       worker.ReportProgress(0);
     97        if (double.IsNaN(firstValue)) continue;
     98        if (double.IsNaN(secondValue)) continue;
    17299
    173       int start = 0;
    174       if (alreadyCalculated != null) {
    175         for (int i = 0; i < alreadyCalculated.GetLength(0); i++) {
    176           Array.Copy(alreadyCalculated, i * alreadyCalculated.GetLength(1), elements, i * elements.GetLength(1), alreadyCalculated.GetLength(1));
    177         }
    178         start = alreadyCalculated.GetLength(1);
     100        yield return Tuple.Create(firstValue, secondValue);
    179101      }
    180102
    181       for (int i = 0; i < length; i++) {
    182         for (int j = start; j <= frames; j++) {
    183           if (worker.CancellationPending) {
    184             worker.ReportProgress(100);
    185             e.Cancel = true;
    186             return;
    187           }
    188 
    189           IEnumerable<double> var1 = problemData.Dataset.GetDoubleValues(variable, indices);
    190           IEnumerable<double> var2 = problemData.Dataset.GetDoubleValues(doubleVariableNames[i], indices);
    191 
    192           var valuesInFrame = var1.Take(j);
    193           var help = var1.Skip(j).ToList();
    194           help.AddRange(valuesInFrame);
    195           var1 = help;
    196 
    197           elements[i, j] = calc.Calculate(var1, var2, out error);
    198 
    199           if (!error.Equals(OnlineCalculatorError.None)) {
    200             elements[i, j] = double.NaN;
    201           }
    202           worker.ReportProgress((int)((100.0 / calculations) * (i * (frames + 1) + j + 1)));
    203         }
     103      if (firstEnumerator.MoveNext() || secondEnumerator.MoveNext()) {
     104        throw new ArgumentException("Number of elements in first and second enumeration doesn't match.");
    204105      }
    205       e.Result = elements;
    206       worker.ReportProgress(100);
    207     }
    208 
    209     private void BwRunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) {
    210       BackgroundWorker worker = sender as BackgroundWorker;
    211       if (!e.Cancelled && !worker.CancellationPending) {
    212         if (e.Error != null) {
    213           ErrorHandling.ShowErrorDialog(e.Error);
    214         } else {
    215           OnCorrelationCalculationFinished((double[,])e.Result, bwInfo.Calculator, bwInfo.Partition, bwInfo.Variable);
    216         }
    217       } else if (bwInfo != null) {
    218         bw.RunWorkerAsync(bwInfo);
    219       }
    220     }
    221     #endregion
    222 
    223     #region events
    224     public class CorrelationCalculationFinishedArgs : EventArgs {
    225       public double[,] Correlation { get; private set; }
    226       public IDependencyCalculator Calculcator { get; private set; }
    227       public string Partition { get; private set; }
    228       public string Variable { get; private set; }
    229 
    230       public CorrelationCalculationFinishedArgs(double[,] correlation, IDependencyCalculator calculator, string partition, string variable = null) {
    231         this.Correlation = correlation;
    232         this.Calculcator = calculator;
    233         this.Partition = partition;
    234         this.Variable = variable;
    235       }
    236     }
    237 
    238     public delegate void CorrelationCalculationFinishedHandler(object sender, CorrelationCalculationFinishedArgs e);
    239     public event CorrelationCalculationFinishedHandler CorrelationCalculationFinished;
    240     protected virtual void OnCorrelationCalculationFinished(double[,] correlation, IDependencyCalculator calculator, string partition, string variable = null) {
    241       var handler = CorrelationCalculationFinished;
    242       if (handler != null)
    243         handler(this, new CorrelationCalculationFinishedArgs(correlation, calculator, partition, variable));
    244     }
    245 
    246     public delegate void ProgressCalculationHandler(object sender, ProgressChangedEventArgs e);
    247     public event ProgressCalculationHandler ProgressCalculation;
    248     protected void BwProgressChanged(object sender, ProgressChangedEventArgs e) {
    249       BackgroundWorker worker = sender as BackgroundWorker;
    250       if (ProgressCalculation != null) {
    251         ProgressCalculation(sender, e);
    252       }
    253     }
    254     #endregion
    255 
    256     private class BackgroundWorkerInfo {
    257       public IDataset Dataset { get; set; }
    258       public IDependencyCalculator Calculator { get; set; }
    259       public string Partition { get; set; }
    260       public IEnumerable<int> Indices { get; set; }
    261       public string Variable { get; set; }
    262       public int Frames { get; set; }
    263       public double[,] AlreadyCalculated { get; set; }
    264106    }
    265107  }
  • trunk/sources/HeuristicLab.Problems.DataAnalysis.Views/3.4/FeatureCorrelation/FeatureCorrelationView.Designer.cs

    r12012 r13938  
    4747    /// </summary>
    4848    private void InitializeComponent() {
     49      this.ignoreMissingValuesCheckbox = new System.Windows.Forms.CheckBox();
     50      ((System.ComponentModel.ISupportInitialize)(this.pictureBox)).BeginInit();
     51      ((System.ComponentModel.ISupportInitialize)(this.splitContainer)).BeginInit();
     52      this.splitContainer.Panel1.SuspendLayout();
     53      this.splitContainer.Panel2.SuspendLayout();
     54      this.splitContainer.SuspendLayout();
     55      this.progressPanel.SuspendLayout();
    4956      this.SuspendLayout();
     57      //
     58      // minimumLabel
     59      //
     60      this.minimumLabel.Location = new System.Drawing.Point(707, 434);
     61      //
     62      // maximumLabel
     63      //
     64      this.maximumLabel.Location = new System.Drawing.Point(707, 2);
     65      //
     66      // pictureBox
     67      //
     68      this.pictureBox.Location = new System.Drawing.Point(727, 30);
     69      this.pictureBox.Size = new System.Drawing.Size(35, 401);
     70      //
     71      // splitContainer
     72      //
     73      //
     74      // splitContainer.Panel1
     75      //
     76      this.splitContainer.Panel1.Controls.Add(this.ignoreMissingValuesCheckbox);
     77      this.splitContainer.Size = new System.Drawing.Size(695, 450);
     78      //
     79      // dataView
     80      //
     81      this.dataView.Size = new System.Drawing.Size(695, 421);
     82      //
     83      // ignoreMissingValuesCheckbox
     84      //
     85      this.ignoreMissingValuesCheckbox.AutoSize = true;
     86      this.ignoreMissingValuesCheckbox.CheckAlign = System.Drawing.ContentAlignment.MiddleRight;
     87      this.ignoreMissingValuesCheckbox.Location = new System.Drawing.Point(481, 6);
     88      this.ignoreMissingValuesCheckbox.Name = "ignoreMissingValuesCheckbox";
     89      this.ignoreMissingValuesCheckbox.Size = new System.Drawing.Size(122, 17);
     90      this.ignoreMissingValuesCheckbox.TabIndex = 11;
     91      this.ignoreMissingValuesCheckbox.Text = "ignoreMissingValues";
     92      this.ignoreMissingValuesCheckbox.UseVisualStyleBackColor = true;
     93      this.ignoreMissingValuesCheckbox.CheckedChanged += new System.EventHandler(this.ignoreMissingValuesCheckbox_CheckedChanged);
    5094      //
    5195      // FeatureCorrelationView
     
    5498      this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
    5599      this.Name = "FeatureCorrelationView";
    56       this.Size = new System.Drawing.Size(569, 336);
     100      this.Size = new System.Drawing.Size(789, 456);
     101      ((System.ComponentModel.ISupportInitialize)(this.pictureBox)).EndInit();
     102      this.splitContainer.Panel1.ResumeLayout(false);
     103      this.splitContainer.Panel1.PerformLayout();
     104      this.splitContainer.Panel2.ResumeLayout(false);
     105      ((System.ComponentModel.ISupportInitialize)(this.splitContainer)).EndInit();
     106      this.splitContainer.ResumeLayout(false);
     107      this.progressPanel.ResumeLayout(false);
     108      this.progressPanel.PerformLayout();
    57109      this.ResumeLayout(false);
     110
    58111    }
    59112    #endregion
     113
     114    private System.Windows.Forms.CheckBox ignoreMissingValuesCheckbox;
    60115  }
    61116}
  • trunk/sources/HeuristicLab.Problems.DataAnalysis.Views/3.4/FeatureCorrelation/FeatureCorrelationView.cs

    r12012 r13938  
    3434
    3535    private FeatureCorrelationCache correlationCache;
     36    private new FeatureCorrelationCalculator CorrelationCalculator {
     37      get { return (FeatureCorrelationCalculator)base.CorrelationCalculator; }
     38      set { base.CorrelationCalculator = value; }
     39    }
    3640
    3741    public FeatureCorrelationView()
    3842      : base() {
    3943      InitializeComponent();
     44      CorrelationCalculator = new FeatureCorrelationCalculator();
    4045      correlationCache = new FeatureCorrelationCache();
    4146    }
     
    4954    }
    5055
     56    private void ignoreMissingValuesCheckbox_CheckedChanged(object sender, EventArgs e) {
     57      CalculateCorrelation();
     58    }
     59
    5160    protected override void CalculateCorrelation() {
    5261      if (correlationCalcComboBox.SelectedItem == null) return;
     
    5665      string partition = (string)partitionComboBox.SelectedValue;
    5766      dataView.Enabled = false;
    58       double[,] corr = correlationCache.GetCorrelation(calc, partition);
     67      double[,] corr = correlationCache.GetCorrelation(calc, partition, ignoreMissingValuesCheckbox.Checked);
    5968      if (corr == null) {
    60         fcc.CalculateElements(calc, partition);
     69        CorrelationCalculator.CalculateElements(Content, calc, partition, ignoreMissingValuesCheckbox.Checked);
    6170      } else {
    62         fcc.TryCancelCalculation();
     71        CorrelationCalculator.TryCancelCalculation();
    6372        var correlation = new DoubleMatrix(corr, Content.Dataset.DoubleVariables, Content.Dataset.DoubleVariables);
    6473        UpdateDataView(correlation);
     
    6675    }
    6776
    68 
    69 
    70     protected override void Content_CorrelationCalculationFinished(object sender, FeatureCorrelationCalculator.CorrelationCalculationFinishedArgs e) {
     77    protected override void FeatureCorrelation_CalculationFinished(object sender, AbstractFeatureCorrelationCalculator.CorrelationCalculationFinishedArgs e) {
    7178      if (InvokeRequired) {
    72         Invoke(new FeatureCorrelationCalculator.CorrelationCalculationFinishedHandler(Content_CorrelationCalculationFinished), sender, e);
     79        Invoke(new AbstractFeatureCorrelationCalculator.CorrelationCalculationFinishedHandler(FeatureCorrelation_CalculationFinished), sender, e);
    7380        return;
    7481      }
    75       correlationCache.SetCorrelation(e.Calculcator, e.Partition, e.Correlation);
     82      correlationCache.SetCorrelation(e.Calculcator, e.Partition, e.IgnoreMissingValues, e.Correlation);
     83
    7684      var correlation = new DoubleMatrix(e.Correlation, Content.Dataset.DoubleVariables, Content.Dataset.DoubleVariables);
    7785      UpdateDataView(correlation);
     
    8088    [NonDiscoverableType]
    8189    private class FeatureCorrelationCache : Object {
    82       private Dictionary<Tuple<IDependencyCalculator, string>, double[,]> correlationsCache;
     90      private Dictionary<Tuple<IDependencyCalculator, string, bool>, double[,]> correlationsCache;
    8391
    8492      public FeatureCorrelationCache()
     
    8896
    8997      private void InitializeCaches() {
    90         correlationsCache = new Dictionary<Tuple<IDependencyCalculator, string>, double[,]>();
     98        correlationsCache = new Dictionary<Tuple<IDependencyCalculator, string, bool>, double[,]>();
    9199      }
    92100
     
    95103      }
    96104
    97       public double[,] GetCorrelation(IDependencyCalculator calc, string partition) {
     105      public double[,] GetCorrelation(IDependencyCalculator calc, string partition, bool ignoreMissingValues) {
    98106        double[,] corr;
    99         var key = new Tuple<IDependencyCalculator, string>(calc, partition);
     107        var key = Tuple.Create(calc, partition, ignoreMissingValues);
    100108        correlationsCache.TryGetValue(key, out corr);
    101109        return corr;
    102110      }
    103111
    104       public void SetCorrelation(IDependencyCalculator calc, string partition, double[,] correlation) {
    105         var key = new Tuple<IDependencyCalculator, string>(calc, partition);
     112      public void SetCorrelation(IDependencyCalculator calc, string partition, bool ignoreMissingValues, double[,] correlation) {
     113        var key = Tuple.Create(calc, partition, ignoreMissingValues);
    106114        correlationsCache[key] = correlation;
    107115      }
  • trunk/sources/HeuristicLab.Problems.DataAnalysis.Views/3.4/FeatureCorrelation/TimeframeFeatureCorrelationView.cs

    r12012 r13938  
    3636    private string lastFramesValue;
    3737
     38    private new TimeframeFeatureCorrelationCalculator CorrelationCalculator {
     39      get { return (TimeframeFeatureCorrelationCalculator)base.CorrelationCalculator; }
     40      set { base.CorrelationCalculator = value; }
     41    }
     42
     43
    3844    public TimeframeFeatureCorrelationView() {
    3945      InitializeComponent();
     46      CorrelationCalculator = new TimeframeFeatureCorrelationCalculator();
    4047      correlationTimeframCache = new FeatureCorrelationTimeframeCache();
    4148      errorProvider.SetIconAlignment(timeframeTextbox, ErrorIconAlignment.MiddleRight);
     
    93100
    94101    protected override void CalculateCorrelation() {
    95       if (correlationCalcComboBox.SelectedItem != null && partitionComboBox.SelectedItem != null
    96         && variableSelectionComboBox.SelectedItem != null) {
    97         string variable = (string)variableSelectionComboBox.SelectedItem;
    98         IDependencyCalculator calc = (IDependencyCalculator)correlationCalcComboBox.SelectedValue;
    99         string partition = (string)partitionComboBox.SelectedValue;
    100         int frames;
    101         int.TryParse(timeframeTextbox.Text, out frames);
    102         dataView.Enabled = false;
    103         double[,] corr = correlationTimeframCache.GetTimeframeCorrelation(calc, partition, variable);
    104         if (corr == null) {
    105           fcc.CalculateTimeframeElements(calc, partition, variable, frames);
    106         } else if (corr.GetLength(1) <= frames) {
    107           fcc.CalculateTimeframeElements(calc, partition, variable, frames, corr);
    108         } else {
    109           fcc.TryCancelCalculation();
    110           var columnNames = Enumerable.Range(0, corr.GetLength(1)).Select(x => x.ToString());
    111           var correlation = new DoubleMatrix(corr, columnNames, Content.Dataset.DoubleVariables);
    112           ((IStringConvertibleMatrix)correlation).Columns = frames + 1;
    113           UpdateDataView(correlation);
    114         }
     102      if (correlationCalcComboBox.SelectedItem == null) return;
     103      if (partitionComboBox.SelectedItem == null) return;
     104      if (variableSelectionComboBox.SelectedItem == null) return;
     105
     106      string variable = (string)variableSelectionComboBox.SelectedItem;
     107      IDependencyCalculator calc = (IDependencyCalculator)correlationCalcComboBox.SelectedValue;
     108      string partition = (string)partitionComboBox.SelectedValue;
     109      int frames;
     110      int.TryParse(timeframeTextbox.Text, out frames);
     111      dataView.Enabled = false;
     112      double[,] corr = correlationTimeframCache.GetTimeframeCorrelation(calc, partition, variable);
     113      if (corr == null) {
     114        CorrelationCalculator.CalculateTimeframeElements(Content, calc, partition, variable, frames);
     115      } else if (corr.GetLength(1) <= frames) {
     116        CorrelationCalculator.CalculateTimeframeElements(Content, calc, partition, variable, frames, corr);
     117      } else {
     118        CorrelationCalculator.TryCancelCalculation();
     119        var columnNames = Enumerable.Range(0, corr.GetLength(1)).Select(x => x.ToString());
     120        var correlation = new DoubleMatrix(corr, columnNames, Content.Dataset.DoubleVariables);
     121        ((IStringConvertibleMatrix)correlation).Columns = frames + 1;
     122        UpdateDataView(correlation);
    115123      }
    116124    }
    117125
    118     protected override void Content_CorrelationCalculationFinished(object sender, FeatureCorrelationCalculator.CorrelationCalculationFinishedArgs e) {
     126    protected override void FeatureCorrelation_CalculationFinished(object sender, AbstractFeatureCorrelationCalculator.CorrelationCalculationFinishedArgs e) {
    119127      if (InvokeRequired) {
    120         Invoke(new FeatureCorrelationCalculator.CorrelationCalculationFinishedHandler(Content_CorrelationCalculationFinished), sender, e);
     128        Invoke(new AbstractFeatureCorrelationCalculator.CorrelationCalculationFinishedHandler(FeatureCorrelation_CalculationFinished), sender, e);
    121129      } else {
    122130        correlationTimeframCache.SetTimeframeCorrelation(e.Calculcator, e.Partition, e.Variable, e.Correlation);
  • trunk/sources/HeuristicLab.Problems.DataAnalysis.Views/3.4/HeuristicLab.Problems.DataAnalysis.Views-3.4.csproj

    r13705 r13938  
    120120  </ItemGroup>
    121121  <ItemGroup>
     122    <Compile Include="FeatureCorrelation\TimeframeFeatureCorrelationCalculator.cs" />
     123    <Compile Include="FeatureCorrelation\AbstractFeatureCorrelationCalculator.cs" />
    122124    <Compile Include="Classification\ClassificationEnsembleSolutionEstimatedClassValuesView.cs">
    123125      <SubType>UserControl</SubType>
  • trunk/sources/HeuristicLab.Problems.DataAnalysis/3.4/Interfaces/IDependencyCalculator.cs

    r12012 r13938  
    2020#endregion
    2121
     22using System;
    2223using System.Collections.Generic;
    2324
     
    2930
    3031    double Calculate(IEnumerable<double> originalValues, IEnumerable<double> estimatedValues, out OnlineCalculatorError errorState);
     32    double Calculate(IEnumerable<Tuple<double, double>> values, out OnlineCalculatorError errorState);
    3133  }
    3234}
  • trunk/sources/HeuristicLab.Problems.DataAnalysis/3.4/OnlineCalculators/DependencyCalculator/HoeffdingsDependenceCalculator.cs

    r12012 r13938  
    4141      if (errorState != OnlineCalculatorError.None) return double.NaN;
    4242      return d;
     43    }
     44
     45    public double Calculate(IEnumerable<Tuple<double, double>> values, out OnlineCalculatorError errorState) {
     46      return HoeffD(values.Select(v => v.Item1), values.Select(v => v.Item2), out errorState);
    4347    }
    4448
  • trunk/sources/HeuristicLab.Problems.DataAnalysis/3.4/OnlineCalculators/DependencyCalculator/PearsonsRDependenceCalculator.cs

    r12492 r13938  
    3535      return OnlinePearsonsRCalculator.Calculate(originalValues, estimatedValues, out errorState);
    3636    }
     37
     38    public double Calculate(IEnumerable<Tuple<double, double>> values, out OnlineCalculatorError errorState) {
     39      var calculator = new OnlinePearsonsRCalculator();
     40      foreach (var tuple in values) {
     41        calculator.Add(tuple.Item1, tuple.Item2);
     42        if (calculator.ErrorState != OnlineCalculatorError.None) break;
     43      }
     44      errorState = calculator.ErrorState;
     45      return calculator.R;
     46    }
    3747  }
    3848}
  • trunk/sources/HeuristicLab.Problems.DataAnalysis/3.4/OnlineCalculators/DependencyCalculator/PearsonsRSquaredDependenceCalculator.cs

    r12641 r13938  
    2020#endregion
    2121
     22using System;
    2223using System.Collections.Generic;
    2324
     
    3536      return r * r;
    3637    }
     38
     39    public double Calculate(IEnumerable<Tuple<double, double>> values, out OnlineCalculatorError errorState) {
     40      var calculator = new OnlinePearsonsRCalculator();
     41      foreach (var tuple in values) {
     42        calculator.Add(tuple.Item1, tuple.Item2);
     43        if (calculator.ErrorState != OnlineCalculatorError.None) break;
     44      }
     45      errorState = calculator.ErrorState;
     46      var r = calculator.R;
     47      return r * r;
     48    }
    3749  }
    3850}
  • trunk/sources/HeuristicLab.Problems.DataAnalysis/3.4/OnlineCalculators/DependencyCalculator/SpearmansRankCorrelationCoefficientCalculator.cs

    r12817 r13938  
    2020#endregion
    2121
     22using System;
    2223using System.Collections.Generic;
    2324using System.Linq;
     
    3334
    3435    public double Calculate(IEnumerable<double> originalValues, IEnumerable<double> estimatedValues, out OnlineCalculatorError errorState) {
    35       return SpearmansRankCorrelationCoefficientCalculator.CalculateSpearmansRank(originalValues, estimatedValues, out errorState);
     36      return CalculateSpearmansRank(originalValues, estimatedValues, out errorState);
     37    }
     38    public double Calculate(IEnumerable<Tuple<double, double>> values, out OnlineCalculatorError errorState) {
     39      return CalculateSpearmansRank(values.Select(v => v.Item1), values.Select(v => v.Item2), out errorState);
    3640    }
    3741
     
    5054      return rs;
    5155    }
     56
     57
    5258  }
    5359}
Note: See TracChangeset for help on using the changeset viewer.