Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.DataPreprocessing/3.4/Content/PreprocessingChartContent.cs @ 15110

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

#2709: merged branch to trunk

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    public enum LegendOrder {
36      Appearance,
37      Alphabetically
38    }
39
40    public static new Image StaticItemImage {
41      get { return VSImageLibrary.PieChart; }
42    }
43
44    private ICheckedItemList<StringValue> variableItemList = null;
45    public ICheckedItemList<StringValue> VariableItemList {
46      get {
47        if (variableItemList == null)
48          variableItemList = CreateVariableItemList(PreprocessingData);
49        return this.variableItemList;
50      }
51    }
52
53    public IFilteredPreprocessingData PreprocessingData { get; private set; }
54    public event DataPreprocessingChangedEventHandler Changed {
55      add { PreprocessingData.Changed += value; }
56      remove { PreprocessingData.Changed -= value; }
57    }
58
59    public PreprocessingChartContent(IFilteredPreprocessingData preprocessingData) {
60      PreprocessingData = preprocessingData;
61    }
62
63    public PreprocessingChartContent(PreprocessingChartContent content, Cloner cloner)
64      : base(content, cloner) {
65      this.PreprocessingData = content.PreprocessingData;
66      this.variableItemList = cloner.Clone<ICheckedItemList<StringValue>>(variableItemList);
67    }
68    public override IDeepCloneable Clone(Cloner cloner) {
69      return new PreprocessingChartContent(this, cloner);
70    }
71
72    public DataRow CreateDataRow(string variableName, DataRowVisualProperties.DataRowChartType chartType) {
73      return CreateDataRow(PreprocessingData, variableName, chartType);
74    }
75
76    public static DataRow CreateDataRow(IFilteredPreprocessingData preprocessingData, string variableName, DataRowVisualProperties.DataRowChartType chartType) {
77      IList<double> values = preprocessingData.GetValues<double>(preprocessingData.GetColumnIndex(variableName));
78      DataRow row = new DataRow(variableName, "", values);
79      row.VisualProperties.ChartType = chartType;
80      return row;
81    }
82
83    private static ICheckedItemList<StringValue> CreateVariableItemList(IPreprocessingData preprocessingData) {
84      ICheckedItemList<StringValue> itemList = new CheckedItemList<StringValue>();
85      foreach (string name in preprocessingData.GetDoubleVariableNames()) {
86        var n = new StringValue(name);
87        bool isInputTarget = preprocessingData.InputVariables.Contains(name) || preprocessingData.TargetVariable == name;
88        itemList.Add(n, isInputTarget);
89      }
90      return new ReadOnlyCheckedItemList<StringValue>(itemList);
91    }
92
93    private const int MAX_DISTINCT_VALUES_FOR_GROUPING = 20;
94    public static IEnumerable<string> GetVariableNamesForGrouping(IPreprocessingData preprocessingData) {
95      var variableNames = new List<string>();
96
97      for (int i = 0; i < preprocessingData.Columns; ++i) {
98        int distinctValues = Int32.MaxValue;
99        if (preprocessingData.VariableHasType<double>(i))
100          distinctValues = preprocessingData.GetValues<double>(i).GroupBy(x => x).Count();
101        else if (preprocessingData.VariableHasType<string>(i))
102          distinctValues = preprocessingData.GetValues<string>(i).GroupBy(x => x).Count();
103        else if (preprocessingData.VariableHasType<DateTime>(i))
104          distinctValues = preprocessingData.GetValues<DateTime>(i).GroupBy(x => x).Count();
105
106        if (distinctValues <= MAX_DISTINCT_VALUES_FOR_GROUPING)
107          variableNames.Add(preprocessingData.GetVariableName(i));
108      }
109      return variableNames;
110    }
111  }
112}
Note: See TracBrowser for help on using the repository browser.