8000 BUG: fixes #8123 Histogram precision issue due to higher precision in internal mn, mx by mherkazandjian · Pull Request #9189 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

BUG: fixes #8123 Histogram precision issue due to higher precision in internal mn, mx #9189

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

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading 8000
Diff view
Diff view
4 changes: 4 additions & 0 deletions numpy/lib/histograms.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,10 @@ def _get_outer_edges(a, range):
first_edge = first_edge - 0.5
last_edge = last_edge + 0.5

if a.dtype.kind not in ['i']:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps this should be np.issubdtype(a.dtype, np.inexact)?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a comment here why we are doing this: I think we're still ensuring ufunc casting does not cause problems...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An alternative fix might just be to promote the bounds to 0d arrays

first_edge = a.dtype.type(first_edge)
last_edge = a.dtype.type(last_edge)

return first_edge, last_edge


Expand Down
9 changes: 9 additions & 0 deletions numpy/lib/tests/test_histograms.py
6C62
Original file line number Diff line numberDiff line change
Expand Up @@ -299,6 +299,15 @@ def test_datetime(self):
assert_equal(d_edge.dtype, dates.dtype)
assert_equal(t_edge.dtype, td)

def test_precision_data_and_range(self):
tiny_shift = 1e-8
count, x_loc = np.histogram(
np.array([1.0], np.float32),
bins=1,
range=np.array([1.0 + tiny_shift, 2.0], np.float64)
)
assert_equal(count, 1)


class TestHistogramOptimBinNums(object):
"""
Expand Down
0