#region License Information /* HeuristicLab * Copyright (C) 2002-2016 Heuristic and Evolutionary Algorithms Laboratory (HEAL) * * This file is part of HeuristicLab. * * HeuristicLab is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * HeuristicLab is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with HeuristicLab. If not, see . */ #endregion using System; using System.Collections.Generic; using System.Linq; using HeuristicLab.Analysis; using HeuristicLab.Common; using HeuristicLab.Problems.DataAnalysis; using HeuristicLab.Visualization.ChartControlsExtensions; namespace HeuristicLab.DataPreprocessing { public abstract class ScatterPlotContent : PreprocessingChartContent { protected ScatterPlotContent(IFilteredPreprocessingData preprocessingData) : base(preprocessingData) { } protected ScatterPlotContent(ScatterPlotContent content, Cloner cloner) : base(content, cloner) { } public ScatterPlot CreateScatterPlot(string variableNameX, string variableNameY, string variableNameGroup = "-") { ScatterPlot scatterPlot = new ScatterPlot(); IList xValues = PreprocessingData.GetValues(PreprocessingData.GetColumnIndex(variableNameX)); IList yValues = PreprocessingData.GetValues(PreprocessingData.GetColumnIndex(variableNameY)); var points = xValues.Zip(yValues, (x, y) => new Point2D(x, y)).ToList(); var validPoints = points.Where(p => !double.IsNaN(p.X) && !double.IsNaN(p.Y) && !double.IsInfinity(p.X) && !double.IsInfinity(p.Y)).ToList(); if (validPoints.Any()) { try { double axisMin, axisMax, axisInterval; ChartUtil.CalculateOptimalAxisInterval(validPoints.Min(p => p.X), validPoints.Max(p => p.X), out axisMin, out axisMax, out axisInterval); scatterPlot.VisualProperties.XAxisMinimumAuto = false; scatterPlot.VisualProperties.XAxisMaximumAuto = false; scatterPlot.VisualProperties.XAxisMinimumFixedValue = axisMin; scatterPlot.VisualProperties.XAxisMaximumFixedValue = axisMax; } catch (ArgumentOutOfRangeException) { } // error during CalculateOptimalAxisInterval try { double axisMin, axisMax, axisInterval; ChartUtil.CalculateOptimalAxisInterval(validPoints.Min(p => p.Y), validPoints.Max(p => p.Y), out axisMin, out axisMax, out axisInterval); scatterPlot.VisualProperties.YAxisMinimumAuto = false; scatterPlot.VisualProperties.YAxisMaximumAuto = false; scatterPlot.VisualProperties.YAxisMinimumFixedValue = axisMin; scatterPlot.VisualProperties.YAxisMaximumFixedValue = axisMax; } catch (ArgumentOutOfRangeException) { } // error during CalculateOptimalAxisInterval } if (variableNameGroup == null || variableNameGroup == "-") { ScatterPlotDataRow scdr = new ScatterPlotDataRow(variableNameX + " - " + variableNameY, "", validPoints); scdr.VisualProperties.IsVisibleInLegend = false; scatterPlot.Rows.Add(scdr); } else { var groupValues = PreprocessingData.GetValues(PreprocessingData.GetColumnIndex(variableNameGroup)); var data = points.Zip(groupValues, (p, g) => new { p, g }) .Where(x => !double.IsNaN(x.p.X) && !double.IsNaN(x.p.Y) && !double.IsInfinity(x.p.X) && !double.IsInfinity(x.p.Y)) .ToList(); foreach (var groupValue in groupValues.Distinct().OrderBy(g => g)) { var values = data.Where(x => x.g == groupValue || (double.IsNaN(x.g) && double.IsNaN(groupValue))).Select(v => v.p); var row = new ScatterPlotDataRow(string.Format("{0} ({1})", variableNameGroup, groupValue), "", values) { Name = groupValue.ToString("R"), VisualProperties = { PointSize = 6 } }; scatterPlot.Rows.Add(row); } } return scatterPlot; } public DataRow GetCorrelationRow(string variableNameX, string variableNameY) { var xValues = PreprocessingData.GetValues(PreprocessingData.GetColumnIndex(variableNameX)); var yValues = PreprocessingData.GetValues(PreprocessingData.GetColumnIndex(variableNameY)); double k, d; OnlineCalculatorError err; OnlineLinearScalingParameterCalculator.Calculate(xValues, yValues, out k, out d, out err); double p = OnlinePearsonsRCalculator.Calculate(xValues, yValues, out err); var data = new double[xValues.Count]; for (int i = 0; i < xValues.Count; i++) { data[i]= k * i + d; } return new DataRow(string.Format("Correlation (R²={0})", p*p), "", data); } } }