Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 16841 was 16565, checked in by gkronber, 6 years ago

#2520: merged changes from PersistenceOverhaul branch (r16451:16564) into trunk

File size: 5.4 KB
RevLine 
[10539]1#region License Information
2/* HeuristicLab
[16565]3 * Copyright (C) 2002-2019 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
[15110]22using System;
[10987]23using System.Collections.Generic;
[13502]24using System.Linq;
[10987]25using HeuristicLab.Analysis;
[10539]26using HeuristicLab.Common;
[15518]27using HeuristicLab.Core;
[16565]28using HEAL.Attic;
[15110]29using HeuristicLab.Visualization.ChartControlsExtensions;
[10242]30
[10539]31namespace HeuristicLab.DataPreprocessing {
[15518]32  [Item("ScatterPlotContent", "")]
[16565]33  [StorableType("CF1014CB-9B19-4653-AEC2-630C231D89B2")]
[15110]34  public abstract class ScatterPlotContent : PreprocessingChartContent {
[15518]35    [Storable]
[15110]36    public string GroupingVariable { get; set; }
[10252]37
[15518]38    #region Constructor, Cloning & Persistence
[15110]39    protected ScatterPlotContent(IFilteredPreprocessingData preprocessingData)
[10992]40      : base(preprocessingData) {
[10252]41    }
42
[15518]43    protected ScatterPlotContent(ScatterPlotContent original, Cloner cloner)
44      : base(original, cloner) {
45      GroupingVariable = original.GroupingVariable;
[10245]46    }
[11001]47
[15518]48    [StorableConstructor]
[16565]49    protected ScatterPlotContent(StorableConstructorFlag _) : base(_) { }
[15518]50    #endregion
51
[15210]52    public static ScatterPlot CreateScatterPlot(IFilteredPreprocessingData preprocessingData, string variableNameX, string variableNameY, string variableNameGroup = "-", LegendOrder legendOrder = LegendOrder.Alphabetically) {
[15110]53      ScatterPlot scatterPlot = new ScatterPlot();
[11001]54
[15110]55      IList<double> xValues = preprocessingData.GetValues<double>(preprocessingData.GetColumnIndex(variableNameX));
56      IList<double> yValues = preprocessingData.GetValues<double>(preprocessingData.GetColumnIndex(variableNameY));
[10245]57
[15110]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      }
[10987]78
79
[15110]80      //No Grouping
81      if (string.IsNullOrEmpty(variableNameGroup) || variableNameGroup == "-") {
82        ScatterPlotDataRow scdr = new ScatterPlotDataRow(variableNameX + " - " + variableNameY, "", validPoints);
83        scdr.VisualProperties.IsVisibleInLegend = false;
[13502]84        scatterPlot.Rows.Add(scdr);
[15110]85        return scatterPlot;
86      }
[13502]87
[15110]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());
[10987]98      }
[15110]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      }
[10987]115      return scatterPlot;
116    }
[10242]117  }
118}
Note: See TracBrowser for help on using the repository browser.