Changeset 10635
- Timestamp:
- 03/19/14 16:04:36 (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/DataPreprocessing/HeuristicLab.DataPreprocessing/3.3/Implementations/StatisticsLogic.cs
r10624 r10635 25 25 using HeuristicLab.Common; 26 26 27 namespace HeuristicLab.DataPreprocessing 28 { 29 30 public class StatisticsLogic : IStatisticsLogic 31 { 27 namespace HeuristicLab.DataPreprocessing { 28 29 public class StatisticsLogic : IStatisticsLogic { 32 30 33 31 private readonly ITransactionalPreprocessingData preprocessingData; 34 32 private readonly ISearchLogic searchLogic; 35 33 36 public StatisticsLogic(ITransactionalPreprocessingData thePreprocessingData, ISearchLogic theSearchLogic) 37 { 34 public StatisticsLogic(ITransactionalPreprocessingData thePreprocessingData, ISearchLogic theSearchLogic) { 38 35 preprocessingData = thePreprocessingData; 39 36 searchLogic = theSearchLogic; 40 37 } 41 38 42 public int GetColumnCount() 43 { 39 public int GetColumnCount() { 44 40 return preprocessingData.Columns; 45 41 } 46 42 47 public int GetRowCount() 48 { 43 public int GetRowCount() { 49 44 return preprocessingData.Rows; 50 45 } 51 46 52 public int GetNumericColumnCount() 53 { 47 public int GetNumericColumnCount() { 54 48 int count = 0; 55 49 56 for (int i = 0; i < preprocessingData.Columns; ++i) 57 { 58 if (preprocessingData.IsType<double>(i)) 59 { 50 for (int i = 0; i < preprocessingData.Columns; ++i) { 51 if (preprocessingData.IsType<double>(i)) { 60 52 ++count; 61 53 } … … 64 56 } 65 57 66 public int GetNominalColumnCount() 67 { 58 public int GetNominalColumnCount() { 68 59 return preprocessingData.Columns - GetNumericColumnCount(); 69 60 } 70 61 71 public int GetMissingValueCount() 72 { 62 public int GetMissingValueCount() { 73 63 int count = 0; 74 for (int i = 0; i < preprocessingData.Columns; ++i) 75 { 64 for (int i = 0; i < preprocessingData.Columns; ++i) { 76 65 count += GetMissingValueCount(i); 77 66 } … … 79 68 } 80 69 81 public int GetMissingValueCount(int columnIndex) 82 { 70 public int GetMissingValueCount(int columnIndex) { 83 71 return searchLogic.GetMissingValueIndices(columnIndex).Count(); 84 72 } 85 73 86 public T GetMin<T>(int columnIndex) where T : IComparable<T> 87 { 74 public T GetMin<T>(int columnIndex) where T : IComparable<T> { 88 75 return preprocessingData.GetValues<T>(columnIndex).Min(); 89 76 } 90 77 91 public T GetMax<T>(int columnIndex) where T : IComparable<T> 92 { 78 public T GetMax<T>(int columnIndex) where T : IComparable<T> { 93 79 return preprocessingData.GetValues<T>(columnIndex).Max(); 94 80 } 95 81 96 public double GetMedian(int columnIndex) 97 { 82 public double GetMedian(int columnIndex) { 98 83 double median = double.NaN; 99 if (preprocessingData.IsType<double>(columnIndex)) 100 { 84 if (preprocessingData.IsType<double>(columnIndex)) { 101 85 median = GetValuesWithoutNaN<double>(columnIndex).Median(); 102 86 } … … 104 88 } 105 89 106 public double GetAverage(int columnIndex) 107 { 90 public double GetAverage(int columnIndex) { 108 91 double avg = double.NaN; 109 if (preprocessingData.IsType<double>(columnIndex)) 110 { 92 if (preprocessingData.IsType<double>(columnIndex)) { 111 93 avg = GetValuesWithoutNaN<double>(columnIndex).Where(x => !double.IsNaN(x)).Average(); 112 94 } … … 114 96 } 115 97 116 public DateTime GetMedianDateTime(int columnIndex) 117 { 98 public DateTime GetMedianDateTime(int columnIndex) { 118 99 DateTime median = new DateTime(); 119 if (preprocessingData.IsType<DateTime>(columnIndex)) 120 { 100 if (preprocessingData.IsType<DateTime>(columnIndex)) { 121 101 median = GetSecondsAsDateTime(GetDateTimeAsSeconds(columnIndex).Median()); 122 102 } … … 124 104 } 125 105 126 public DateTime GetAverageDateTime(int columnIndex) 127 { 106 public DateTime GetAverageDateTime(int columnIndex) { 128 107 DateTime avg = new DateTime(); 129 if (preprocessingData.IsType<DateTime>(columnIndex)) 130 { 108 if (preprocessingData.IsType<DateTime>(columnIndex)) { 131 109 avg = GetSecondsAsDateTime(GetDateTimeAsSeconds(columnIndex).Average()); 132 110 } … … 134 112 } 135 113 136 public T GetMostCommonValue<T>(int columnIndex) 137 { 114 public T GetMostCommonValue<T>(int columnIndex) { 138 115 var t = preprocessingData.GetValues<T>(columnIndex); 139 116 var t2 = t.GroupBy(x => x); … … 148 125 149 126 150 public double GetStandardDeviation(int columnIndex) 151 { 127 public double GetStandardDeviation(int columnIndex) { 152 128 double stdDev = double.NaN; 153 if (preprocessingData.IsType<double>(columnIndex)) 154 { 129 if (preprocessingData.IsType<double>(columnIndex)) { 155 130 stdDev = GetValuesWithoutNaN<double>(columnIndex).StandardDeviation(); 156 } 157 else if (preprocessingData.IsType<DateTime>(columnIndex)) 158 { 131 } else if (preprocessingData.IsType<DateTime>(columnIndex)) { 159 132 stdDev = GetDateTimeAsSeconds(columnIndex).StandardDeviation(); 160 133 } … … 162 135 } 163 136 164 public double GetVariance(int columnIndex) 165 { 137 public double GetVariance(int columnIndex) { 166 138 double variance = double.NaN; 167 if (preprocessingData.IsType<double>(columnIndex)) 168 { 139 if (preprocessingData.IsType<double>(columnIndex)) { 169 140 variance = preprocessingData.GetValues<double>(columnIndex).Variance(); 170 } 171 else if (preprocessingData.IsType<DateTime>(columnIndex)) 172 { 141 } else if (preprocessingData.IsType<DateTime>(columnIndex)) { 173 142 variance = GetDateTimeAsSeconds(columnIndex).Variance(); 174 143 } … … 176 145 } 177 146 178 public int GetDifferentValuesCount<T>(int columnIndex) 179 { 147 public int GetDifferentValuesCount<T>(int columnIndex) { 180 148 return preprocessingData.GetValues<T>(columnIndex).GroupBy(x => x).Count(); 181 149 } 182 150 183 public int GetRowMissingValueCount(int rowIndex) 184 { 151 public int GetRowMissingValueCount(int rowIndex) { 185 152 int count = 0; 186 for (int i = 0; i < preprocessingData.Columns; ++i) 187 { 188 if (searchLogic.IsMissingValue(i, rowIndex)) 189 { 153 for (int i = 0; i < preprocessingData.Columns; ++i) { 154 if (searchLogic.IsMissingValue(i, rowIndex)) { 190 155 ++count; 191 156 } … … 194 159 } 195 160 196 public string GetVariableName(int columnIndex) 197 { 161 public string GetVariableName(int columnIndex) { 198 162 return preprocessingData.GetVariableName(columnIndex); 199 163 } 200 164 201 public bool IsType<T>(int columnIndex) 202 { 165 public bool IsType<T>(int columnIndex) { 203 166 return preprocessingData.IsType<T>(columnIndex); 204 167 } 205 168 206 public string GetColumnTypeAsString(int columnIndex) 207 { 208 if (preprocessingData.IsType<double>(columnIndex)) 209 { 169 public string GetColumnTypeAsString(int columnIndex) { 170 if (preprocessingData.IsType<double>(columnIndex)) { 210 171 return "double"; 211 } 212 else if (preprocessingData.IsType<string>(columnIndex)) 213 { 172 } else if (preprocessingData.IsType<string>(columnIndex)) { 214 173 return "string"; 215 } 216 else if (preprocessingData.IsType<DateTime>(columnIndex)) 217 { 174 } else if (preprocessingData.IsType<DateTime>(columnIndex)) { 218 175 return "DateTime"; 219 176 } … … 221 178 } 222 179 223 private List<T> GetValuesWithoutNaN<T>(int columnIndex) 224 { 180 private List<T> GetValuesWithoutNaN<T>(int columnIndex) { 225 181 IEnumerable<int> missing = searchLogic.GetMissingValueIndices(columnIndex); 226 return (List<T>)preprocessingData.GetValues<T>(columnIndex).Select((v, i) => new { i, v }).Where(x => !missing.Contains(x.i)); 227 } 228 private IEnumerable<double> GetDateTimeAsSeconds(int columnIndex) 229 { 182 return preprocessingData.GetValues<T>(columnIndex) 183 .Select((v, i) => new { i, v }) 184 .Where(x => !missing.Contains(x.i)) 185 .Select(x => x.v).ToList<T>(); 186 } 187 private IEnumerable<double> GetDateTimeAsSeconds(int columnIndex) { 230 188 return GetValuesWithoutNaN<DateTime>(columnIndex).Select(x => (double)x.Ticks / TimeSpan.TicksPerSecond); 231 189 } 232 190 233 private DateTime GetSecondsAsDateTime(double seconds) 234 { 191 private DateTime GetSecondsAsDateTime(double seconds) { 235 192 DateTime dateTime = new DateTime(); 236 193 return dateTime.AddSeconds(seconds); 237 194 } 238 195 239 public event DataPreprocessingChangedEventHandler Changed 240 { 196 public event DataPreprocessingChangedEventHandler Changed { 241 197 add { preprocessingData.Changed += value; } 242 198 remove { preprocessingData.Changed -= value; }
Note: See TracChangeset
for help on using the changeset viewer.