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, 6 years ago

#2809: merged branch to trunk

File size: 4.5 KB
RevLine 
[10539]1#region License Information
2/* HeuristicLab
[14185]3 * Copyright (C) 2002-2016 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
[15110]22using System;
[10539]23using System.Drawing;
[10992]24using System.Linq;
[15110]25using HeuristicLab.Analysis;
[10539]26using HeuristicLab.Common;
[10245]27using HeuristicLab.Core;
[15518]28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
[10245]29
[10539]30namespace HeuristicLab.DataPreprocessing {
[10313]31  [Item("Histogram", "Represents the histogram grid.")]
[15518]32  [StorableClass]
[10658]33  public class HistogramContent : PreprocessingChartContent {
[10992]34    public static new Image StaticItemImage {
35      get { return HeuristicLab.Common.Resources.VSImageLibrary.Statistics; }
[10252]36    }
37
[15518]38    [Storable]
[15110]39    public string GroupingVariableName { get; set; }
[10245]40
[15518]41    [Storable]
[15110]42    public int Bins { get; set; }
[15518]43    [Storable]
[15110]44    public bool ExactBins { get; set; }
[10871]45
[15518]46    [Storable]
[15110]47    public LegendOrder Order { get; set; }
[10871]48
[15518]49    #region Constructor, Cloning & Persistence
[10992]50    public HistogramContent(IFilteredPreprocessingData preprocessingData)
51      : base(preprocessingData) {
[15110]52      Bins = 10;
53      ExactBins = false;
[10245]54    }
55
[15518]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
[15518]67    [StorableConstructor]
68    protected HistogramContent(bool deserializing)
69      : base(deserializing) { }
70    #endregion
71
[15210]72    public static DataTable CreateHistogram(IFilteredPreprocessingData preprocessingData, string variableName, string groupingVariableName, DataTableVisualProperties.DataTableHistogramAggregation aggregation, LegendOrder legendOrder = LegendOrder.Alphabetically) {
[15110]73      var dataTable = new DataTable {
74        VisualProperties = { Title = variableName, HistogramAggregation = aggregation },
75      };
[10992]76
[15110]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;
[10992]82      }
[15110]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;
[10992]114    }
[10242]115  }
116}
Note: See TracBrowser for help on using the repository browser.