1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2016 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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Drawing;
|
---|
25 | using System.Linq;
|
---|
26 | using System.Threading.Tasks;
|
---|
27 | using System.Windows.Forms;
|
---|
28 | using HeuristicLab.Common;
|
---|
29 | using HeuristicLab.MainForm;
|
---|
30 | using HeuristicLab.Visualization.ChartControlsExtensions;
|
---|
31 |
|
---|
32 | namespace HeuristicLab.Problems.DataAnalysis.Views {
|
---|
33 | [View("Target Response Gradients")]
|
---|
34 | [Content(typeof(IRegressionSolution))]
|
---|
35 | public partial class RegressionSolutionTargetResponseGradientView : DataAnalysisSolutionEvaluationView {
|
---|
36 | private readonly Dictionary<string, GradientChart> gradientCharts;
|
---|
37 | private readonly Dictionary<string, DensityChart> densityCharts;
|
---|
38 | private readonly Dictionary<string, Panel> groupingPanels;
|
---|
39 |
|
---|
40 | private const int Points = 200;
|
---|
41 | private int MaxColumns = 4;
|
---|
42 |
|
---|
43 | private IEnumerable<string> VisibleVariables {
|
---|
44 | get {
|
---|
45 | foreach (ListViewItem item in variableListView.CheckedItems)
|
---|
46 | yield return item.Text;
|
---|
47 | }
|
---|
48 | }
|
---|
49 | private IEnumerable<GradientChart> VisibleGradientCharts {
|
---|
50 | get { return VisibleVariables.Select(v => gradientCharts[v]); }
|
---|
51 | }
|
---|
52 | private IEnumerable<DensityChart> VisibleDensityCharts {
|
---|
53 | get { return VisibleVariables.Select(v => densityCharts[v]); }
|
---|
54 | }
|
---|
55 | private IEnumerable<Panel> VisibleChartsPanels {
|
---|
56 | get { return VisibleVariables.Select(v => groupingPanels[v]); }
|
---|
57 | }
|
---|
58 |
|
---|
59 | public RegressionSolutionTargetResponseGradientView() {
|
---|
60 | InitializeComponent();
|
---|
61 | gradientCharts = new Dictionary<string, GradientChart>();
|
---|
62 | densityCharts = new Dictionary<string, DensityChart>();
|
---|
63 | groupingPanels = new Dictionary<string, Panel>();
|
---|
64 |
|
---|
65 | limitView.Content = new DoubleLimit(0, 1);
|
---|
66 | limitView.Content.ValueChanged += limit_ValueChanged;
|
---|
67 |
|
---|
68 | densityComboBox.SelectedIndex = 1; // select Training
|
---|
69 |
|
---|
70 | // Avoid additional horizontal scrollbar
|
---|
71 | var vertScrollWidth = SystemInformation.VerticalScrollBarWidth;
|
---|
72 | scrollPanel.Padding = new Padding(0, 0, vertScrollWidth, 0);
|
---|
73 | scrollPanel.AutoScroll = true;
|
---|
74 | }
|
---|
75 |
|
---|
76 | public new IRegressionSolution Content {
|
---|
77 | get { return (IRegressionSolution)base.Content; }
|
---|
78 | set { base.Content = value; }
|
---|
79 | }
|
---|
80 |
|
---|
81 | protected override void RegisterContentEvents() {
|
---|
82 | base.RegisterContentEvents();
|
---|
83 | Content.ModelChanged += solution_ModelChanged;
|
---|
84 | }
|
---|
85 |
|
---|
86 | protected override void DeregisterContentEvents() {
|
---|
87 | Content.ModelChanged -= solution_ModelChanged;
|
---|
88 | base.DeregisterContentEvents();
|
---|
89 | }
|
---|
90 |
|
---|
91 | protected override void OnContentChanged() {
|
---|
92 | base.OnContentChanged();
|
---|
93 | if (Content == null) return;
|
---|
94 | var problemData = Content.ProblemData;
|
---|
95 |
|
---|
96 | // Init Y-axis range
|
---|
97 | double min = double.MaxValue, max = double.MinValue;
|
---|
98 | var trainingTarget = problemData.Dataset.GetDoubleValues(problemData.TargetVariable, problemData.TrainingIndices);
|
---|
99 | foreach (var t in trainingTarget) {
|
---|
100 | if (t < min) min = t;
|
---|
101 | if (t > max) max = t;
|
---|
102 | }
|
---|
103 | double range = max - min;
|
---|
104 | const double scale = 1.0 / 3.0;
|
---|
105 | double axisMin, axisMax, axisInterval;
|
---|
106 | ChartUtil.CalculateAxisInterval(min - scale * range, max + scale * range, 5, out axisMin, out axisMax, out axisInterval);
|
---|
107 | automaticYAxisCheckBox.Checked = false;
|
---|
108 | limitView.ReadOnly = false;
|
---|
109 | limitView.Content.Lower = axisMin;
|
---|
110 | limitView.Content.Upper = axisMax;
|
---|
111 |
|
---|
112 | // create dataset
|
---|
113 | var allowedInputVariables = Content.ProblemData.AllowedInputVariables;
|
---|
114 | var variableValues = allowedInputVariables.Select(x => new List<double> { problemData.Dataset.GetDoubleValues(x, problemData.TrainingIndices).Median() });
|
---|
115 | var sharedFixedVariables = new ModifiableDataset(allowedInputVariables, variableValues);
|
---|
116 |
|
---|
117 | // create controls
|
---|
118 | gradientCharts.Clear();
|
---|
119 | densityCharts.Clear();
|
---|
120 | groupingPanels.Clear();
|
---|
121 | foreach (var variableName in allowedInputVariables) {
|
---|
122 | var gradientChart = CreateGradientChart(variableName, sharedFixedVariables);
|
---|
123 | gradientCharts.Add(variableName, gradientChart);
|
---|
124 |
|
---|
125 | var densityChart = new DensityChart() {
|
---|
126 | Anchor = AnchorStyles.Left | AnchorStyles.Top | AnchorStyles.Right,
|
---|
127 | Margin = Padding.Empty,
|
---|
128 | Height = 12,
|
---|
129 | Visible = false
|
---|
130 | };
|
---|
131 | densityCharts.Add(variableName, densityChart);
|
---|
132 |
|
---|
133 | gradientChart.ZoomChanged += (o, e) => {
|
---|
134 | var gradient = (GradientChart)o;
|
---|
135 | var density = densityCharts[gradient.FreeVariable];
|
---|
136 | density.Visible = densityComboBox.SelectedIndex != 0 && !gradient.IsZoomed;
|
---|
137 | if (density.Visible)
|
---|
138 | UpdateDensityChart(density, gradient.FreeVariable);
|
---|
139 | };
|
---|
140 |
|
---|
141 | var panel = new Panel() {
|
---|
142 | Dock = DockStyle.Fill,
|
---|
143 | Margin = Padding.Empty,
|
---|
144 | BackColor = Color.White
|
---|
145 | };
|
---|
146 |
|
---|
147 | panel.Controls.Add(densityChart);
|
---|
148 | panel.Controls.Add(gradientChart);
|
---|
149 | groupingPanels.Add(variableName, panel);
|
---|
150 | }
|
---|
151 |
|
---|
152 | // update variable list
|
---|
153 | variableListView.ItemChecked -= variableListView_ItemChecked;
|
---|
154 | variableListView.Items.Clear();
|
---|
155 | foreach (var variable in allowedInputVariables)
|
---|
156 | variableListView.Items.Add(key: variable, text: variable, imageIndex: 0);
|
---|
157 |
|
---|
158 | foreach (var variable in Content.Model.VariablesUsedForPrediction)
|
---|
159 | variableListView.Items[variable].Checked = true;
|
---|
160 | variableListView.ItemChecked += variableListView_ItemChecked;
|
---|
161 |
|
---|
162 | RecalculateAndRelayoutCharts();
|
---|
163 | }
|
---|
164 |
|
---|
165 | private async void RecalculateAndRelayoutCharts() {
|
---|
166 | foreach (var variable in VisibleVariables) {
|
---|
167 | var gradientChart = gradientCharts[variable];
|
---|
168 | await gradientChart.RecalculateAsync();
|
---|
169 | }
|
---|
170 | gradientChartTableLayout.SuspendLayout();
|
---|
171 | SetupYAxis();
|
---|
172 | ReOrderControls();
|
---|
173 | SetStyles();
|
---|
174 | gradientChartTableLayout.ResumeLayout();
|
---|
175 | gradientChartTableLayout.Refresh();
|
---|
176 | foreach (var variable in VisibleVariables) {
|
---|
177 | var densityChart = densityCharts[variable];
|
---|
178 | UpdateDensityChart(densityChart, variable);
|
---|
179 | }
|
---|
180 | }
|
---|
181 | private GradientChart CreateGradientChart(string variableName, ModifiableDataset sharedFixedVariables) {
|
---|
182 | var gradientChart = new GradientChart {
|
---|
183 | Dock = DockStyle.Fill,
|
---|
184 | Margin = Padding.Empty,
|
---|
185 | ShowLegend = false,
|
---|
186 | ShowCursor = true,
|
---|
187 | ShowConfigButton = false,
|
---|
188 | YAxisTicks = 5,
|
---|
189 | };
|
---|
190 | gradientChart.VariableValueChanged += async (o, e) => {
|
---|
191 | var recalculations = VisibleGradientCharts.Except(new[] { (GradientChart)o }).Select(async chart => {
|
---|
192 | await chart.RecalculateAsync(updateOnFinish: false, resetYAxis: false);
|
---|
193 | }).ToList();
|
---|
194 | await Task.WhenAll(recalculations);
|
---|
195 |
|
---|
196 | if (recalculations.All(t => t.IsCompleted))
|
---|
197 | SetupYAxis();
|
---|
198 | };
|
---|
199 | gradientChart.Configure(new[] { Content }, sharedFixedVariables, variableName, Points);
|
---|
200 | gradientChart.SolutionAdded += gradientChart_SolutionAdded;
|
---|
201 | gradientChart.SolutionRemoved += gradientChart_SolutionRemoved;
|
---|
202 | return gradientChart;
|
---|
203 | }
|
---|
204 |
|
---|
205 | private void SetupYAxis() {
|
---|
206 | double axisMin, axisMax;
|
---|
207 | if (automaticYAxisCheckBox.Checked) {
|
---|
208 | double min = double.MaxValue, max = double.MinValue;
|
---|
209 | foreach (var chart in VisibleGradientCharts) {
|
---|
210 | if (chart.YMin < min) min = chart.YMin;
|
---|
211 | if (chart.YMax > max) max = chart.YMax;
|
---|
212 | }
|
---|
213 |
|
---|
214 | double axisInterval;
|
---|
215 | ChartUtil.CalculateAxisInterval(min, max, 5, out axisMin, out axisMax, out axisInterval);
|
---|
216 | } else {
|
---|
217 | axisMin = limitView.Content.Lower;
|
---|
218 | axisMax = limitView.Content.Upper;
|
---|
219 | }
|
---|
220 |
|
---|
221 | foreach (var chart in VisibleGradientCharts) {
|
---|
222 | chart.FixedYAxisMin = axisMin;
|
---|
223 | chart.FixedYAxisMax = axisMax;
|
---|
224 | }
|
---|
225 | }
|
---|
226 |
|
---|
227 | // reorder chart controls so that they always appear in the same order as in the list view
|
---|
228 | // the table layout containing the controls should be suspended before calling this method
|
---|
229 | private void ReOrderControls() {
|
---|
230 | var tl = gradientChartTableLayout;
|
---|
231 | tl.Controls.Clear();
|
---|
232 | int row = 0, column = 0;
|
---|
233 | foreach (var v in VisibleVariables) {
|
---|
234 | var chartsPanel = groupingPanels[v];
|
---|
235 | tl.Controls.Add(chartsPanel, column, row);
|
---|
236 |
|
---|
237 | var chart = gradientCharts[v];
|
---|
238 | chart.YAxisTitle = column == 0 ? Content.ProblemData.TargetVariable : string.Empty;
|
---|
239 | column++;
|
---|
240 |
|
---|
241 | if (column == MaxColumns) {
|
---|
242 | row++;
|
---|
243 | column = 0;
|
---|
244 | }
|
---|
245 | }
|
---|
246 | }
|
---|
247 |
|
---|
248 | private void SetStyles() {
|
---|
249 | var tl = gradientChartTableLayout;
|
---|
250 | tl.RowStyles.Clear();
|
---|
251 | tl.ColumnStyles.Clear();
|
---|
252 | int numVariables = VisibleVariables.Count();
|
---|
253 | if (numVariables == 0)
|
---|
254 | return;
|
---|
255 |
|
---|
256 | // set column styles
|
---|
257 | tl.ColumnCount = Math.Min(numVariables, MaxColumns);
|
---|
258 | for (int c = 0; c < tl.ColumnCount; c++)
|
---|
259 | tl.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 100.0f / tl.ColumnCount));
|
---|
260 |
|
---|
261 | // set row styles
|
---|
262 | tl.RowCount = (int)Math.Ceiling((double)numVariables / tl.ColumnCount);
|
---|
263 | var columnWidth = tl.Width / tl.ColumnCount; // assume all columns have the same width
|
---|
264 | var rowHeight = (int)(0.8 * columnWidth);
|
---|
265 | for (int r = 0; r < tl.RowCount; r++)
|
---|
266 | tl.RowStyles.Add(new RowStyle(SizeType.Absolute, rowHeight));
|
---|
267 | }
|
---|
268 |
|
---|
269 | private async void gradientChart_SolutionAdded(object sender, EventArgs<IRegressionSolution> e) {
|
---|
270 | var solution = e.Value;
|
---|
271 | foreach (var chart in gradientCharts.Values) {
|
---|
272 | if (sender == chart) continue;
|
---|
273 | await chart.AddSolutionAsync(solution);
|
---|
274 | }
|
---|
275 | }
|
---|
276 |
|
---|
277 | private async void gradientChart_SolutionRemoved(object sender, EventArgs<IRegressionSolution> e) {
|
---|
278 | var solution = e.Value;
|
---|
279 | foreach (var chart in gradientCharts.Values) {
|
---|
280 | if (sender == chart) continue;
|
---|
281 | await chart.RemoveSolutionAsync(solution);
|
---|
282 | }
|
---|
283 | }
|
---|
284 |
|
---|
285 | private async void variableListView_ItemChecked(object sender, ItemCheckedEventArgs e) {
|
---|
286 | var item = e.Item;
|
---|
287 | var variable = item.Text;
|
---|
288 | var gradientChart = gradientCharts[variable];
|
---|
289 | var chartsPanel = groupingPanels[variable];
|
---|
290 | var tl = gradientChartTableLayout;
|
---|
291 |
|
---|
292 | tl.SuspendLayout();
|
---|
293 | if (item.Checked) {
|
---|
294 | tl.Controls.Add(chartsPanel);
|
---|
295 | await gradientChart.RecalculateAsync();
|
---|
296 | } else {
|
---|
297 | tl.Controls.Remove(chartsPanel);
|
---|
298 | }
|
---|
299 |
|
---|
300 | if (tl.Controls.Count > 0) {
|
---|
301 | SetupYAxis();
|
---|
302 | ReOrderControls();
|
---|
303 | SetStyles();
|
---|
304 | }
|
---|
305 | tl.ResumeLayout();
|
---|
306 | tl.Refresh();
|
---|
307 | densityComboBox_SelectedIndexChanged(this, EventArgs.Empty);
|
---|
308 | }
|
---|
309 |
|
---|
310 | private void automaticYAxisCheckBox_CheckedChanged(object sender, EventArgs e) {
|
---|
311 | limitView.ReadOnly = automaticYAxisCheckBox.Checked;
|
---|
312 | SetupYAxis();
|
---|
313 | gradientChartTableLayout.Refresh();
|
---|
314 | densityComboBox_SelectedIndexChanged(this, EventArgs.Empty); // necessary to realign the density plots
|
---|
315 | }
|
---|
316 |
|
---|
317 | private void limit_ValueChanged(object sender, EventArgs e) {
|
---|
318 | if (automaticYAxisCheckBox.Checked)
|
---|
319 | return;
|
---|
320 | SetupYAxis();
|
---|
321 | gradientChartTableLayout.Refresh();
|
---|
322 | densityComboBox_SelectedIndexChanged(this, EventArgs.Empty); // necessary to realign the density plots
|
---|
323 | }
|
---|
324 |
|
---|
325 | private void densityComboBox_SelectedIndexChanged(object sender, EventArgs e) {
|
---|
326 | if (Content == null)
|
---|
327 | return;
|
---|
328 |
|
---|
329 | int si = densityComboBox.SelectedIndex;
|
---|
330 | if (si == 0) {
|
---|
331 | foreach (var densityChart in densityCharts.Values)
|
---|
332 | densityChart.Visible = false;
|
---|
333 | } else {
|
---|
334 | var indices = GetDensityIndices(si).ToList();
|
---|
335 |
|
---|
336 | foreach (var entry in densityCharts) {
|
---|
337 | var variableName = entry.Key;
|
---|
338 | var densityChart = entry.Value;
|
---|
339 | if (!VisibleVariables.Contains(variableName) || gradientCharts[variableName].IsZoomed)
|
---|
340 | continue;
|
---|
341 |
|
---|
342 | UpdateDensityChart(densityChart, variableName, indices);
|
---|
343 | }
|
---|
344 | }
|
---|
345 | }
|
---|
346 | private IEnumerable<int> GetDensityIndices(int selectedIndex) {
|
---|
347 | var problemData = Content.ProblemData;
|
---|
348 | return
|
---|
349 | selectedIndex == 1 ? problemData.TrainingIndices :
|
---|
350 | selectedIndex == 2 ? problemData.TestIndices :
|
---|
351 | problemData.AllIndices;
|
---|
352 | }
|
---|
353 | private void UpdateDensityChart(DensityChart densityChart, string variable, IList<int> indices = null) {
|
---|
354 | if (densityComboBox.SelectedIndex == 0)
|
---|
355 | return;
|
---|
356 | if (indices == null) {
|
---|
357 | indices = GetDensityIndices(densityComboBox.SelectedIndex).ToList();
|
---|
358 | }
|
---|
359 | var data = Content.ProblemData.Dataset.GetDoubleValues(variable, indices).ToList();
|
---|
360 | var gradientChart = gradientCharts[variable];
|
---|
361 | var min = gradientChart.FixedXAxisMin;
|
---|
362 | var max = gradientChart.FixedXAxisMax;
|
---|
363 | var buckets = gradientChart.DrawingSteps;
|
---|
364 | if (min.HasValue && max.HasValue) {
|
---|
365 | densityChart.UpdateChart(data, min.Value, max.Value, buckets);
|
---|
366 | densityChart.Width = gradientChart.Width;
|
---|
367 |
|
---|
368 | var gcPlotPosition = gradientChart.InnerPlotPosition;
|
---|
369 | densityChart.Left = (int)(gcPlotPosition.X / 100.0 * gradientChart.Width);
|
---|
370 | densityChart.Width = (int)(gcPlotPosition.Width / 100.0 * gradientChart.Width);
|
---|
371 |
|
---|
372 | densityChart.Visible = true;
|
---|
373 | }
|
---|
374 | }
|
---|
375 |
|
---|
376 | private void columnsNumericUpDown_ValueChanged(object sender, EventArgs e) {
|
---|
377 | MaxColumns = (int)columnsNumericUpDown.Value;
|
---|
378 | int columns = Math.Min(VisibleVariables.Count(), MaxColumns);
|
---|
379 | if (columns > 0) {
|
---|
380 | var tl = gradientChartTableLayout;
|
---|
381 | MaxColumns = columns;
|
---|
382 | tl.SuspendLayout();
|
---|
383 | ReOrderControls();
|
---|
384 | SetStyles();
|
---|
385 | tl.ResumeLayout();
|
---|
386 | tl.Refresh();
|
---|
387 | densityComboBox_SelectedIndexChanged(this, EventArgs.Empty);
|
---|
388 | }
|
---|
389 | }
|
---|
390 |
|
---|
391 | private async void solution_ModelChanged(object sender, EventArgs e) {
|
---|
392 | foreach (var variable in VisibleVariables) {
|
---|
393 | var gradientChart = gradientCharts[variable];
|
---|
394 | var densityChart = densityCharts[variable];
|
---|
395 | // recalculate and refresh
|
---|
396 | await gradientChart.RecalculateAsync();
|
---|
397 | gradientChart.Refresh();
|
---|
398 | UpdateDensityChart(densityChart, variable);
|
---|
399 | }
|
---|
400 | }
|
---|
401 | }
|
---|
402 | }
|
---|