Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2709

  • Fixed initial point size for scatterplots.
  • Reuse the visual properties of the old data row if a single variable is changed in the ScatterPlotSingleView
File size: 5.1 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)).Where(x => !double.IsNaN(x) && !double.IsInfinity(x)).ToList();
45      IList<double> yValues = PreprocessingData.GetValues<double>(PreprocessingData.GetColumnIndex(variableNameY)).Where(x => !double.IsNaN(x) && !double.IsInfinity(x)).ToList();
46
47      if (xValues.Any()) {
48        try {
49          double axisMin, axisMax, axisInterval;
50          ChartUtil.CalculateOptimalAxisInterval(xValues.Min(), xValues.Max(), out axisMin, out axisMax, out axisInterval);
51          scatterPlot.VisualProperties.XAxisMinimumAuto = false;
52          scatterPlot.VisualProperties.XAxisMaximumAuto = false;
53          scatterPlot.VisualProperties.XAxisMinimumFixedValue = axisMin;
54          scatterPlot.VisualProperties.XAxisMaximumFixedValue = axisMax;
55        } catch (ArgumentOutOfRangeException) { } // error during CalculateOptimalAxisInterval
56      }
57      if (yValues.Any()) {
58        try {
59          double axisMin, axisMax, axisInterval;
60          ChartUtil.CalculateOptimalAxisInterval(yValues.Min(), yValues.Max(), out axisMin, out axisMax, out axisInterval);
61          scatterPlot.VisualProperties.YAxisMinimumAuto = false;
62          scatterPlot.VisualProperties.YAxisMaximumAuto = false;
63          scatterPlot.VisualProperties.YAxisMinimumFixedValue = axisMin;
64          scatterPlot.VisualProperties.YAxisMaximumFixedValue = axisMax;
65        } catch (ArgumentOutOfRangeException) { } // error during CalculateOptimalAxisInterval
66      }
67
68      if (variableNameGroup == null || variableNameGroup == "-") {
69        List<Point2D<double>> points = new List<Point2D<double>>();
70
71        for (int i = 0; i < xValues.Count; i++) {
72          Point2D<double> point = new Point2D<double>(xValues[i], yValues[i]);
73          points.Add(point);
74        }
75
76        ScatterPlotDataRow scdr = new ScatterPlotDataRow(variableNameX + " - " + variableNameY, "", points);
77        scdr.VisualProperties.IsVisibleInLegend = false;
78        scatterPlot.Rows.Add(scdr);
79
80      } else {
81        var groupValues = PreprocessingData.GetValues<double>(PreprocessingData.GetColumnIndex(variableNameGroup));
82        var data = xValues.Zip(yValues, (x, y) => new { x, y }).Zip(groupValues, (v, c) => new { v.x, v.y, c }).ToList();
83        foreach (var groupValue in groupValues.Distinct()) {
84          var values = data.Where(x => x.c == groupValue);
85          var row = new ScatterPlotDataRow(
86            variableNameGroup + " (" + groupValue + ")",
87            "",
88            values.Select(v => new Point2D<double>(v.x, v.y))) {
89            VisualProperties = { PointSize = 6 }
90          };
91          scatterPlot.Rows.Add(row);
92        }
93      }
94      return scatterPlot;
95    }
96
97    public DataRow GetCorrelationRow(string variableNameX, string variableNameY) {
98      var xValues = PreprocessingData.GetValues<double>(PreprocessingData.GetColumnIndex(variableNameX));
99      var yValues = PreprocessingData.GetValues<double>(PreprocessingData.GetColumnIndex(variableNameY));
100
101      double k, d;
102      OnlineCalculatorError err;
103      OnlineLinearScalingParameterCalculator.Calculate(xValues, yValues, out k, out d, out err);
104      double p = OnlinePearsonsRCalculator.Calculate(xValues, yValues, out err);
105
106      var data = new double[xValues.Count];
107      for (int i = 0; i < xValues.Count; i++) {
108        data[i]= k * i + d;
109      }
110
111      return new DataRow(string.Format("Correlation (R²={0})", p*p), "", data);
112    }
113  }
114}
Note: See TracBrowser for help on using the repository browser.