Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2709

  • Added suggestion feature for singlescatterplotview.
  • Shows NaN groups in scatterplot (black if gradient is selected).
  • Only enables input variables in DataGridContentView per default.
  • Added missing resx file (gradient image).
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));
45      IList<double> yValues = PreprocessingData.GetValues<double>(PreprocessingData.GetColumnIndex(variableNameY));
46
47      var points = xValues.Zip(yValues, (x, y) => new Point2D<double>(x, y)).ToList();
48      var validPoints = points.Where(p => !double.IsNaN(p.X) && !double.IsNaN(p.Y) && !double.IsInfinity(p.X) && !double.IsInfinity(p.Y)).ToList();
49      if (validPoints.Any()) {
50        try {
51          double axisMin, axisMax, axisInterval;
52          ChartUtil.CalculateOptimalAxisInterval(validPoints.Min(p => p.X), validPoints.Max(p => p.X), out axisMin, out axisMax, out axisInterval);
53          scatterPlot.VisualProperties.XAxisMinimumAuto = false;
54          scatterPlot.VisualProperties.XAxisMaximumAuto = false;
55          scatterPlot.VisualProperties.XAxisMinimumFixedValue = axisMin;
56          scatterPlot.VisualProperties.XAxisMaximumFixedValue = axisMax;
57        } catch (ArgumentOutOfRangeException) { } // error during CalculateOptimalAxisInterval
58        try {
59          double axisMin, axisMax, axisInterval;
60          ChartUtil.CalculateOptimalAxisInterval(validPoints.Min(p => p.Y), validPoints.Max(p => p.Y), 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        ScatterPlotDataRow scdr = new ScatterPlotDataRow(variableNameX + " - " + variableNameY, "", validPoints);
70        scdr.VisualProperties.IsVisibleInLegend = false;
71        scatterPlot.Rows.Add(scdr);
72      } else {
73        var groupValues = PreprocessingData.GetValues<double>(PreprocessingData.GetColumnIndex(variableNameGroup));
74        var data = points.Zip(groupValues, (p, g) => new { p, g })
75          .Where(x => !double.IsNaN(x.p.X) && !double.IsNaN(x.p.Y) && !double.IsInfinity(x.p.X) && !double.IsInfinity(x.p.Y))
76          .ToList();
77
78        foreach (var groupValue in groupValues.Distinct().OrderBy(g => g)) {
79          var values = data.Where(x => x.g == groupValue || (double.IsNaN(x.g) && double.IsNaN(groupValue))).Select(v => v.p);
80          var row = new ScatterPlotDataRow(string.Format("{0} ({1})", variableNameGroup, groupValue), "", values) {
81            Name = groupValue.ToString("R"),
82            VisualProperties = { PointSize = 6 }
83          };
84          scatterPlot.Rows.Add(row);
85        }
86      }
87      return scatterPlot;
88    }
89
90    public DataRow GetCorrelationRow(string variableNameX, string variableNameY) {
91      var xValues = PreprocessingData.GetValues<double>(PreprocessingData.GetColumnIndex(variableNameX));
92      var yValues = PreprocessingData.GetValues<double>(PreprocessingData.GetColumnIndex(variableNameY));
93
94      double k, d;
95      OnlineCalculatorError err;
96      OnlineLinearScalingParameterCalculator.Calculate(xValues, yValues, out k, out d, out err);
97      double p = OnlinePearsonsRCalculator.Calculate(xValues, yValues, out err);
98
99      var data = new double[xValues.Count];
100      for (int i = 0; i < xValues.Count; i++) {
101        data[i]= k * i + d;
102      }
103
104      return new DataRow(string.Format("Correlation (R²={0})", p*p), "", data);
105    }
106  }
107}
Note: See TracBrowser for help on using the repository browser.