Free cookie consent management tool by TermsFeed Policy Generator

Changeset 14579


Ignore:
Timestamp:
01/18/17 11:17:19 (7 years ago)
Author:
mkommend
Message:

#2709: Refactored histogram view and content to support grouping by string and datetime variables.

Location:
branches/DataPreprocessing Enhancements
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/DataPreprocessing Enhancements/HeuristicLab.DataPreprocessing.Views/3.4/HistogramView.cs

    r14474 r14579  
    2121
    2222using System;
    23 using System.Collections.Generic;
    2423using System.Linq;
    2524using HeuristicLab.Analysis;
     
    2726
    2827namespace HeuristicLab.DataPreprocessing.Views {
    29 
    3028  [View("Histogram View")]
    3129  [Content(typeof(HistogramContent), true)]
    3230  public partial class HistogramView : PreprocessingChartView {
    33 
    34     public List<double> Classification { get; set; }
    35     public bool IsDetailedChartViewEnabled { get; set; }
     31    public new HistogramContent Content {
     32      get { return (HistogramContent)base.Content; }
     33      set { base.Content = value; }
     34    }
    3635
    3736    public HistogramView() {
     
    4140    protected override void OnContentChanged() {
    4241      base.OnContentChanged();
     42      classifierComboBox.Items.Clear();
     43      classifierComboBox.Items.Add("");
     44
    4345      if (Content != null) {
    44         classifierComboBox.Items.Clear();
    45         classifierComboBox.Items.Add("None");
    46 
    47         foreach (string var in Content.GetVariableNamesForHistogramClassification()) {
     46        foreach (string var in Content.GetVariableNamesForGrouping()) {
    4847          classifierComboBox.Items.Add(var);
    4948        }
    5049
    51         if (classifierComboBox.SelectedItem == null && Content.ClassifierVariableIndex < classifierComboBox.Items.Count) {
    52           classifierComboBox.SelectedIndex = Content.ClassifierVariableIndex;
    53         }
     50        classifierComboBox.SelectedItem = Content.GroupingVariableName;
    5451      }
    5552    }
    5653
    5754    protected override DataTable CreateDataTable(string variableName) {
    58       var dt = new DataTable();
    59       var row = Content.CreateDataRow(variableName, DataRowVisualProperties.DataRowChartType.Histogram);
    60       if (Classification == null) {
    61         dt.Rows.Add(row);
    62       } else {
    63         dt.VisualProperties.Title = variableName;
    64         var valuesPerClass = row.Values.Zip(Classification, (value, @class) => new { value, @class })
    65                                 .GroupBy(x => x.@class)
    66                                 .ToDictionary(x => x.Key, x => x.Select(v => v.value));
    67         foreach (var entry in valuesPerClass) {
    68           var classRow = new DataRow(string.Format("{0} ({1})", classifierComboBox.SelectedItem, entry.Key));
    69           classRow.VisualProperties.ChartType = DataRowVisualProperties.DataRowChartType.Histogram;
    70           classRow.Values.AddRange(entry.Value);
    71           dt.Rows.Add(classRow);
    72         }
     55      var dataTable = new DataTable();
     56
     57      if (string.IsNullOrEmpty(Content.GroupingVariableName)) {
     58        var row = Content.CreateDataRow(variableName, DataRowVisualProperties.DataRowChartType.Histogram);
     59        dataTable.Rows.Add(row);
     60        return dataTable;
    7361      }
    74       return dt;
     62
     63      dataTable.VisualProperties.Title = variableName;
     64
     65      int variableIndex = Content.PreprocessingData.GetColumnIndex(variableName);
     66      var variableValues = Content.PreprocessingData.GetValues<double>(variableIndex);
     67      int groupVariableIndex = Content.PreprocessingData.GetColumnIndex(Content.GroupingVariableName);
     68      var groupingValues = Enumerable.Empty<string>();
     69
     70      if (Content.PreprocessingData.VariableHasType<double>(groupVariableIndex)) {
     71        groupingValues = Content.PreprocessingData.GetValues<double>(groupVariableIndex).Select(x => x.ToString());
     72      } else if (Content.PreprocessingData.VariableHasType<string>(groupVariableIndex)) {
     73        groupingValues = Content.PreprocessingData.GetValues<string>(groupVariableIndex);
     74      } else if (Content.PreprocessingData.VariableHasType<DateTime>(groupVariableIndex)) {
     75        groupingValues = Content.PreprocessingData.GetValues<DateTime>(groupVariableIndex).Select(x => x.ToString());
     76      }
     77
     78      var groups = groupingValues.Zip(variableValues, Tuple.Create).GroupBy(t => t.Item1, t => t.Item2);
     79
     80      foreach (var group in groups) {
     81        var classRow = new DataRow();
     82        classRow.Name = group.Key;
     83        classRow.VisualProperties.ChartType = DataRowVisualProperties.DataRowChartType.Histogram;
     84        classRow.Values.AddRange(group);
     85        dataTable.Rows.Add(classRow);
     86      }
     87      return dataTable;
    7588    }
    7689
    77     public new HistogramContent Content {
    78       get { return (HistogramContent)base.Content; }
    79       set { base.Content = value; }
    80     }
    8190
    8291    private void classifierComboBox_SelectedIndexChanged(object sender, EventArgs e) {
    83       if (classifierComboBox.SelectedItem == null)
    84         return;
    85 
    86       if (classifierComboBox.SelectedIndex != 0) {
    87         int columndIndex = Content.PreprocessingData.GetColumnIndex(classifierComboBox.SelectedItem.ToString());
    88         Classification = Content.PreprocessingData.GetValues<double>(columndIndex).ToList();
    89       } else {
    90         Classification = null;
    91       }
    92 
    93       Content.ClassifierVariableIndex = classifierComboBox.SelectedIndex;
     92      Content.GroupingVariableName = classifierComboBox.SelectedItem.ToString();
    9493
    9594      // rebuild datatables
  • branches/DataPreprocessing Enhancements/HeuristicLab.DataPreprocessing/3.4/Content/HistogramContent.cs

    r14459 r14579  
    2020#endregion
    2121
     22using System;
    2223using System.Collections.Generic;
    2324using System.Drawing;
     
    3334      get { return HeuristicLab.Common.Resources.VSImageLibrary.Statistics; }
    3435    }
    35     private const int MAX_DISTINCT_VALUES_FOR_CLASSIFCATION = 20;
    3636
    37     public int ClassifierVariableIndex { get; set; }
     37    private const int MAX_DISTINCT_VALUES_FOR_GROUPING = 20;
     38
     39    public string GroupingVariableName { get; set; }
    3840
    3941    public int Bins { get; set; }
     
    5355    }
    5456
    55     public IEnumerable<string> GetVariableNamesForHistogramClassification() {
    56       List<string> doubleVariableNames = new List<string>();
     57    public IEnumerable<string> GetVariableNamesForGrouping() {
     58      var variableNames = new List<string>();
    5759
    58       //only return variable names from type double
    5960      for (int i = 0; i < PreprocessingData.Columns; ++i) {
    60         if (PreprocessingData.VariableHasType<double>(i)) {
    61           double distinctValueCount = PreprocessingData.GetValues<double>(i).GroupBy(x => x).Count();
    62           bool distinctValuesOk = distinctValueCount <= MAX_DISTINCT_VALUES_FOR_CLASSIFCATION;
    63           if (distinctValuesOk)
    64             doubleVariableNames.Add(PreprocessingData.GetVariableName(i));
    65         }
     61        int distinctValues = int.MaxValue;
     62        if (PreprocessingData.VariableHasType<double>(i))
     63          distinctValues = PreprocessingData.GetValues<double>(i).GroupBy(x => x).Count();
     64        else if (PreprocessingData.VariableHasType<string>(i))
     65          distinctValues = PreprocessingData.GetValues<string>(i).GroupBy(x => x).Count();
     66        else if (PreprocessingData.VariableHasType<DateTime>(i))
     67          distinctValues = PreprocessingData.GetValues<DateTime>(i).GroupBy(x => x).Count();
     68
     69        if (distinctValues <= MAX_DISTINCT_VALUES_FOR_GROUPING)
     70          variableNames.Add(PreprocessingData.GetVariableName(i));
    6671      }
    67       return doubleVariableNames;
     72      return variableNames;
    6873    }
    6974  }
Note: See TracChangeset for help on using the changeset viewer.