summaryrefslogtreecommitdiff
path: root/numpy/lib/histograms.py
diff options
context:
space:
mode:
authorEric Wieser <wieser.eric@gmail.com>2017-12-09 16:52:00 -0800
committerEric Wieser <wieser.eric@gmail.com>2017-12-22 10:42:40 -0800
commitaecee019491defc79c7a881a2418803164887d74 (patch)
treee19c8ba52c943ba87d28eeb97f87f21dec31ffcc /numpy/lib/histograms.py
parent0bcc57a4dbfd186b252dbdd73ae543da57fd81e4 (diff)
downloadnumpy-aecee019491defc79c7a881a2418803164887d74.tar.gz
MAINT: Extract helper functions from histogram
Diffstat (limited to 'numpy/lib/histograms.py')
-rw-r--r--numpy/lib/histograms.py71
1 files changed, 44 insertions, 27 deletions
diff --git a/numpy/lib/histograms.py b/numpy/lib/histograms.py
index 61bbab6eb..2dc2df31b 100644
--- a/numpy/lib/histograms.py
+++ b/numpy/lib/histograms.py
@@ -199,6 +199,45 @@ _hist_bin_selectors = {'auto': _hist_bin_auto,
'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"""
@@ -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