#region License Information
/* HeuristicLab
* Copyright (C) 2002-2013 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("Correlations")]
[Content(typeof(RunCollection), false)]
public sealed partial class CorrelationView : ItemView {
private enum ResultParameterType {
Result,
Parameter
}
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 */}
}
private Dictionary resultsParameters;
public CorrelationView() {
InitializeComponent();
stringConvertibleMatrixView.Minimum = -1.0;
stringConvertibleMatrixView.Maximum = 1.0;
}
protected override void OnContentChanged() {
base.OnContentChanged();
resultComboBox.Items.Clear();
if (Content != null) {
resultsParameters = GetRowNames();
UpdateResultComboBox();
}
UpdateCaption();
}
private void UpdateCaption() {
Caption = Content != null ? Content.OptimizerName + " Correlations" : ViewAttribute.GetViewName(GetType());
}
#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);
Content.UpdateOfRunsInProgressChanged += Content_UpdateOfRunsInProgressChanged;
}
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);
Content.UpdateOfRunsInProgressChanged -= Content_UpdateOfRunsInProgressChanged;
}
private void Content_CollectionReset(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs e) {
RebuildCorrelationTable();
}
private void Content_ItemsRemoved(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs e) {
RebuildCorrelationTable();
}
private void Content_ItemsAdded(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs e) {
RebuildCorrelationTable();
}
void Content_UpdateOfRunsInProgressChanged(object sender, EventArgs e) {
if (!Content.UpdateOfRunsInProgress) {
RebuildCorrelationTable();
}
}
#endregion
private void UpdateResultComboBox() {
resultComboBox.Items.Clear();
resultComboBox.Items.AddRange(resultsParameters.Keys.ToArray());
if (resultComboBox.Items.Count > 0) resultComboBox.SelectedItem = resultComboBox.Items[0];
}
private List GetResultRowNames() {
var results = (from run in Content
where run.Visible
from result in run.Results
where result.Value is DoubleValue || result.Value is IntValue
select result.Key).Distinct().ToList();
return results;
}
private List GetParameterRowNames() {
var parameters = (from run in Content
where run.Visible
from parameter in run.Parameters
where parameter.Value is DoubleValue || parameter.Value is IntValue
select parameter.Key).Distinct().ToList();
return parameters;
}
private Dictionary GetRowNames() {
Dictionary ret = new Dictionary();
var results = GetResultRowNames();
var parameters = GetParameterRowNames();
foreach (var r in results) {
ret.Add(r, ResultParameterType.Result);
}
foreach (var p in parameters) {
if (!ret.ContainsKey(p)) {
ret.Add(p, ResultParameterType.Parameter);
}
}
return ret;
}
private List GetDoublesFromResults(List runs, string key) {
List res = new List();
foreach (var r in runs) {
if (r.Results[key] is DoubleValue) {
res.Add(((DoubleValue)r.Results[key]).Value);
} else {
res.Add(((IntValue)r.Results[key]).Value);
}
}
return res;
}
private List GetDoublesFromParameters(List runs, string key) {
List res = new List();
foreach (var r in runs) {
if (r.Parameters[key] is DoubleValue) {
res.Add(((DoubleValue)r.Parameters[key]).Value);
} else {
res.Add(((IntValue)r.Parameters[key]).Value);
}
}
return res;
}
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 runs = Content.Where(x => x.Results.ContainsKey(resultName) && x.Visible);
DoubleMatrix dt = new DoubleMatrix(resultsParameters.Count(), columnNames.Count());
dt.RowNames = resultsParameters.Keys.ToArray();
dt.ColumnNames = columnNames;
int j = 0;
foreach (var rowName in resultsParameters) {
var resultVals = GetDoublesFromResults(runs.Where(x => x.Results.ContainsKey(resultName)).Where(x => x.Results[resultName] is DoubleValue || x.Results[resultName] is IntValue).ToList(), resultName);
List resultRowVals = new List();
if (rowName.Value == ResultParameterType.Result) {
resultRowVals = GetDoublesFromResults(runs.Where(x => x.Results.ContainsKey(rowName.Key)).Where(x => x.Results[rowName.Key] is DoubleValue || x.Results[rowName.Key] is IntValue).ToList(), rowName.Key);
} else {
resultRowVals = GetDoublesFromParameters(runs.Where(x => x.Parameters.ContainsKey(rowName.Key)).Where(x => x.Parameters[rowName.Key] is DoubleValue || x.Parameters[rowName.Key] is IntValue).ToList(), rowName.Key);
}
resultVals = resultVals.Where(x => !double.IsNaN(x) && !double.IsNegativeInfinity(x) && !double.IsPositiveInfinity(x)).ToList();
resultRowVals = resultRowVals.Where(x => !double.IsNaN(x) && !double.IsNegativeInfinity(x) && !double.IsPositiveInfinity(x)).ToList();
if (resultRowVals.Count == 0 || resultVals.Count == 0 || resultRowVals.Count != resultVals.Count) {
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());
}
}
dt.SortableView = true;
stringConvertibleMatrixView.Content = dt;
}
private void resultComboBox_SelectedIndexChanged(object sender, EventArgs e) {
RebuildCorrelationTable();
}
}
}