-
Notifications
You must be signed in to change notification settings - Fork 0
/
handystats.py
128 lines (95 loc) · 2.58 KB
/
handystats.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#!/usr/bin/env python
"""Handy library for making quick basic statistics"""
import builtins
import math
import statistics
VERSION = (1, 0, 0)
__version__ = '.'.join([str(v) for v in VERSION])
__author__ = "Anthony Monthe"
__email__ = "[email protected]"
def mean(values):
values = [v for v in values if v is not None]
if not values:
return
return statistics.mean(values)
def stdev(values):
values = [v for v in values if v is not None]
if not values:
return
if len(values) == 1:
return .0
return statistics.stdev(values)
def min(values):
values = [v for v in values if v is not None]
if not values:
return
return builtins.min(values)
def max(values):
values = [v for v in values if v is not None]
if not values:
return
return builtins.max(values)
def median(values):
values = [v for v in values if v is not None]
if not values:
return
return statistics.median(values)
def harmonic_mean(values):
values = [v for v in values if v is not None]
if not values:
return
return statistics.harmonic_mean(values)
def geo_mean(values):
values = [v for v in values if v is not None]
if not values:
return
return statistics.geometric_mean(values)
def perc_n(values, n):
values = [v for v in values if v is not None]
if not values:
return
if len(values) == 1:
return values[0]
size = len(values)
return sorted(values)[int(math.ceil((size * n) / 100)) - 1]
def perc1(values):
return perc_n(values, 1)
def perc5(values):
return perc_n(values, 5)
def perc95(values):
return perc_n(values, 95)
def perc99(values):
return perc_n(values, 99)
def variance(values):
values = [v for v in values if v is not None]
if not values:
return
return statistics.variance(values)
def pvariance(values):
values = [v for v in values if v is not None]
if not values:
return
return statistics.pvariance(values)
def full_stats(data, aggrs=None, prefix=''):
aggrs = aggrs or DEFAULT_AGGRS
results = {}
for aggr in aggrs:
key = f'{prefix}{aggr}'
results[key] = AGGREGATIONS[aggr](data)
return results
DEFAULT_AGGRS = ('mean', 'stdev', 'min', 'max')
AGGREGATIONS = {
'mean': mean,
'stdev': stdev,
'min': min,
'max': max,
'median': median,
'harmonic_mean': harmonic_mean,
'geo_mean': geo_mean,
'perc1': perc1,
'perc5': perc5,
'perc95': perc95,
'perc99': perc99,
'variance': variance,
'pvariance': pvariance,
}