Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2597:

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