[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) {
|
---|
[11378] | 142 | UpdateUI();
|
---|
[9353] | 143 | }
|
---|
| 144 |
|
---|
| 145 | private void Content_ItemsRemoved(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<IRun> e) {
|
---|
[11378] | 146 | UpdateUI();
|
---|
[9353] | 147 | }
|
---|
| 148 |
|
---|
| 149 | private void Content_ItemsAdded(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<IRun> e) {
|
---|
[11378] | 150 | UpdateUI();
|
---|
[9353] | 151 | }
|
---|
| 152 |
|
---|
[11378] | 153 | private void Content_UpdateOfRunsInProgressChanged(object sender, EventArgs e) {
|
---|
| 154 | suppressUpdates = Content.UpdateOfRunsInProgress;
|
---|
| 155 | UpdateUI();
|
---|
| 156 | }
|
---|
| 157 |
|
---|
| 158 | private void run_Changed(object sender, EventArgs e) {
|
---|
| 159 | if (InvokeRequired) {
|
---|
| 160 | Invoke(new EventHandler(run_Changed), sender, e);
|
---|
[9353] | 161 | }
|
---|
[11378] | 162 | UpdateUI();
|
---|
[9353] | 163 | }
|
---|
[11378] | 164 |
|
---|
| 165 | void run_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e) {
|
---|
| 166 | UpdateUI();
|
---|
| 167 | }
|
---|
[9353] | 168 | #endregion
|
---|
| 169 |
|
---|
[11378] | 170 | private void UpdateUI() {
|
---|
| 171 | if (!suppressUpdates) {
|
---|
| 172 | UpdateResultComboBox();
|
---|
| 173 | RebuildCorrelationTable();
|
---|
| 174 | }
|
---|
| 175 | }
|
---|
| 176 |
|
---|
[9382] | 177 | private void UpdateResultComboBox() {
|
---|
[11378] | 178 | resultsParameters = GetRowNames();
|
---|
| 179 | string selectedResult = (string)this.resultComboBox.SelectedItem;
|
---|
[9382] | 180 | resultComboBox.Items.Clear();
|
---|
[11378] | 181 | resultComboBox.Items.AddRange(resultsParameters.Keys.ToArray());
|
---|
[9382] | 182 |
|
---|
[11378] | 183 | if (selectedResult != null && resultComboBox.Items.Contains(selectedResult)) {
|
---|
| 184 | resultComboBox.SelectedItem = selectedResult;
|
---|
| 185 | } else if (resultComboBox.Items.Count > 0) {
|
---|
| 186 | resultComboBox.SelectedItem = resultComboBox.Items[0];
|
---|
| 187 | }
|
---|
[9353] | 188 | }
|
---|
| 189 |
|
---|
[9382] | 190 | private List<string> GetResultRowNames() {
|
---|
[9353] | 191 | var results = (from run in Content
|
---|
[9382] | 192 | where run.Visible
|
---|
[9353] | 193 | from result in run.Results
|
---|
[9382] | 194 | where result.Value is DoubleValue || result.Value is IntValue
|
---|
| 195 | select result.Key).Distinct().ToList();
|
---|
[9353] | 196 |
|
---|
[9382] | 197 | return results;
|
---|
[9353] | 198 | }
|
---|
| 199 |
|
---|
[9382] | 200 | private List<string> GetParameterRowNames() {
|
---|
| 201 | var parameters = (from run in Content
|
---|
| 202 | where run.Visible
|
---|
| 203 | from parameter in run.Parameters
|
---|
| 204 | where parameter.Value is DoubleValue || parameter.Value is IntValue
|
---|
| 205 | select parameter.Key).Distinct().ToList();
|
---|
[9353] | 206 |
|
---|
[9382] | 207 | return parameters;
|
---|
| 208 | }
|
---|
[9353] | 209 |
|
---|
[9382] | 210 | private Dictionary<string, ResultParameterType> GetRowNames() {
|
---|
| 211 | Dictionary<string, ResultParameterType> ret = new Dictionary<string, ResultParameterType>();
|
---|
| 212 |
|
---|
| 213 | var results = GetResultRowNames();
|
---|
| 214 | var parameters = GetParameterRowNames();
|
---|
| 215 |
|
---|
| 216 | foreach (var r in results) {
|
---|
| 217 | ret.Add(r, ResultParameterType.Result);
|
---|
| 218 | }
|
---|
| 219 | foreach (var p in parameters) {
|
---|
| 220 | if (!ret.ContainsKey(p)) {
|
---|
| 221 | ret.Add(p, ResultParameterType.Parameter);
|
---|
[9353] | 222 | }
|
---|
| 223 | }
|
---|
| 224 |
|
---|
| 225 | return ret;
|
---|
| 226 | }
|
---|
| 227 |
|
---|
[9382] | 228 | private List<double> GetDoublesFromResults(List<IRun> runs, string key) {
|
---|
| 229 | List<double> res = new List<double>();
|
---|
| 230 |
|
---|
| 231 | foreach (var r in runs) {
|
---|
| 232 | if (r.Results[key] is DoubleValue) {
|
---|
| 233 | res.Add(((DoubleValue)r.Results[key]).Value);
|
---|
| 234 | } else {
|
---|
| 235 | res.Add(((IntValue)r.Results[key]).Value);
|
---|
| 236 | }
|
---|
| 237 | }
|
---|
| 238 | return res;
|
---|
| 239 | }
|
---|
| 240 |
|
---|
| 241 | private List<double> GetDoublesFromParameters(List<IRun> runs, string key) {
|
---|
| 242 | List<double> res = new List<double>();
|
---|
| 243 |
|
---|
| 244 | foreach (var r in runs) {
|
---|
| 245 | if (r.Parameters[key] is DoubleValue) {
|
---|
| 246 | res.Add(((DoubleValue)r.Parameters[key]).Value);
|
---|
| 247 | } else {
|
---|
| 248 | res.Add(((IntValue)r.Parameters[key]).Value);
|
---|
| 249 | }
|
---|
| 250 | }
|
---|
| 251 | return res;
|
---|
| 252 | }
|
---|
| 253 |
|
---|
[9353] | 254 | private void RebuildCorrelationTable() {
|
---|
| 255 | string resultName = (string)resultComboBox.SelectedItem;
|
---|
| 256 |
|
---|
| 257 | var columnNames = new string[2];
|
---|
| 258 | columnNames[0] = "Pearson product-moment correlation coefficient";
|
---|
| 259 | columnNames[1] = "Spearman's rank correlation coefficient";
|
---|
| 260 |
|
---|
| 261 | var runs = Content.Where(x => x.Results.ContainsKey(resultName) && x.Visible);
|
---|
| 262 |
|
---|
[9382] | 263 | DoubleMatrix dt = new DoubleMatrix(resultsParameters.Count(), columnNames.Count());
|
---|
| 264 | dt.RowNames = resultsParameters.Keys.ToArray();
|
---|
[9353] | 265 | dt.ColumnNames = columnNames;
|
---|
| 266 |
|
---|
| 267 | int j = 0;
|
---|
[9382] | 268 | foreach (var rowName in resultsParameters) {
|
---|
| 269 | 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] | 270 |
|
---|
[11378] | 271 | List<double> resultRowVals;
|
---|
[9382] | 272 | if (rowName.Value == ResultParameterType.Result) {
|
---|
| 273 | 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);
|
---|
| 274 | } else {
|
---|
| 275 | 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);
|
---|
| 276 | }
|
---|
| 277 |
|
---|
[9936] | 278 | resultVals = resultVals.Where(x => !double.IsNaN(x) && !double.IsNegativeInfinity(x) && !double.IsPositiveInfinity(x)).ToList();
|
---|
| 279 | resultRowVals = resultRowVals.Where(x => !double.IsNaN(x) && !double.IsNegativeInfinity(x) && !double.IsPositiveInfinity(x)).ToList();
|
---|
| 280 |
|
---|
| 281 | if (resultRowVals.Count == 0 || resultVals.Count == 0 || resultRowVals.Count != resultVals.Count) {
|
---|
[9380] | 282 | dt[j, 0] = double.NaN;
|
---|
| 283 | dt[j++, 1] = double.NaN;
|
---|
[9353] | 284 | } else {
|
---|
[9380] | 285 | dt[j, 0] = alglib.pearsoncorr2(resultVals.ToArray(), resultRowVals.ToArray());
|
---|
| 286 | dt[j++, 1] = alglib.spearmancorr2(resultVals.ToArray(), resultRowVals.ToArray());
|
---|
[9353] | 287 | }
|
---|
| 288 | }
|
---|
| 289 |
|
---|
[9383] | 290 | dt.SortableView = true;
|
---|
[9353] | 291 | stringConvertibleMatrixView.Content = dt;
|
---|
| 292 | }
|
---|
[9912] | 293 |
|
---|
| 294 | private void resultComboBox_SelectedIndexChanged(object sender, EventArgs e) {
|
---|
| 295 | RebuildCorrelationTable();
|
---|
| 296 | }
|
---|
[9353] | 297 | }
|
---|
| 298 | }
|
---|