Permalink
Browse files

Add docstring and tests for digitize.

  • Loading branch information...
1 parent 339afa6 commit bbabc5c6e3ac6356056ec3f997c81bd430f3214b @stefanv stefanv committed Jun 30, 2006
Showing with 45 additions and 24 deletions.
  1. +13 −2 numpy/lib/function_base.py
  2. +32 −22 numpy/lib/tests/test_function_base.py
View
15 numpy/lib/function_base.py
@@ -21,11 +21,11 @@
from numpy.core.numerictypes import typecodes
from numpy.lib.shape_base import atleast_1d
from numpy.lib.twodim_base import diag
-from _compiled_base import digitize, bincount, _insert, add_docstring
+from _compiled_base import bincount, _insert, add_docstring
+from _compiled_base import digitize as _digitize
#end Fernando's utilities
-
def linspace(start, stop, num=50, endpoint=True, retstep=False):
"""Return evenly spaced numbers.
@@ -378,6 +378,17 @@ def diff(a, n=1, axis=-1):
else:
return a[slice1]-a[slice2]
+def digitize(x,bins):
+ """For each value in x, return the index of the bin to which it belongs.
+
+ Each index i returned is such that bins[i-1] <= x < bins[i] if
+ bins is monotonically increasing, or bins [i-1] > x >= bins[i] if
+ bins is monotonically decreasing.
+
+ Beyond the bounds of the bins 0 or len(bins) is returned as appropriate.
+ """
+ return _digitize(x,bins)
+
def angle(z, deg=0):
"""Return the angle of the complex argument z.
"""
View
54 numpy/lib/tests/test_function_base.py
@@ -8,7 +8,7 @@
from numpy.core import *
del sys.path[0]
-class test_any(ScipyTestCase):
+class test_any(NumpyTestCase):
def check_basic(self):
y1 = [0,0,1,0]
y2 = [0,0,0,0]
@@ -23,7 +23,7 @@ def check_nd(self):
assert_array_equal(sometrue(y1),[1,1,0])
assert_array_equal(sometrue(y1,axis=1),[0,1,1])
-class test_all(ScipyTestCase):
+class test_all(NumpyTestCase):
def check_basic(self):
y1 = [0,1,1,0]
y2 = [0,0,0,0]
@@ -39,7 +39,7 @@ def check_nd(self):
assert_array_equal(alltrue(y1),[0,0,1])
assert_array_equal(alltrue(y1,axis=1),[0,0,1])
-class test_average(ScipyTestCase):
+class test_average(NumpyTestCase):
def check_basic(self):
y1 = array([1,2,3])
assert(average(y1) == 2.)
@@ -58,7 +58,7 @@ def check_basic(self):
assert_array_equal(y5.mean(0), average(y5, 0))
assert_array_equal(y5.mean(1), average(y5, 1))
-class test_logspace(ScipyTestCase):
+class test_logspace(NumpyTestCase):
def check_basic(self):
y = logspace(0,6)
assert(len(y)==50)
@@ -69,7 +69,7 @@ def check_basic(self):
y = logspace(0,6,num=7)
assert_array_equal(y,[1,10,100,1e3,1e4,1e5,1e6])
-class test_linspace(ScipyTestCase):
+class test_linspace(NumpyTestCase):
def check_basic(self):
y = linspace(0,10)
assert(len(y)==50)
@@ -94,7 +94,7 @@ def check_type(self):
assert_equal(t1, t2)
assert_equal(t2, t3)
-class test_amax(ScipyTestCase):
+class test_amax(NumpyTestCase):
def check_basic(self):
a = [3,4,5,10,-3,-5,6.0]
assert_equal(amax(a),10.0)
@@ -104,7 +104,7 @@ def check_basic(self):
assert_equal(amax(b,axis=0),[8.0,10.0,9.0])
assert_equal(amax(b,axis=1),[9.0,10.0,8.0])
-class test_amin(ScipyTestCase):
+class test_amin(NumpyTestCase):
def check_basic(self):
a = [3,4,5,10,-3,-5,6.0]
assert_equal(amin(a),-5.0)
@@ -114,7 +114,7 @@ def check_basic(self):
assert_equal(amin(b,axis=0),[3.0,3.0,2.0])
assert_equal(amin(b,axis=1),[3.0,4.0,2.0])
-class test_ptp(ScipyTestCase):
+class test_ptp(NumpyTestCase):
def check_basic(self):
a = [3,4,5,10,-3,-5,6.0]
assert_equal(ptp(a),15.0)
@@ -124,7 +124,7 @@ def check_basic(self):
assert_equal(ptp(b,axis=0),[5.0,7.0,7.0])
assert_equal(ptp(b,axis=-1),[6.0,6.0,6.0])
-class test_cumsum(ScipyTestCase):
+class test_cumsum(NumpyTestCase):
def check_basic(self):
ba = [1,2,10,11,6,5,4]
ba2 = [[1,2,3,4],[5,6,7,9],[10,3,4,5]]
@@ -140,7 +140,7 @@ def check_basic(self):
[5,11,18,27],
[10,13,17,22]],ctype))
-class test_prod(ScipyTestCase):
+class test_prod(NumpyTestCase):
def check_basic(self):
ba = [1,2,10,11,6,5,4]
ba2 = [[1,2,3,4],[5,6,7,9],[10,3,4,5]]
@@ -158,7 +158,7 @@ def check_basic(self):
array([50,36,84,180],ctype))
assert_array_equal(prod(a2,axis=-1),array([24, 1890, 600],ctype))
-class test_cumprod(ScipyTestCase):
+class test_cumprod(NumpyTestCase):
def check_basic(self):
ba = [1,2,10,11,6,5,4]
ba2 = [[1,2,3,4],[5,6,7,9],[10,3,4,5]]
@@ -183,7 +183,7 @@ def check_basic(self):
[ 5, 30, 210, 1890],
[10, 30, 120, 600]],ctype))
-class test_diff(ScipyTestCase):
+class test_diff(NumpyTestCase):
def check_basic(self):
x = [1,4,6,7,12]
out = array([3,2,1,5])
@@ -204,7 +204,7 @@ def check_nd(self):
assert_array_equal(diff(x,axis=0),out3)
assert_array_equal(diff(x,n=2,axis=0),out4)
-class test_angle(ScipyTestCase):
+class test_angle(NumpyTestCase):
def check_basic(self):
x = [1+3j,sqrt(2)/2.0+1j*sqrt(2)/2,1,1j,-1,-1j,1-3j,-1+3j]
y = angle(x)
@@ -215,7 +215,7 @@ def check_basic(self):
assert_array_almost_equal(y,yo,11)
assert_array_almost_equal(z,zo,11)
-class test_trim_zeros(ScipyTestCase):
+class test_trim_zeros(NumpyTestCase):
""" only testing for integer splits.
"""
def check_basic(self):
@@ -232,7 +232,7 @@ def check_trailing_skip(self):
assert_array_equal(res,array([1,0,2,3,0,4]))
-class test_extins(ScipyTestCase):
+class test_extins(NumpyTestCase):
def check_basic(self):
a = array([1,3,2,1,2,3,3])
b = extract(a>1,a)
@@ -250,7 +250,7 @@ def check_both(self):
insert(a,mask,c)
assert_array_equal(a,ac)
-class test_vectorize(ScipyTestCase):
+class test_vectorize(NumpyTestCase):
def check_simple(self):
def addsubtract(a,b):
if a > b:
@@ -274,17 +274,27 @@ def check_large(self):
f = vectorize(lambda x: x)
y = f(x)
assert_array_equal(y, x)
-
-class test_unwrap(ScipyTestCase):
+class test_digitize(NumpyTestCase):
+ def check_forward(self):
+ x = arange(-6,5)
+ bins = arange(-5,5)
+ assert_array_equal(digitize(x,bins),arange(11))
+
+ def check_reverse(self):
+ x = arange(5,-6,-1)
+ bins = arange(5,-5,-1)
+ assert_array_equal(digitize(x,bins),arange(11))
+
+class test_unwrap(NumpyTestCase):
def check_simple(self):
#check that unwrap removes jumps greather that 2*pi
assert_array_equal(unwrap([1,1+2*pi]),[1,1])
#check that unwrap maintans continuity
assert(all(diff(unwrap(rand(10)*100))<pi))
-class test_filterwindows(ScipyTestCase):
+class test_filterwindows(NumpyTestCase):
def check_hanning(self):
#check symmetry
w=hanning(10)
@@ -314,20 +324,20 @@ def check_blackman(self):
assert_almost_equal(sum(w),3.7800,4)
-class test_trapz(ScipyTestCase):
+class test_trapz(NumpyTestCase):
def check_simple(self):
r=trapz(exp(-1.0/2*(arange(-10,10,.1))**2)/sqrt(2*pi),dx=0.1)
#check integral of normal equals 1
assert_almost_equal(sum(r),1,7)
-class test_sinc(ScipyTestCase):
+class test_sinc(NumpyTestCase):
def check_simple(self):
assert(sinc(0)==1)
w=sinc(linspace(-1,1,100))
#check symmetry
assert_array_almost_equal(w,flipud(w),7)
-class test_histogram(ScipyTestCase):
+class test_histogram(NumpyTestCase):
def check_simple(self):
n=100
v=rand(n)

0 comments on commit bbabc5c

Please sign in to comment.