Skip to content

Commit f8ca1c5

Browse files
committed
fix: prevent bin-edges machine rounding bug when requesting number of bins
- Added dedicated unit test to cover this.
1 parent 52429f2 commit f8ca1c5

File tree

3 files changed

+11
-2
lines changed

3 files changed

+11
-2
lines changed

histogrammar/primitives/bin.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1097,7 +1097,7 @@ def bin_edges(self, low=None, high=None):
10971097
maxBin -= 1
10981098
high = self.low + self.bin_width() * (maxBin + 1)
10991099
# new low and high values reset, so redo num_bins
1100-
num_bins = self.num_bins(low, high)
1100+
num_bins = self.num_bins(low + np.finfo(float).eps, high - np.finfo(float).eps)
11011101
edges = np.linspace(low, high, num_bins + 1)
11021102
return edges
11031103

histogrammar/primitives/sparselybin.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -751,7 +751,7 @@ def bin_edges(self, low=None, high=None):
751751
maxBin -= 1
752752
high = self.origin + self.bin_width() * (maxBin + 1)
753753
# number of bins, after low/high adjustments
754-
num_bins = self.num_bins(low, high)
754+
num_bins = self.num_bins(low + np.finfo(float).eps, high - np.finfo(float).eps)
755755
edges = np.linspace(low, high, num_bins + 1)
756756
return edges
757757

tests/testnumpy.py

+9
Original file line numberDiff line numberDiff line change
@@ -723,6 +723,7 @@ def test_num_bins(self):
723723
hist3 = hg.SparselyBin(origin=0.0, binWidth=1.0, quantity=unit('A'))
724724
hist4 = hg.Bin(num=20, low=0.0, high=20., quantity=unit('A'))
725725
hist5 = hg.Bin(num=20, low=0.0, high=20., quantity=unit('A'))
726+
hist6 = hg.Bin(num=201, low=0.0, high=1.005)
726727

727728
# fill them
728729
hist2.fill.numpy(df1)
@@ -734,16 +735,19 @@ def test_num_bins(self):
734735
assert hist3.num_bins() == 18
735736
assert hist4.num_bins() == 20
736737
assert hist5.num_bins() == 20
738+
assert hist6.num_bins() == 201
737739

738740
assert hist2.num_bins(low=10, high=25) == 15
739741
assert hist3.num_bins(low=10, high=25) == 15
740742
assert hist4.num_bins(low=10, high=25) == 10
741743
assert hist5.num_bins(low=10, high=25) == 10
744+
assert hist6.num_bins(low=0.2089, high=0.9333) == 146
742745

743746
assert hist2.num_bins(low=-10, high=28) == 38
744747
assert hist3.num_bins(low=-10, high=28) == 38
745748
assert hist4.num_bins(low=-10, high=28) == 20
746749
assert hist5.num_bins(low=-10, high=28) == 20
750+
assert hist6.num_bins(low=0.205, high=0.935) == 146
747751

748752
def test_most_probable_value(self):
749753
""" Test getting most probable value or label from histogram
@@ -917,6 +921,7 @@ def test_bin_edges(self):
917921
hist3 = hg.SparselyBin(origin=0.0, binWidth=1.0, quantity=unit('A'))
918922
hist4 = hg.Bin(num=10, low=0.0, high=10., quantity=unit('A'))
919923
hist5 = hg.Bin(num=10, low=0.0, high=10., quantity=unit('A'))
924+
hist6 = hg.Bin(num=201, low=0.0, high=1.005)
920925

921926
# fill them
922927
hist2.fill.numpy(df1)
@@ -937,6 +942,10 @@ def test_bin_edges(self):
937942
2., 3., 4., 5., 6., 7., 8., 9., 10.])
938943
np.testing.assert_array_equal(hist5.bin_edges(low=1.1, high=5.4), [1., 2., 3., 4., 5., 6.])
939944

945+
assert len(hist6.bin_edges()) == 202
946+
assert len(hist6.bin_edges(low=0.2089, high=0.9333)) == 147
947+
assert len(hist6.bin_edges(low=0.205, high=0.935)) == 147
948+
940949
def test_bin_width(self):
941950
""" Test getting the bin width of bin and sparselybin histograms
942951
"""

0 commit comments

Comments
 (0)