[10539] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[14185] | 3 | * Copyright (C) 2002-2016 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[10539] | 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 |
|
---|
[14472] | 22 | using System;
|
---|
[10987] | 23 | using System.Collections.Generic;
|
---|
[13502] | 24 | using System.Linq;
|
---|
[10987] | 25 | using HeuristicLab.Analysis;
|
---|
[10539] | 26 | using HeuristicLab.Common;
|
---|
[14474] | 27 | using HeuristicLab.Problems.DataAnalysis;
|
---|
[14472] | 28 | using HeuristicLab.Visualization.ChartControlsExtensions;
|
---|
[10242] | 29 |
|
---|
[10539] | 30 | namespace HeuristicLab.DataPreprocessing {
|
---|
[10709] | 31 |
|
---|
[14467] | 32 | public abstract class ScatterPlotContent : PreprocessingChartContent {
|
---|
| 33 | protected ScatterPlotContent(IFilteredPreprocessingData preprocessingData)
|
---|
[10992] | 34 | : base(preprocessingData) {
|
---|
[10252] | 35 | }
|
---|
| 36 |
|
---|
[14467] | 37 | protected ScatterPlotContent(ScatterPlotContent content, Cloner cloner)
|
---|
[10539] | 38 | : base(content, cloner) {
|
---|
[10245] | 39 | }
|
---|
[11001] | 40 |
|
---|
[14474] | 41 | public ScatterPlot CreateScatterPlot(string variableNameX, string variableNameY, string variableNameGroup = "-") {
|
---|
[10987] | 42 | ScatterPlot scatterPlot = new ScatterPlot();
|
---|
| 43 |
|
---|
[14511] | 44 | IList<double> xValues = PreprocessingData.GetValues<double>(PreprocessingData.GetColumnIndex(variableNameX));
|
---|
| 45 | IList<double> yValues = PreprocessingData.GetValues<double>(PreprocessingData.GetColumnIndex(variableNameY));
|
---|
[14472] | 46 |
|
---|
[14511] | 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()) {
|
---|
[14495] | 50 | try {
|
---|
| 51 | double axisMin, axisMax, axisInterval;
|
---|
[14511] | 52 | ChartUtil.CalculateOptimalAxisInterval(validPoints.Min(p => p.X), validPoints.Max(p => p.X), out axisMin, out axisMax, out axisInterval);
|
---|
[14495] | 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;
|
---|
[14511] | 60 | ChartUtil.CalculateOptimalAxisInterval(validPoints.Min(p => p.Y), validPoints.Max(p => p.Y), out axisMin, out axisMax, out axisInterval);
|
---|
[14495] | 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 | }
|
---|
[14472] | 67 |
|
---|
[14474] | 68 | if (variableNameGroup == null || variableNameGroup == "-") {
|
---|
[14511] | 69 | ScatterPlotDataRow scdr = new ScatterPlotDataRow(variableNameX + " - " + variableNameY, "", validPoints);
|
---|
[14446] | 70 | scdr.VisualProperties.IsVisibleInLegend = false;
|
---|
[13502] | 71 | scatterPlot.Rows.Add(scdr);
|
---|
| 72 | } else {
|
---|
[14474] | 73 | var groupValues = PreprocessingData.GetValues<double>(PreprocessingData.GetColumnIndex(variableNameGroup));
|
---|
[14521] | 74 | var data = points.Zip(groupValues, (p, g) => new { p, g })
|
---|
[14525] | 75 | .Where(x => !double.IsNaN(x.p.X) && !double.IsNaN(x.p.Y) && !double.IsInfinity(x.p.X) && !double.IsInfinity(x.p.Y))
|
---|
[14521] | 76 | .ToList();
|
---|
| 77 |
|
---|
| 78 | foreach (var groupValue in groupValues.Distinct().OrderBy(g => g)) {
|
---|
[14525] | 79 | var values = data.Where(x => x.g == groupValue || (double.IsNaN(x.g) && double.IsNaN(groupValue))).Select(v => v.p);
|
---|
[14511] | 80 | var row = new ScatterPlotDataRow(string.Format("{0} ({1})", variableNameGroup, groupValue), "", values) {
|
---|
[14521] | 81 | Name = groupValue.ToString("R"),
|
---|
[14474] | 82 | VisualProperties = { PointSize = 6 }
|
---|
| 83 | };
|
---|
[13502] | 84 | scatterPlot.Rows.Add(row);
|
---|
| 85 | }
|
---|
[10987] | 86 | }
|
---|
| 87 | return scatterPlot;
|
---|
| 88 | }
|
---|
[14474] | 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 | }
|
---|
[10242] | 106 | }
|
---|
| 107 | }
|
---|