Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2520_PersistenceReintegration/HeuristicLab.DataPreprocessing/3.4/Content/PreprocessingChartContent.cs @ 16462

Last change on this file since 16462 was 16462, checked in by jkarder, 5 years ago

#2520: worked on reintegration of new persistence

  • added nuget references to HEAL.Fossil
  • added StorableType attributes to many classes
  • changed signature of StorableConstructors
  • removed some classes in old persistence
  • removed some unnecessary usings
File size: 4.7 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2019 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;
31using HEAL.Fossil;
32
33namespace HeuristicLab.DataPreprocessing {
34  [Item("PreprocessingChart", "Represents a preprocessing chart.")]
35  [StorableType("7EDAFA6E-E4B1-4150-BB57-280A9F9E61D8")]
36  public class PreprocessingChartContent : PreprocessingContent, IViewShortcut {
37    public enum LegendOrder {
38      Alphabetically,
39      Appearance
40    }
41
42    public static new Image StaticItemImage {
43      get { return VSImageLibrary.PieChart; }
44    }
45
46    [Storable]
47    private ICheckedItemList<StringValue> variableItemList;
48    public ICheckedItemList<StringValue> VariableItemList {
49      get {
50        if (variableItemList == null)
51          variableItemList = CreateVariableItemList(PreprocessingData);
52        return variableItemList;
53      }
54    }
55
56    public event DataPreprocessingChangedEventHandler Changed {
57      add { PreprocessingData.Changed += value; }
58      remove { PreprocessingData.Changed -= value; }
59    }
60
61    #region Constructor, Cloning & Persistence
62    public PreprocessingChartContent(IFilteredPreprocessingData preprocessingData)
63       : base(preprocessingData) {
64    }
65
66    public PreprocessingChartContent(PreprocessingChartContent original, Cloner cloner)
67      : base(original, cloner) {
68      variableItemList = cloner.Clone(original.variableItemList);
69    }
70    public override IDeepCloneable Clone(Cloner cloner) {
71      return new PreprocessingChartContent(this, cloner);
72    }
73
74    [StorableConstructor]
75    protected PreprocessingChartContent(StorableConstructorFlag _) : base(_) { }
76    #endregion
77
78    public DataRow CreateDataRow(string variableName, DataRowVisualProperties.DataRowChartType chartType) {
79      return CreateDataRow(PreprocessingData, variableName, chartType);
80    }
81
82    public static DataRow CreateDataRow(IFilteredPreprocessingData preprocessingData, string variableName, DataRowVisualProperties.DataRowChartType chartType) {
83      var values = preprocessingData.GetValues<double>(preprocessingData.GetColumnIndex(variableName));
84      var row = new DataRow(variableName, "", values) {
85        VisualProperties = {
86          ChartType = chartType,
87          StartIndexZero = true
88        }
89      };
90      return row;
91    }
92
93    private static ICheckedItemList<StringValue> CreateVariableItemList(IPreprocessingData preprocessingData) {
94      ICheckedItemList<StringValue> itemList = new CheckedItemList<StringValue>();
95      foreach (string name in preprocessingData.GetDoubleVariableNames()) {
96        var n = new StringValue(name);
97        bool isInputTarget = preprocessingData.InputVariables.Contains(name) || preprocessingData.TargetVariable == name;
98        itemList.Add(n, isInputTarget);
99      }
100      return new ReadOnlyCheckedItemList<StringValue>(itemList);
101    }
102
103    public static IEnumerable<string> GetVariableNamesForGrouping(IPreprocessingData preprocessingData, int maxDistinctValues = 20) {
104      var variableNames = new List<string>();
105
106      for (int i = 0; i < preprocessingData.Columns; ++i) {
107        int distinctValues = Int32.MaxValue;
108        if (preprocessingData.VariableHasType<double>(i))
109          distinctValues = preprocessingData.GetValues<double>(i).GroupBy(x => x).Count();
110        else if (preprocessingData.VariableHasType<string>(i))
111          distinctValues = preprocessingData.GetValues<string>(i).GroupBy(x => x).Count();
112        else if (preprocessingData.VariableHasType<DateTime>(i))
113          distinctValues = preprocessingData.GetValues<DateTime>(i).GroupBy(x => x).Count();
114
115        if (distinctValues <= maxDistinctValues)
116          variableNames.Add(preprocessingData.GetVariableName(i));
117      }
118      return variableNames;
119    }
120  }
121}
Note: See TracBrowser for help on using the repository browser.