Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.DataPreprocessing/3.4/Content/HistogramContent.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: 4.5 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;
[10539]23using System.Drawing;
[10992]24using System.Linq;
[15242]25using HeuristicLab.Analysis;
[10539]26using HeuristicLab.Common;
[10245]27using HeuristicLab.Core;
[17097]28using HEAL.Attic;
[10245]29
[10539]30namespace HeuristicLab.DataPreprocessing {
[10313]31  [Item("Histogram", "Represents the histogram grid.")]
[17097]32  [StorableType("FD29EAB3-ABE6-49F4-A162-16E11790F7D5")]
[10658]33  public class HistogramContent : PreprocessingChartContent {
[10992]34    public static new Image StaticItemImage {
35      get { return HeuristicLab.Common.Resources.VSImageLibrary.Statistics; }
[10252]36    }
37
[15535]38    [Storable]
[15242]39    public string GroupingVariableName { get; set; }
[10245]40
[15535]41    [Storable]
[15242]42    public int Bins { get; set; }
[15535]43    [Storable]
[15242]44    public bool ExactBins { get; set; }
[10871]45
[15535]46    [Storable]
[15242]47    public LegendOrder Order { get; set; }
[10871]48
[15535]49    #region Constructor, Cloning & Persistence
[10992]50    public HistogramContent(IFilteredPreprocessingData preprocessingData)
51      : base(preprocessingData) {
[15242]52      Bins = 10;
53      ExactBins = false;
[10245]54    }
55
[15535]56    public HistogramContent(HistogramContent original, Cloner cloner)
57      : base(original, cloner) {
58      GroupingVariableName = original.GroupingVariableName;
59      Bins = original.Bins;
60      ExactBins = original.ExactBins;
61      Order = original.Order;
[10992]62    }
[10539]63    public override IDeepCloneable Clone(Cloner cloner) {
[10245]64      return new HistogramContent(this, cloner);
65    }
[10992]66
[15535]67    [StorableConstructor]
[17097]68    protected HistogramContent(StorableConstructorFlag _) : base(_) { }
[15535]69    #endregion
70
[15242]71    public static DataTable CreateHistogram(IFilteredPreprocessingData preprocessingData, string variableName, string groupingVariableName, DataTableVisualProperties.DataTableHistogramAggregation aggregation, LegendOrder legendOrder = LegendOrder.Alphabetically) {
72      var dataTable = new DataTable {
73        VisualProperties = { Title = variableName, HistogramAggregation = aggregation },
74      };
[10992]75
[15242]76      if (string.IsNullOrEmpty(groupingVariableName)) {
77        var row = PreprocessingChartContent.CreateDataRow(preprocessingData, variableName, DataRowVisualProperties.DataRowChartType.Histogram);
78        row.VisualProperties.IsVisibleInLegend = false;
79        dataTable.Rows.Add(row);
80        return dataTable;
[10992]81      }
[15242]82
83      int variableIndex = preprocessingData.GetColumnIndex(variableName);
84      var variableValues = preprocessingData.GetValues<double>(variableIndex);
85      int groupVariableIndex = preprocessingData.GetColumnIndex(groupingVariableName);
86      var groupingValues = Enumerable.Empty<string>();
87
88      if (preprocessingData.VariableHasType<double>(groupVariableIndex)) {
89        groupingValues = preprocessingData.GetValues<double>(groupVariableIndex).Select(x => x.ToString());
90      } else if (preprocessingData.VariableHasType<string>(groupVariableIndex)) {
91        groupingValues = preprocessingData.GetValues<string>(groupVariableIndex);
92      } else if (preprocessingData.VariableHasType<DateTime>(groupVariableIndex)) {
93        groupingValues = preprocessingData.GetValues<DateTime>(groupVariableIndex).Select(x => x.ToString());
94      }
95
96      var groups = groupingValues.Zip(variableValues, Tuple.Create).GroupBy(t => t.Item1, t => t.Item2);
97
98      if (legendOrder == LegendOrder.Alphabetically)
99        groups = groups.OrderBy(x => x.Key, new NaturalStringComparer());
100
101      foreach (var group in groups) {
102        var classRow = new DataRow {
103          Name = group.Key,
104          VisualProperties = {
105              ChartType = DataRowVisualProperties.DataRowChartType.Histogram,
106              IsVisibleInLegend = !string.IsNullOrEmpty(groupingVariableName)
107            }
108        };
109        classRow.Values.AddRange(group);
110        dataTable.Rows.Add(classRow);
111      }
112      return dataTable;
[10992]113    }
[10242]114  }
115}
Note: See TracBrowser for help on using the repository browser.