Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DataPreprocessing/HeuristicLab.DataPreprocessing/3.4/Implementations/ChartLogic.cs @ 10973

Last change on this file since 10973 was 10973, checked in by aesterer, 8 years ago

Handling of string and dates values for histogram classification

File size: 6.7 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2014 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.Linq;
25using System.Drawing;
26using HeuristicLab.Analysis;
27using HeuristicLab.Core;
28using HeuristicLab.Data;
29using HeuristicLab.Common;
30
31namespace HeuristicLab.DataPreprocessing {
32
33  public class ChartLogic : IChartLogic {
34    private const int MAX_DISTINCT_VALUES_FOR_CLASSIFCATION = 20;
35    private ITransactionalPreprocessingData preprocessingData;
36
37    public ChartLogic(ITransactionalPreprocessingData preprocessingData) {
38      this.preprocessingData = preprocessingData;
39    }
40
41    #region IChartLogic Members
42
43    public DataRow CreateDataRow(string variableName, DataRowVisualProperties.DataRowChartType chartType) {
44      IList<double> values = preprocessingData.GetValues<double>(variableName);
45      DataRow row = new DataRow(variableName, "", values);
46      row.VisualProperties.ChartType = chartType;
47      return row;
48    }
49
50    public DataRow CreateDataRowRange(string variableName,int start, int end, DataRowVisualProperties.DataRowChartType chartType) {
51      IList<double> values = preprocessingData.GetValues<double>(variableName);
52      IList<double> valuesRange = new List<double>();
53      for (int i = 0; i < values.Count; i++) {
54        if (i >= start && i <= end)
55          valuesRange.Add(values[i]);
56        else
57          valuesRange.Add(Double.NaN);
58      }
59
60      DataRow row = new DataRow(variableName, "", valuesRange);
61      row.VisualProperties.ChartType = chartType;
62      return row;
63    }
64
65    public IEnumerable<double> GetVariableValues(string variableName) {
66      return preprocessingData.GetValues<double>(preprocessingData.GetColumnIndex(variableName));
67    }
68
69    public IEnumerable<string> GetVariableNames() {
70      List<string> doubleVariableNames = new List<string>();
71
72      //only return variable names from type double
73      foreach (string variableName in preprocessingData.VariableNames) {
74        if (preprocessingData.IsType<double>(preprocessingData.GetColumnIndex(variableName)))
75          doubleVariableNames.Add(variableName);
76      }
77
78      return doubleVariableNames;
79    }
80
81    public IEnumerable<string> GetVariableNamesForHistogramClassification() {
82      List<string> doubleVariableNames = new List<string>();
83
84      //only return variable names from type double
85      foreach (string variableName in preprocessingData.VariableNames)
86      {
87        int columnIndex           = preprocessingData.GetColumnIndex(variableName);
88        bool isDouble             = preprocessingData.IsType<double>(columnIndex);
89        if (isDouble) {
90          double distinctValueCount = preprocessingData.GetValues<double>(columnIndex).GroupBy(x => x).Count();
91          bool distinctValuesOk = distinctValueCount <= MAX_DISTINCT_VALUES_FOR_CLASSIFCATION;
92          if (distinctValuesOk)
93            doubleVariableNames.Add(variableName);
94        }
95      }
96
97      return doubleVariableNames;
98    }
99
100    public ICheckedItemList<StringValue> CreateVariableItemList() {
101      ICheckedItemList<StringValue> itemList = new CheckedItemList<StringValue>();
102      foreach (string name in GetVariableNames()) {
103        itemList.Add(new StringValue(name), true);
104      }
105      return new ReadOnlyCheckedItemList<StringValue>(itemList);
106    }
107
108    public event DataPreprocessingChangedEventHandler Changed {
109      add { preprocessingData.Changed += value; }
110      remove { preprocessingData.Changed -= value; }
111    }
112
113    public event EventHandler SelectionChanged {
114      add { preprocessingData.SelectionChanged += value; }
115      remove { preprocessingData.SelectionChanged -= value; }
116    }
117
118    public string GetVariableNameByIndex(int index) {
119      return preprocessingData.GetVariableName(index);
120    }
121
122
123    public List<DataRow> CreateAllDataRows(DataRowVisualProperties.DataRowChartType chartType) {
124      List<DataRow> dataRows = new List<DataRow>();
125      foreach (var name in GetVariableNames())
126        dataRows.Add(CreateDataRow(name, chartType));
127      return dataRows;
128    }
129
130    public List<DataRow> CreateAllSelectedDataRows(DataRowVisualProperties.DataRowChartType chartType) {
131      List<DataRow> dataRows = new List<DataRow>();
132      foreach (var name in GetVariableNames()) {
133        DataRow row = CreateSelectedDataRow(name, chartType);
134        if(row != null)
135          dataRows.Add(row);
136      }
137      return dataRows;
138    }
139
140    public DataRow CreateSelectedDataRow(string variableName, DataRowVisualProperties.DataRowChartType chartType) {
141     
142      IDictionary<int,IList<int>> selection = preprocessingData.GetSelection();
143      int variableIndex = preprocessingData.GetColumnIndex(variableName);
144
145      if (selection.Keys.Contains(variableIndex))
146      {
147        List<int> selectedIndices = new List<int>(selection[variableIndex]);
148        //need selection with more than 1 value
149        if(selectedIndices.Count < 2)
150          return null;
151
152        selectedIndices.Sort();
153        int start = selectedIndices[0];     
154        int end = selectedIndices[selectedIndices.Count-1];
155
156        DataRow rowSelect = CreateDataRowRange(variableName, start, end, chartType);
157        return rowSelect;
158      }
159      else
160       return null;
161    }
162
163    public ScatterPlot CreateScatterPlot(string variableNameX, string variableNameY) {
164      ScatterPlot scatterPlot = new ScatterPlot();
165     
166      List<double> xValues = GetVariableValues(variableNameX).ToList();
167      List<double> yValues = GetVariableValues(variableNameY).ToList();
168
169      List<Point2D<double>> points = new List<Point2D<double>>();
170
171      for( int i = 0; i < xValues.Count; i++)
172      {
173          Point2D<double> point = new Point2D<double>(xValues[i],yValues[i]);
174          points.Add(point);
175      }
176
177      ScatterPlotDataRow scdr = new ScatterPlotDataRow(variableNameX + " - " + variableNameY,"",points);
178      scatterPlot.Rows.Add(scdr);
179      return scatterPlot;
180    }
181
182    #endregion
183  }
184}
Note: See TracBrowser for help on using the repository browser.