Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 13817 was 13817, checked in by bburlacu, 8 years ago

#2597: Added RegressionSolutionTargetResponseGradientView and updated GradientChart.

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