summaryrefslogtreecommitdiff
path: root/numpy
diff options
context:
space:
mode:
authorlzkelley <lkelley@cfa.harvard.edu>2015-11-15 17:25:45 -0500
committerlzkelley <lkelley@cfa.harvard.edu>2015-11-17 14:00:38 -0500
commit46d2e8356760e7549d0c80da9fe232177924183c (patch)
tree1fe484ba26adcc55b923ed3ac25defb73df26f7f /numpy
parentcf66c68c6a560c934f4a767934573c7f85dcb4ae (diff)
downloadnumpy-46d2e8356760e7549d0c80da9fe232177924183c.tar.gz
BUG, MAINT: check that histogram range parameters are finite, add tests to assure this. Improved some error-types.
Diffstat (limited to 'numpy')
-rw-r--r--numpy/lib/function_base.py13
-rw-r--r--numpy/lib/tests/test_function_base.py17
2 files changed, 27 insertions, 3 deletions
diff --git a/numpy/lib/function_base.py b/numpy/lib/function_base.py
index fef69dff3..9261dba22 100644
--- a/numpy/lib/function_base.py
+++ b/numpy/lib/function_base.py
@@ -336,8 +336,12 @@ def histogram(a, bins=10, range=None, normed=False, weights=None,
if (range is not None):
mn, mx = range
if (mn > mx):
- raise AttributeError(
+ raise ValueError(
'max must be larger than min in range parameter.')
+ if not np.all(np.isfinite([mn, mx])):
+ raise ValueError(
+ 'range parameter must be finite.')
+
if isinstance(bins, basestring):
bins = _hist_optim_numbins_estimator(a, bins)
@@ -422,7 +426,7 @@ def histogram(a, bins=10, range=None, normed=False, weights=None,
else:
bins = asarray(bins)
if (np.diff(bins) < 0).any():
- raise AttributeError(
+ raise ValueError(
'bins must increase monotonically.')
# Initialize empty histogram
@@ -533,7 +537,7 @@ def histogramdd(sample, bins=10, range=None, normed=False, weights=None):
try:
M = len(bins)
if M != D:
- raise AttributeError(
+ raise ValueError(
'The dimension of bins must be equal to the dimension of the '
' sample x.')
except TypeError:
@@ -551,6 +555,9 @@ def histogramdd(sample, bins=10, range=None, normed=False, weights=None):
smin = atleast_1d(array(sample.min(0), float))
smax = atleast_1d(array(sample.max(0), float))
else:
+ if not np.all(np.isfinite(range)):
+ raise ValueError(
+ 'range parameter must be finite.')
smin = zeros(D)
smax = zeros(D)
for i in arange(D):
diff --git a/numpy/lib/tests/test_function_base.py b/numpy/lib/tests/test_function_base.py
index cc53c2b8e..88c932692 100644
--- a/numpy/lib/tests/test_function_base.py
+++ b/numpy/lib/tests/test_function_base.py
@@ -1267,6 +1267,13 @@ class TestHistogram(TestCase):
assert_array_equal(a, np.array([0]))
assert_array_equal(b, np.array([0, 1]))
+ def test_finite_range(self):
+ # Normal ranges should be fine
+ vals = np.linspace(0.0, 1.0, num=100)
+ histogram(vals, range=[0.25,0.75])
+ assert_raises(ValueError, histogram, vals, range=[np.nan,0.75])
+ assert_raises(ValueError, histogram, vals, range=[0.25,np.inf])
+
class TestHistogramOptimBinNums(TestCase):
"""
@@ -1489,6 +1496,16 @@ class TestHistogramdd(TestCase):
assert_(hist[0] == 0.0)
assert_(hist[1] == 0.0)
+ def test_finite_range(self):
+ vals = np.random.random((100,3))
+ histogramdd(vals, range=[[0.0,1.0],[0.25,0.75],[0.25,0.5]])
+ assert_raises(ValueError, histogramdd, vals,
+ range=[[0.0,1.0],[0.25,0.75],[0.25,np.inf]])
+ assert_raises(ValueError, histogramdd, vals,
+ range=[[0.0,1.0],[np.nan,0.75],[0.25,0.5]])
+
+
+
class TestUnique(TestCase):