#region License Information /* HeuristicLab * Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL) * * This file is part of HeuristicLab. * * HeuristicLab is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * HeuristicLab is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with HeuristicLab. If not, see . */ #endregion using System; using System.Collections.Generic; using System.Linq; using System.Windows.Forms; using HeuristicLab.Core.Views; using HeuristicLab.Data; using HeuristicLab.MainForm; using HeuristicLab.Optimization; namespace HeuristicLab.Analysis.Statistics { [View("Result Correlation View")] [Content(typeof(RunCollection), false)] public sealed partial class ResultCorrelationView : ItemView { public ResultCorrelationView() { InitializeComponent(); stringConvertibleMatrixView.Minimum = -1.0; stringConvertibleMatrixView.Maximum = 1.0; } public new RunCollection Content { get { return (RunCollection)base.Content; } set { base.Content = value; } } public override bool ReadOnly { get { return true; } set { /*not needed because results are always readonly */} } protected override void OnContentChanged() { base.OnContentChanged(); resultComboBox.Items.Clear(); if (Content != null) { UpdateResultComboBox(); } } #region events protected override void RegisterContentEvents() { base.RegisterContentEvents(); Content.ItemsAdded += new HeuristicLab.Collections.CollectionItemsChangedEventHandler(Content_ItemsAdded); Content.ItemsRemoved += new HeuristicLab.Collections.CollectionItemsChangedEventHandler(Content_ItemsRemoved); Content.CollectionReset += new HeuristicLab.Collections.CollectionItemsChangedEventHandler(Content_CollectionReset); RegisterRunEvents(Content); } private void RegisterRunEvents(IEnumerable runs) { foreach (IRun run in runs) run.Changed += new EventHandler(run_Changed); } protected override void DeregisterContentEvents() { base.DeregisterContentEvents(); Content.ItemsAdded -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler(Content_ItemsAdded); Content.ItemsRemoved -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler(Content_ItemsRemoved); Content.CollectionReset -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler(Content_CollectionReset); DeregisterRunEvents(Content); } private void DeregisterRunEvents(IEnumerable runs) { foreach (IRun run in runs) run.Changed -= new EventHandler(run_Changed); } private void Content_CollectionReset(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs e) { DeregisterRunEvents(e.OldItems); RegisterRunEvents(e.Items); //RebuildInfluenceDataTable(); } private void Content_ItemsRemoved(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs e) { DeregisterRunEvents(e.Items); //RebuildInfluenceDataTable(); } private void Content_ItemsAdded(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs e) { RegisterRunEvents(e.Items); //RebuildInfluenceDataTable(); } private void run_Changed(object sender, EventArgs e) { if (InvokeRequired) this.Invoke(new EventHandler(run_Changed), sender, e); else { IRun run = (IRun)sender; UpdateRun(run); } } #endregion private void UpdateRun(IRun run) { //TODO: hacky di hack... this is baaaadddd RebuildCorrelationTable(); } private void UpdateResultComboBox() { resultComboBox.Items.Clear(); var results = (from run in Content from result in run.Results select result.Key).Distinct().ToArray(); resultComboBox.Items.AddRange(results); if (resultComboBox.Items.Count > 0) resultComboBox.SelectedItem = resultComboBox.Items[0]; } private List GetRowNames() { string resultName = (string)resultComboBox.SelectedItem; var runs = Content.Where(x => x.Results.ContainsKey(resultName) && x.Visible); List ret = new List(); foreach (var r in runs) { var ress = r.Results.Where(y => y.Value is DoubleValue).Select(z => z.Key); foreach (var s in ress) { if (!ret.Contains(s)) ret.Add(s); } } return ret; } private void RebuildCorrelationTable() { string resultName = (string)resultComboBox.SelectedItem; var columnNames = new string[2]; columnNames[0] = "Pearson product-moment correlation coefficient"; columnNames[1] = "Spearman's rank correlation coefficient"; var rowNames = GetRowNames(); var runs = Content.Where(x => x.Results.ContainsKey(resultName) && x.Visible); DoubleMatrix dt = new DoubleMatrix(rowNames.Count(), columnNames.Count()); dt.RowNames = rowNames; dt.ColumnNames = columnNames; int j = 0; foreach (var rowName in rowNames) { var resultRowVals = runs.Where(x => x.Results.ContainsKey(rowName)).Where(x => x.Results[rowName] is DoubleValue).Select(x => ((DoubleValue)x.Results[rowName]).Value); var resultVals = runs.Where(x => x.Results.ContainsKey(resultName)).Where(x => x.Results[resultName] is DoubleValue).Select(x => ((DoubleValue)x.Results[resultName]).Value); if (resultVals.Contains(double.NaN) || resultRowVals.Contains(double.NaN) || resultVals.Contains(double.NegativeInfinity) || resultVals.Contains(double.PositiveInfinity) || resultRowVals.Contains(double.NegativeInfinity) || resultRowVals.Contains(double.PositiveInfinity)) { dt[j, 0] = double.NaN; dt[j++, 1] = double.NaN; } else { dt[j, 0] = alglib.pearsoncorr2(resultVals.ToArray(), resultRowVals.ToArray()); dt[j++, 1] = alglib.spearmancorr2(resultVals.ToArray(), resultRowVals.ToArray()); } } stringConvertibleMatrixView.Content = dt; } private void calculateCorrelation_Click(object sender, EventArgs e) { RebuildCorrelationTable(); } } }