Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/HeuristicLab.DataPreprocessing/3.4/Content/ScatterPlotContent.cs @ 17189

Last change on this file since 17189 was 17180, checked in by swagner, 5 years ago

#2875: Removed years in copyrights

File size: 5.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 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.Core;
28using HEAL.Attic;
29using HeuristicLab.Visualization.ChartControlsExtensions;
30
31namespace HeuristicLab.DataPreprocessing {
32  [Item("ScatterPlotContent", "")]
33  [StorableType("CF1014CB-9B19-4653-AEC2-630C231D89B2")]
34  public abstract class ScatterPlotContent : PreprocessingChartContent {
35    [Storable]
36    public string GroupingVariable { get; set; }
37
38    #region Constructor, Cloning & Persistence
39    protected ScatterPlotContent(IFilteredPreprocessingData preprocessingData)
40      : base(preprocessingData) {
41    }
42
43    protected ScatterPlotContent(ScatterPlotContent original, Cloner cloner)
44      : base(original, cloner) {
45      GroupingVariable = original.GroupingVariable;
46    }
47
48    [StorableConstructor]
49    protected ScatterPlotContent(StorableConstructorFlag _) : base(_) { }
50    #endregion
51
52    public static ScatterPlot CreateScatterPlot(IFilteredPreprocessingData preprocessingData, string variableNameX, string variableNameY, string variableNameGroup = "-", LegendOrder legendOrder = LegendOrder.Alphabetically) {
53      ScatterPlot scatterPlot = new ScatterPlot();
54
55      IList<double> xValues = preprocessingData.GetValues<double>(preprocessingData.GetColumnIndex(variableNameX));
56      IList<double> yValues = preprocessingData.GetValues<double>(preprocessingData.GetColumnIndex(variableNameY));
57
58      var points = xValues.Zip(yValues, (x, y) => new Point2D<double>(x, y)).ToList();
59      var validPoints = points.Where(p => !double.IsNaN(p.X) && !double.IsNaN(p.Y) && !double.IsInfinity(p.X) && !double.IsInfinity(p.Y)).ToList();
60      if (validPoints.Any()) {
61        try {
62          double axisMin, axisMax, axisInterval;
63          ChartUtil.CalculateOptimalAxisInterval(validPoints.Min(p => p.X), validPoints.Max(p => p.X), out axisMin, out axisMax, out axisInterval);
64          scatterPlot.VisualProperties.XAxisMinimumAuto = false;
65          scatterPlot.VisualProperties.XAxisMaximumAuto = false;
66          scatterPlot.VisualProperties.XAxisMinimumFixedValue = axisMin;
67          scatterPlot.VisualProperties.XAxisMaximumFixedValue = axisMax;
68        } catch (ArgumentOutOfRangeException) { } // error during CalculateOptimalAxisInterval
69        try {
70          double axisMin, axisMax, axisInterval;
71          ChartUtil.CalculateOptimalAxisInterval(validPoints.Min(p => p.Y), validPoints.Max(p => p.Y), out axisMin, out axisMax, out axisInterval);
72          scatterPlot.VisualProperties.YAxisMinimumAuto = false;
73          scatterPlot.VisualProperties.YAxisMaximumAuto = false;
74          scatterPlot.VisualProperties.YAxisMinimumFixedValue = axisMin;
75          scatterPlot.VisualProperties.YAxisMaximumFixedValue = axisMax;
76        } catch (ArgumentOutOfRangeException) { } // error during CalculateOptimalAxisInterval
77      }
78
79
80      //No Grouping
81      if (string.IsNullOrEmpty(variableNameGroup) || variableNameGroup == "-") {
82        ScatterPlotDataRow scdr = new ScatterPlotDataRow(variableNameX + " - " + variableNameY, "", validPoints);
83        scdr.VisualProperties.IsVisibleInLegend = false;
84        scatterPlot.Rows.Add(scdr);
85        return scatterPlot;
86      }
87
88      //Grouping
89      int groupVariableIndex = preprocessingData.GetColumnIndex(variableNameGroup);
90      var groupingValues = Enumerable.Empty<string>();
91
92      if (preprocessingData.VariableHasType<double>(groupVariableIndex)) {
93        groupingValues = preprocessingData.GetValues<double>(groupVariableIndex).Select(x => x.ToString());
94      } else if (preprocessingData.VariableHasType<string>(groupVariableIndex)) {
95        groupingValues = preprocessingData.GetValues<string>(groupVariableIndex);
96      } else if (preprocessingData.VariableHasType<DateTime>(groupVariableIndex)) {
97        groupingValues = preprocessingData.GetValues<DateTime>(groupVariableIndex).Select(x => x.ToString());
98      }
99      var groups = groupingValues.Zip(validPoints, Tuple.Create).GroupBy(t => t.Item1, t => t.Item2);
100
101      if (legendOrder == LegendOrder.Alphabetically)
102        groups = groups.OrderBy(x => x.Key, new NaturalStringComparer());
103
104      foreach (var group in groups) {
105        var scdr = new ScatterPlotDataRow {
106          Name = group.Key,
107          VisualProperties = {
108            IsVisibleInLegend = true,
109            PointSize = 6
110          }
111        };
112        scdr.Points.AddRange(group);
113        scatterPlot.Rows.Add(scdr);
114      }
115      return scatterPlot;
116    }
117  }
118}
Note: See TracBrowser for help on using the repository browser.