-
-
Notifications
You must be signed in to change notification settings - Fork 10.9k
BUG: np.histogramdd loses precision on its inputs, leading to incorrect results #11023
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -612,8 +612,6 @@ def test_bins_errors(self): | |
x = np.arange(8).reshape(2, 4) | ||
assert_raises(ValueError, np.histogramdd, x, bins=[-1, 2, 4, 5]) | ||
assert_raises(ValueError, np.histogramdd, x, bins=[1, 0.99, 1, 1]) | ||
assert_raises( | ||
ValueError, np.histogramdd, x, bins=[1, 1, 1, [1, 2, 2, 3]]) | ||
assert_raises( | ||
ValueError, np.histogramdd, x, bins=[1, 1, 1, [1, 2, 3, -3]]) | ||
assert_(np.histogramdd(x, bins=[1, 1, 1, [1, 2, 3, 4]])) | ||
|
@@ -646,7 +644,7 @@ def test_rightmost_binedge(self): | |
bins = [[0., 0.5, 1.0]] | ||
hist, _ = histogramdd(x, bins=bins) | ||
assert_(hist[0] == 0.0) | ||
assert_(hist[1] == 1.) | ||
assert_(hist[1] == 0.0) | ||
x = [1.0001] | ||
bins = [[0., 0.5, 1.0]] | ||
hist, _ = histogramdd(x, bins=bins) | ||
|
@@ -660,3 +658,40 @@ def test_finite_range(self): | |
range=[[0.0, 1.0], [0.25, 0.75], [0.25, np.inf]]) | ||
assert_raises(ValueError, histogramdd, vals, | ||
range=[[0.0, 1.0], [np.nan, 0.75], [0.25, 0.5]]) | ||
|
||
def test_equal_edges(self): | ||
""" Test that adjacent entries in an edge array can be equal """ | ||
x = np.array([0, 1, 2]) | ||
y = np.array([0, 1, 2]) | ||
x_edges = np.array([0, 2, 2]) | ||
y_edges = 1 | ||
hist, edges = histogramdd((x, y), bins=(x_edges, y_edges)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. On master, this test crashes with
|
||
|
||
hist_expected = np.array([ | ||
[2.], | ||
[1.], # x == 2 falls in the final bin | ||
]) | ||
assert_equal(hist, hist_expected) | ||
|
||
def test_edge_dtype(self): | ||
""" Test that if an edge array is input, its type is preserved """ | ||
x = np.array([0, 10, 20]) | ||
y = x / 10 | ||
x_edges = np.array([0, 5, 15, 20]) | ||
y_edges = x_edges / 10 | ||
hist, edges = histogramdd((x, y), bins=(x_edges, y_edges)) | ||
|
||
assert_equal(edges[0].dtype, x_edges.dtype) | ||
assert_equal(edges[1].dtype, y_edges.dtype) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These fail on master, as |
||
|
||
def test_large_integers(self): | ||
big = 2**60 # Too large to represent with a full precision float | ||
|
||
x = np.array([0], np.int64) | ||
x_edges = np.array([-1, +1], np.int64) | ||
y = big + x | ||
y_edges = big + x_edges | ||
|
||
hist, edges = histogramdd((x, y), bins=(x_edges, y_edges)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This also fails on master with
There exist more malicious test cases that give the wrong result rather than crash, but I figured this was sufficient. |
||
|
||
assert_equal(hist[0, 0], 1) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This test was weird.
1.0000000001 <= 1.0
should be false.