Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2709

  • Added Check Inputs/All/None buttons instead of showing disabled buttons of the ItemCollectionView.
  • Removed the PreprocessingCheckedItemListView. A standard ListView is used instead.
  • Fixed slow updating when simultaneously (un-)checking multiple variables in the chart views. (currently only works by using the new buttons)
File size: 4.9 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        foreach (var groupValue in groupValues.Distinct()) {
76          var values = data.Where(x => x.g == groupValue).Select(v => v.p);
77          var row = new ScatterPlotDataRow(string.Format("{0} ({1})", variableNameGroup, groupValue), "", values) {
78            VisualProperties = { PointSize = 6 }
79          };
80          scatterPlot.Rows.Add(row);
81        }
82      }
83      return scatterPlot;
84    }
85
86    public DataRow GetCorrelationRow(string variableNameX, string variableNameY) {
87      var xValues = PreprocessingData.GetValues<double>(PreprocessingData.GetColumnIndex(variableNameX));
88      var yValues = PreprocessingData.GetValues<double>(PreprocessingData.GetColumnIndex(variableNameY));
89
90      double k, d;
91      OnlineCalculatorError err;
92      OnlineLinearScalingParameterCalculator.Calculate(xValues, yValues, out k, out d, out err);
93      double p = OnlinePearsonsRCalculator.Calculate(xValues, yValues, out err);
94
95      var data = new double[xValues.Count];
96      for (int i = 0; i < xValues.Count; i++) {
97        data[i]= k * i + d;
98      }
99
100      return new DataRow(string.Format("Correlation (R²={0})", p*p), "", data);
101    }
102  }
103}
Note: See TracBrowser for help on using the repository browser.