Skip to content
Browse files

ENH: Make all histogram functions work with empty input.

  • Loading branch information...
1 parent 87e12c1 commit 2af2e60fb488dfe251112d139446da61d44e77ab @rgommers rgommers committed
Showing with 31 additions and 13 deletions.
  1. +18 −11 numpy/lib/function_base.py
  2. +9 −2 numpy/lib/tests/test_function_base.py
  3. +4 −0 numpy/lib/tests/test_twodim_base.py
View
29 numpy/lib/function_base.py
@@ -195,14 +195,12 @@ def histogram(a, bins=10, range=None, normed=False, weights=None):
db = array(np.diff(bins), float)
if not uniform:
warnings.warn("""
- This release of NumPy fixes a normalization bug in histogram
- function occuring with non-uniform bin widths. The returned
- value is now a density: n / (N * bin width), where n is the
- bin count and N the total number of points.
+ This release of NumPy (1.6) fixes a normalization bug in histogram
+ function occuring with non-uniform bin widths. The returned value
+ is now a density: n / (N * bin width), where n is the bin count and
+ N the total number of points.
""")
return n/db/n.sum(), bins
-
-
else:
return n, bins
@@ -228,7 +226,7 @@ def histogramdd(sample, bins=10, range=None, normed=False, weights=None):
A sequence of lower and upper bin edges to be used if the edges are
not given explicitely in `bins`. Defaults to the minimum and maximum
values along each dimension.
- normed : boolean, optional
+ normed : bool, optional
If False, returns the number of samples in each bin. If True, returns
the bin density, ie, the bin count divided by the bin hypervolume.
weights : array_like (N,), optional
@@ -247,8 +245,8 @@ def histogramdd(sample, bins=10, range=None, normed=False, weights=None):
See Also
--------
- histogram: 1D histogram
- histogram2d: 2D histogram
+ histogram: 1-D histogram
+ histogram2d: 2-D histogram
Examples
--------
@@ -285,8 +283,13 @@ def histogramdd(sample, bins=10, range=None, normed=False, weights=None):
# Select range for each dimension
# Used only if number of bins is given.
if range is None:
- smin = atleast_1d(array(sample.min(0), float))
- smax = atleast_1d(array(sample.max(0), float))
+ # Handle empty input. Range can't be determined in that case, use 0-1.
+ if N == 0:
+ smin = zeros(D)
+ smax = ones(D)
+ else:
+ smin = atleast_1d(array(sample.min(0), float))
+ smax = atleast_1d(array(sample.max(0), float))
else:
smin = zeros(D)
smax = zeros(D)
@@ -309,6 +312,10 @@ def histogramdd(sample, bins=10, range=None, normed=False, weights=None):
nbin[i] = len(edges[i])+1 # +1 for outlier bins
dedges[i] = diff(edges[i])
+ # Handle empty input.
+ if N == 0:
+ return np.zeros(D), edges
+
nbin = asarray(nbin)
# Compute the bin number each sample falls into.
View
11 numpy/lib/tests/test_function_base.py
@@ -565,8 +565,6 @@ def test_normed(self):
area = sum(a * diff(b))
assert_almost_equal(area, 1)
- warnings.filterwarnings('ignore',
- message="\s*This release of NumPy fixes a normalization bug")
# Check with non-constant bin widths
v = np.arange(10)
bins = [0,1,3,6,10]
@@ -656,6 +654,11 @@ def test_weights(self):
assert_almost_equal(a, [.2, .1, .1, .075])
warnings.filters.pop(0)
+ def test_empty(self):
+ a, b = histogram([], bins=([0,1]))
+ assert_array_equal(a, array([0]))
+ assert_array_equal(b, array([0, 1]))
+
class TestHistogramdd(TestCase):
def test_simple(self):
@@ -729,6 +732,10 @@ def test_identical_samples(self):
hist, edges = histogramdd(x, bins=2)
assert_array_equal(edges[0], array([-0.5, 0. , 0.5]))
+ def test_empty(self):
+ a, b = histogramdd([[], []], bins=([0,1], [0,1]))
+ assert_array_max_ulp(a, array([ 0., 0.]))
+
class TestUnique(TestCase):
def test_simple(self):
View
4 numpy/lib/tests/test_twodim_base.py
@@ -222,6 +222,10 @@ def test_all_outliers(self):
H, xed, yed = histogram2d(r, r, (4, 5), range=([0,1], [0,1]))
assert_array_equal(H, 0)
+ def test_empty(self):
+ a, edge1, edge2 = histogram2d([],[], bins=([0,1],[0,1]))
+ assert_array_max_ulp(a, array([ 0., 0.]))
+
class TestTri(TestCase):
def test_dtype(self):

0 comments on commit 2af2e60

Please sign in to comment.
Something went wrong with that request. Please try again.