summaryrefslogtreecommitdiff
path: root/numpy
diff options
context:
space:
mode:
authorRalf Gommers <ralf.gommers@googlemail.com>2011-03-23 21:12:19 +0100
committerRalf Gommers <ralf.gommers@googlemail.com>2011-03-27 11:43:55 +0200
commit2af2e60fb488dfe251112d139446da61d44e77ab (patch)
tree13710fec949e75ffbfcdb0c0974ff368df2a9c6b /numpy
parent87e12c1dfc90fae5d0a2576add0c1879df815740 (diff)
downloadnumpy-2af2e60fb488dfe251112d139446da61d44e77ab.tar.gz
ENH: Make all histogram functions work with empty input.
Diffstat (limited to 'numpy')
-rw-r--r--numpy/lib/function_base.py29
-rw-r--r--numpy/lib/tests/test_function_base.py11
-rw-r--r--numpy/lib/tests/test_twodim_base.py4
3 files changed, 31 insertions, 13 deletions
diff --git a/numpy/lib/function_base.py b/numpy/lib/function_base.py
index 0944e0ef0..d30361dab 100644
--- a/numpy/lib/function_base.py
+++ b/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.
diff --git a/numpy/lib/tests/test_function_base.py b/numpy/lib/tests/test_function_base.py
index 13930c79e..cf8429a84 100644
--- a/numpy/lib/tests/test_function_base.py
+++ b/numpy/lib/tests/test_function_base.py
@@ -565,8 +565,6 @@ class TestHistogram(TestCase):
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 @@ class TestHistogram(TestCase):
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 @@ class TestHistogramdd(TestCase):
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):
diff --git a/numpy/lib/tests/test_twodim_base.py b/numpy/lib/tests/test_twodim_base.py
index f2724a951..9e78916b9 100644
--- a/numpy/lib/tests/test_twodim_base.py
+++ b/numpy/lib/tests/test_twodim_base.py
@@ -222,6 +222,10 @@ class TestHistogram2d(TestCase):
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):