Skip to content

MAINT: Tidy np.histogram, and improve error messages #9889

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

Merged
merged 2 commits into from
Oct 20, 2017

Conversation

eric-wieser
Copy link
Member

Split up the overloaded bins variable into separate names depending on its meaning

Helpful errors are now emitted for:

  • non-integer bin counts
  • non-1d bin edges

Removes another use of np.isscalar...

Had to read through this to work out the purpose of the np.scalar, so seemed worth fixing up at the same time

@@ -646,7 +647,7 @@ def histogram(a, bins=10, range=None, normed=False, weights=None,
a = asarray(a)
if weights is not None:
weights = asarray(weights)
if np.any(weights.shape != a.shape):
if weights.shape != a.shape:
Copy link
Member Author

Choose a reason for hiding this comment

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

No need for np.any here - these are tuples

@@ -671,34 +672,65 @@ def histogram(a, bins=10, range=None, normed=False, weights=None,
mn -= 0.5
mx += 0.5

# density overrides the normed keyword
if density is not None:
normed = False
Copy link
Member Author

Choose a reason for hiding this comment

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

A little tidier to handle this first


# parse the overloaded bins argument
n_equal_bins = None
bin_edges = None
Copy link
Member Author

Choose a reason for hiding this comment

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

Using these names makes things a lot clearer, rather than bins meaning different things in different places

elif np.ndim(bins) == 1:
bin_edges = np.asarray(bins)
if np.any(bin_edges[:-1] > bin_edges[1:]):
raise ValueError('`bins` must increase monotonically')
Copy link
Member Author

Choose a reason for hiding this comment

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

Moved from below - previously this was needlessly checked for a n_bins input if the weights were weird.

Copy link
Member

@charris charris Oct 19, 2017

Choose a reason for hiding this comment

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

Looks like 'non-decreasing', not monotonically increasing.

Copy link
Member Author

Choose a reason for hiding this comment

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

Message wasn't touched by this CR, but I guess I may as well fix it while I'm here. Can you suggest the replacement message?

Copy link
Member

Choose a reason for hiding this comment

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

NVM, I checked, and monotonic is the correct word.

try:
n_equal_bins = operator.index(bins)
except TypeError:
raise TypeError('If a scalar, `bins` must be an integer')
Copy link
Member

Choose a reason for hiding this comment

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

Maybe 'If `bins` is a scalar, it must be an integer'

Same for next error message.

Copy link
Member Author

@eric-wieser eric-wieser Oct 19, 2017

Choose a reason for hiding this comment

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

Or perhaps `bins` must be an integer, if [it is ]a scalar, to try and start all the messages with bins?

Copy link
Member

Choose a reason for hiding this comment

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

Let me think about it a bit more.

Copy link
Member Author

Choose a reason for hiding this comment

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

Thinking again, this particular error message should be the generic one - see my update,

raise ValueError('`bins` must increase monotonically')

else:
raise ValueError("If an array, `bins` must be 1d")
Copy link
Member

Choose a reason for hiding this comment

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

See above about error messages.

else:
raise ValueError("If an array, `bins` must be 1d")

del bins
Copy link
Member

Choose a reason for hiding this comment

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

Is this needed?

Copy link
Member Author

Choose a reason for hiding this comment

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

No, but it enforces that the code below does not use the overloaded variable any more, which is probably a good thing


# compute the bins if only the count was specified
if n_equal_bins is not None:
bin_edges = linspace(mn, mx, n_equal_bins + 1, endpoint=True)
Copy link
Member

Choose a reason for hiding this comment

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

Maybe binmin, binmax instead of mn, mx?

Copy link
Member Author

@eric-wieser eric-wieser Oct 19, 2017

Choose a reason for hiding this comment

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

Not all they're used for though - nevermind, seems reasonable - perhaps (upper|lower)_edge, (first|last)_edge,or bin_edge_(upper|lower)?

Copy link
Member

Choose a reason for hiding this comment

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

(first|last) _edge sounds good.

Copy link
Member Author

Choose a reason for hiding this comment

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

Done

Split up the overloaded `bins` variable into separate names depending on its meaning

Helpful errors are now emitted for:
* non-integer bin counts (fixes numpygh-8072)
* non-1d bin edges

Removes another use of `np.isscalar`...
@charris charris merged commit 07e0cfe into numpy:master Oct 20, 2017
@charris
Copy link
Member

charris commented Oct 20, 2017

Thanks Eric.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants