1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2015 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 HeuristicLab.Common;
|
---|
26 |
|
---|
27 | namespace HeuristicLab.DataPreprocessing {
|
---|
28 | public class StatisticsLogic {
|
---|
29 |
|
---|
30 | private readonly ITransactionalPreprocessingData preprocessingData;
|
---|
31 | private readonly SearchLogic searchLogic;
|
---|
32 |
|
---|
33 | public StatisticsLogic(ITransactionalPreprocessingData thePreprocessingData, SearchLogic theSearchLogic) {
|
---|
34 | preprocessingData = thePreprocessingData;
|
---|
35 | searchLogic = theSearchLogic;
|
---|
36 | }
|
---|
37 |
|
---|
38 | public int GetColumnCount() {
|
---|
39 | return searchLogic.Columns;
|
---|
40 | }
|
---|
41 |
|
---|
42 | public int GetRowCount() {
|
---|
43 | return searchLogic.Rows;
|
---|
44 | }
|
---|
45 |
|
---|
46 | public int GetNumericColumnCount() {
|
---|
47 | int count = 0;
|
---|
48 |
|
---|
49 | for (int i = 0; i < searchLogic.Columns; ++i) {
|
---|
50 | if (preprocessingData.VariableHasType<double>(i)) {
|
---|
51 | ++count;
|
---|
52 | }
|
---|
53 | }
|
---|
54 | return count;
|
---|
55 | }
|
---|
56 |
|
---|
57 | public int GetNominalColumnCount() {
|
---|
58 | return searchLogic.Columns - GetNumericColumnCount();
|
---|
59 | }
|
---|
60 |
|
---|
61 | public int GetMissingValueCount() {
|
---|
62 | int count = 0;
|
---|
63 | for (int i = 0; i < searchLogic.Columns; ++i) {
|
---|
64 | count += GetMissingValueCount(i);
|
---|
65 | }
|
---|
66 | return count;
|
---|
67 | }
|
---|
68 |
|
---|
69 | public int GetMissingValueCount(int columnIndex) {
|
---|
70 | return searchLogic.GetMissingValueIndices(columnIndex).Count();
|
---|
71 | }
|
---|
72 |
|
---|
73 | public T GetMin<T>(int columnIndex, T defaultValue, bool considerSelection = false) where T : IComparable<T> {
|
---|
74 | var min = defaultValue;
|
---|
75 | if (preprocessingData.VariableHasType<T>(columnIndex)) {
|
---|
76 | var values = GetValuesWithoutNaN<T>(columnIndex, considerSelection);
|
---|
77 | if (values.Any()) {
|
---|
78 | min = values.Min();
|
---|
79 | }
|
---|
80 | }
|
---|
81 | return min;
|
---|
82 | }
|
---|
83 |
|
---|
84 | public T GetMax<T>(int columnIndex, T defaultValue, bool considerSelection = false) where T : IComparable<T> {
|
---|
85 | var max = defaultValue;
|
---|
86 | if (preprocessingData.VariableHasType<T>(columnIndex)) {
|
---|
87 | var values = GetValuesWithoutNaN<T>(columnIndex, considerSelection);
|
---|
88 | if (values.Any()) {
|
---|
89 | max = values.Max();
|
---|
90 | }
|
---|
91 | }
|
---|
92 | return max;
|
---|
93 | }
|
---|
94 |
|
---|
95 | public double GetMedian(int columnIndex, bool considerSelection = false) {
|
---|
96 | double median = double.NaN;
|
---|
97 | if (preprocessingData.VariableHasType<double>(columnIndex)) {
|
---|
98 | var values = GetValuesWithoutNaN<double>(columnIndex, considerSelection);
|
---|
99 | if (values.Any()) {
|
---|
100 | median = values.Median();
|
---|
101 | }
|
---|
102 | }
|
---|
103 | return median;
|
---|
104 | }
|
---|
105 |
|
---|
106 | public double GetAverage(int columnIndex, bool considerSelection = false) {
|
---|
107 | double avg = double.NaN;
|
---|
108 | if (preprocessingData.VariableHasType<double>(columnIndex)) {
|
---|
109 | var values = GetValuesWithoutNaN<double>(columnIndex, considerSelection);
|
---|
110 | if (values.Any()) {
|
---|
111 | avg = values.Average();
|
---|
112 | }
|
---|
113 | }
|
---|
114 | return avg;
|
---|
115 | }
|
---|
116 |
|
---|
117 | public DateTime GetMedianDateTime(int columnIndex, bool considerSelection = false) {
|
---|
118 | DateTime median = new DateTime();
|
---|
119 | if (preprocessingData.VariableHasType<DateTime>(columnIndex)) {
|
---|
120 | median = GetSecondsAsDateTime(GetDateTimeAsSeconds(columnIndex, considerSelection).Median());
|
---|
121 | }
|
---|
122 | return median;
|
---|
123 | }
|
---|
124 |
|
---|
125 | public DateTime GetAverageDateTime(int columnIndex, bool considerSelection = false) {
|
---|
126 | DateTime avg = new DateTime();
|
---|
127 | if (preprocessingData.VariableHasType<DateTime>(columnIndex)) {
|
---|
128 | avg = GetSecondsAsDateTime(GetDateTimeAsSeconds(columnIndex, considerSelection).Average());
|
---|
129 | }
|
---|
130 | return avg;
|
---|
131 | }
|
---|
132 |
|
---|
133 | public T GetMostCommonValue<T>(int columnIndex, T defaultValue, bool considerSelection = false) {
|
---|
134 | var values = GetValuesWithoutNaN<T>(columnIndex, considerSelection);
|
---|
135 | if (!values.Any())
|
---|
136 | return defaultValue;
|
---|
137 | return values.GroupBy(x => x)
|
---|
138 | .OrderByDescending(g => g.Count())
|
---|
139 | .Select(g => g.Key)
|
---|
140 | .First();
|
---|
141 | }
|
---|
142 |
|
---|
143 |
|
---|
144 | public double GetStandardDeviation(int columnIndex) {
|
---|
145 | double stdDev = double.NaN;
|
---|
146 | if (preprocessingData.VariableHasType<double>(columnIndex)) {
|
---|
147 | stdDev = GetValuesWithoutNaN<double>(columnIndex).StandardDeviation();
|
---|
148 | } else if (preprocessingData.VariableHasType<DateTime>(columnIndex)) {
|
---|
149 | stdDev = GetDateTimeAsSeconds(columnIndex).StandardDeviation();
|
---|
150 | }
|
---|
151 | return stdDev;
|
---|
152 | }
|
---|
153 |
|
---|
154 | public double GetVariance(int columnIndex) {
|
---|
155 | double variance = double.NaN;
|
---|
156 | if (preprocessingData.VariableHasType<double>(columnIndex)) {
|
---|
157 | variance = GetValuesWithoutNaN<double>(columnIndex).Variance();
|
---|
158 | } else if (preprocessingData.VariableHasType<DateTime>(columnIndex)) {
|
---|
159 | variance = GetDateTimeAsSeconds(columnIndex).Variance();
|
---|
160 | }
|
---|
161 | return variance;
|
---|
162 | }
|
---|
163 |
|
---|
164 | public double GetOneQuarterPercentile(int columnIndex) {
|
---|
165 | double percentile = double.NaN;
|
---|
166 | if (preprocessingData.VariableHasType<double>(columnIndex)) {
|
---|
167 | percentile = GetValuesWithoutNaN<double>(columnIndex).DefaultIfEmpty(double.NaN).Quantile(0.25);
|
---|
168 | } else if (preprocessingData.VariableHasType<DateTime>(columnIndex)) {
|
---|
169 | percentile = GetDateTimeAsSeconds(columnIndex).DefaultIfEmpty(double.NaN).Quantile(0.25);
|
---|
170 | }
|
---|
171 | return percentile;
|
---|
172 | }
|
---|
173 |
|
---|
174 | public double GetThreeQuarterPercentile(int columnIndex) {
|
---|
175 | double percentile = double.NaN;
|
---|
176 | if (preprocessingData.VariableHasType<double>(columnIndex)) {
|
---|
177 | percentile = GetValuesWithoutNaN<double>(columnIndex).DefaultIfEmpty(double.NaN).Quantile(0.75);
|
---|
178 | } else if (preprocessingData.VariableHasType<DateTime>(columnIndex)) {
|
---|
179 | percentile = GetDateTimeAsSeconds(columnIndex).DefaultIfEmpty(double.NaN).Quantile(0.75);
|
---|
180 | }
|
---|
181 | return percentile;
|
---|
182 | }
|
---|
183 |
|
---|
184 | public int GetDifferentValuesCount<T>(int columnIndex) {
|
---|
185 | return preprocessingData.GetValues<T>(columnIndex).GroupBy(x => x).Count();
|
---|
186 | }
|
---|
187 |
|
---|
188 | public int GetRowMissingValueCount(int rowIndex) {
|
---|
189 | int count = 0;
|
---|
190 | for (int i = 0; i < preprocessingData.Columns; ++i) {
|
---|
191 | if (searchLogic.IsMissingValue(i, rowIndex)) {
|
---|
192 | ++count;
|
---|
193 | }
|
---|
194 | }
|
---|
195 | return count;
|
---|
196 | }
|
---|
197 |
|
---|
198 | public string GetVariableName(int columnIndex) {
|
---|
199 | return preprocessingData.GetVariableName(columnIndex);
|
---|
200 | }
|
---|
201 |
|
---|
202 | public bool VariableHasType<T>(int columnIndex) {
|
---|
203 | return preprocessingData.VariableHasType<T>(columnIndex);
|
---|
204 | }
|
---|
205 |
|
---|
206 | public string GetColumnTypeAsString(int columnIndex) {
|
---|
207 | if (preprocessingData.VariableHasType<double>(columnIndex)) {
|
---|
208 | return "double";
|
---|
209 | } else if (preprocessingData.VariableHasType<string>(columnIndex)) {
|
---|
210 | return "string";
|
---|
211 | } else if (preprocessingData.VariableHasType<DateTime>(columnIndex)) {
|
---|
212 | return "DateTime";
|
---|
213 | }
|
---|
214 | return "Unknown Type";
|
---|
215 | }
|
---|
216 |
|
---|
217 | private IEnumerable<double> GetDateTimeAsSeconds(int columnIndex, bool considerSelection = false) {
|
---|
218 | return GetValuesWithoutNaN<DateTime>(columnIndex, considerSelection).Select(x => (double)x.Ticks / TimeSpan.TicksPerSecond);
|
---|
219 | }
|
---|
220 |
|
---|
221 | private IEnumerable<T> GetValuesWithoutNaN<T>(int columnIndex, bool considerSelection = false) {
|
---|
222 | return searchLogic.GetValuesWithoutNaN<T>(columnIndex, considerSelection);
|
---|
223 | }
|
---|
224 |
|
---|
225 | private DateTime GetSecondsAsDateTime(double seconds) {
|
---|
226 | DateTime dateTime = new DateTime();
|
---|
227 | return dateTime.AddSeconds(seconds);
|
---|
228 | }
|
---|
229 |
|
---|
230 | public event DataPreprocessingChangedEventHandler Changed {
|
---|
231 | add { preprocessingData.Changed += value; }
|
---|
232 | remove { preprocessingData.Changed -= value; }
|
---|
233 | }
|
---|
234 | }
|
---|
235 | }
|
---|