[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] | 22 | using System;
|
---|
[10539] | 23 | using System.Drawing;
|
---|
[10992] | 24 | using System.Linq;
|
---|
[15110] | 25 | using HeuristicLab.Analysis;
|
---|
[10539] | 26 | using HeuristicLab.Common;
|
---|
[10245] | 27 | using HeuristicLab.Core;
|
---|
| 28 |
|
---|
[10539] | 29 | namespace HeuristicLab.DataPreprocessing {
|
---|
[10313] | 30 | [Item("Histogram", "Represents the histogram grid.")]
|
---|
[10658] | 31 | public class HistogramContent : PreprocessingChartContent {
|
---|
[10992] | 32 | public static new Image StaticItemImage {
|
---|
| 33 | get { return HeuristicLab.Common.Resources.VSImageLibrary.Statistics; }
|
---|
[10252] | 34 | }
|
---|
| 35 |
|
---|
[15110] | 36 | public string GroupingVariableName { get; set; }
|
---|
[10245] | 37 |
|
---|
[15110] | 38 | public int Bins { get; set; }
|
---|
| 39 | public bool ExactBins { get; set; }
|
---|
[10871] | 40 |
|
---|
[15110] | 41 | public LegendOrder Order { get; set; }
|
---|
[10871] | 42 |
|
---|
[10992] | 43 | public HistogramContent(IFilteredPreprocessingData preprocessingData)
|
---|
| 44 | : base(preprocessingData) {
|
---|
[15110] | 45 | Bins = 10;
|
---|
| 46 | ExactBins = false;
|
---|
[10245] | 47 | }
|
---|
| 48 |
|
---|
[10992] | 49 | public HistogramContent(HistogramContent content, Cloner cloner)
|
---|
| 50 | : base(content, cloner) {
|
---|
| 51 | }
|
---|
[10539] | 52 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
[10245] | 53 | return new HistogramContent(this, cloner);
|
---|
| 54 | }
|
---|
[10992] | 55 |
|
---|
[15210] | 56 | public static DataTable CreateHistogram(IFilteredPreprocessingData preprocessingData, string variableName, string groupingVariableName, DataTableVisualProperties.DataTableHistogramAggregation aggregation, LegendOrder legendOrder = LegendOrder.Alphabetically) {
|
---|
[15110] | 57 | var dataTable = new DataTable {
|
---|
| 58 | VisualProperties = { Title = variableName, HistogramAggregation = aggregation },
|
---|
| 59 | };
|
---|
[10992] | 60 |
|
---|
[15110] | 61 | if (string.IsNullOrEmpty(groupingVariableName)) {
|
---|
| 62 | var row = PreprocessingChartContent.CreateDataRow(preprocessingData, variableName, DataRowVisualProperties.DataRowChartType.Histogram);
|
---|
| 63 | row.VisualProperties.IsVisibleInLegend = false;
|
---|
| 64 | dataTable.Rows.Add(row);
|
---|
| 65 | return dataTable;
|
---|
[10992] | 66 | }
|
---|
[15110] | 67 |
|
---|
| 68 | int variableIndex = preprocessingData.GetColumnIndex(variableName);
|
---|
| 69 | var variableValues = preprocessingData.GetValues<double>(variableIndex);
|
---|
| 70 | int groupVariableIndex = preprocessingData.GetColumnIndex(groupingVariableName);
|
---|
| 71 | var groupingValues = Enumerable.Empty<string>();
|
---|
| 72 |
|
---|
| 73 | if (preprocessingData.VariableHasType<double>(groupVariableIndex)) {
|
---|
| 74 | groupingValues = preprocessingData.GetValues<double>(groupVariableIndex).Select(x => x.ToString());
|
---|
| 75 | } else if (preprocessingData.VariableHasType<string>(groupVariableIndex)) {
|
---|
| 76 | groupingValues = preprocessingData.GetValues<string>(groupVariableIndex);
|
---|
| 77 | } else if (preprocessingData.VariableHasType<DateTime>(groupVariableIndex)) {
|
---|
| 78 | groupingValues = preprocessingData.GetValues<DateTime>(groupVariableIndex).Select(x => x.ToString());
|
---|
| 79 | }
|
---|
| 80 |
|
---|
| 81 | var groups = groupingValues.Zip(variableValues, Tuple.Create).GroupBy(t => t.Item1, t => t.Item2);
|
---|
| 82 |
|
---|
| 83 | if (legendOrder == LegendOrder.Alphabetically)
|
---|
| 84 | groups = groups.OrderBy(x => x.Key, new NaturalStringComparer());
|
---|
| 85 |
|
---|
| 86 | foreach (var group in groups) {
|
---|
| 87 | var classRow = new DataRow {
|
---|
| 88 | Name = group.Key,
|
---|
| 89 | VisualProperties = {
|
---|
| 90 | ChartType = DataRowVisualProperties.DataRowChartType.Histogram,
|
---|
| 91 | IsVisibleInLegend = !string.IsNullOrEmpty(groupingVariableName)
|
---|
| 92 | }
|
---|
| 93 | };
|
---|
| 94 | classRow.Values.AddRange(group);
|
---|
| 95 | dataTable.Rows.Add(classRow);
|
---|
| 96 | }
|
---|
| 97 | return dataTable;
|
---|
[10992] | 98 | }
|
---|
[10242] | 99 | }
|
---|
| 100 | }
|
---|