Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Problems.DataAnalysis.Views/3.4/Clustering/ClusteringSolutionVisualizationView.cs @ 16124

Last change on this file since 16124 was 15584, checked in by swagner, 7 years ago

#2640: Updated year of copyrights in license headers on stable

File size: 4.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2018 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
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
22using System;
23using System.Collections.Generic;
24using System.Linq;
25using System.Windows.Forms;
26using HeuristicLab.Analysis;
27using HeuristicLab.Common;
28using HeuristicLab.MainForm;
29using HeuristicLab.MainForm.WindowsForms;
30
31namespace HeuristicLab.Problems.DataAnalysis.Views {
32  [View("Cluster Visualization")]
33  [Content(typeof(IClusteringSolution), IsDefaultView = false)]
34  public partial class ClusteringSolutionVisualizationView : DataAnalysisSolutionEvaluationView {
35    private ViewHost viewHost = new ViewHost();
36    private ScatterPlot scatterPlot = new ScatterPlot();
37
38    public new IClusteringSolution Content {
39      get { return (IClusteringSolution)base.Content; }
40      set { base.Content = value; }
41    }
42
43    public ClusteringSolutionVisualizationView() {
44      InitializeComponent();
45      viewHost.Dock = DockStyle.Fill;
46      splitContainer.Panel2.Controls.Add(viewHost);
47      rangeComboBox.SelectedIndex = 0;
48    }
49
50    protected override void OnContentChanged() {
51      base.OnContentChanged();
52      if (Content == null) {
53        viewHost.Content = null;
54        scatterPlot.Rows.Clear();
55      } else {
56        UpdateScatterPlot();
57        viewHost.Content = scatterPlot;
58      }
59    }
60
61    protected override void SetEnabledStateOfControls() {
62      base.SetEnabledStateOfControls();
63      rangeComboBox.Enabled = Content != null;
64    }
65
66    private void UpdateScatterPlot() {
67      scatterPlot.Rows.Clear();
68
69      IEnumerable<int> range = null;
70      if (rangeComboBox.SelectedIndex == 0) range = Content.ProblemData.TrainingIndices;
71      else if (rangeComboBox.SelectedIndex == 1) range = Content.ProblemData.TestIndices;
72      else range = Enumerable.Range(0, Content.ProblemData.Dataset.Rows);
73
74      IDictionary<int, Tuple<double, string>> classes = Content.Model.GetClusterValues(Content.ProblemData.Dataset, Enumerable.Range(0, Content.ProblemData.Dataset.Rows))
75          .Select((v, i) => new { Row = i, Cluster = (double)v })
76          .ToDictionary(x => x.Row, y => Tuple.Create(y.Cluster, "Cluster " + y.Cluster));
77
78      var rows = classes.Values.Select(x => x.Item2).Distinct().ToDictionary(c => c, c => new ScatterPlotDataRow(c, string.Empty, Enumerable.Empty<Point2D<double>>()));
79
80      var reduced = PCAReduce(Content.ProblemData.Dataset, range, Content.ProblemData.AllowedInputVariables);
81
82      int idx = 0;
83      var oneDim = reduced.GetLength(1) == 1;
84      foreach (var r in range) {
85        Point2D<double> point;
86        point = oneDim ? new Point2D<double>(reduced[idx, 0], 0.0) : new Point2D<double>(reduced[idx, 0], reduced[idx, 1]);
87        rows[classes[r].Item2].Points.Add(point);
88        idx++;
89      }
90
91      scatterPlot.Rows.AddRange(rows.Values);
92    }
93
94    private static double[,] PCAReduce(IDataset dataset, IEnumerable<int> rows, IEnumerable<string> variables) {
95      var instances = rows.ToArray();
96      var attributes = variables.ToArray();
97      var data = new double[instances.Length, attributes.Length + 1];
98
99      for (int j = 0; j < attributes.Length; j++) {
100        int i = 0;
101        var values = dataset.GetDoubleValues(attributes[j], instances);
102        foreach (var v in values) {
103          data[i++, j] = v;
104        }
105      }
106      int info;
107      double[] variances;
108      var matrix = new double[0, 0];
109      alglib.pcabuildbasis(data, instances.Length, attributes.Length, out info, out variances, out matrix);
110
111      var result = new double[instances.Length, matrix.GetLength(1)];
112      int r = 0;
113      foreach (var inst in instances) {
114        int i = 0;
115        foreach (var attrib in attributes) {
116          double val = dataset.GetDoubleValue(attrib, inst);
117          for (int j = 0; j < result.GetLength(1); j++)
118            result[r, j] += val * matrix[i, j];
119          i++;
120        }
121        r++;
122      }
123
124      return result;
125    }
126
127    #region Event Handlers
128    private void rangeComboBox_SelectedIndexChanged(object sender, System.EventArgs e) {
129      if (Content != null) UpdateScatterPlot();
130    }
131    #endregion
132  }
133}
Note: See TracBrowser for help on using the repository browser.