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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Linq;
|
---|
25 | using System.Drawing;
|
---|
26 | using HeuristicLab.Analysis;
|
---|
27 | using HeuristicLab.Core;
|
---|
28 | using HeuristicLab.Data;
|
---|
29 | using HeuristicLab.Common;
|
---|
30 |
|
---|
31 | namespace 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 | double distinctValueCount = preprocessingData.GetValues<double>(columnIndex).GroupBy(x => x).Count();
|
---|
90 | bool distinctValuesOk = distinctValueCount <= MAX_DISTINCT_VALUES_FOR_CLASSIFCATION;
|
---|
91 |
|
---|
92 | if (isDouble && distinctValuesOk)
|
---|
93 | doubleVariableNames.Add(variableName);
|
---|
94 | }
|
---|
95 |
|
---|
96 | return doubleVariableNames;
|
---|
97 | }
|
---|
98 |
|
---|
99 | public ICheckedItemList<StringValue> CreateVariableItemList() {
|
---|
100 | ICheckedItemList<StringValue> itemList = new CheckedItemList<StringValue>();
|
---|
101 | foreach (string name in GetVariableNames()) {
|
---|
102 | itemList.Add(new StringValue(name), true);
|
---|
103 | }
|
---|
104 | return new ReadOnlyCheckedItemList<StringValue>(itemList);
|
---|
105 | }
|
---|
106 |
|
---|
107 | public event DataPreprocessingChangedEventHandler Changed {
|
---|
108 | add { preprocessingData.Changed += value; }
|
---|
109 | remove { preprocessingData.Changed -= value; }
|
---|
110 | }
|
---|
111 |
|
---|
112 | public event EventHandler SelectionChanged {
|
---|
113 | add { preprocessingData.SelectionChanged += value; }
|
---|
114 | remove { preprocessingData.SelectionChanged -= value; }
|
---|
115 | }
|
---|
116 |
|
---|
117 | public string GetVariableNameByIndex(int index) {
|
---|
118 | return preprocessingData.GetVariableName(index);
|
---|
119 | }
|
---|
120 |
|
---|
121 |
|
---|
122 | public List<DataRow> CreateAllDataRows(DataRowVisualProperties.DataRowChartType chartType) {
|
---|
123 | List<DataRow> dataRows = new List<DataRow>();
|
---|
124 | foreach (var name in GetVariableNames())
|
---|
125 | dataRows.Add(CreateDataRow(name, chartType));
|
---|
126 | return dataRows;
|
---|
127 | }
|
---|
128 |
|
---|
129 | public List<DataRow> CreateAllSelectedDataRows(DataRowVisualProperties.DataRowChartType chartType) {
|
---|
130 | List<DataRow> dataRows = new List<DataRow>();
|
---|
131 | foreach (var name in GetVariableNames()) {
|
---|
132 | DataRow row = CreateSelectedDataRow(name, chartType);
|
---|
133 | if(row != null)
|
---|
134 | dataRows.Add(row);
|
---|
135 | }
|
---|
136 | return dataRows;
|
---|
137 | }
|
---|
138 |
|
---|
139 | public DataRow CreateSelectedDataRow(string variableName, DataRowVisualProperties.DataRowChartType chartType) {
|
---|
140 |
|
---|
141 | IDictionary<int,IList<int>> selection = preprocessingData.GetSelection();
|
---|
142 | int variableIndex = preprocessingData.GetColumnIndex(variableName);
|
---|
143 |
|
---|
144 | if (selection.Keys.Contains(variableIndex))
|
---|
145 | {
|
---|
146 | List<int> selectedIndices = new List<int>(selection[variableIndex]);
|
---|
147 | //need selection with more than 1 value
|
---|
148 | if(selectedIndices.Count < 2)
|
---|
149 | return null;
|
---|
150 |
|
---|
151 | selectedIndices.Sort();
|
---|
152 | int start = selectedIndices[0];
|
---|
153 | int end = selectedIndices[selectedIndices.Count-1];
|
---|
154 |
|
---|
155 | DataRow rowSelect = CreateDataRowRange(variableName, start, end, chartType);
|
---|
156 | return rowSelect;
|
---|
157 | }
|
---|
158 | else
|
---|
159 | return null;
|
---|
160 | }
|
---|
161 |
|
---|
162 | public ScatterPlot CreateScatterPlot(string variableNameX, string variableNameY) {
|
---|
163 | ScatterPlot scatterPlot = new ScatterPlot();
|
---|
164 |
|
---|
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 | }
|
---|