Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DataAnalysis/HeuristicLab.Problems.DataAnalysis.MultiVariate.TimeSeriesPrognosis.Views/3.3/TimeSeriesPrognosisLineChartView.cs @ 5698

Last change on this file since 5698 was 4556, checked in by gkronber, 14 years ago

Added classes and views for analysis of symbolic time series prognosis results. #1142

File size: 7.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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.Windows.Forms;
25using System.Windows.Forms.DataVisualization.Charting;
26using HeuristicLab.MainForm;
27using HeuristicLab.MainForm.WindowsForms;
28using System.Collections.Generic;
29using HeuristicLab.Problems.DataAnalysis.MultiVariate.TimeSeriesPrognosis.Symbolic;
30
31namespace HeuristicLab.Problems.DataAnalysis.MultiVariate.TimeSeriesPrognosis.Views {
32  [View("Symbolic Time Series Prognosis Line Chart View")]
33  [Content(typeof(SymbolicTimeSeriesPrognosisSolution))]
34  public partial class TimeSeriesPrognosisLineChartView : AsynchronousContentView {
35    private const string TARGETVARIABLE_SERIES_NAME = "(target)";
36    private const string ESTIMATEDVALUES_SERIES_NAME = "(estimated)";
37
38    public new SymbolicTimeSeriesPrognosisSolution Content {
39      get { return (SymbolicTimeSeriesPrognosisSolution)base.Content; }
40      set { base.Content = value; }
41    }
42
43    public TimeSeriesPrognosisLineChartView()
44      : base() {
45      InitializeComponent();
46      //configure axis
47      this.chart.ChartAreas[0].CursorX.IsUserSelectionEnabled = true;
48      this.chart.ChartAreas[0].AxisX.ScaleView.Zoomable = true;
49      this.chart.ChartAreas[0].CursorX.Interval = 1;
50
51      this.chart.ChartAreas[0].CursorY.IsUserSelectionEnabled = true;
52      this.chart.ChartAreas[0].AxisY.ScaleView.Zoomable = true;
53      this.chart.ChartAreas[0].CursorY.Interval = 0;
54    }
55
56    private void RedrawChart() {
57      this.chart.Series.Clear();
58      if (Content != null) {
59        int selectedHorizonValue = Math.Min(Content.Horizon, Math.Max(1, (int)numericUpDown.Value));
60        numericUpDown.Minimum = 1;
61        numericUpDown.Maximum = Content.Horizon;
62        numericUpDown.Value = selectedHorizonValue;
63        List<string> targetVariables = Content.ProblemData.TargetVariables.CheckedItems.Select(x => x.Value.Value).ToList();
64        // horizon=1 (t+0) =>  0 .. 10
65        // horizon=2 (t+0..1) => -1 .. 9
66        // horizon=3 (t+0..2) => -2 .. 8
67        // ...
68        // horizon=10 (t+0..9) => -9 .. 1
69        int firstIndex = 1 - selectedHorizonValue;
70        int lastIndex = Content.ProblemData.Dataset.Rows ;
71        double[] nanVector =
72          targetVariables
73          .Select(x => double.NaN)
74          .ToArray();
75        IEnumerable<int> rows = Enumerable.Range(firstIndex, lastIndex );
76        IEnumerable<IEnumerable<double[]>> values = from row in rows
77                                                    let evaluate = string.IsNullOrEmpty(Content.ConditionalEvaluationVariable) ||
78                                                                  (row > 0 && row < lastIndex &&
79                                                                  Content.ProblemData.Dataset[Content.ConditionalEvaluationVariable, row + selectedHorizonValue - 1] > 0)
80                                                    select evaluate ? Content.GetPrognosis(row) : Enumerable.Repeat(nanVector, Content.Horizon);
81
82        for (int col = 0; col < targetVariables.Count; col++) {
83          string targetVariable = targetVariables[col];
84          this.chart.Series.Add(targetVariable + " " + TARGETVARIABLE_SERIES_NAME);
85          this.chart.Series[targetVariable + " " + TARGETVARIABLE_SERIES_NAME].LegendText = targetVariable;
86          this.chart.Series[targetVariable + " " + TARGETVARIABLE_SERIES_NAME].ChartType = SeriesChartType.FastLine;
87          this.chart.Series[targetVariable + " " + TARGETVARIABLE_SERIES_NAME].Points.DataBindY(Content.ProblemData.Dataset.GetVariableValues(targetVariable));
88
89          this.chart.Series.Add(targetVariable + " " + ESTIMATEDVALUES_SERIES_NAME);
90          this.chart.Series[targetVariable + " " + ESTIMATEDVALUES_SERIES_NAME].LegendText = targetVariable + " " + ESTIMATEDVALUES_SERIES_NAME;
91          this.chart.Series[targetVariable + " " + ESTIMATEDVALUES_SERIES_NAME].ChartType = SeriesChartType.FastLine;
92          this.chart.Series[targetVariable + " " + ESTIMATEDVALUES_SERIES_NAME].Points.DataBindY(values.Select(x => x.ElementAt(selectedHorizonValue - 1)[col]).ToArray());
93          this.chart.Series[targetVariable + " " + ESTIMATEDVALUES_SERIES_NAME].Tag = Content;
94        }
95        this.UpdateStripLines();
96        UpdateCursorInterval();
97      }
98    }
99
100    private void UpdateCursorInterval() {
101      var allValues = (from series in this.chart.Series
102                       from point in series.Points
103                       select point.YValues[0])
104                            .DefaultIfEmpty(1.0);
105      double range = allValues.Max() - allValues.Min();
106      double interestingValuesRange = Math.Min(Math.Max(range, 1.0), Math.Max(range, 1.0));
107      double digits = (int)Math.Log10(interestingValuesRange) - 3;
108      double yZoomInterval = Math.Max(Math.Pow(10, digits), 10E-5);
109      this.chart.ChartAreas[0].CursorY.Interval = yZoomInterval;
110    }
111
112    #region events
113    protected override void RegisterContentEvents() {
114      base.RegisterContentEvents();
115      Content.ProblemDataChanged += new EventHandler(Content_ProblemDataChanged);
116    }
117    protected override void DeregisterContentEvents() {
118      base.DeregisterContentEvents();
119      Content.ProblemDataChanged -= new EventHandler(Content_ProblemDataChanged);
120    }
121
122    private void Content_ProblemDataChanged(object sender, EventArgs e) {
123      RedrawChart();
124    }
125
126    protected override void OnContentChanged() {
127      base.OnContentChanged();
128      RedrawChart();
129    }
130
131    #endregion
132
133    private void UpdateStripLines() {
134      this.chart.ChartAreas[0].AxisX.StripLines.Clear();
135      this.CreateAndAddStripLine("Training", Color.FromArgb(20, Color.Green),
136        Content.ProblemData.TrainingSamplesStart.Value,
137        Content.ProblemData.TrainingSamplesEnd.Value);
138      this.CreateAndAddStripLine("Test", Color.FromArgb(20, Color.Red),
139        Content.ProblemData.TestSamplesStart.Value,
140        Content.ProblemData.TestSamplesEnd.Value);
141    }
142
143    private void CreateAndAddStripLine(string title, Color c, int start, int end) {
144      StripLine stripLine = new StripLine();
145      stripLine.BackColor = c;
146      stripLine.Text = title;
147      stripLine.Font = new Font("Times New Roman", 12, FontStyle.Bold);
148      stripLine.StripWidth = end - start;
149      stripLine.IntervalOffset = start;
150      this.chart.ChartAreas[0].AxisX.StripLines.Add(stripLine);
151    }
152
153    private void numericUpDown_ValueChanged(object sender, EventArgs e) {
154      RedrawChart();
155    }
156  }
157}
Note: See TracBrowser for help on using the repository browser.