8000 MAINT: Extract helper functions from histogram by eric-wieser · Pull Request #10261 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

MAINT: Extract helper functions from histogram #10261

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 1 commit into from
Dec 22, 2017
Merged
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
Diff view
Diff view
71 changes: 44 additions & 27 deletions numpy/lib/histograms.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,45 @@ def _hist_bin_auto(x):
'sturges': _hist_bin_sturges}


def _ravel_and_check_weights(a, weights):
""" Check a and weights have matching shapes, and ravel both """
a = np.asarray(a)
if weights is not None:
weights = np.asarray(weights)
if weights.shape != a.shape:
raise ValueError(
'weights should have the same shape as a.')
weights = weights.ravel()
a = a.ravel()
return a, weights


def _get_outer_edges(a, range):
"""
Determine the outer bin edges to use, from either the data or the range
argument
"""
if range is None:
if a.size == 0:
# handle empty arrays. Can't determine range, so use 0-1.
first_edge, last_edge = 0.0, 1.0
else:
first_edge, last_edge = a.min() + 0.0, a.max() + 0.0
else:
first_edge, last_edge = [mi + 0.0 for mi in range]
if first_edge > last_edge:
raise ValueError(
'max must be larger than min in range parameter.')
if not np.all(np.isfinite([first_edge, last_edge])):
raise ValueError(
'range parameter must be finite.')
if first_edge == last_edge:
first_edge -= 0.5
last_edge += 0.5

return first_edge, last_edge


def histogram(a, bins=10, range=None, normed=False, weights=None,
density=None):
r"""
Expand Down Expand Up @@ -414,38 +453,16 @@ def histogram(a, bins=10, range=None, normed=False, weights=None,
>>> plt.show()

"""
a = np.asarray(a)
if weights is not None:
weights = np.asarray(weights)
if weights.shape != a.shape:
raise ValueError(
'weights should have the same shape as a.')
weights = weights.ravel()
a = a.ravel()

# Do not modify the original value of range so we can check for `None`
if range is None:
if a.size == 0:
# handle empty arrays. Can't determine range, so use 0-1.
first_edge, last_edge = 0.0, 1.0
else:
first_edge, last_edge = a.min() + 0.0, a.max() + 0.0
else:
first_edge, last_edge = [mi + 0.0 for mi in range]
if first_edge > last_edge:
raise ValueError(
'max must be larger than min in range parameter.')
if not np.all(np.isfinite([first_edge, last_edge])):
raise ValueError(
'range parameter must be finite.')
if first_edge == last_edge:
first_edge -= 0.5
last_edge += 0.5

# density overrides the normed keyword
if density is not None:
normed = False

a, weights = _ravel_and_check_weights(a, weights)

# Do not modify the original value of range so we can check for `None`
first_edge, last_edge = _get_outer_edges(a, range)

# parse the overloaded bins argument
n_equal_bins = None
bin_edges = None
Expand Down
0