Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
03/07/17 15:32:44 (8 years ago)
Author:
mkommend
Message:

#2709: Adapted data preprocessing scatter plot to allow grouping of string variables.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/DataPreprocessing Enhancements/HeuristicLab.DataPreprocessing/3.4/Content/ScatterPlotContent.cs

    r14525 r14724  
    2525using HeuristicLab.Analysis;
    2626using HeuristicLab.Common;
    27 using HeuristicLab.Problems.DataAnalysis;
    2827using HeuristicLab.Visualization.ChartControlsExtensions;
    2928
     
    5554          scatterPlot.VisualProperties.XAxisMinimumFixedValue = axisMin;
    5655          scatterPlot.VisualProperties.XAxisMaximumFixedValue = axisMax;
    57         } catch (ArgumentOutOfRangeException) { } // error during CalculateOptimalAxisInterval
     56        }
     57        catch (ArgumentOutOfRangeException) { } // error during CalculateOptimalAxisInterval
    5858        try {
    5959          double axisMin, axisMax, axisInterval;
     
    6363          scatterPlot.VisualProperties.YAxisMinimumFixedValue = axisMin;
    6464          scatterPlot.VisualProperties.YAxisMaximumFixedValue = axisMax;
    65         } catch (ArgumentOutOfRangeException) { } // error during CalculateOptimalAxisInterval
     65        }
     66        catch (ArgumentOutOfRangeException) { } // error during CalculateOptimalAxisInterval
    6667      }
    6768
    68       if (variableNameGroup == null || variableNameGroup == "-") {
     69
     70      //No Grouping
     71      if (string.IsNullOrEmpty(variableNameGroup) || variableNameGroup == "-") {
    6972        ScatterPlotDataRow scdr = new ScatterPlotDataRow(variableNameX + " - " + variableNameY, "", validPoints);
    7073        scdr.VisualProperties.IsVisibleInLegend = false;
    7174        scatterPlot.Rows.Add(scdr);
    72       } else {
    73         var groupValues = PreprocessingData.GetValues<double>(PreprocessingData.GetColumnIndex(variableNameGroup));
    74         var data = points.Zip(groupValues, (p, g) => new { p, g })
    75           .Where(x => !double.IsNaN(x.p.X) && !double.IsNaN(x.p.Y) && !double.IsInfinity(x.p.X) && !double.IsInfinity(x.p.Y))
    76           .ToList();
     75        return scatterPlot;
     76      }
    7777
    78         foreach (var groupValue in groupValues.Distinct().OrderBy(g => g)) {
    79           var values = data.Where(x => x.g == groupValue || (double.IsNaN(x.g) && double.IsNaN(groupValue))).Select(v => v.p);
    80           var row = new ScatterPlotDataRow(string.Format("{0} ({1})", variableNameGroup, groupValue), "", values) {
    81             Name = groupValue.ToString("R"),
    82             VisualProperties = { PointSize = 6 }
    83           };
    84           scatterPlot.Rows.Add(row);
    85         }
     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      foreach (var group in groups) {
     92        var scdr = new ScatterPlotDataRow();
     93        scdr.Name = group.Key;
     94        scdr.VisualProperties.IsVisibleInLegend = true;
     95        scdr.VisualProperties.PointSize = 6;
     96        scdr.Points.AddRange(group);
     97        scatterPlot.Rows.Add(scdr);
    8698      }
    8799      return scatterPlot;
    88100    }
    89101
    90     public DataRow GetCorrelationRow(string variableNameX, string variableNameY) {
    91       var xValues = PreprocessingData.GetValues<double>(PreprocessingData.GetColumnIndex(variableNameX));
    92       var yValues = PreprocessingData.GetValues<double>(PreprocessingData.GetColumnIndex(variableNameY));
    93 
    94       double k, d;
    95       OnlineCalculatorError err;
    96       OnlineLinearScalingParameterCalculator.Calculate(xValues, yValues, out k, out d, out err);
    97       double p = OnlinePearsonsRCalculator.Calculate(xValues, yValues, out err);
    98 
    99       var data = new double[xValues.Count];
    100       for (int i = 0; i < xValues.Count; i++) {
    101         data[i]= k * i + d;
    102       }
    103 
    104       return new DataRow(string.Format("Correlation (R²={0})", p*p), "", data);
    105     }
    106102  }
    107103}
Note: See TracChangeset for help on using the changeset viewer.