Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 15529 was 15518, checked in by pfleck, 7 years ago

#2809: merged branch to trunk

File size: 5.4 KB
RevLine 
[10539]1#region License Information
2/* HeuristicLab
[14185]3 * Copyright (C) 2002-2016 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;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
[15110]29using HeuristicLab.Visualization.ChartControlsExtensions;
[10242]30
[10539]31namespace HeuristicLab.DataPreprocessing {
[15518]32  [Item("ScatterPlotContent", "")]
33  [StorableClass]
[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]
49    protected ScatterPlotContent(bool deserializing)
50      : base(deserializing) { }
51    #endregion
52
[15210]53    public static ScatterPlot CreateScatterPlot(IFilteredPreprocessingData preprocessingData, string variableNameX, string variableNameY, string variableNameGroup = "-", LegendOrder legendOrder = LegendOrder.Alphabetically) {
[15110]54      ScatterPlot scatterPlot = new ScatterPlot();
[11001]55
[15110]56      IList<double> xValues = preprocessingData.GetValues<double>(preprocessingData.GetColumnIndex(variableNameX));
57      IList<double> yValues = preprocessingData.GetValues<double>(preprocessingData.GetColumnIndex(variableNameY));
[10245]58
[15110]59      var points = xValues.Zip(yValues, (x, y) => new Point2D<double>(x, y)).ToList();
60      var validPoints = points.Where(p => !double.IsNaN(p.X) && !double.IsNaN(p.Y) && !double.IsInfinity(p.X) && !double.IsInfinity(p.Y)).ToList();
61      if (validPoints.Any()) {
62        try {
63          double axisMin, axisMax, axisInterval;
64          ChartUtil.CalculateOptimalAxisInterval(validPoints.Min(p => p.X), validPoints.Max(p => p.X), out axisMin, out axisMax, out axisInterval);
65          scatterPlot.VisualProperties.XAxisMinimumAuto = false;
66          scatterPlot.VisualProperties.XAxisMaximumAuto = false;
67          scatterPlot.VisualProperties.XAxisMinimumFixedValue = axisMin;
68          scatterPlot.VisualProperties.XAxisMaximumFixedValue = axisMax;
69        } catch (ArgumentOutOfRangeException) { } // error during CalculateOptimalAxisInterval
70        try {
71          double axisMin, axisMax, axisInterval;
72          ChartUtil.CalculateOptimalAxisInterval(validPoints.Min(p => p.Y), validPoints.Max(p => p.Y), out axisMin, out axisMax, out axisInterval);
73          scatterPlot.VisualProperties.YAxisMinimumAuto = false;
74          scatterPlot.VisualProperties.YAxisMaximumAuto = false;
75          scatterPlot.VisualProperties.YAxisMinimumFixedValue = axisMin;
76          scatterPlot.VisualProperties.YAxisMaximumFixedValue = axisMax;
77        } catch (ArgumentOutOfRangeException) { } // error during CalculateOptimalAxisInterval
78      }
[10987]79
80
[15110]81      //No Grouping
82      if (string.IsNullOrEmpty(variableNameGroup) || variableNameGroup == "-") {
83        ScatterPlotDataRow scdr = new ScatterPlotDataRow(variableNameX + " - " + variableNameY, "", validPoints);
84        scdr.VisualProperties.IsVisibleInLegend = false;
[13502]85        scatterPlot.Rows.Add(scdr);
[15110]86        return scatterPlot;
87      }
[13502]88
[15110]89      //Grouping
90      int groupVariableIndex = preprocessingData.GetColumnIndex(variableNameGroup);
91      var groupingValues = Enumerable.Empty<string>();
92
93      if (preprocessingData.VariableHasType<double>(groupVariableIndex)) {
94        groupingValues = preprocessingData.GetValues<double>(groupVariableIndex).Select(x => x.ToString());
95      } else if (preprocessingData.VariableHasType<string>(groupVariableIndex)) {
96        groupingValues = preprocessingData.GetValues<string>(groupVariableIndex);
97      } else if (preprocessingData.VariableHasType<DateTime>(groupVariableIndex)) {
98        groupingValues = preprocessingData.GetValues<DateTime>(groupVariableIndex).Select(x => x.ToString());
[10987]99      }
[15110]100      var groups = groupingValues.Zip(validPoints, Tuple.Create).GroupBy(t => t.Item1, t => t.Item2);
101
102      if (legendOrder == LegendOrder.Alphabetically)
103        groups = groups.OrderBy(x => x.Key, new NaturalStringComparer());
104
105      foreach (var group in groups) {
106        var scdr = new ScatterPlotDataRow {
107          Name = group.Key,
108          VisualProperties = {
109            IsVisibleInLegend = true,
110            PointSize = 6
111          }
112        };
113        scdr.Points.AddRange(group);
114        scatterPlot.Rows.Add(scdr);
115      }
[10987]116      return scatterPlot;
117    }
[10242]118  }
119}
Note: See TracBrowser for help on using the repository browser.