@@ -51,6 +51,16 @@ def discretize(data, bin_edges, right_continuous=False):
51
51
x_new = bin_edges [idx ]
52
52
return x_new
53
53
54
+ def _get_tolerance (v ):
55
+ """Determine a numerical tolerance due to limited precision of floating-point values.
56
+
57
+ In other words, returns a maximum possible difference that can be considered negligible.
58
+ Only relevant for floating-point values.
59
+ """
60
+ if issubclass (v .dtype .type , numpy .floating ):
61
+ return numpy .abs (v ) * numpy .finfo (v .dtype ).eps
62
+ return 0 # assuming it's an int
63
+
54
64
def bin1d_vec (p , bins , tol = None , right_continuous = False ):
55
65
"""Efficient implementation of binning routine on 1D Cartesian Grid.
56
66
@@ -71,26 +81,24 @@ def bin1d_vec(p, bins, tol=None, right_continuous=False):
71
81
"""
72
82
bins = numpy .array (bins )
73
83
p = numpy .array (p )
84
+
74
85
a0 = numpy .min (bins )
75
- # if user supplies only a single bin, do 2 things: 1) fix right continuous to true, and use of h is arbitrary
76
86
if bins .size == 1 :
87
+ # for a single bin, set `right_continuous` to true; h is now arbitrary
77
88
right_continuous = True
78
- h = 1
89
+ h = bins [ 0 ] # (just take over dtype)
79
90
else :
80
91
h = bins [1 ] - bins [0 ]
81
92
82
- a0_tol = numpy .abs (a0 ) * numpy .finfo (numpy .float64 ).eps
83
- h_tol = a0_tol # must be based on *involved* numbers
84
- p_tol = numpy .abs (p ) * numpy .finfo (numpy .float64 ).eps
85
-
86
- # absolute tolerance
87
- if tol is None :
88
- idx = numpy .floor ((p + (p_tol + a0_tol ) - a0 ) / (h - h_tol ))
89
- else :
90
- idx = numpy .floor ((p + (tol + a0_tol ) - a0 ) / (h - h_tol ))
91
93
if h < 0 :
92
94
raise ValueError ("grid spacing must be positive and monotonically increasing." )
93
- # account for floating point uncertainties by considering extreme case
95
+
96
+ # account for floating point precision
97
+ a0_tol = _get_tolerance (a0 )
98
+ h_tol = a0_tol # must be based on *involved* numbers
99
+ p_tol = tol or _get_tolerance (p )
100
+
101
+ idx = numpy .floor ((p - a0 + p_tol + a0_tol ) / (h - h_tol ))
94
102
95
103
if right_continuous :
96
104
# set upper bin index to last
0 commit comments