Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2597: Fixed small bug when the values are not correctly rendered in the chart initially. removed unused variable limits from view.

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