Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 17712 was 17180, checked in by swagner, 5 years ago

#2875: Removed years in copyrights

File size: 4.5 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 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 HEAL.Attic;
29
30namespace HeuristicLab.DataPreprocessing {
31  [Item("Histogram", "Represents the histogram grid.")]
32  [StorableType("FD29EAB3-ABE6-49F4-A162-16E11790F7D5")]
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(StorableConstructorFlag _) : base(_) { }
69    #endregion
70
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      };
75
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;
81      }
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;
113    }
114  }
115}
Note: See TracBrowser for help on using the repository browser.