Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2521_ProblemRefactoring/HeuristicLab.Problems.DataAnalysis.Views/3.4/Classification/ClassificationSolutionComparisonView.cs @ 17442

Last change on this file since 17442 was 17226, checked in by mkommend, 5 years ago

#2521: Merged trunk changes into problem refactoring branch.

File size: 6.8 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 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.Algorithms.DataAnalysis;
27using HeuristicLab.MainForm;
28using HeuristicLab.Problems.DataAnalysis.OnlineCalculators;
29
30namespace HeuristicLab.Problems.DataAnalysis.Views.Classification {
31  [View("Solution Comparison")]
32  [Content(typeof(IClassificationSolution))]
33  public partial class ClassificationSolutionComparisonView : DataAnalysisSolutionEvaluationView {
34    private List<IClassificationSolution> solutions;
35
36    public ClassificationSolutionComparisonView() {
37      InitializeComponent();
38    }
39
40    public new IClassificationSolution Content {
41      get { return (IClassificationSolution)base.Content; }
42      set { base.Content = value; }
43    }
44
45    protected override void RegisterContentEvents() {
46      base.RegisterContentEvents();
47      Content.ModelChanged += new EventHandler(Content_ModelChanged);
48      Content.ProblemDataChanged += new EventHandler(Content_ProblemDataChanged);
49    }
50    protected override void DeregisterContentEvents() {
51      base.DeregisterContentEvents();
52      Content.ModelChanged -= new EventHandler(Content_ModelChanged);
53      Content.ProblemDataChanged -= new EventHandler(Content_ProblemDataChanged);
54    }
55
56    protected virtual void Content_ModelChanged(object sender, EventArgs e) {
57      if (InvokeRequired) Invoke((Action<object, EventArgs>)Content_ModelChanged, sender, e);
58      else UpdateDataGridView();
59    }
60    protected virtual void Content_ProblemDataChanged(object sender, EventArgs e) {
61      if (InvokeRequired) Invoke((Action<object, EventArgs>)Content_ProblemDataChanged, sender, e);
62      else {
63        UpdateDataGridView();
64      }
65    }
66    protected override void OnContentChanged() {
67      base.OnContentChanged();
68      UpdateDataGridView();
69    }
70
71    private void UpdateDataGridView() {
72      if (InvokeRequired) {
73        Invoke((Action)UpdateDataGridView);
74      } else {
75        if (Content == null) {
76          dataGridView.Rows.Clear();
77          dataGridView.Columns.Clear();
78          solutions = null;
79        } else {
80
81          IClassificationProblemData problemData = Content.ProblemData;
82          solutions = new List<IClassificationSolution>() { Content };
83          solutions.AddRange(GenerateClassificationSolutions().OrderBy(s=>s.Name));
84
85          dataGridView.ColumnCount = 4;
86          dataGridView.RowCount = solutions.Count();
87          dataGridView.Columns[0].HeaderText = "Accuracy (training)";
88          dataGridView.Columns[1].HeaderText = "Accuracy (test)";
89          dataGridView.Columns[2].HeaderText = "Matthews Correlation Coefficient (training)";
90          dataGridView.Columns[3].HeaderText = "Matthews Correlation Coefficient (test)";
91          if (problemData.Classes == 2) {
92            dataGridView.ColumnCount = 6;
93            dataGridView.Columns[4].HeaderText = "F1 Score (training)";
94            dataGridView.Columns[5].HeaderText = "F1 Score (test)";
95          }
96
97          for (int row = 0; row < solutions.Count; row++) {
98            var solution = solutions[row];
99
100            dataGridView.Rows[row].HeaderCell.Value = solution.Name;
101            dataGridView[0, row].Value = solution.TrainingAccuracy;
102            dataGridView[1, row].Value = solution.TestAccuracy;
103
104            var trainingIndizes = problemData.TrainingIndices;
105            var originalTrainingValues = problemData.Dataset.GetDoubleValues(problemData.TargetVariable, trainingIndizes);
106            var estimatedTrainingValues = solution.EstimatedTrainingClassValues;
107
108            var testIndices = problemData.TestIndices;
109            var originalTestValues = problemData.Dataset.GetDoubleValues(problemData.TargetVariable, testIndices);
110            var estimatedTestValues = solution.EstimatedTestClassValues;
111
112            OnlineCalculatorError errorState;
113            dataGridView[2, row].Value = MatthewsCorrelationCoefficientCalculator.Calculate(originalTrainingValues, estimatedTrainingValues, out errorState);
114            dataGridView[3, row].Value = MatthewsCorrelationCoefficientCalculator.Calculate(originalTestValues, estimatedTestValues, out errorState);
115            if (problemData.Classes == 2) {
116              dataGridView[4, row].Value = FOneScoreCalculator.Calculate(originalTrainingValues, estimatedTrainingValues, out errorState);
117              dataGridView[5, row].Value = FOneScoreCalculator.Calculate(originalTestValues, estimatedTestValues, out errorState);
118            }
119          }
120
121          dataGridView.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.ColumnHeader);
122          dataGridView.AutoResizeRowHeadersWidth(DataGridViewRowHeadersWidthSizeMode.AutoSizeToAllHeaders);
123        }
124      }
125    }
126
127    protected virtual IEnumerable<IClassificationSolution> GenerateClassificationSolutions() {
128      var problemData = Content.ProblemData;
129      var newSolutions = new List<IClassificationSolution>();
130      var zeroR = ZeroR.CreateZeroRSolution(problemData);
131      zeroR.Name = "ZeroR Classification Solution";
132      newSolutions.Add(zeroR);
133      try {
134        var oneR = OneR.CreateOneRSolution(problemData);
135        oneR.Name = "OneR Classification Solution (all variables)";
136        newSolutions.Add(oneR);
137      } catch (NotSupportedException) { } catch (ArgumentException) { }
138      try {
139        var lda = LinearDiscriminantAnalysis.CreateLinearDiscriminantAnalysisSolution(problemData);
140        lda.Name = "Linear Discriminant Analysis Solution (all variables)";
141        newSolutions.Add(lda);
142      } catch (NotSupportedException) { } catch (ArgumentException) { }
143      return newSolutions;
144    }
145
146    private void dataGridView_MouseDoubleClick(object sender, MouseEventArgs e) {
147      var hittestinfo = dataGridView.HitTest(e.X, e.Y);
148      if (hittestinfo.Type != DataGridViewHitTestType.RowHeader) { return; }
149      if (hittestinfo.RowIndex > solutions.Count) { return; }
150
151      MainFormManager.MainForm.ShowContent(solutions[hittestinfo.RowIndex]);
152    }
153  }
154}
Note: See TracBrowser for help on using the repository browser.