Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.RegressionSolutionGradientView/HeuristicLab.Problems.DataAnalysis.Views/3.4/RegressionSolutionTargetResponseGradientView.cs @ 13828

Last change on this file since 13828 was 13828, checked in by bburlacu, 9 years ago

#2597: Simplify code and ensure charts are sorted according to the variable order in the list view.

File size: 5.5 KB
Line 
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
22using System;
23using System.Collections.Generic;
24using System.Linq;
25using System.Windows.Forms;
26using HeuristicLab.Common;
27using HeuristicLab.MainForm;
28using HeuristicLab.MainForm.WindowsForms;
29
30namespace HeuristicLab.Problems.DataAnalysis.Views {
31  [View("Target Response Gradient View")]
32  [Content(typeof(IRegressionSolution))]
33  public partial class RegressionSolutionTargetResponseGradientView : DataAnalysisSolutionEvaluationView {
34    private Dictionary<string, GradientChart> charts;
35    private const int Points = 1000;
36
37    public RegressionSolutionTargetResponseGradientView() {
38      InitializeComponent();
39    }
40
41    public new IRegressionSolution Content {
42      get { return (IRegressionSolution)base.Content; }
43      set {
44        if (value == null || value == Content) return;
45        base.Content = value;
46      }
47    }
48
49    protected override void RegisterContentEvents() {
50      base.RegisterContentEvents();
51      variableListView.ItemChecked += variableListView_ItemChecked;
52    }
53
54    protected override void DeregisterContentEvents() {
55      variableListView.ItemChecked -= variableListView_ItemChecked;
56      base.DeregisterContentEvents();
57    }
58
59    protected override void OnContentChanged() {
60      base.OnContentChanged();
61      if (Content == null) return;
62      var pd = Content.ProblemData;
63      var ds = pd.Dataset;
64      // create dataset
65      var variableNames = Content.GetUsedVariablesForPrediction().ToList();
66      var variableValues = variableNames.Select(x => new List<double> { pd.Dataset.GetDoubleValues(x, pd.TrainingIndices).Median() });
67      var dataset = new ModifiableDataset(variableNames, variableValues);
68      // create charts
69      charts = new Dictionary<string, GradientChart>();
70      foreach (var x in variableNames) {
71        double min = 0, max = 0;
72        foreach (var v in ds.GetDoubleValues(x, pd.TrainingIndices)) {
73          if (v > max) max = v;
74          if (v < min) min = v;
75        }
76        var gradientChart = new GradientChart {
77          Dock = DockStyle.Fill,
78          Margin = Padding.Empty,
79          ShowLegend = false,
80          ShowCursor = true,
81          ShowXAxisLabel = true,
82          ShowYAxisLabel = true
83        };
84        gradientChart.VariableValueChanged += (o, e) => {
85          foreach (var chart in gradientChartTableLayout.Controls.Cast<GradientChart>()) {
86            if (chart == (GradientChart)o) continue;
87            chart.UpdateChart();
88          }
89        };
90        gradientChart.Configure(new[] { Content }, pd, dataset, x, min, max, Points);
91        charts[x] = gradientChart;
92      }
93      // update variable list
94      variableListView.Items.Clear();
95      variableListView.Items.AddRange(variableNames.Select(x => new ListViewItem(x, 0)).ToArray());
96    }
97    // sort chart controls so that they always appear in the same order as in the list view
98    // the gradient chart layout should be suspended before calling this method
99    private void SortControls() {
100      var tl = gradientChartTableLayout;
101      var indices = variableListView.CheckedItems.Cast<ListViewItem>().ToDictionary(checkedItem => checkedItem.Text, checkedItem => checkedItem.Index);
102      var count = tl.Controls.Count;
103      var controls = new GradientChart[count];
104      for (int i = 0; i < count; ++i) controls[i] = (GradientChart)tl.Controls[i];
105      Array.Sort(controls, (a, b) => indices[a.Variable].CompareTo(indices[b.Variable]));
106      tl.Controls.Clear();
107      tl.Controls.AddRange(controls);
108    }
109
110    private void variableListView_ItemChecked(object sender, ItemCheckedEventArgs e) {
111      if (charts == null) return;
112      var item = e.Item;
113      var variable = item.Text;
114      var chart = charts[variable];
115      var tl = gradientChartTableLayout;
116      tl.SuspendLayout();
117      var columnWidth = tl.GetColumnWidths()[0]; // all columns have the same width
118      var rowHeight = 0.8f * columnWidth;
119      tl.RowStyles.Clear();
120      if (item.Checked) {
121        tl.Controls.Add(chart);
122        chart.UpdateChart();
123      } else {
124        tl.Controls.Remove(chart);
125      }
126      var count = tl.Controls.Count;
127      SortControls();
128      tl.RowCount = count / tl.ColumnCount + (count % tl.ColumnCount == 0 ? 0 : 1);
129      for (int i = 0; i < count; ++i) {
130        var control = tl.Controls[i];
131        int rowIndex = i / tl.ColumnCount;
132        int columnIndex = i % tl.ColumnCount;
133        tl.SetRow(control, rowIndex);
134        tl.SetColumn(control, columnIndex);
135      }
136      for (int i = 0; i < tl.RowCount; ++i) {
137        tl.RowStyles.Add(new RowStyle(SizeType.Absolute, rowHeight));
138      }
139      tl.ResumeLayout();
140    }
141  }
142}
Note: See TracBrowser for help on using the repository browser.