Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.DataPreprocessing/3.4/Content/HistogramContent.cs @ 15518

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

#2809: merged branch to trunk

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