Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DataPreprocessing Enhancements/HeuristicLab.DataPreprocessing/3.4/Content/PreprocessingChartContent.cs @ 14993

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

#2709

  • Added Legend order when grouping for histogram and (single and multi)scatterplot.
  • Removed the limitation of distinct values for the singlescatterplot (for the color gradient).
  • Added a legend-visible checkbox for the multi-scatterplot.
File size: 4.6 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.Collections.Generic;
24using System.Drawing;
25using System.Linq;
26using HeuristicLab.Analysis;
27using HeuristicLab.Common;
28using HeuristicLab.Common.Resources;
29using HeuristicLab.Core;
30using HeuristicLab.Data;
31
32namespace HeuristicLab.DataPreprocessing {
33  [Item("PreprocessingChart", "Represents a preprocessing chart.")]
34  public class PreprocessingChartContent : Item, IViewShortcut {
35
36    public enum LegendOrder {
37      Appearance,
38      Alphabetically
39    }
40
41    public static new Image StaticItemImage {
42      get { return VSImageLibrary.PieChart; }
43    }
44
45    private ICheckedItemList<StringValue> variableItemList = null;
46    public ICheckedItemList<StringValue> VariableItemList {
47      get {
48        if (variableItemList == null)
49          variableItemList = CreateVariableItemList(PreprocessingData);
50        return this.variableItemList;
51      }
52    }
53
54    public IFilteredPreprocessingData PreprocessingData { get; private set; }
55    public event DataPreprocessingChangedEventHandler Changed {
56      add { PreprocessingData.Changed += value; }
57      remove { PreprocessingData.Changed -= value; }
58    }
59
60    public PreprocessingChartContent(IFilteredPreprocessingData preprocessingData) {
61      PreprocessingData = preprocessingData;
62    }
63
64    public PreprocessingChartContent(PreprocessingChartContent content, Cloner cloner)
65      : base(content, cloner) {
66      this.PreprocessingData = content.PreprocessingData;
67      this.variableItemList = cloner.Clone<ICheckedItemList<StringValue>>(variableItemList);
68    }
69    public override IDeepCloneable Clone(Cloner cloner) {
70      return new PreprocessingChartContent(this, cloner);
71    }
72
73    public DataRow CreateDataRow(string variableName, DataRowVisualProperties.DataRowChartType chartType) {
74      return CreateDataRow(PreprocessingData, variableName, chartType);
75    }
76
77    public static DataRow CreateDataRow(IFilteredPreprocessingData preprocessingData, string variableName, DataRowVisualProperties.DataRowChartType chartType) {
78      IList<double> values = preprocessingData.GetValues<double>(preprocessingData.GetColumnIndex(variableName));
79      DataRow row = new DataRow(variableName, "", values);
80      row.VisualProperties.ChartType = chartType;
81      return row;
82    }
83
84    private static ICheckedItemList<StringValue> CreateVariableItemList(IPreprocessingData preprocessingData) {
85      ICheckedItemList<StringValue> itemList = new CheckedItemList<StringValue>();
86      foreach (string name in preprocessingData.GetDoubleVariableNames()) {
87        var n = new StringValue(name);
88        bool isInputTarget = preprocessingData.InputVariables.Contains(name) || preprocessingData.TargetVariable == name;
89        itemList.Add(n, isInputTarget);
90      }
91      return new ReadOnlyCheckedItemList<StringValue>(itemList);
92    }
93
94    private const int MAX_DISTINCT_VALUES_FOR_GROUPING = 20;
95    public static IEnumerable<string> GetVariableNamesForGrouping(IPreprocessingData preprocessingData) {
96      var variableNames = new List<string>();
97
98      for (int i = 0; i < preprocessingData.Columns; ++i) {
99        int distinctValues = Int32.MaxValue;
100        if (preprocessingData.VariableHasType<double>(i))
101          distinctValues = preprocessingData.GetValues<double>(i).GroupBy(x => x).Count();
102        else if (preprocessingData.VariableHasType<string>(i))
103          distinctValues = preprocessingData.GetValues<string>(i).GroupBy(x => x).Count();
104        else if (preprocessingData.VariableHasType<DateTime>(i))
105          distinctValues = preprocessingData.GetValues<DateTime>(i).GroupBy(x => x).Count();
106
107        if (distinctValues <= MAX_DISTINCT_VALUES_FOR_GROUPING)
108          variableNames.Add(preprocessingData.GetVariableName(i));
109      }
110      return variableNames;
111    }
112  }
113}
Note: See TracBrowser for help on using the repository browser.