#region License Information
/* HeuristicLab
* Copyright (C) 2002-2011 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
*
* This file is part of HeuristicLab.
*
* HeuristicLab is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* HeuristicLab is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with HeuristicLab. If not, see .
*/
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using HeuristicLab.MainForm;
using HeuristicLab.MainForm.WindowsForms;
namespace HeuristicLab.Problems.DataAnalysis.Classification.Views {
[View("Classification solution confusion matrix view")]
[Content(typeof(IClassificationSolution))]
public partial class ClassificationSolutionConfusionMatrixView : AsynchronousContentView {
private const string TrainingSamples = "Training";
private const string TestSamples = "Test";
public ClassificationSolutionConfusionMatrixView() {
InitializeComponent();
cmbSamples.Items.Add(TrainingSamples);
cmbSamples.Items.Add(TestSamples);
cmbSamples.SelectedIndex = 0;
}
public new IClassificationSolution Content {
get { return (IClassificationSolution)base.Content; }
set { base.Content = value; }
}
protected override void RegisterContentEvents() {
base.RegisterContentEvents();
Content.ModelChanged += new EventHandler(Content_ModelChanged);
Content.ProblemDataChanged += new EventHandler(Content_ProblemDataChanged);
}
protected override void DeregisterContentEvents() {
base.DeregisterContentEvents();
Content.ModelChanged -= new EventHandler(Content_ModelChanged);
Content.ProblemDataChanged -= new EventHandler(Content_ProblemDataChanged);
}
private void Content_ModelChanged(object sender, EventArgs e) {
FillDataGridView();
}
private void Content_ProblemDataChanged(object sender, EventArgs e) {
UpdateDataGridView();
}
protected override void OnContentChanged() {
base.OnContentChanged();
UpdateDataGridView();
}
private void UpdateDataGridView() {
if (InvokeRequired) Invoke((Action)UpdateDataGridView);
else {
if (Content == null) {
dataGridView.RowCount = 1;
dataGridView.ColumnCount = 1;
} else {
dataGridView.ColumnCount = Content.ProblemData.Classes;
dataGridView.RowCount = Content.ProblemData.Classes;
int i = 0;
foreach (string headerText in Content.ProblemData.ClassNames) {
dataGridView.Columns[i].HeaderText = "Actual " + headerText;
dataGridView.Rows[i].HeaderCell.Value = "Predicted " + headerText;
i++;
}
dataGridView.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.ColumnHeader);
dataGridView.AutoResizeRowHeadersWidth(DataGridViewRowHeadersWidthSizeMode.AutoSizeToAllHeaders);
FillDataGridView();
}
}
}
private void FillDataGridView() {
if (InvokeRequired) Invoke((Action)FillDataGridView);
else {
if (Content == null) return;
double[,] confusionMatrix = new double[Content.ProblemData.Classes, Content.ProblemData.Classes];
IEnumerable rows;
if (cmbSamples.SelectedItem.ToString() == TrainingSamples) {
rows = Content.ProblemData.TrainingIndizes;
} else if (cmbSamples.SelectedItem.ToString() == TestSamples) {
rows = Content.ProblemData.TestIndizes;
} else throw new InvalidOperationException();
Dictionary classValueIndexMapping = new Dictionary();
int index = 0;
foreach (double classValue in Content.ProblemData.ClassValues.OrderBy(x => x)) {
classValueIndexMapping.Add(classValue, index);
index++;
}
double[] targetValues = Content.ProblemData.Dataset.GetEnumeratedVariableValues(Content.ProblemData.TargetVariable, rows).ToArray();
double[] predictedValues = Content.GetEstimatedClassValues(rows).ToArray();
for (int i = 0; i < targetValues.Length; i++) {
double targetValue = targetValues[i];
double predictedValue = predictedValues[i];
int targetIndex = classValueIndexMapping[targetValue];
int predictedIndex = classValueIndexMapping[predictedValue];
confusionMatrix[predictedIndex, targetIndex] += 1;
}
for (int row = 0; row < confusionMatrix.GetLength(0); row++) {
for (int col = 0; col < confusionMatrix.GetLength(1); col++) {
//TODO add scaling to relative values;
dataGridView[col, row].Value = confusionMatrix[row, col];
}
}
}
}
private void cmbSamples_SelectedIndexChanged(object sender, System.EventArgs e) {
FillDataGridView();
}
}
}