Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2839_HiveProjectManagement/HeuristicLab.Problems.DataAnalysis.Views/3.4/Classification/ClassificationSolutionComparisonView.cs @ 17530

Last change on this file since 17530 was 16057, checked in by jkarder, 6 years ago

#2839:

File size: 6.8 KB
RevLine 
[9119]1#region License Information
2/* HeuristicLab
[16057]3 * Copyright (C) 2002-2018 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[9119]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 {
[13102]31  [View("Solution Comparison")]
[9119]32  [Content(typeof(IClassificationSolution))]
33  public partial class ClassificationSolutionComparisonView : DataAnalysisSolutionEvaluationView {
[9135]34    private List<IClassificationSolution> solutions;
35
[9119]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();
[13104]78          solutions = null;
[9119]79        } else {
80
81          IClassificationProblemData problemData = Content.ProblemData;
[9135]82          solutions = new List<IClassificationSolution>() { Content };
[14826]83          solutions.AddRange(GenerateClassificationSolutions().OrderBy(s=>s.Name));
[9119]84
85          dataGridView.ColumnCount = 4;
86          dataGridView.RowCount = solutions.Count();
[13089]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)";
[9119]91          if (problemData.Classes == 2) {
92            dataGridView.ColumnCount = 6;
[13089]93            dataGridView.Columns[4].HeaderText = "F1 Score (training)";
94            dataGridView.Columns[5].HeaderText = "F1 Score (test)";
[9119]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);
[14826]106            var estimatedTrainingValues = solution.EstimatedTrainingClassValues;
[9119]107
[10560]108            var testIndices = problemData.TestIndices;
[9119]109            var originalTestValues = problemData.Dataset.GetDoubleValues(problemData.TargetVariable, testIndices);
[14826]110            var estimatedTestValues = solution.EstimatedTestClassValues;
[9119]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
[14826]127    protected virtual IEnumerable<IClassificationSolution> GenerateClassificationSolutions() {
128      var problemData = Content.ProblemData;
[10560]129      var newSolutions = new List<IClassificationSolution>();
[9119]130      var zeroR = ZeroR.CreateZeroRSolution(problemData);
[13089]131      zeroR.Name = "ZeroR Classification Solution";
[10560]132      newSolutions.Add(zeroR);
[9135]133      try {
[14826]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 {
[9135]139        var lda = LinearDiscriminantAnalysis.CreateLinearDiscriminantAnalysisSolution(problemData);
[14826]140        lda.Name = "Linear Discriminant Analysis Solution (all variables)";
[10560]141        newSolutions.Add(lda);
[13104]142      } catch (NotSupportedException) { } catch (ArgumentException) { }
[10560]143      return newSolutions;
[9119]144    }
[9135]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    }
[9119]153  }
154}
Note: See TracBrowser for help on using the repository browser.