forked from vladounet/evrythng-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
corestats.py
executable file
·89 lines (66 loc) · 2.26 KB
/
corestats.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
#!/usr/bin/env python
# corestats.py (COREy STATS)
# Copyright (c) 2006-2007, Corey Goldberg ([email protected])
#
# statistical calculation class
# for processing numeric sequences
#
# license: GNU LGPL
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
import sys
class Stats:
def __init__(self, sequence):
# sequence of numbers we will process
# convert all items to floats for numerical processing
self.sequence = [float(item) for item in sequence]
def sum(self):
if len(self.sequence) < 1:
return None
else:
return sum(self.sequence)
def count(self):
return len(self.sequence)
def min(self):
if len(self.sequence) < 1:
return None
else:
return min(self.sequence)
def max(self):
if len(self.sequence) < 1:
return None
else:
return max(self.sequence)
def avg(self):
if len(self.sequence) < 1:
return None
else:
return sum(self.sequence) / len(self.sequence)
def median(self):
if len(self.sequence) < 1:
return None
else:
self.sequence.sort()
return self.sequence[len(self.sequence) // 2]
def stdev(self):
if len(self.sequence) < 1:
return None
else:
avg = self.avg()
sdsq = sum([(i - avg) ** 2 for i in self.sequence])
stdev = (sdsq / (len(self.sequence) - 1)) ** .5
return stdev
def percentile(self, percentile):
if len(self.sequence) < 1:
value = None
elif (percentile >= 100):
sys.stderr.write('ERROR: percentile must be < 100. you supplied: %s\n'% percentile)
value = None
else:
element_idx = int(len(self.sequence) * (percentile / 100.0))
self.sequence.sort()
value = self.sequence[element_idx]
return value