[9353] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[9706] | 3 | * Copyright (C) 2002-2013 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[9353] | 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 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.Collections.Generic;
|
---|
| 24 | using System.Linq;
|
---|
| 25 | using System.Windows.Forms;
|
---|
| 26 | using HeuristicLab.Core.Views;
|
---|
| 27 | using HeuristicLab.Data;
|
---|
| 28 | using HeuristicLab.MainForm;
|
---|
| 29 | using HeuristicLab.Optimization;
|
---|
| 30 |
|
---|
| 31 | namespace HeuristicLab.Analysis.Statistics {
|
---|
[9911] | 32 | [View("Correlations")]
|
---|
[9353] | 33 | [Content(typeof(RunCollection), false)]
|
---|
[9383] | 34 | public sealed partial class CorrelationView : ItemView {
|
---|
| 35 | private enum ResultParameterType {
|
---|
| 36 | Result,
|
---|
| 37 | Parameter
|
---|
[9353] | 38 | }
|
---|
| 39 |
|
---|
| 40 | public new RunCollection Content {
|
---|
| 41 | get { return (RunCollection)base.Content; }
|
---|
| 42 | set { base.Content = value; }
|
---|
| 43 | }
|
---|
| 44 |
|
---|
| 45 | public override bool ReadOnly {
|
---|
| 46 | get { return true; }
|
---|
| 47 | set { /*not needed because results are always readonly */}
|
---|
| 48 | }
|
---|
| 49 |
|
---|
[9382] | 50 | private Dictionary<string, ResultParameterType> resultsParameters;
|
---|
| 51 |
|
---|
[9383] | 52 | public CorrelationView() {
|
---|
| 53 | InitializeComponent();
|
---|
| 54 | stringConvertibleMatrixView.Minimum = -1.0;
|
---|
| 55 | stringConvertibleMatrixView.Maximum = 1.0;
|
---|
| 56 | }
|
---|
| 57 |
|
---|
[9353] | 58 | protected override void OnContentChanged() {
|
---|
| 59 | base.OnContentChanged();
|
---|
| 60 | resultComboBox.Items.Clear();
|
---|
| 61 |
|
---|
| 62 | if (Content != null) {
|
---|
[9382] | 63 | resultsParameters = GetRowNames();
|
---|
[9353] | 64 | UpdateResultComboBox();
|
---|
| 65 | }
|
---|
[9911] | 66 | UpdateCaption();
|
---|
[9353] | 67 | }
|
---|
| 68 |
|
---|
[9911] | 69 | private void UpdateCaption() {
|
---|
| 70 | Caption = Content != null ? Content.OptimizerName + " Correlations" : ViewAttribute.GetViewName(GetType());
|
---|
| 71 | }
|
---|
| 72 |
|
---|
[9353] | 73 | #region events
|
---|
| 74 | protected override void RegisterContentEvents() {
|
---|
| 75 | base.RegisterContentEvents();
|
---|
| 76 | Content.ItemsAdded += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_ItemsAdded);
|
---|
| 77 | Content.ItemsRemoved += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_ItemsRemoved);
|
---|
| 78 | Content.CollectionReset += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_CollectionReset);
|
---|
[9382] | 79 | Content.UpdateOfRunsInProgressChanged += Content_UpdateOfRunsInProgressChanged;
|
---|
[9353] | 80 | }
|
---|
| 81 |
|
---|
| 82 | protected override void DeregisterContentEvents() {
|
---|
| 83 | base.DeregisterContentEvents();
|
---|
| 84 | Content.ItemsAdded -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_ItemsAdded);
|
---|
| 85 | Content.ItemsRemoved -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_ItemsRemoved);
|
---|
| 86 | Content.CollectionReset -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_CollectionReset);
|
---|
[9382] | 87 | Content.UpdateOfRunsInProgressChanged -= Content_UpdateOfRunsInProgressChanged;
|
---|
[9353] | 88 | }
|
---|
| 89 |
|
---|
| 90 | private void Content_CollectionReset(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<IRun> e) {
|
---|
[9382] | 91 | RebuildCorrelationTable();
|
---|
[9353] | 92 | }
|
---|
| 93 |
|
---|
| 94 | private void Content_ItemsRemoved(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<IRun> e) {
|
---|
[9382] | 95 | RebuildCorrelationTable();
|
---|
[9353] | 96 | }
|
---|
| 97 |
|
---|
| 98 | private void Content_ItemsAdded(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<IRun> e) {
|
---|
[9382] | 99 | RebuildCorrelationTable();
|
---|
[9353] | 100 | }
|
---|
| 101 |
|
---|
[9382] | 102 | void Content_UpdateOfRunsInProgressChanged(object sender, EventArgs e) {
|
---|
| 103 | if (!Content.UpdateOfRunsInProgress) {
|
---|
| 104 | RebuildCorrelationTable();
|
---|
[9353] | 105 | }
|
---|
| 106 | }
|
---|
| 107 | #endregion
|
---|
| 108 |
|
---|
[9382] | 109 | private void UpdateResultComboBox() {
|
---|
| 110 | resultComboBox.Items.Clear();
|
---|
| 111 |
|
---|
| 112 | resultComboBox.Items.AddRange(resultsParameters.Keys.ToArray());
|
---|
| 113 | if (resultComboBox.Items.Count > 0) resultComboBox.SelectedItem = resultComboBox.Items[0];
|
---|
[9353] | 114 | }
|
---|
| 115 |
|
---|
[9382] | 116 | private List<string> GetResultRowNames() {
|
---|
[9353] | 117 | var results = (from run in Content
|
---|
[9382] | 118 | where run.Visible
|
---|
[9353] | 119 | from result in run.Results
|
---|
[9382] | 120 | where result.Value is DoubleValue || result.Value is IntValue
|
---|
| 121 | select result.Key).Distinct().ToList();
|
---|
[9353] | 122 |
|
---|
[9382] | 123 | return results;
|
---|
[9353] | 124 | }
|
---|
| 125 |
|
---|
[9382] | 126 | private List<string> GetParameterRowNames() {
|
---|
| 127 | var parameters = (from run in Content
|
---|
| 128 | where run.Visible
|
---|
| 129 | from parameter in run.Parameters
|
---|
| 130 | where parameter.Value is DoubleValue || parameter.Value is IntValue
|
---|
| 131 | select parameter.Key).Distinct().ToList();
|
---|
[9353] | 132 |
|
---|
[9382] | 133 | return parameters;
|
---|
| 134 | }
|
---|
[9353] | 135 |
|
---|
[9382] | 136 | private Dictionary<string, ResultParameterType> GetRowNames() {
|
---|
| 137 | Dictionary<string, ResultParameterType> ret = new Dictionary<string, ResultParameterType>();
|
---|
| 138 |
|
---|
| 139 | var results = GetResultRowNames();
|
---|
| 140 | var parameters = GetParameterRowNames();
|
---|
| 141 |
|
---|
| 142 | foreach (var r in results) {
|
---|
| 143 | ret.Add(r, ResultParameterType.Result);
|
---|
| 144 | }
|
---|
| 145 | foreach (var p in parameters) {
|
---|
| 146 | if (!ret.ContainsKey(p)) {
|
---|
| 147 | ret.Add(p, ResultParameterType.Parameter);
|
---|
[9353] | 148 | }
|
---|
| 149 | }
|
---|
| 150 |
|
---|
| 151 | return ret;
|
---|
| 152 | }
|
---|
| 153 |
|
---|
[9382] | 154 | private List<double> GetDoublesFromResults(List<IRun> runs, string key) {
|
---|
| 155 | List<double> res = new List<double>();
|
---|
| 156 |
|
---|
| 157 | foreach (var r in runs) {
|
---|
| 158 | if (r.Results[key] is DoubleValue) {
|
---|
| 159 | res.Add(((DoubleValue)r.Results[key]).Value);
|
---|
| 160 | } else {
|
---|
| 161 | res.Add(((IntValue)r.Results[key]).Value);
|
---|
| 162 | }
|
---|
| 163 | }
|
---|
| 164 | return res;
|
---|
| 165 | }
|
---|
| 166 |
|
---|
| 167 | private List<double> GetDoublesFromParameters(List<IRun> runs, string key) {
|
---|
| 168 | List<double> res = new List<double>();
|
---|
| 169 |
|
---|
| 170 | foreach (var r in runs) {
|
---|
| 171 | if (r.Parameters[key] is DoubleValue) {
|
---|
| 172 | res.Add(((DoubleValue)r.Parameters[key]).Value);
|
---|
| 173 | } else {
|
---|
| 174 | res.Add(((IntValue)r.Parameters[key]).Value);
|
---|
| 175 | }
|
---|
| 176 | }
|
---|
| 177 | return res;
|
---|
| 178 | }
|
---|
| 179 |
|
---|
[9353] | 180 | private void RebuildCorrelationTable() {
|
---|
| 181 | string resultName = (string)resultComboBox.SelectedItem;
|
---|
| 182 |
|
---|
| 183 | var columnNames = new string[2];
|
---|
| 184 | columnNames[0] = "Pearson product-moment correlation coefficient";
|
---|
| 185 | columnNames[1] = "Spearman's rank correlation coefficient";
|
---|
| 186 |
|
---|
| 187 | var runs = Content.Where(x => x.Results.ContainsKey(resultName) && x.Visible);
|
---|
| 188 |
|
---|
[9382] | 189 | DoubleMatrix dt = new DoubleMatrix(resultsParameters.Count(), columnNames.Count());
|
---|
| 190 | dt.RowNames = resultsParameters.Keys.ToArray();
|
---|
[9353] | 191 | dt.ColumnNames = columnNames;
|
---|
| 192 |
|
---|
| 193 | int j = 0;
|
---|
[9382] | 194 | foreach (var rowName in resultsParameters) {
|
---|
| 195 | var resultVals = GetDoublesFromResults(runs.Where(x => x.Results.ContainsKey(resultName)).Where(x => x.Results[resultName] is DoubleValue || x.Results[resultName] is IntValue).ToList(), resultName);
|
---|
[9353] | 196 |
|
---|
[9382] | 197 | List<double> resultRowVals = new List<double>();
|
---|
| 198 | if (rowName.Value == ResultParameterType.Result) {
|
---|
| 199 | 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);
|
---|
| 200 | } else {
|
---|
| 201 | 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);
|
---|
| 202 | }
|
---|
| 203 |
|
---|
[9936] | 204 | resultVals = resultVals.Where(x => !double.IsNaN(x) && !double.IsNegativeInfinity(x) && !double.IsPositiveInfinity(x)).ToList();
|
---|
| 205 | resultRowVals = resultRowVals.Where(x => !double.IsNaN(x) && !double.IsNegativeInfinity(x) && !double.IsPositiveInfinity(x)).ToList();
|
---|
| 206 |
|
---|
| 207 | if (resultRowVals.Count == 0 || resultVals.Count == 0 || resultRowVals.Count != resultVals.Count) {
|
---|
[9380] | 208 | dt[j, 0] = double.NaN;
|
---|
| 209 | dt[j++, 1] = double.NaN;
|
---|
[9353] | 210 | } else {
|
---|
[9380] | 211 | dt[j, 0] = alglib.pearsoncorr2(resultVals.ToArray(), resultRowVals.ToArray());
|
---|
| 212 | dt[j++, 1] = alglib.spearmancorr2(resultVals.ToArray(), resultRowVals.ToArray());
|
---|
[9353] | 213 | }
|
---|
| 214 | }
|
---|
| 215 |
|
---|
[9383] | 216 | dt.SortableView = true;
|
---|
[9353] | 217 | stringConvertibleMatrixView.Content = dt;
|
---|
| 218 | }
|
---|
[9912] | 219 |
|
---|
| 220 | private void resultComboBox_SelectedIndexChanged(object sender, EventArgs e) {
|
---|
| 221 | RebuildCorrelationTable();
|
---|
| 222 | }
|
---|
[9353] | 223 | }
|
---|
| 224 | }
|
---|