Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PersistenceReintegration/HeuristicLab.Problems.DataAnalysis.Views/3.4/Regression/RegressionSolutionResidualAnalysisView.cs @ 15071

Last change on this file since 15071 was 14927, checked in by gkronber, 8 years ago

#2520: changed all usages of StorableClass to use StorableType with an auto-generated GUID (did not add StorableType to other type definitions yet)

File size: 5.4 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
21using System;
22using System.Drawing;
23using System.Linq;
24using System.Runtime.Remoting.Contexts;
25using System.Threading.Tasks;
26using HeuristicLab.Data;
27using HeuristicLab.MainForm;
28using HeuristicLab.Optimization;
29
30namespace HeuristicLab.Problems.DataAnalysis.Views {
31  [View("Residual Analysis")]
32  [Content(typeof(IRegressionSolution))]
33  public partial class RegressionSolutionResidualAnalysisView : DataAnalysisSolutionEvaluationView {
34
35    public new IRegressionSolution Content {
36      get { return (IRegressionSolution)base.Content; }
37      set {
38        base.Content = value;
39      }
40    }
41
42    public RegressionSolutionResidualAnalysisView()
43      : base() {
44      InitializeComponent();
45    }
46
47    #region events
48    protected override void RegisterContentEvents() {
49      base.RegisterContentEvents();
50      Content.ModelChanged += new EventHandler(Content_ModelChanged);
51      Content.ProblemDataChanged += new EventHandler(Content_ProblemDataChanged);
52    }
53
54    protected override void DeregisterContentEvents() {
55      base.DeregisterContentEvents();
56      Content.ModelChanged -= new EventHandler(Content_ModelChanged);
57      Content.ProblemDataChanged -= new EventHandler(Content_ProblemDataChanged);
58    }
59
60    protected virtual void Content_ProblemDataChanged(object sender, EventArgs e) {
61      OnContentChanged();
62    }
63
64    protected virtual void Content_ModelChanged(object sender, EventArgs e) {
65      OnContentChanged();
66    }
67
68    protected override void OnContentChanged() {
69      base.OnContentChanged();
70      if (Content == null) {
71        bubbleChartView.Content = null;
72      } else {
73        UpdateBubbleChart();
74      }
75    }
76
77    private void UpdateBubbleChart() {
78      if (Content == null) return;
79      var selectedXAxis = bubbleChartView.SelectedXAxis;
80      var selectedYAxis = bubbleChartView.SelectedYAxis;
81
82      var problemData = Content.ProblemData;
83      var ds = problemData.Dataset;
84      var runs = new RunCollection();
85      // produce training and test values separately as they might overlap (e.g. for ensembles)
86      var predictedValuesTrain = Content.EstimatedTrainingValues.ToArray();
87      int j = 0; // idx for predictedValues array
88      var partitionId = "Partition";
89      while (ds.VariableNames.Contains(partitionId)) partitionId += "_";
90      foreach (var i in problemData.TrainingIndices) {
91        var run = CreateRunForIdx(i, problemData);
92        var targetValue = ds.GetDoubleValue(problemData.TargetVariable, i);
93        AddErrors(run, predictedValuesTrain[j++], targetValue);
94        run.Results.Add(partitionId, new StringValue("Training"));
95        run.Color = Color.Gold;
96        runs.Add(run);
97      }
98      var predictedValuesTest = Content.EstimatedTestValues.ToArray();
99      j = 0;
100      foreach (var i in problemData.TestIndices) {
101        var run = CreateRunForIdx(i, problemData);
102        var targetValue = ds.GetDoubleValue(problemData.TargetVariable, i);
103        AddErrors(run, predictedValuesTest[j++], targetValue);
104        run.Results.Add(partitionId, new StringValue("Test"));
105        run.Color = Color.Red;
106        runs.Add(run);
107      }
108      if (string.IsNullOrEmpty(selectedXAxis))
109        selectedXAxis = "Index";
110      if (string.IsNullOrEmpty(selectedYAxis))
111        selectedYAxis = "Residual";
112
113      bubbleChartView.Content = runs;
114      bubbleChartView.SelectedXAxis = selectedXAxis;
115      bubbleChartView.SelectedYAxis = selectedYAxis;
116    }
117
118    private void AddErrors(IRun run, double pred, double target) {
119      var residual = target - pred;
120      var relError = residual / target;
121      var predId = "Prediction";
122      while (run.Results.ContainsKey(predId)) predId += "_";
123      var resId = "Residual";
124      while (run.Results.ContainsKey(resId)) resId += "_";
125      var relErrorId = "Rel. Error";
126      while (run.Results.ContainsKey(relErrorId)) relErrorId += "_";
127      run.Results.Add(predId, new DoubleValue(pred));
128      run.Results.Add(resId, new DoubleValue(residual));
129      run.Results.Add(relErrorId, new DoubleValue(relError));
130    }
131
132    private IRun CreateRunForIdx(int i, IRegressionProblemData problemData) {
133      var ds = problemData.Dataset;
134      var run = new Run();
135      foreach (var variableName in ds.DoubleVariables) {
136        run.Results.Add(variableName, new DoubleValue(ds.GetDoubleValue(variableName, i)));
137      }
138      foreach (var variableName in ds.StringVariables) {
139        run.Results.Add(variableName, new StringValue(ds.GetStringValue(variableName, i)));
140      }
141
142      return run;
143    }
144    #endregion
145
146  }
147}
Note: See TracBrowser for help on using the repository browser.