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