Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
07/03/17 10:57:53 (7 years ago)
Author:
pfleck
Message:

#2709: merged branch to trunk

Location:
trunk/sources/HeuristicLab.DataPreprocessing
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.DataPreprocessing

  • trunk/sources/HeuristicLab.DataPreprocessing/3.4

  • trunk/sources/HeuristicLab.DataPreprocessing/3.4/Content/ScatterPlotContent.cs

    r14185 r15110  
    2020#endregion
    2121
     22using System;
    2223using System.Collections.Generic;
    23 using System.Drawing;
    2424using System.Linq;
    2525using HeuristicLab.Analysis;
    2626using HeuristicLab.Common;
    27 using HeuristicLab.Core;
     27using HeuristicLab.Visualization.ChartControlsExtensions;
    2828
    2929namespace HeuristicLab.DataPreprocessing {
    3030
    31   [Item("ScatterPlot", "Represents a scatter plot.")]
    32   public class ScatterPlotContent : PreprocessingChartContent {
     31  public abstract class ScatterPlotContent : PreprocessingChartContent {
     32    public string GroupingVariable { get; set; }
    3333
    34     public string SelectedXVariable { get; set; }
    35     public string SelectedYVariable { get; set; }
    36     public string SelectedColorVariable { get; set; }
    37 
    38     public ScatterPlotContent(IFilteredPreprocessingData preprocessingData)
     34    protected ScatterPlotContent(IFilteredPreprocessingData preprocessingData)
    3935      : base(preprocessingData) {
    4036    }
    4137
    42     public ScatterPlotContent(ScatterPlotContent content, Cloner cloner)
     38    protected ScatterPlotContent(ScatterPlotContent content, Cloner cloner)
    4339      : base(content, cloner) {
    44       this.SelectedXVariable = content.SelectedXVariable;
    45       this.SelectedYVariable = content.SelectedYVariable;
    46       this.SelectedColorVariable = content.SelectedColorVariable;
    4740    }
    4841
    49     public static new Image StaticItemImage {
    50       get { return HeuristicLab.Common.Resources.VSImageLibrary.Performance; }
    51     }
    52 
    53     public override IDeepCloneable Clone(Cloner cloner) {
    54       return new ScatterPlotContent(this, cloner);
    55     }
    56 
    57     public ScatterPlot CreateScatterPlot(string variableNameX, string variableNameY, string variableNameColor = "-") {
     42    public static ScatterPlot CreateScatterPlot(IFilteredPreprocessingData preprocessingData, string variableNameX, string variableNameY, string variableNameGroup = "-", LegendOrder legendOrder = LegendOrder.Appearance) {
    5843      ScatterPlot scatterPlot = new ScatterPlot();
    5944
    60       IList<double> xValues = PreprocessingData.GetValues<double>(PreprocessingData.GetColumnIndex(variableNameX));
    61       IList<double> yValues = PreprocessingData.GetValues<double>(PreprocessingData.GetColumnIndex(variableNameY));
    62       if (variableNameColor == null || variableNameColor == "-") {
    63         List<Point2D<double>> points = new List<Point2D<double>>();
     45      IList<double> xValues = preprocessingData.GetValues<double>(preprocessingData.GetColumnIndex(variableNameX));
     46      IList<double> yValues = preprocessingData.GetValues<double>(preprocessingData.GetColumnIndex(variableNameY));
    6447
    65         for (int i = 0; i < xValues.Count; i++) {
    66           Point2D<double> point = new Point2D<double>(xValues[i], yValues[i]);
    67           points.Add(point);
    68         }
     48      var points = xValues.Zip(yValues, (x, y) => new Point2D<double>(x, y)).ToList();
     49      var validPoints = points.Where(p => !double.IsNaN(p.X) && !double.IsNaN(p.Y) && !double.IsInfinity(p.X) && !double.IsInfinity(p.Y)).ToList();
     50      if (validPoints.Any()) {
     51        try {
     52          double axisMin, axisMax, axisInterval;
     53          ChartUtil.CalculateOptimalAxisInterval(validPoints.Min(p => p.X), validPoints.Max(p => p.X), out axisMin, out axisMax, out axisInterval);
     54          scatterPlot.VisualProperties.XAxisMinimumAuto = false;
     55          scatterPlot.VisualProperties.XAxisMaximumAuto = false;
     56          scatterPlot.VisualProperties.XAxisMinimumFixedValue = axisMin;
     57          scatterPlot.VisualProperties.XAxisMaximumFixedValue = axisMax;
     58        } catch (ArgumentOutOfRangeException) { } // error during CalculateOptimalAxisInterval
     59        try {
     60          double axisMin, axisMax, axisInterval;
     61          ChartUtil.CalculateOptimalAxisInterval(validPoints.Min(p => p.Y), validPoints.Max(p => p.Y), out axisMin, out axisMax, out axisInterval);
     62          scatterPlot.VisualProperties.YAxisMinimumAuto = false;
     63          scatterPlot.VisualProperties.YAxisMaximumAuto = false;
     64          scatterPlot.VisualProperties.YAxisMinimumFixedValue = axisMin;
     65          scatterPlot.VisualProperties.YAxisMaximumFixedValue = axisMax;
     66        } catch (ArgumentOutOfRangeException) { } // error during CalculateOptimalAxisInterval
     67      }
    6968
    70         ScatterPlotDataRow scdr = new ScatterPlotDataRow(variableNameX + " - " + variableNameY, "", points);
     69
     70      //No Grouping
     71      if (string.IsNullOrEmpty(variableNameGroup) || variableNameGroup == "-") {
     72        ScatterPlotDataRow scdr = new ScatterPlotDataRow(variableNameX + " - " + variableNameY, "", validPoints);
     73        scdr.VisualProperties.IsVisibleInLegend = false;
    7174        scatterPlot.Rows.Add(scdr);
     75        return scatterPlot;
     76      }
    7277
    73       } else {
    74         var colorValues = PreprocessingData.GetValues<double>(PreprocessingData.GetColumnIndex(variableNameColor));
    75         var data = xValues.Zip(yValues, (x, y) => new { x, y }).Zip(colorValues, (v, c) => new { v.x, v.y, c }).ToList();
    76         var gradients = ColorGradient.Colors;
    77         int curGradient = 0;
    78         int numColors = colorValues.Distinct().Count();
    79         foreach (var colorValue in colorValues.Distinct()) {
    80           var values = data.Where(x => x.c == colorValue);
    81           var row = new ScatterPlotDataRow(
    82             variableNameX + " - " + variableNameY + " (" + colorValue + ")",
    83             "",
    84             values.Select(v => new Point2D<double>(v.x, v.y)),
    85             new ScatterPlotDataRowVisualProperties() { Color = gradients[curGradient] });
    86           curGradient += gradients.Count / numColors;
    87           scatterPlot.Rows.Add(row);
    88         }
     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      if (legendOrder == LegendOrder.Alphabetically)
     92        groups = groups.OrderBy(x => x.Key, new NaturalStringComparer());
     93
     94      foreach (var group in groups) {
     95        var scdr = new ScatterPlotDataRow {
     96          Name = group.Key,
     97          VisualProperties = {
     98            IsVisibleInLegend = true,
     99            PointSize = 6
     100          }
     101        };
     102        scdr.Points.AddRange(group);
     103        scatterPlot.Rows.Add(scdr);
    89104      }
    90105      return scatterPlot;
Note: See TracChangeset for help on using the changeset viewer.