Free cookie consent management tool by TermsFeed Policy Generator

source: branches/gteufl/HeuristicLab.Problems.DataAnalysis.Views/3.4/Clustering/ClusteringSolutionVisualizationView.cs @ 10331

Last change on this file since 10331 was 9456, checked in by swagner, 12 years ago

Updated copyright year and added some missing license headers (#1889)

File size: 4.7 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2013 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      foreach (var r in range) {
84        rows[classes[r].Item2].Points.Add(new Point2D<double>(reduced[idx, 0], reduced[idx, 1]));
85        idx++;
86      }
87
88      scatterPlot.Rows.AddRange(rows.Values);
89    }
90
91    private static double[,] PCAReduce(Dataset dataset, IEnumerable<int> rows, IEnumerable<string> variables) {
92      var instances = rows.ToArray();
93      var attributes = variables.ToArray();
94      var data = new double[instances.Length, attributes.Length + 1];
95
96      for (int j = 0; j < attributes.Length; j++) {
97        int i = 0;
98        var values = dataset.GetDoubleValues(attributes[j], instances);
99        foreach (var v in values) {
100          data[i++, j] = v;
101        }
102      }
103      int info;
104      double[] variances;
105      var matrix = new double[0, 0];
106      alglib.pcabuildbasis(data, instances.Length, attributes.Length, out info, out variances, out matrix);
107
108      var result = new double[instances.Length, 2];
109      int r = 0;
110      foreach (var inst in instances) {
111        int i = 0;
112        foreach (var attrib in attributes) {
113          double val = dataset.GetDoubleValue(attrib, inst);
114          for (int j = 0; j < result.GetLength(1); j++)
115            result[r, j] += val * matrix[i, j];
116          i++;
117        }
118        r++;
119      }
120
121      return result;
122    }
123
124    #region Event Handlers
125    private void rangeComboBox_SelectedIndexChanged(object sender, System.EventArgs e) {
126      if (Content != null) UpdateScatterPlot();
127    }
128    #endregion
129  }
130}
Note: See TracBrowser for help on using the repository browser.