[9353] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[11375] | 3 | * Copyright (C) 2002-2014 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;
|
---|
[11378] | 25 | using HeuristicLab.Collections;
|
---|
| 26 | using HeuristicLab.Core;
|
---|
[9353] | 27 | using HeuristicLab.Core.Views;
|
---|
| 28 | using HeuristicLab.Data;
|
---|
| 29 | using HeuristicLab.MainForm;
|
---|
| 30 | using HeuristicLab.Optimization;
|
---|
| 31 |
|
---|
| 32 | namespace HeuristicLab.Analysis.Statistics {
|
---|
[9911] | 33 | [View("Correlations")]
|
---|
[9353] | 34 | [Content(typeof(RunCollection), false)]
|
---|
[9383] | 35 | public sealed partial class CorrelationView : ItemView {
|
---|
| 36 | private enum ResultParameterType {
|
---|
| 37 | Result,
|
---|
| 38 | Parameter
|
---|
[9353] | 39 | }
|
---|
| 40 |
|
---|
| 41 | public new RunCollection Content {
|
---|
| 42 | get { return (RunCollection)base.Content; }
|
---|
| 43 | set { base.Content = value; }
|
---|
| 44 | }
|
---|
| 45 |
|
---|
| 46 | public override bool ReadOnly {
|
---|
| 47 | get { return true; }
|
---|
| 48 | set { /*not needed because results are always readonly */}
|
---|
| 49 | }
|
---|
| 50 |
|
---|
[9382] | 51 | private Dictionary<string, ResultParameterType> resultsParameters;
|
---|
[11378] | 52 | private bool suppressUpdates = false;
|
---|
[9382] | 53 |
|
---|
[9383] | 54 | public CorrelationView() {
|
---|
| 55 | InitializeComponent();
|
---|
| 56 | stringConvertibleMatrixView.Minimum = -1.0;
|
---|
| 57 | stringConvertibleMatrixView.Maximum = 1.0;
|
---|
| 58 | }
|
---|
| 59 |
|
---|
[9353] | 60 | protected override void OnContentChanged() {
|
---|
| 61 | base.OnContentChanged();
|
---|
| 62 | resultComboBox.Items.Clear();
|
---|
| 63 |
|
---|
| 64 | if (Content != null) {
|
---|
| 65 | UpdateResultComboBox();
|
---|
| 66 | }
|
---|
[9911] | 67 | UpdateCaption();
|
---|
[9353] | 68 | }
|
---|
| 69 |
|
---|
[9911] | 70 | private void UpdateCaption() {
|
---|
| 71 | Caption = Content != null ? Content.OptimizerName + " Correlations" : ViewAttribute.GetViewName(GetType());
|
---|
| 72 | }
|
---|
| 73 |
|
---|
[9353] | 74 | #region events
|
---|
| 75 | protected override void RegisterContentEvents() {
|
---|
| 76 | base.RegisterContentEvents();
|
---|
| 77 | Content.ItemsAdded += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_ItemsAdded);
|
---|
| 78 | Content.ItemsRemoved += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_ItemsRemoved);
|
---|
| 79 | Content.CollectionReset += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_CollectionReset);
|
---|
[9382] | 80 | Content.UpdateOfRunsInProgressChanged += Content_UpdateOfRunsInProgressChanged;
|
---|
[11378] | 81 | RegisterRunEvents(Content);
|
---|
[9353] | 82 | }
|
---|
| 83 |
|
---|
| 84 | protected override void DeregisterContentEvents() {
|
---|
| 85 | base.DeregisterContentEvents();
|
---|
| 86 | Content.ItemsAdded -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_ItemsAdded);
|
---|
| 87 | Content.ItemsRemoved -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_ItemsRemoved);
|
---|
| 88 | Content.CollectionReset -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_CollectionReset);
|
---|
[9382] | 89 | Content.UpdateOfRunsInProgressChanged -= Content_UpdateOfRunsInProgressChanged;
|
---|
[11378] | 90 | DeregisterRunEvents(Content);
|
---|
[9353] | 91 | }
|
---|
| 92 |
|
---|
[11378] | 93 | private void RegisterRunEvents(IEnumerable<IRun> runs) {
|
---|
| 94 | foreach (IRun run in runs) {
|
---|
| 95 | RegisterRunResultsEvents(run);
|
---|
| 96 | RegisterRunParametersEvents(run);
|
---|
| 97 | run.PropertyChanged += run_PropertyChanged;
|
---|
| 98 | }
|
---|
| 99 | }
|
---|
| 100 |
|
---|
| 101 | private void DeregisterRunEvents(IEnumerable<IRun> runs) {
|
---|
| 102 | foreach (IRun run in runs) {
|
---|
| 103 | DeregisterRunResultsEvents(run);
|
---|
| 104 | DeregisterRunParametersEvents(run);
|
---|
| 105 | run.PropertyChanged -= run_PropertyChanged;
|
---|
| 106 | }
|
---|
| 107 | }
|
---|
| 108 |
|
---|
| 109 | private void RegisterRunResultsEvents(IRun run) {
|
---|
| 110 | IObservableDictionary<string, IItem> dict = run.Results;
|
---|
| 111 | dict.ItemsAdded += run_Changed;
|
---|
| 112 | dict.ItemsRemoved += run_Changed;
|
---|
| 113 | dict.ItemsReplaced += run_Changed;
|
---|
| 114 | dict.CollectionReset += run_Changed;
|
---|
| 115 | }
|
---|
| 116 |
|
---|
| 117 | private void DeregisterRunResultsEvents(IRun run) {
|
---|
| 118 | IObservableDictionary<string, IItem> dict = run.Results;
|
---|
| 119 | dict.ItemsAdded -= run_Changed;
|
---|
| 120 | dict.ItemsRemoved -= run_Changed;
|
---|
| 121 | dict.ItemsReplaced -= run_Changed;
|
---|
| 122 | dict.CollectionReset -= run_Changed;
|
---|
| 123 | }
|
---|
| 124 |
|
---|
| 125 | private void RegisterRunParametersEvents(IRun run) {
|
---|
| 126 | IObservableDictionary<string, IItem> dict = run.Parameters;
|
---|
| 127 | dict.ItemsAdded += run_Changed;
|
---|
| 128 | dict.ItemsRemoved += run_Changed;
|
---|
| 129 | dict.ItemsReplaced += run_Changed;
|
---|
| 130 | dict.CollectionReset += run_Changed;
|
---|
| 131 | }
|
---|
| 132 |
|
---|
| 133 | private void DeregisterRunParametersEvents(IRun run) {
|
---|
| 134 | IObservableDictionary<string, IItem> dict = run.Parameters;
|
---|
| 135 | dict.ItemsAdded -= run_Changed;
|
---|
| 136 | dict.ItemsRemoved -= run_Changed;
|
---|
| 137 | dict.ItemsReplaced -= run_Changed;
|
---|
| 138 | dict.CollectionReset -= run_Changed;
|
---|
| 139 | }
|
---|
| 140 |
|
---|
[9353] | 141 | private void Content_CollectionReset(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<IRun> e) {
|
---|
[11379] | 142 | DeregisterRunEvents(e.OldItems);
|
---|
| 143 | RegisterRunEvents(e.Items);
|
---|
[11378] | 144 | UpdateUI();
|
---|
[9353] | 145 | }
|
---|
| 146 |
|
---|
| 147 | private void Content_ItemsRemoved(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<IRun> e) {
|
---|
[11379] | 148 | DeregisterRunEvents(e.Items);
|
---|
[11378] | 149 | UpdateUI();
|
---|
[9353] | 150 | }
|
---|
| 151 |
|
---|
| 152 | private void Content_ItemsAdded(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<IRun> e) {
|
---|
[11379] | 153 | RegisterRunEvents(e.Items);
|
---|
[11378] | 154 | UpdateUI();
|
---|
[9353] | 155 | }
|
---|
| 156 |
|
---|
[11378] | 157 | private void Content_UpdateOfRunsInProgressChanged(object sender, EventArgs e) {
|
---|
| 158 | suppressUpdates = Content.UpdateOfRunsInProgress;
|
---|
| 159 | UpdateUI();
|
---|
| 160 | }
|
---|
| 161 |
|
---|
| 162 | private void run_Changed(object sender, EventArgs e) {
|
---|
| 163 | if (InvokeRequired) {
|
---|
| 164 | Invoke(new EventHandler(run_Changed), sender, e);
|
---|
[9353] | 165 | }
|
---|
[11378] | 166 | UpdateUI();
|
---|
[9353] | 167 | }
|
---|
[11378] | 168 |
|
---|
| 169 | void run_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e) {
|
---|
| 170 | UpdateUI();
|
---|
| 171 | }
|
---|
[9353] | 172 | #endregion
|
---|
| 173 |
|
---|
[11378] | 174 | private void UpdateUI() {
|
---|
| 175 | if (!suppressUpdates) {
|
---|
| 176 | UpdateResultComboBox();
|
---|
| 177 | RebuildCorrelationTable();
|
---|
| 178 | }
|
---|
| 179 | }
|
---|
| 180 |
|
---|
[9382] | 181 | private void UpdateResultComboBox() {
|
---|
[11378] | 182 | resultsParameters = GetRowNames();
|
---|
| 183 | string selectedResult = (string)this.resultComboBox.SelectedItem;
|
---|
[9382] | 184 | resultComboBox.Items.Clear();
|
---|
[11378] | 185 | resultComboBox.Items.AddRange(resultsParameters.Keys.ToArray());
|
---|
[9382] | 186 |
|
---|
[11378] | 187 | if (selectedResult != null && resultComboBox.Items.Contains(selectedResult)) {
|
---|
| 188 | resultComboBox.SelectedItem = selectedResult;
|
---|
| 189 | } else if (resultComboBox.Items.Count > 0) {
|
---|
| 190 | resultComboBox.SelectedItem = resultComboBox.Items[0];
|
---|
| 191 | }
|
---|
[9353] | 192 | }
|
---|
| 193 |
|
---|
[9382] | 194 | private List<string> GetResultRowNames() {
|
---|
[9353] | 195 | var results = (from run in Content
|
---|
[9382] | 196 | where run.Visible
|
---|
[9353] | 197 | from result in run.Results
|
---|
[9382] | 198 | where result.Value is DoubleValue || result.Value is IntValue
|
---|
| 199 | select result.Key).Distinct().ToList();
|
---|
[9353] | 200 |
|
---|
[9382] | 201 | return results;
|
---|
[9353] | 202 | }
|
---|
| 203 |
|
---|
[9382] | 204 | private List<string> GetParameterRowNames() {
|
---|
| 205 | var parameters = (from run in Content
|
---|
| 206 | where run.Visible
|
---|
| 207 | from parameter in run.Parameters
|
---|
| 208 | where parameter.Value is DoubleValue || parameter.Value is IntValue
|
---|
| 209 | select parameter.Key).Distinct().ToList();
|
---|
[9353] | 210 |
|
---|
[9382] | 211 | return parameters;
|
---|
| 212 | }
|
---|
[9353] | 213 |
|
---|
[9382] | 214 | private Dictionary<string, ResultParameterType> GetRowNames() {
|
---|
| 215 | Dictionary<string, ResultParameterType> ret = new Dictionary<string, ResultParameterType>();
|
---|
| 216 |
|
---|
| 217 | var results = GetResultRowNames();
|
---|
| 218 | var parameters = GetParameterRowNames();
|
---|
| 219 |
|
---|
| 220 | foreach (var r in results) {
|
---|
| 221 | ret.Add(r, ResultParameterType.Result);
|
---|
| 222 | }
|
---|
| 223 | foreach (var p in parameters) {
|
---|
| 224 | if (!ret.ContainsKey(p)) {
|
---|
| 225 | ret.Add(p, ResultParameterType.Parameter);
|
---|
[9353] | 226 | }
|
---|
| 227 | }
|
---|
| 228 |
|
---|
| 229 | return ret;
|
---|
| 230 | }
|
---|
| 231 |
|
---|
[9382] | 232 | private List<double> GetDoublesFromResults(List<IRun> runs, string key) {
|
---|
| 233 | List<double> res = new List<double>();
|
---|
| 234 |
|
---|
| 235 | foreach (var r in runs) {
|
---|
| 236 | if (r.Results[key] is DoubleValue) {
|
---|
| 237 | res.Add(((DoubleValue)r.Results[key]).Value);
|
---|
| 238 | } else {
|
---|
| 239 | res.Add(((IntValue)r.Results[key]).Value);
|
---|
| 240 | }
|
---|
| 241 | }
|
---|
| 242 | return res;
|
---|
| 243 | }
|
---|
| 244 |
|
---|
| 245 | private List<double> GetDoublesFromParameters(List<IRun> runs, string key) {
|
---|
| 246 | List<double> res = new List<double>();
|
---|
| 247 |
|
---|
| 248 | foreach (var r in runs) {
|
---|
| 249 | if (r.Parameters[key] is DoubleValue) {
|
---|
| 250 | res.Add(((DoubleValue)r.Parameters[key]).Value);
|
---|
| 251 | } else {
|
---|
| 252 | res.Add(((IntValue)r.Parameters[key]).Value);
|
---|
| 253 | }
|
---|
| 254 | }
|
---|
| 255 | return res;
|
---|
| 256 | }
|
---|
| 257 |
|
---|
[9353] | 258 | private void RebuildCorrelationTable() {
|
---|
| 259 | string resultName = (string)resultComboBox.SelectedItem;
|
---|
| 260 |
|
---|
| 261 | var columnNames = new string[2];
|
---|
| 262 | columnNames[0] = "Pearson product-moment correlation coefficient";
|
---|
| 263 | columnNames[1] = "Spearman's rank correlation coefficient";
|
---|
| 264 |
|
---|
| 265 | var runs = Content.Where(x => x.Results.ContainsKey(resultName) && x.Visible);
|
---|
| 266 |
|
---|
[9382] | 267 | DoubleMatrix dt = new DoubleMatrix(resultsParameters.Count(), columnNames.Count());
|
---|
| 268 | dt.RowNames = resultsParameters.Keys.ToArray();
|
---|
[9353] | 269 | dt.ColumnNames = columnNames;
|
---|
| 270 |
|
---|
| 271 | int j = 0;
|
---|
[9382] | 272 | foreach (var rowName in resultsParameters) {
|
---|
| 273 | 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] | 274 |
|
---|
[11378] | 275 | List<double> resultRowVals;
|
---|
[9382] | 276 | if (rowName.Value == ResultParameterType.Result) {
|
---|
| 277 | 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);
|
---|
| 278 | } else {
|
---|
| 279 | 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);
|
---|
| 280 | }
|
---|
| 281 |
|
---|
[9936] | 282 | resultVals = resultVals.Where(x => !double.IsNaN(x) && !double.IsNegativeInfinity(x) && !double.IsPositiveInfinity(x)).ToList();
|
---|
| 283 | resultRowVals = resultRowVals.Where(x => !double.IsNaN(x) && !double.IsNegativeInfinity(x) && !double.IsPositiveInfinity(x)).ToList();
|
---|
| 284 |
|
---|
| 285 | if (resultRowVals.Count == 0 || resultVals.Count == 0 || resultRowVals.Count != resultVals.Count) {
|
---|
[9380] | 286 | dt[j, 0] = double.NaN;
|
---|
| 287 | dt[j++, 1] = double.NaN;
|
---|
[9353] | 288 | } else {
|
---|
[9380] | 289 | dt[j, 0] = alglib.pearsoncorr2(resultVals.ToArray(), resultRowVals.ToArray());
|
---|
| 290 | dt[j++, 1] = alglib.spearmancorr2(resultVals.ToArray(), resultRowVals.ToArray());
|
---|
[9353] | 291 | }
|
---|
| 292 | }
|
---|
| 293 |
|
---|
[9383] | 294 | dt.SortableView = true;
|
---|
[9353] | 295 | stringConvertibleMatrixView.Content = dt;
|
---|
| 296 | }
|
---|
[9912] | 297 |
|
---|
| 298 | private void resultComboBox_SelectedIndexChanged(object sender, EventArgs e) {
|
---|
| 299 | RebuildCorrelationTable();
|
---|
| 300 | }
|
---|
[9353] | 301 | }
|
---|
| 302 | }
|
---|