#region License Information
/* HeuristicLab
* Copyright (C) 2002-2016 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.Windows.Forms;
using HeuristicLab.Core.Views;
using HeuristicLab.Data;
using HeuristicLab.MainForm;
namespace HeuristicLab.DataPreprocessing.Views {
[View("Statistics View")]
[Content(typeof(StatisticsContent), true)]
public partial class StatisticsView : ItemView {
private StringMatrix statisticsMatrix;
private static readonly string[] RowNames = new[] {
"Type",
"Missing Values",
"Min",
"Max",
"Median",
"Average",
"std. Deviation",
"Variance",
"25th Percentile",
"75th Percentile",
"Most Common Value",
"Num. diff. Values"
};
public new StatisticsContent Content {
get { return (StatisticsContent)base.Content; }
set { base.Content = value; }
}
public StatisticsView() {
InitializeComponent();
}
protected override void OnContentChanged() {
base.OnContentChanged();
if (Content == null) {
txtRows.Text = "";
txtColumns.Text = "";
txtNumericColumns.Text = "";
txtNominalColumns.Text = "";
txtMissingValuesTotal.Text = "";
stringMatrixView.Content = null;
statisticsMatrix = null;
} else {
UpdateData();
}
}
protected override void RegisterContentEvents() {
base.RegisterContentEvents();
Content.Changed += Content_Changed;
}
protected override void DeregisterContentEvents() {
Content.Changed -= Content_Changed;
base.DeregisterContentEvents();
}
private void UpdateData() {
var logic = Content.StatisticsLogic;
var rowCount = logic.GetRowCount();
txtRows.Text = logic.GetColumnCount().ToString();
txtColumns.Text = rowCount.ToString();
txtNumericColumns.Text = logic.GetNumericColumnCount().ToString();
txtNominalColumns.Text = logic.GetNominalColumnCount().ToString();
txtMissingValuesTotal.Text = logic.GetMissingValueCount().ToString();
statisticsMatrix = new StringMatrix(RowNames.Length, Content.PreprocessingData.Columns) {
RowNames = StatisticsView.RowNames,
ColumnNames = Content.PreprocessingData.VariableNames
};
if (rowCount > 0) {
for (int i = 0; i < logic.GetColumnCount(); ++i) {
var data = GetList(i);
for (int j = 0; j < data.Count; j++) {
statisticsMatrix[j, i] = data[j];
}
}
}
stringMatrixView.Content = statisticsMatrix;
foreach (DataGridViewColumn column in stringMatrixView.DataGridView.Columns) {
var variable = column.HeaderText;
bool isInputTarget = Content.PreprocessingData.InputVariables.Contains(variable)
|| Content.PreprocessingData.TargetVariable == variable;
column.Visible = isInputTarget;
}
stringMatrixView.DataGridView.AutoResizeColumns();
//foreach (DataGridViewRow row in stringMatrixView.DataGridView.Rows) {
// var variable = (string)row.HeaderCell.Value;
// bool isInputTarget = Content.PreprocessingData.InputVariables.Contains(variable)
// || Content.PreprocessingData.TargetVariable == variable;
// row.Visible = isInputTarget;
//}
}
private List GetList(int i) {
List list;
var logic = Content.StatisticsLogic;
if (logic.VariableHasType(i)) {
list = GetDoubleColumns(i);
} else if (logic.VariableHasType(i)) {
list = GetStringColumns(i);
} else if (logic.VariableHasType(i)) {
list = GetDateTimeColumns(i);
} else {
list = new List();
for (int j = 0; j < RowNames.Length; ++j) {
list.Add("unknown column type");
}
}
return list;
}
private List GetDoubleColumns(int columnIndex) {
var logic = Content.StatisticsLogic;
return new List {
logic.GetColumnTypeAsString(columnIndex),
logic.GetMissingValueCount(columnIndex).ToString(),
logic.GetMin(columnIndex,double.NaN).ToString(),
logic.GetMax(columnIndex,double.NaN).ToString(),
logic.GetMedian(columnIndex).ToString(),
logic.GetAverage(columnIndex).ToString(),
logic.GetStandardDeviation(columnIndex).ToString(),
logic.GetVariance(columnIndex).ToString(),
logic.GetOneQuarterPercentile(columnIndex).ToString(),
logic.GetThreeQuarterPercentile(columnIndex).ToString(),
logic.GetMostCommonValue(columnIndex,double.NaN).ToString(),
logic.GetDifferentValuesCount(columnIndex).ToString()
};
}
private List GetStringColumns(int columnIndex) {
var logic = Content.StatisticsLogic;
return new List {
logic.GetColumnTypeAsString(columnIndex),
logic.GetMissingValueCount(columnIndex).ToString(),
"", //min
"", //max
"", //median
"", //average
"", //standard deviation
"", //variance
"", //quarter percentile
"", //three quarter percentile
logic.GetMostCommonValue(columnIndex,string.Empty) ?? "",
logic.GetDifferentValuesCount(columnIndex).ToString()
};
}
private List GetDateTimeColumns(int columnIndex) {
var logic = Content.StatisticsLogic;
return new List {
logic.GetColumnTypeAsString(columnIndex),
logic.GetMissingValueCount(columnIndex).ToString(),
logic.GetMin(columnIndex,DateTime.MinValue).ToString(),
logic.GetMax(columnIndex,DateTime.MinValue).ToString(),
logic.GetMedianDateTime(columnIndex).ToString(),
logic.GetAverageDateTime(columnIndex).ToString(),
logic.GetStandardDeviation(columnIndex).ToString(),
logic.GetVariance(columnIndex).ToString(), //variance
logic.GetOneQuarterPercentile(columnIndex).ToString(),
logic.GetThreeQuarterPercentile(columnIndex).ToString(),
logic.GetMostCommonValue(columnIndex,DateTime.MinValue).ToString(),
logic.GetDifferentValuesCount(columnIndex).ToString()
};
}
private void Content_Changed(object sender, DataPreprocessingChangedEventArgs e) {
UpdateData();
}
private void checkInputsTargetButton_Click(object sender, EventArgs e) {
foreach (DataGridViewColumn column in stringMatrixView.DataGridView.Columns) {
var variable = column.HeaderText;
bool isInputTarget = Content.PreprocessingData.InputVariables.Contains(variable)
|| Content.PreprocessingData.TargetVariable == variable;
column.Visible = isInputTarget;
}
}
private void checkAllButton_Click(object sender, EventArgs e) {
foreach (DataGridViewColumn column in stringMatrixView.DataGridView.Columns) {
column.Visible = true;
}
}
private void uncheckAllButton_Click(object sender, EventArgs e) {
foreach (DataGridViewColumn column in stringMatrixView.DataGridView.Columns) {
column.Visible = false;
}
}
}
}