-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinning.py
65 lines (50 loc) · 2.14 KB
/
binning.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
import numpy as np
class Bins:
def __init__(self, centerOfFirstBin, binWidth, centerOfLastBin ):
self.centerOfFirstBin = centerOfFirstBin
self.binWidth = binWidth
self.centerOfLastBin = centerOfLastBin
start_of_first_bin = self.centerOfFirstBin - (0.5*self.binWidth)
end_of_last_bin = self.centerOfLastBin + (0.5*self.binWidth)
self.numberOfBins = (end_of_last_bin - start_of_first_bin)/self.binWidth
#if not float(self.numberOfBins).is_integer():
if not abs(float(self.numberOfBins)) % int(1) < 1e-12: #tolerance on int check
if not abs(1 - (float(self.numberOfBins) % int(1))) < 1e-12: #tolerance on int check
raise Exception("An integer number of bins must exist. The inputs have led to: {0}".format(self.numberOfBins))
self.numberOfBins = int(self.numberOfBins)
self.centers = []
for i in range(self.numberOfBins):
self.centers.append(self.binCenterByIndex(i))
def binCenterForFirstCenterAndWidth(self, x, centerOfFirstBin, binWidth):
if np.isnan(x): return np.nan
return round((x - centerOfFirstBin)/binWidth,0) * binWidth + centerOfFirstBin
def binCenterByIndex(self, index):
return self.centerOfFirstBin + index * self.binWidth
def binCenter(self, x):
if np.isnan(x): return np.nan
return self.binCenterForFirstCenterAndWidth(x, self.centerOfFirstBin, self.binWidth)
class Aggregations:
def __init__(self, minimumCount = 0):
self.minimumCount = minimumCount
def standardError(self, x):
if self.count(x) >= self.minimumCount:
return x.std() / x.count()
else:
return np.nan
def stddev(self, x):
if self.count(x) >= self.minimumCount:
return x.std()
else:
return np.nan
def average(self, x):
if self.count(x) >= self.minimumCount:
return x.mean()
else:
return np.nan
def count(self, x):
return x.count()
def minimum(self, x):
if self.count(x) >= 0:
return x.min()
else:
return np.nan