Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ProblemRefactoring/HeuristicLab.DataPreprocessing/3.4/Implementations/StatisticsLogic.cs @ 14778

Last change on this file since 14778 was 13051, checked in by gkronber, 9 years ago

#2491: removed usage of Percentile extension method

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