#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.Linq;
using System.Windows.Forms;
using HeuristicLab.Common;
using HeuristicLab.MainForm;
using HeuristicLab.MainForm.WindowsForms;
namespace HeuristicLab.Problems.DataAnalysis.Views {
[View("Target Response Gradient View")]
[Content(typeof(IRegressionSolution))]
public partial class RegressionSolutionTargetResponseGradientView : DataAnalysisSolutionEvaluationView {
private Dictionary charts;
private const int Points = 1000;
public RegressionSolutionTargetResponseGradientView() {
InitializeComponent();
}
public new IRegressionSolution Content {
get { return (IRegressionSolution)base.Content; }
set {
if (value == null || value == Content) return;
base.Content = value;
}
}
protected override void RegisterContentEvents() {
base.RegisterContentEvents();
variableListView.ItemChecked += variableListView_ItemChecked;
}
protected override void DeregisterContentEvents() {
variableListView.ItemChecked -= variableListView_ItemChecked;
base.DeregisterContentEvents();
}
protected override void OnContentChanged() {
base.OnContentChanged();
if (Content == null) return;
var pd = Content.ProblemData;
var ds = pd.Dataset;
// create dataset
var variableNames = Content.GetUsedVariablesForPrediction().ToList();
var variableValues = variableNames.Select(x => new List { pd.Dataset.GetDoubleValues(x, pd.TrainingIndices).Median() });
var dataset = new ModifiableDataset(variableNames, variableValues);
// create charts
charts = new Dictionary();
foreach (var x in variableNames) {
double min = 0, max = 0;
foreach (var v in ds.GetDoubleValues(x, pd.TrainingIndices)) {
if (v > max) max = v;
if (v < min) min = v;
}
var gradientChart = new GradientChart {
Dock = DockStyle.Fill,
Margin = Padding.Empty,
ShowLegend = false,
ShowCursor = true,
ShowXAxisLabel = true,
ShowYAxisLabel = true
};
gradientChart.VariableValueChanged += (o, e) => {
foreach (var chart in gradientChartTableLayout.Controls.Cast()) {
if (chart == (GradientChart)o) continue;
chart.UpdateChart();
}
};
gradientChart.Configure(new[] { Content }, pd, dataset, x, min, max, Points);
charts[x] = gradientChart;
}
// update variable list
variableListView.Items.Clear();
variableListView.Items.AddRange(variableNames.Select(x => new ListViewItem(x, 0)).ToArray());
}
// sort chart controls so that they always appear in the same order as in the list view
// the gradient chart layout should be suspended before calling this method
private void SortControls() {
var tl = gradientChartTableLayout;
var indices = variableListView.CheckedItems.Cast().ToDictionary(checkedItem => checkedItem.Text, checkedItem => checkedItem.Index);
var count = tl.Controls.Count;
var controls = new GradientChart[count];
for (int i = 0; i < count; ++i) controls[i] = (GradientChart)tl.Controls[i];
Array.Sort(controls, (a, b) => indices[a.Variable].CompareTo(indices[b.Variable]));
tl.Controls.Clear();
tl.Controls.AddRange(controls);
}
private void variableListView_ItemChecked(object sender, ItemCheckedEventArgs e) {
if (charts == null) return;
var item = e.Item;
var variable = item.Text;
var chart = charts[variable];
var tl = gradientChartTableLayout;
tl.SuspendLayout();
var columnWidth = tl.GetColumnWidths()[0]; // all columns have the same width
var rowHeight = 0.8f * columnWidth;
tl.RowStyles.Clear();
if (item.Checked) {
tl.Controls.Add(chart);
chart.UpdateChart();
} else {
tl.Controls.Remove(chart);
}
var count = tl.Controls.Count;
SortControls();
tl.RowCount = count / tl.ColumnCount + (count % tl.ColumnCount == 0 ? 0 : 1);
for (int i = 0; i < count; ++i) {
var control = tl.Controls[i];
int rowIndex = i / tl.ColumnCount;
int columnIndex = i % tl.ColumnCount;
tl.SetRow(control, rowIndex);
tl.SetColumn(control, columnIndex);
}
for (int i = 0; i < tl.RowCount; ++i) {
tl.RowStyles.Add(new RowStyle(SizeType.Absolute, rowHeight));
}
tl.ResumeLayout();
}
}
}