Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.DataPreprocessing/3.4/Content/PreprocessingChartContent.cs @ 17189

Last change on this file since 17189 was 17181, checked in by swagner, 5 years ago

#2875: Merged r17180 from trunk to stable

File size: 4.8 KB
RevLine 
[10539]1#region License Information
2/* HeuristicLab
[17181]3 * Copyright (C) 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
[10992]22using System;
23using System.Collections.Generic;
[10539]24using System.Drawing;
[15242]25using System.Linq;
[10992]26using HeuristicLab.Analysis;
[10539]27using HeuristicLab.Common;
[15242]28using HeuristicLab.Common.Resources;
[10245]29using HeuristicLab.Core;
[10818]30using HeuristicLab.Data;
[17097]31using HEAL.Attic;
[10242]32
[10539]33namespace HeuristicLab.DataPreprocessing {
[10658]34  [Item("PreprocessingChart", "Represents a preprocessing chart.")]
[17097]35  [StorableType("7EDAFA6E-E4B1-4150-BB57-280A9F9E61D8")]
[15535]36  public class PreprocessingChartContent : PreprocessingContent, IViewShortcut {
[17097]37    [StorableType("d4c1c81a-f0c5-496f-8264-76b75572c0fc")]
[15242]38    public enum LegendOrder {
39      Alphabetically,
40      Appearance
[10992]41    }
[10252]42
[15242]43    public static new Image StaticItemImage {
44      get { return VSImageLibrary.PieChart; }
[10992]45    }
[10818]46
[15535]47    [Storable]
48    private ICheckedItemList<StringValue> variableItemList;
[10992]49    public ICheckedItemList<StringValue> VariableItemList {
[15242]50      get {
51        if (variableItemList == null)
52          variableItemList = CreateVariableItemList(PreprocessingData);
[15535]53        return variableItemList;
[15242]54      }
[10992]55    }
[10818]56
[15242]57    public event DataPreprocessingChangedEventHandler Changed {
58      add { PreprocessingData.Changed += value; }
59      remove { PreprocessingData.Changed -= value; }
60    }
[10818]61
[15535]62    #region Constructor, Cloning & Persistence
63    public PreprocessingChartContent(IFilteredPreprocessingData preprocessingData)
64       : base(preprocessingData) {
[10252]65    }
66
[15535]67    public PreprocessingChartContent(PreprocessingChartContent original, Cloner cloner)
68      : base(original, cloner) {
69      variableItemList = cloner.Clone(original.variableItemList);
[10245]70    }
[10992]71    public override IDeepCloneable Clone(Cloner cloner) {
72      return new PreprocessingChartContent(this, cloner);
73    }
[10245]74
[15535]75    [StorableConstructor]
[17097]76    protected PreprocessingChartContent(StorableConstructorFlag _) : base(_) { }
[15535]77    #endregion
78
[10992]79    public DataRow CreateDataRow(string variableName, DataRowVisualProperties.DataRowChartType chartType) {
[15242]80      return CreateDataRow(PreprocessingData, variableName, chartType);
[10252]81    }
82
[15242]83    public static DataRow CreateDataRow(IFilteredPreprocessingData preprocessingData, string variableName, DataRowVisualProperties.DataRowChartType chartType) {
[15535]84      var values = preprocessingData.GetValues<double>(preprocessingData.GetColumnIndex(variableName));
85      var row = new DataRow(variableName, "", values) {
86        VisualProperties = {
87          ChartType = chartType,
88          StartIndexZero = true
89        }
90      };
[10992]91      return row;
[10573]92    }
[10818]93
[15242]94    private static ICheckedItemList<StringValue> CreateVariableItemList(IPreprocessingData preprocessingData) {
[10992]95      ICheckedItemList<StringValue> itemList = new CheckedItemList<StringValue>();
[15242]96      foreach (string name in preprocessingData.GetDoubleVariableNames()) {
[12676]97        var n = new StringValue(name);
[15242]98        bool isInputTarget = preprocessingData.InputVariables.Contains(name) || preprocessingData.TargetVariable == name;
99        itemList.Add(n, isInputTarget);
[10992]100      }
101      return new ReadOnlyCheckedItemList<StringValue>(itemList);
[10818]102    }
[10992]103
[15242]104    public static IEnumerable<string> GetVariableNamesForGrouping(IPreprocessingData preprocessingData, int maxDistinctValues = 20) {
105      var variableNames = new List<string>();
106
107      for (int i = 0; i < preprocessingData.Columns; ++i) {
108        int distinctValues = Int32.MaxValue;
109        if (preprocessingData.VariableHasType<double>(i))
110          distinctValues = preprocessingData.GetValues<double>(i).GroupBy(x => x).Count();
111        else if (preprocessingData.VariableHasType<string>(i))
112          distinctValues = preprocessingData.GetValues<string>(i).GroupBy(x => x).Count();
113        else if (preprocessingData.VariableHasType<DateTime>(i))
114          distinctValues = preprocessingData.GetValues<DateTime>(i).GroupBy(x => x).Count();
115
116        if (distinctValues <= maxDistinctValues)
117          variableNames.Add(preprocessingData.GetVariableName(i));
118      }
119      return variableNames;
[10992]120    }
[10242]121  }
122}
Note: See TracBrowser for help on using the repository browser.