Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 13846 was 13846, checked in by pfleck, 8 years ago

#2597

  • Added a manual y-axis control.
  • Fixed bug with dragging the cursor in the GradientChart out of the chart.
File size: 7.7 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.Collections;
27using HeuristicLab.MainForm;
28using HeuristicLab.MainForm.WindowsForms;
29using HeuristicLab.Problems.DataAnalysis;
30using HeuristicLab.Problems.DataAnalysis.Views;
31using HeuristicLab.Visualization.ChartControlsExtensions;
32
33namespace HeuristicLab.Algorithms.DataAnalysis.Views {
34  [View("Gradient View")]
35  [Content(typeof(IRegressionSolution))]
36  public partial class RegressionSolutionGradientView : DataAnalysisSolutionEvaluationView {
37    private const int DrawingSteps = 1000;
38
39    private readonly List<string> variableNames;
40    private readonly ObservableList<DensityTrackbar> trackbars;
41    private ModifiableDataset sharedFixedVariables;
42
43    private int ActiveDimension {
44      get { return trackbars.FindIndex(tb => tb.Checked); }
45    }
46
47    public new IRegressionSolution Content {
48      get { return (IRegressionSolution)base.Content; }
49      set { base.Content = value; }
50    }
51
52    public RegressionSolutionGradientView()
53      : base() {
54      variableNames = new List<string>();
55
56      trackbars = new ObservableList<DensityTrackbar>();
57      trackbars.ItemsAdded += (sender, args) => {
58        args.Items.Select(i => i.Value).ForEach(RegisterEvents);
59      };
60      trackbars.ItemsRemoved += (sender, args) => {
61        args.Items.Select(i => i.Value).ForEach(DeregisterEvents);
62      };
63      trackbars.CollectionReset += (sender, args) => {
64        args.OldItems.Select(i => i.Value).ForEach(DeregisterEvents);
65        args.Items.Select(i => i.Value).ForEach(RegisterEvents);
66      };
67
68      InitializeComponent();
69
70      // Avoid additional horizontal scrollbar
71      var vertScrollWidth = SystemInformation.VerticalScrollBarWidth;
72      tableLayoutPanel.Padding = new Padding(0, 0, vertScrollWidth, 0);
73    }
74
75    private async void UpdateConfigurationControls() {
76      variableNames.Clear();
77      trackbars.Clear();
78
79      tableLayoutPanel.SuspendRepaint();
80      tableLayoutPanel.SuspendLayout();
81
82      tableLayoutPanel.RowCount = 0;
83      tableLayoutPanel.Controls.Clear();
84
85      if (Content == null) {
86        tableLayoutPanel.ResumeLayout(false);
87        tableLayoutPanel.ResumeRepaint(false);
88        return;
89      }
90
91      variableNames.AddRange(Content.ProblemData.AllowedInputVariables);
92
93      var newTrackbars = CreateConfiguration();
94
95      sharedFixedVariables = new ModifiableDataset(variableNames, newTrackbars.Select(tb => new List<double>(1) { (double)tb.Value }));
96      gradientChart.Configure(new[] { Content }, sharedFixedVariables, variableNames.First(), DrawingSteps);
97      await gradientChart.RecalculateAsync();
98
99      // Add to table and observable lists
100      tableLayoutPanel.RowCount = variableNames.Count;
101      while (tableLayoutPanel.RowStyles.Count < variableNames.Count)
102        tableLayoutPanel.RowStyles.Add(new RowStyle(SizeType.AutoSize));
103      for (int i = 0; i < newTrackbars.Count; i++) {
104        // events registered automatically
105        trackbars.Add(newTrackbars[i]);
106        tableLayoutPanel.Controls.Add(newTrackbars[i], 0, i);
107      }
108
109      tableLayoutPanel.ResumeLayout(true);
110      tableLayoutPanel.ResumeRepaint(true);
111
112      trackbars.First().Checked = true;
113    }
114
115    private IList<DensityTrackbar> CreateConfiguration() {
116      var ranges = new List<DoubleLimit>();
117      foreach (string variableName in variableNames) {
118        var values = Content.ProblemData.Dataset.GetDoubleValues(variableName, Content.ProblemData.AllIndices);
119        double min, max, interval;
120        ChartUtil.CalculateAxisInterval(values.Min(), values.Max(), 10, out min, out max, out interval);
121        ranges.Add(new DoubleLimit(min, max));
122      }
123
124      var newTrackbars = new List<DensityTrackbar>();
125      for (int i = 0; i < variableNames.Count; i++) {
126        var name = variableNames[i];
127        var trainingData = Content.ProblemData.Dataset.GetDoubleValues(name, Content.ProblemData.TrainingIndices).ToList();
128
129        var dimensionTrackbar = new DensityTrackbar(name, ranges[i], trainingData) {
130          Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right
131        };
132        newTrackbars.Add(dimensionTrackbar);
133      }
134
135      return newTrackbars;
136    }
137
138    private void RegisterEvents(DensityTrackbar trackbar) {
139      trackbar.CheckedChanged += trackbar_CheckedChanged;
140      trackbar.ValueChanged += trackbar_ValueChanged;
141      trackbar.LimitsChanged += trackbar_LimitsChanged;
142    }
143    private void DeregisterEvents(DensityTrackbar trackbar) {
144      trackbar.CheckedChanged -= trackbar_CheckedChanged;
145      trackbar.ValueChanged -= trackbar_ValueChanged;
146      trackbar.LimitsChanged -= trackbar_LimitsChanged;
147    }
148
149    private void trackbar_CheckedChanged(object sender, EventArgs e) {
150      var trackBar = sender as DensityTrackbar;
151      if (trackBar == null || !trackBar.Checked) return;
152      // Uncheck all others
153      foreach (var tb in trackbars.Except(new[] { trackBar }))
154        tb.Checked = false;
155      gradientChart.FreeVariable = variableNames[trackbars.IndexOf(trackBar)];
156      gradientChart.RecalculateAsync();
157    }
158
159    private void trackbar_LimitsChanged(object sender, EventArgs e) {
160      var trackBar = sender as DensityTrackbar;
161      if (trackBar == null || !trackBar.Checked) return;
162      gradientChart.FixedXAxisMin = trackBar.Limits.Lower;
163      gradientChart.FixedXAxisMax = trackBar.Limits.Upper;
164      gradientChart.RecalculateAsync();
165    }
166
167    private void trackbar_ValueChanged(object sender, EventArgs e) {
168      var trackBar = sender as DensityTrackbar;
169      if (trackBar == null) return;
170      sharedFixedVariables.SetVariableValue((double)trackBar.Value, variableNames[trackbars.IndexOf(trackBar)], 0);
171      gradientChart.RecalculateAsync();
172    }
173
174    #region Events
175    protected override void RegisterContentEvents() {
176      base.RegisterContentEvents();
177      Content.ModelChanged += Content_ModelChanged;
178      Content.ProblemDataChanged += Content_ProblemDataChanged;
179    }
180
181    protected override void DeregisterContentEvents() {
182      base.DeregisterContentEvents();
183      Content.ModelChanged -= Content_ModelChanged;
184      Content.ProblemDataChanged -= Content_ProblemDataChanged;
185    }
186
187    protected override void OnContentChanged() {
188      base.OnContentChanged();
189      UpdateConfigurationControls();
190    }
191
192    private void Content_ModelChanged(object sender, EventArgs e) {
193      UpdateConfigurationControls();
194    }
195
196    private void Content_ProblemDataChanged(object sender, EventArgs e) {
197      UpdateConfigurationControls();
198    }
199    #endregion
200  }
201
202  internal static class Extensions {
203    public static void ForEach<T>(this IEnumerable<T> source, Action<T> action) {
204      foreach (T item in source)
205        action(item);
206    }
207  }
208}
Note: See TracBrowser for help on using the repository browser.