Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DataPreprocessing Enhancements/HeuristicLab.DataPreprocessing/3.4/Content/ScatterPlotContent.cs @ 14474

Last change on this file since 14474 was 14474, checked in by pfleck, 7 years ago

#2709

  • Improved legend description for grouped histogram and scatterplots.
  • Fixed initial size of points for scatterplots.
  • Added correlation calculation for scatterplots (not used yet).
File size: 4.8 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 HeuristicLab.Analysis;
26using HeuristicLab.Common;
27using HeuristicLab.Problems.DataAnalysis;
28using HeuristicLab.Visualization.ChartControlsExtensions;
29
30namespace HeuristicLab.DataPreprocessing {
31
32  public abstract class ScatterPlotContent : PreprocessingChartContent {
33    protected ScatterPlotContent(IFilteredPreprocessingData preprocessingData)
34      : base(preprocessingData) {
35    }
36
37    protected ScatterPlotContent(ScatterPlotContent content, Cloner cloner)
38      : base(content, cloner) {
39    }
40
41    public ScatterPlot CreateScatterPlot(string variableNameX, string variableNameY, string variableNameGroup = "-") {
42      ScatterPlot scatterPlot = new ScatterPlot();
43
44      IList<double> xValues = PreprocessingData.GetValues<double>(PreprocessingData.GetColumnIndex(variableNameX));
45      IList<double> yValues = PreprocessingData.GetValues<double>(PreprocessingData.GetColumnIndex(variableNameY));
46
47      double axisMin, axisMax, axisInterval;
48      try {
49        ChartUtil.CalculateOptimalAxisInterval(xValues.Min(), xValues.Max(), out axisMin, out axisMax, out axisInterval);
50        scatterPlot.VisualProperties.XAxisMinimumAuto = false;
51        scatterPlot.VisualProperties.XAxisMaximumAuto = false;
52        scatterPlot.VisualProperties.XAxisMinimumFixedValue = axisMin;
53        scatterPlot.VisualProperties.XAxisMaximumFixedValue = axisMax;
54      } catch (ArgumentOutOfRangeException) { } // missing values lead to NaNs
55      try {
56        ChartUtil.CalculateOptimalAxisInterval(yValues.Min(), yValues.Max(), out axisMin, out axisMax, out axisInterval);
57        scatterPlot.VisualProperties.YAxisMinimumAuto = false;
58        scatterPlot.VisualProperties.YAxisMaximumAuto = false;
59        scatterPlot.VisualProperties.YAxisMinimumFixedValue = axisMin;
60        scatterPlot.VisualProperties.YAxisMaximumFixedValue = axisMax;
61      } catch (ArgumentOutOfRangeException) { } // missing values lead to NaNs
62
63      if (variableNameGroup == null || variableNameGroup == "-") {
64        List<Point2D<double>> points = new List<Point2D<double>>();
65
66        for (int i = 0; i < xValues.Count; i++) {
67          Point2D<double> point = new Point2D<double>(xValues[i], yValues[i]);
68          points.Add(point);
69        }
70
71        ScatterPlotDataRow scdr = new ScatterPlotDataRow(variableNameX + " - " + variableNameY, "", points) {
72          VisualProperties = { PointSize = 6 }
73        };
74        scdr.VisualProperties.IsVisibleInLegend = false;
75        scatterPlot.Rows.Add(scdr);
76
77      } else {
78        var groupValues = PreprocessingData.GetValues<double>(PreprocessingData.GetColumnIndex(variableNameGroup));
79        var data = xValues.Zip(yValues, (x, y) => new { x, y }).Zip(groupValues, (v, c) => new { v.x, v.y, c }).ToList();
80        foreach (var groupValue in groupValues.Distinct()) {
81          var values = data.Where(x => x.c == groupValue);
82          var row = new ScatterPlotDataRow(
83            variableNameGroup + " (" + groupValue + ")",
84            "",
85            values.Select(v => new Point2D<double>(v.x, v.y))) {
86            VisualProperties = { PointSize = 6 }
87          };
88          scatterPlot.Rows.Add(row);
89        }
90      }
91      return scatterPlot;
92    }
93
94    public DataRow GetCorrelationRow(string variableNameX, string variableNameY) {
95      var xValues = PreprocessingData.GetValues<double>(PreprocessingData.GetColumnIndex(variableNameX));
96      var yValues = PreprocessingData.GetValues<double>(PreprocessingData.GetColumnIndex(variableNameY));
97
98      double k, d;
99      OnlineCalculatorError err;
100      OnlineLinearScalingParameterCalculator.Calculate(xValues, yValues, out k, out d, out err);
101      double p = OnlinePearsonsRCalculator.Calculate(xValues, yValues, out err);
102
103      var data = new double[xValues.Count];
104      for (int i = 0; i < xValues.Count; i++) {
105        data[i]= k * i + d;
106      }
107
108      return new DataRow(string.Format("Correlation (R²={0})", p*p), "", data);
109    }
110  }
111}
Note: See TracBrowser for help on using the repository browser.