Free cookie consent management tool by TermsFeed Policy Generator

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

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