GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — BUG: histogram small range robust by charris · Pull Request #24185 · numpy/numpy

BUG: histogram small range robust #24185

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.

Sign up for GitHub

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

Merged
charris merged 1 commit into numpy:maintenance/1.25.x from charris:backport-24161
Jul 15, 2023

Uh oh!

There was an error while loading. Please reload this page.

Merged

BUG: histogram small range robust #24185

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
1 commit
Select commit
78137ba
BUG: histogram small range robust
tylerjereddy Jul 10, 2023
File filter
Filter by extension

Clear filters
Conversations
Failed to load comments.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Jump to
Jump to file
Failed to load files.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Diff view
Diff view
BUG: histogram small range robust
* Fixes #23110

* the histogram `norm` variable is used to determine the bin
index of input values, and `norm` is calculated in some cases
by dividing `n_equal_bins` by the range of the data; when the
range of the data is extraordinarily small, the `norm` can become
floating point infinity

* in this patch, we delay calculating `norm` to increase resistance
to the generation of infinite values--for example, a really small
input value divided by a really small range is more resistant
to generating infinity, so we effectively just change the order
of operations a bit

* however, I haven't considered whether this is broadly superior
for resisting floating point non-finite values for other `histogram`
input/extreme value permutations--one might speculate that this is just
patching one extreme case that happened to show up in the wild, but
may increase likelihood of some other extreme case that isn't in our
testsuite yet

* the main logic for this patch is that it fixes an issue that
occurred in the wild and adds a test for
it--if another extreme value case eventually pops up, at least
this case will have a regression guard to keep guiding us in the right
direction
  • Loading branch information

Uh oh!

There was an error while loading. Please reload this page.

@tylerjereddy @charris
tylerjereddy authored and charris committed Jul 14, 2023
commit 78137bafef59abb9ebef1aaf67a7301bc32672c2

There are no files selected for viewing

6 changes: 4 additions & 2 deletions 6 numpy/lib/histograms.py
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -807,7 +807,8 @@ def histogram(a, bins=10, range=None, density=None, weights=None):
n = np.zeros(n_equal_bins, ntype)

# Pre-compute histogram scaling factor
norm = n_equal_bins / _unsigned_subtract(last_edge, first_edge)
norm_numerator = n_equal_bins
norm_denom = _unsigned_subtract(last_edge, first_edge)

# We iterate over blocks here for two reasons: the first is that for
# large arrays, it is actually faster (for example for a 10^8 array it
Expand Down Expand Up @@ -835,7 +836,8 @@ def histogram(a, bins=10, range=None, density=None, weights=None):

# Compute the bin indices, and for values that lie exactly on
# last_edge we need to subtract one
f_indices = _unsigned_subtract(tmp_a, first_edge) * norm
f_indices = ((_unsigned_subtract(tmp_a, first_edge) / norm_denom)
* norm_numerator)
indices = f_indices.astype(np.intp)
indices[indices == n_equal_bins] -= 1

Expand Down
7 changes: 7 additions & 0 deletions 7 numpy/lib/tests/test_histograms.py
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,13 @@ def test_big_arrays(self):
hist = np.histogramdd(sample=sample, bins=(xbins, ybins, zbins))
assert_equal(type(hist), type((1, 2)))

def test_gh_23110(self):
hist, e = np.histogram(np.array([-0.9e-308], dtype='>f8'),
bins=2,
range=(-1e-308, -2e-313))
expected_hist = np.array([1, 0])
assert_array_equal(hist, expected_hist)


class TestHistogramOptimBinNums:
"""
Expand Down
Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.