#region License Information /* HeuristicLab * Copyright (C) 2002-2013 Heuristic and Evolutionary Algorithms Laboratory (HEAL) * * This file is part of HeuristicLab. * * HeuristicLab is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * HeuristicLab is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with HeuristicLab. If not, see . */ #endregion using System; using System.Collections.Generic; using System.Linq; using HeuristicLab.Common; namespace HeuristicLab.DataPreprocessing { public class StatisticsLogic : IStatisticsLogic { private readonly ITransactionalPreprocessingData preprocessingData; private readonly ISearchLogic searchLogic; public StatisticsLogic(ITransactionalPreprocessingData thePreprocessingData, ISearchLogic theSearchLogic) { preprocessingData = thePreprocessingData; searchLogic = theSearchLogic; } public int GetColumnCount() { return preprocessingData.Columns; } public int GetRowCount() { return preprocessingData.Rows; } public int GetNumericColumnCount() { int count = 0; for (int i = 0; i < preprocessingData.Columns; ++i) { if (preprocessingData.VariableHasType(i)) { ++count; } } return count; } public int GetNominalColumnCount() { return preprocessingData.Columns - GetNumericColumnCount(); } public int GetMissingValueCount() { int count = 0; for (int i = 0; i < preprocessingData.Columns; ++i) { count += GetMissingValueCount(i); } return count; } public int GetMissingValueCount(int columnIndex) { return searchLogic.GetMissingValueIndices(columnIndex).Count(); } public T GetMin(int columnIndex, bool considerSelection) where T : IComparable { return preprocessingData.GetValues(columnIndex, considerSelection).Min(); } public T GetMax(int columnIndex, bool considerSelection) where T : IComparable { return preprocessingData.GetValues(columnIndex, considerSelection).Max(); } public double GetMedian(int columnIndex, bool considerSelection) { double median = double.NaN; if (preprocessingData.VariableHasType(columnIndex)) { median = GetValuesWithoutNaN(columnIndex, considerSelection).Median(); } return median; } public double GetAverage(int columnIndex, bool considerSelection) { double avg = double.NaN; if (preprocessingData.VariableHasType(columnIndex)) { avg = GetValuesWithoutNaN(columnIndex, considerSelection).Average(); } return avg; } public DateTime GetMedianDateTime(int columnIndex, bool considerSelection) { DateTime median = new DateTime(); if (preprocessingData.VariableHasType(columnIndex)) { median = GetSecondsAsDateTime(GetDateTimeAsSeconds(columnIndex, considerSelection).Median()); } return median; } public DateTime GetAverageDateTime(int columnIndex, bool considerSelection) { DateTime avg = new DateTime(); if (preprocessingData.VariableHasType(columnIndex)) { avg = GetSecondsAsDateTime(GetDateTimeAsSeconds(columnIndex, considerSelection).Average()); } return avg; } public T GetMostCommonValue(int columnIndex, bool considerSelection) { var t = preprocessingData.GetValues(columnIndex, considerSelection); var t2 = t.GroupBy(x => x); var t3 = t2.Select(g => g.Key); return preprocessingData.GetValues(columnIndex, considerSelection) .GroupBy(x => x) .OrderByDescending(g => g.Count()) .Select(g => g.Key) .First(); } public double GetStandardDeviation(int columnIndex) { double stdDev = double.NaN; if (preprocessingData.VariableHasType(columnIndex)) { stdDev = GetValuesWithoutNaN(columnIndex).StandardDeviation(); } else if (preprocessingData.VariableHasType(columnIndex)) { stdDev = GetDateTimeAsSeconds(columnIndex).StandardDeviation(); } return stdDev; } public double GetVariance(int columnIndex) { double variance = double.NaN; if (preprocessingData.VariableHasType(columnIndex)) { variance = GetValuesWithoutNaN(columnIndex).Variance(); } else if (preprocessingData.VariableHasType(columnIndex)) { variance = GetDateTimeAsSeconds(columnIndex).Variance(); } return variance; } public int GetDifferentValuesCount(int columnIndex) { return preprocessingData.GetValues(columnIndex).GroupBy(x => x).Count(); } public int GetRowMissingValueCount(int rowIndex) { int count = 0; for (int i = 0; i < preprocessingData.Columns; ++i) { if (searchLogic.IsMissingValue(i, rowIndex)) { ++count; } } return count; } public string GetVariableName(int columnIndex) { return preprocessingData.GetVariableName(columnIndex); } public bool VariableHasType(int columnIndex) { return preprocessingData.VariableHasType(columnIndex); } public string GetColumnTypeAsString(int columnIndex) { if (preprocessingData.VariableHasType(columnIndex)) { return "double"; } else if (preprocessingData.VariableHasType(columnIndex)) { return "string"; } else if (preprocessingData.VariableHasType(columnIndex)) { return "DateTime"; } return "Unknown Type"; } private IEnumerable GetDateTimeAsSeconds(int columnIndex, bool considerSelection = false) { return GetValuesWithoutNaN(columnIndex, considerSelection).Select(x => (double)x.Ticks / TimeSpan.TicksPerSecond); } private IEnumerable GetValuesWithoutNaN(int columnIndex, bool considerSelection = false) { return searchLogic.GetValuesWithoutNaN(columnIndex, considerSelection); } private DateTime GetSecondsAsDateTime(double seconds) { DateTime dateTime = new DateTime(); return dateTime.AddSeconds(seconds); } public event DataPreprocessingChangedEventHandler Changed { add { preprocessingData.Changed += value; } remove { preprocessingData.Changed -= value; } } } }