Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.DataPreprocessing/3.4/Content/ScatterPlotContent.cs @ 17819

Last change on this file since 17819 was 17181, checked in by swagner, 5 years ago

#2875: Merged r17180 from trunk to stable

File size: 5.3 KB
RevLine 
[10539]1#region License Information
2/* HeuristicLab
[17181]3 * Copyright (C) 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
[15242]22using System;
[10987]23using System.Collections.Generic;
[13502]24using System.Linq;
[10987]25using HeuristicLab.Analysis;
[10539]26using HeuristicLab.Common;
[15535]27using HeuristicLab.Core;
[17097]28using HEAL.Attic;
[15242]29using HeuristicLab.Visualization.ChartControlsExtensions;
[10242]30
[10539]31namespace HeuristicLab.DataPreprocessing {
[15535]32  [Item("ScatterPlotContent", "")]
[17097]33  [StorableType("CF1014CB-9B19-4653-AEC2-630C231D89B2")]
[15242]34  public abstract class ScatterPlotContent : PreprocessingChartContent {
[15535]35    [Storable]
[15242]36    public string GroupingVariable { get; set; }
[10252]37
[15535]38    #region Constructor, Cloning & Persistence
[15242]39    protected ScatterPlotContent(IFilteredPreprocessingData preprocessingData)
[10992]40      : base(preprocessingData) {
[10252]41    }
42
[15535]43    protected ScatterPlotContent(ScatterPlotContent original, Cloner cloner)
44      : base(original, cloner) {
45      GroupingVariable = original.GroupingVariable;
[10245]46    }
[11001]47
[15535]48    [StorableConstructor]
[17097]49    protected ScatterPlotContent(StorableConstructorFlag _) : base(_) { }
[15535]50    #endregion
51
[15242]52    public static ScatterPlot CreateScatterPlot(IFilteredPreprocessingData preprocessingData, string variableNameX, string variableNameY, string variableNameGroup = "-", LegendOrder legendOrder = LegendOrder.Alphabetically) {
53      ScatterPlot scatterPlot = new ScatterPlot();
[11001]54
[15242]55      IList<double> xValues = preprocessingData.GetValues<double>(preprocessingData.GetColumnIndex(variableNameX));
56      IList<double> yValues = preprocessingData.GetValues<double>(preprocessingData.GetColumnIndex(variableNameY));
[10245]57
[15242]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
[15242]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);
[15242]85        return scatterPlot;
86      }
[13502]87
[15242]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      }
[15242]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.