Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 14724 was 14724, checked in by mkommend, 7 years ago

#2709: Adapted data preprocessing scatter plot to allow grouping of string variables.

File size: 4.7 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.Visualization.ChartControlsExtensions;
28
29namespace HeuristicLab.DataPreprocessing {
30
31  public abstract class ScatterPlotContent : PreprocessingChartContent {
32    protected ScatterPlotContent(IFilteredPreprocessingData preprocessingData)
33      : base(preprocessingData) {
34    }
35
36    protected ScatterPlotContent(ScatterPlotContent content, Cloner cloner)
37      : base(content, cloner) {
38    }
39
40    public ScatterPlot CreateScatterPlot(string variableNameX, string variableNameY, string variableNameGroup = "-") {
41      ScatterPlot scatterPlot = new ScatterPlot();
42
43      IList<double> xValues = PreprocessingData.GetValues<double>(PreprocessingData.GetColumnIndex(variableNameX));
44      IList<double> yValues = PreprocessingData.GetValues<double>(PreprocessingData.GetColumnIndex(variableNameY));
45
46      var points = xValues.Zip(yValues, (x, y) => new Point2D<double>(x, y)).ToList();
47      var validPoints = points.Where(p => !double.IsNaN(p.X) && !double.IsNaN(p.Y) && !double.IsInfinity(p.X) && !double.IsInfinity(p.Y)).ToList();
48      if (validPoints.Any()) {
49        try {
50          double axisMin, axisMax, axisInterval;
51          ChartUtil.CalculateOptimalAxisInterval(validPoints.Min(p => p.X), validPoints.Max(p => p.X), out axisMin, out axisMax, out axisInterval);
52          scatterPlot.VisualProperties.XAxisMinimumAuto = false;
53          scatterPlot.VisualProperties.XAxisMaximumAuto = false;
54          scatterPlot.VisualProperties.XAxisMinimumFixedValue = axisMin;
55          scatterPlot.VisualProperties.XAxisMaximumFixedValue = axisMax;
56        }
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        }
66        catch (ArgumentOutOfRangeException) { } // error during CalculateOptimalAxisInterval
67      }
68
69
70      //No Grouping
71      if (string.IsNullOrEmpty(variableNameGroup) || variableNameGroup == "-") {
72        ScatterPlotDataRow scdr = new ScatterPlotDataRow(variableNameX + " - " + variableNameY, "", validPoints);
73        scdr.VisualProperties.IsVisibleInLegend = false;
74        scatterPlot.Rows.Add(scdr);
75        return scatterPlot;
76      }
77
78      //Grouping
79      int groupVariableIndex = PreprocessingData.GetColumnIndex(variableNameGroup);
80      var groupingValues = Enumerable.Empty<string>();
81
82      if (PreprocessingData.VariableHasType<double>(groupVariableIndex)) {
83        groupingValues = PreprocessingData.GetValues<double>(groupVariableIndex).Select(x => x.ToString());
84      } else if (PreprocessingData.VariableHasType<string>(groupVariableIndex)) {
85        groupingValues = PreprocessingData.GetValues<string>(groupVariableIndex);
86      } else if (PreprocessingData.VariableHasType<DateTime>(groupVariableIndex)) {
87        groupingValues = PreprocessingData.GetValues<DateTime>(groupVariableIndex).Select(x => x.ToString());
88      }
89      var groups = groupingValues.Zip(validPoints, Tuple.Create).GroupBy(t => t.Item1, t => t.Item2);
90
91      foreach (var group in groups) {
92        var scdr = new ScatterPlotDataRow();
93        scdr.Name = group.Key;
94        scdr.VisualProperties.IsVisibleInLegend = true;
95        scdr.VisualProperties.PointSize = 6;
96        scdr.Points.AddRange(group);
97        scatterPlot.Rows.Add(scdr);
98      }
99      return scatterPlot;
100    }
101
102  }
103}
Note: See TracBrowser for help on using the repository browser.