Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DataPreprocessing/HeuristicLab.DataPreprocessing/3.3/Implementations/StatisticsLogic.cs @ 10615

Last change on this file since 10615 was 10613, checked in by sbreuer, 11 years ago
  • fixed date time bug
File size: 6.3 KB
RevLine 
[10539]1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2013 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;
[10383]23using System.Collections.Generic;
[10165]24using System.Linq;
[10216]25using HeuristicLab.Common;
[10148]26
27namespace HeuristicLab.DataPreprocessing {
[10165]28
[10249]29  public class StatisticsLogic : IStatisticsLogic {
[10148]30
[10586]31    private readonly ITransactionalPreprocessingData preprocessingData;
[10236]32    private readonly ISearchLogic searchLogic;
[10148]33
[10586]34    public StatisticsLogic(ITransactionalPreprocessingData thePreprocessingData, ISearchLogic theSearchLogic) {
[10165]35      preprocessingData = thePreprocessingData;
[10236]36      searchLogic = theSearchLogic;
[10148]37    }
38
39    public int GetColumnCount() {
[10159]40      return preprocessingData.Columns;
[10148]41    }
42
43    public int GetRowCount() {
[10159]44      return preprocessingData.Rows;
[10148]45    }
46
47    public int GetNumericColumnCount() {
[10159]48      int count = 0;
[10369]49
50      for (int i = 0; i < preprocessingData.Columns; ++i) {
51        if (preprocessingData.IsType<double>(i)) {
[10159]52          ++count;
53        }
54      }
55      return count;
[10148]56    }
57
58    public int GetNominalColumnCount() {
[10159]59      return preprocessingData.Columns - GetNumericColumnCount();
[10148]60    }
61
62    public int GetMissingValueCount() {
63      int count = 0;
[10369]64      for (int i = 0; i < preprocessingData.Columns; ++i) {
[10367]65        count += GetMissingValueCount(i);
[10148]66      }
67      return count;
68    }
69
[10367]70    public int GetMissingValueCount(int columnIndex) {
71      return searchLogic.GetMissingValueIndices(columnIndex).Count();
[10148]72    }
73
[10367]74    public T GetMin<T>(int columnIndex) where T : IComparable<T> {
75      return preprocessingData.GetValues<T>(columnIndex).Min();
[10148]76    }
77
[10367]78    public T GetMax<T>(int columnIndex) where T : IComparable<T> {
79      return preprocessingData.GetValues<T>(columnIndex).Max();
[10148]80    }
81
[10367]82    public double GetMedian(int columnIndex) {
[10166]83      double median = double.NaN;
[10367]84      if (preprocessingData.IsType<double>(columnIndex)) {
85        median = preprocessingData.GetValues<double>(columnIndex).Median();
[10166]86      }
87      return median;
[10148]88    }
89
[10367]90    public double GetAverage(int columnIndex) {
[10166]91      double avg = double.NaN;
[10367]92      if (preprocessingData.IsType<double>(columnIndex)) {
93        avg = preprocessingData.GetValues<double>(columnIndex).Average();
[10166]94      }
95      return avg;
[10148]96    }
97
[10383]98    public DateTime GetMedianDateTime(int columnIndex) {
[10381]99      DateTime median = new DateTime();
[10383]100      if (preprocessingData.IsType<DateTime>(columnIndex)) {
101        median = GetSecondsAsDateTime(GetDateTimeAsSeconds(columnIndex).Median());
[10381]102      }
103      return median;
104    }
105
[10383]106    public DateTime GetAverageDateTime(int columnIndex) {
[10381]107      DateTime avg = new DateTime();
[10383]108      if (preprocessingData.IsType<DateTime>(columnIndex)) {
109        avg = GetSecondsAsDateTime(GetDateTimeAsSeconds(columnIndex).Average());
[10381]110      }
111      return avg;
112    }
113
[10367]114    public T GetMostCommonValue<T>(int columnIndex) {
[10534]115      var t = preprocessingData.GetValues<T>(columnIndex);
116      var t2 = t.GroupBy(x => x);
117      var t3 = t2.Select(g => g.Key);
118
[10367]119      return preprocessingData.GetValues<T>(columnIndex)
[10180]120                              .GroupBy(x => x)
121                              .OrderByDescending(g => g.Count())
122                              .Select(g => g.Key)
123                              .First();
[10148]124    }
125
[10167]126
[10367]127    public double GetStandardDeviation(int columnIndex) {
[10169]128      double stdDev = double.NaN;
[10367]129      if (preprocessingData.IsType<double>(columnIndex)) {
130        stdDev = preprocessingData.GetValues<double>(columnIndex).StandardDeviation();
[10532]131      } else if (preprocessingData.IsType<DateTime>(columnIndex)) {
132        stdDev = GetDateTimeAsSeconds(columnIndex).StandardDeviation();
[10169]133      }
134      return stdDev;
[10148]135    }
136
[10367]137    public double GetVariance(int columnIndex) {
[10383]138      double variance = double.NaN;
[10367]139      if (preprocessingData.IsType<double>(columnIndex)) {
[10381]140        variance = preprocessingData.GetValues<double>(columnIndex).Variance();
[10532]141      } else if (preprocessingData.IsType<DateTime>(columnIndex)) {
142        variance = GetDateTimeAsSeconds(columnIndex).Variance();
[10216]143      }
[10381]144      return variance;
[10216]145    }
146
[10367]147    public int GetDifferentValuesCount<T>(int columnIndex) {
148      return preprocessingData.GetValues<T>(columnIndex).GroupBy(x => x).Count();
[10179]149    }
[10191]150
151    public int GetRowMissingValueCount(int rowIndex) {
152      int count = 0;
[10369]153      for (int i = 0; i < preprocessingData.Columns; ++i) {
[10367]154        if (searchLogic.IsMissingValue(i, rowIndex)) {
[10191]155          ++count;
156        }
157      }
158      return count;
159    }
[10367]160
161    public string GetVariableName(int columnIndex) {
162      return preprocessingData.GetVariableName(columnIndex);
163    }
164
165    public bool IsType<T>(int columnIndex) {
166      return preprocessingData.IsType<T>(columnIndex);
167    }
[10371]168
169    public string GetColumnTypeAsString(int columnIndex) {
170      if (preprocessingData.IsType<double>(columnIndex)) {
171        return "double";
172      } else if (preprocessingData.IsType<string>(columnIndex)) {
173        return "string";
174      } else if (preprocessingData.IsType<DateTime>(columnIndex)) {
175        return "DateTime";
176      }
177      return "Unknown Type";
178    }
[10383]179    private IEnumerable<double> GetDateTimeAsSeconds(int columnIndex) {
180      return preprocessingData.GetValues<DateTime>(columnIndex).Select(x => (double)x.Ticks / TimeSpan.TicksPerSecond);
[10381]181    }
182
[10383]183    private DateTime GetSecondsAsDateTime(double seconds) {
184      DateTime dateTime = new DateTime();
[10613]185      return dateTime.AddSeconds(seconds);
[10381]186    }
[10551]187
188    public event DataPreprocessingChangedEventHandler Changed {
189      add { preprocessingData.Changed += value; }
190      remove { preprocessingData.Changed -= value; }
191    }
[10148]192  }
193}
Note: See TracBrowser for help on using the repository browser.