summaryrefslogtreecommitdiff
path: root/numpy/lib
diff options
context:
space:
mode:
authorPaul Ivanov <paul.ivanov@local>2009-12-28 20:49:52 +0000
committerPaul Ivanov <paul.ivanov@local>2009-12-28 20:49:52 +0000
commite4f233ecfedd2aafa258db2d3ae27e30604cc020 (patch)
tree6d32fbdd19b8dca00cd7cafd8df076bac55ddfd8 /numpy/lib
parent5ba01996a9ab2fdfb7c120a5afae801f854a781a (diff)
downloadnumpy-e4f233ecfedd2aafa258db2d3ae27e30604cc020.tar.gz
fixed a whole bunch of doctests
Diffstat (limited to 'numpy/lib')
-rw-r--r--numpy/lib/function_base.py84
-rw-r--r--numpy/lib/index_tricks.py9
-rw-r--r--numpy/lib/io.py17
-rw-r--r--numpy/lib/polynomial.py26
-rw-r--r--numpy/lib/recfunctions.py112
-rw-r--r--numpy/lib/shape_base.py6
-rw-r--r--numpy/lib/tests/test_format.py2
-rw-r--r--numpy/lib/twodim_base.py4
-rw-r--r--numpy/lib/type_check.py3
-rw-r--r--numpy/lib/ufunclike.py4
-rw-r--r--numpy/lib/utils.py10
11 files changed, 189 insertions, 88 deletions
diff --git a/numpy/lib/function_base.py b/numpy/lib/function_base.py
index 76ea08dfb..7253740be 100644
--- a/numpy/lib/function_base.py
+++ b/numpy/lib/function_base.py
@@ -112,7 +112,6 @@ def histogram(a, bins=10, range=None, normed=False, weights=None, new=None):
(array([ 0.25, 0.25, 0.25, 0.25]), array([0, 1, 2, 3, 4]))
>>> np.histogram([[1, 2, 1], [1, 0, 1]], bins=[0,1,2,3])
(array([1, 4, 1]), array([0, 1, 2, 3]))
- ]), array([0, 1, 2, 3]))
>>> a = np.arange(5)
>>> hist, bin_edges = np.histogram(a, normed=True)
@@ -147,7 +146,7 @@ def histogram(a, bins=10, range=None, normed=False, weights=None, new=None):
if mn == mx:
mn -= 0.5
mx += 0.5
- bins = linspace(mn, mx, bins, endpoint=False)
+ bins = np.linspace(mn, mx, bins, endpoint=False)
else:
if normed:
raise ValueError, 'Use new=True to pass bin edges explicitly.'
@@ -293,7 +292,7 @@ def histogramdd(sample, bins=10, range=None, normed=False, weights=None):
>>> r = np.random.randn(100,3)
>>> H, edges = np.histogramdd(r, bins = (5, 8, 4))
>>> H.shape, edges[0].size, edges[1].size, edges[2].size
- ((5,8,4), 6, 9, 5)
+ ((5, 8, 4), 6, 9, 5)
"""
@@ -791,7 +790,7 @@ def copy(a):
-----
This is equivalent to
- >>> np.array(a, copy=True)
+ >>> np.array(a, copy=True) #doctest: +SKIP
Examples
--------
@@ -1019,7 +1018,7 @@ def interp(x, xp, fp, left=None, right=None):
>>> np.interp(2.5, xp, fp)
1.0
>>> np.interp([0, 1, 1.5, 2.72, 3.14], xp, fp)
- array([ 3. , 3. , 2.5, 0.56, 0. ])
+ array([ 3. , 3. , 2.5 , 0.56, 0. ])
>>> UNDEF = -99.0
>>> np.interp(3.14, xp, fp, right=UNDEF)
-99.0
@@ -1032,7 +1031,9 @@ def interp(x, xp, fp, left=None, right=None):
>>> yinterp = np.interp(xvals, x, y)
>>> import matplotlib.pyplot as plt
>>> plt.plot(x, y, 'o')
+ [<matplotlib.lines.Line2D object at 0x...>]
>>> plt.plot(xvals, yinterp, '-x')
+ [<matplotlib.lines.Line2D object at 0x...>]
>>> plt.show()
"""
@@ -1161,7 +1162,7 @@ def sort_complex(a):
array([ 1.+0.j, 2.+0.j, 3.+0.j, 5.+0.j, 6.+0.j])
>>> np.sort_complex([1 + 2j, 2 - 1j, 3 - 2j, 3 - 3j, 3 + 5j])
- array([ 1.+2.j, 2.-1.j, 3.-5.j, 3.-3.j, 3.+2.j])
+ array([ 1.+2.j, 2.-1.j, 3.-3.j, 3.-2.j, 3.+5.j])
"""
b = array(a,copy=True)
@@ -2041,28 +2042,38 @@ def blackman(M):
Plot the window and the frequency response:
- >>> from numpy import clip, log10, array, bartlett, linspace
- >>> from scipy.fftpack import fft, fftshift
+ >>> from numpy import clip, log10, array, blackman, linspace
+ >>> from numpy.fft import fft, fftshift
>>> import matplotlib.pyplot as plt
>>> window = blackman(51)
>>> plt.plot(window)
+ [<matplotlib.lines.Line2D object at 0x...>]
>>> plt.title("Blackman window")
+ <matplotlib.text.Text object at 0x...>
>>> plt.ylabel("Amplitude")
+ <matplotlib.text.Text object at 0x...>
>>> plt.xlabel("Sample")
+ <matplotlib.text.Text object at 0x...>
>>> plt.show()
>>> plt.figure()
+ <matplotlib.figure.Figure object at 0x...>
>>> A = fft(window, 2048) / 25.5
>>> mag = abs(fftshift(A))
>>> freq = linspace(-0.5,0.5,len(A))
>>> response = 20*log10(mag)
>>> response = clip(response,-100,100)
>>> plt.plot(freq, response)
+ [<matplotlib.lines.Line2D object at 0x...>]
>>> plt.title("Frequency response of Blackman window")
+ <matplotlib.text.Text object at 0x...>
>>> plt.ylabel("Magnitude [dB]")
+ <matplotlib.text.Text object at 0x...>
>>> plt.xlabel("Normalized frequency [cycles per sample]")
+ <matplotlib.text.Text object at 0x...>
>>> plt.axis('tight')
+ (-0.5, 0.5, -100.0, ...)
>>> plt.show()
"""
@@ -2146,22 +2157,32 @@ def bartlett(M):
>>> window = bartlett(51)
>>> plt.plot(window)
+ [<matplotlib.lines.Line2D object at 0x...>]
>>> plt.title("Bartlett window")
+ <matplotlib.text.Text object at 0x...>
>>> plt.ylabel("Amplitude")
+ <matplotlib.text.Text object at 0x...>
>>> plt.xlabel("Sample")
+ <matplotlib.text.Text object at 0x...>
>>> plt.show()
>>> plt.figure()
+ <matplotlib.figure.Figure object at 0x...>
>>> A = fft(window, 2048) / 25.5
>>> mag = abs(fftshift(A))
>>> freq = linspace(-0.5,0.5,len(A))
>>> response = 20*log10(mag)
>>> response = clip(response,-100,100)
>>> plt.plot(freq, response)
- >>> plt.title("Frequency response of Blackman window")
+ [<matplotlib.lines.Line2D object at 0x...>]
+ >>> plt.title("Frequency response of Bartlett window")
+ <matplotlib.text.Text object at 0x...>
>>> plt.ylabel("Magnitude [dB]")
+ <matplotlib.text.Text object at 0x...>
>>> plt.xlabel("Normalized frequency [cycles per sample]")
+ <matplotlib.text.Text object at 0x...>
>>> plt.axis('tight')
+ (-0.5, 0.5, -100.0, ...)
>>> plt.show()
"""
@@ -2237,25 +2258,39 @@ def hanning(M):
>>> window = np.hanning(51)
>>> plt.plot(window)
+ [<matplotlib.lines.Line2D object at 0x...>]
>>> plt.title("Hann window")
+ <matplotlib.text.Text object at 0x...>
>>> plt.ylabel("Amplitude")
+ <matplotlib.text.Text object at 0x...>
>>> plt.xlabel("Sample")
+ <matplotlib.text.Text object at 0x...>
>>> plt.show()
>>> plt.figure()
+ <matplotlib.figure.Figure object at 0x...>
>>> A = fft(window, 2048) / 25.5
>>> mag = abs(fftshift(A))
>>> freq = np.linspace(-0.5,0.5,len(A))
>>> response = 20*np.log10(mag)
>>> response = np.clip(response,-100,100)
>>> plt.plot(freq, response)
+ [<matplotlib.lines.Line2D object at 0x...>]
>>> plt.title("Frequency response of the Hann window")
+ <matplotlib.text.Text object at 0x...>
>>> plt.ylabel("Magnitude [dB]")
+ <matplotlib.text.Text object at 0x...>
>>> plt.xlabel("Normalized frequency [cycles per sample]")
+ <matplotlib.text.Text object at 0x...>
>>> plt.axis('tight')
+ (-0.5, 0.5, -100.0, ...)
>>> plt.show()
"""
+ # XXX: this docstring is inconsistent with other filter windows, e.g.
+ # Blackman and Bartlett - they should all follow the same convention for
+ # clarity. Either use np. for all numpy members (as above), or import all
+ # numpy members (as in Blackman and Bartlett examples)
if M < 1:
return array([])
if M == 1:
@@ -2321,27 +2356,37 @@ def hamming(M):
Plot the window and the frequency response:
- >>> from scipy.fftpack import fft, fftshift
+ >>> from numpy.fft import fft, fftshift
>>> import matplotlib.pyplot as plt
>>> window = np.hamming(51)
>>> plt.plot(window)
+ [<matplotlib.lines.Line2D object at 0x...>]
>>> plt.title("Hamming window")
+ <matplotlib.text.Text object at 0x...>
>>> plt.ylabel("Amplitude")
+ <matplotlib.text.Text object at 0x...>
>>> plt.xlabel("Sample")
+ <matplotlib.text.Text object at 0x...>
>>> plt.show()
>>> plt.figure()
+ <matplotlib.figure.Figure object at 0x...>
>>> A = fft(window, 2048) / 25.5
>>> mag = np.abs(fftshift(A))
>>> freq = np.linspace(-0.5, 0.5, len(A))
>>> response = 20 * np.log10(mag)
>>> response = np.clip(response, -100, 100)
>>> plt.plot(freq, response)
+ [<matplotlib.lines.Line2D object at 0x...>]
>>> plt.title("Frequency response of Hamming window")
+ <matplotlib.text.Text object at 0x...>
>>> plt.ylabel("Magnitude [dB]")
+ <matplotlib.text.Text object at 0x...>
>>> plt.xlabel("Normalized frequency [cycles per sample]")
+ <matplotlib.text.Text object at 0x...>
>>> plt.axis('tight')
+ (-0.5, 0.5, -100.0, ...)
>>> plt.show()
"""
@@ -2587,27 +2632,37 @@ def kaiser(M,beta):
Plot the window and the frequency response:
>>> from numpy import clip, log10, array, kaiser, linspace
- >>> from scipy.fftpack import fft, fftshift
+ >>> from numpy.fft import fft, fftshift
>>> import matplotlib.pyplot as plt
>>> window = kaiser(51, 14)
>>> plt.plot(window)
+ [<matplotlib.lines.Line2D object at 0x...>]
>>> plt.title("Kaiser window")
+ <matplotlib.text.Text object at 0x...>
>>> plt.ylabel("Amplitude")
+ <matplotlib.text.Text object at 0x...>
>>> plt.xlabel("Sample")
+ <matplotlib.text.Text object at 0x...>
>>> plt.show()
>>> plt.figure()
+ <matplotlib.figure.Figure object at 0x...>
>>> A = fft(window, 2048) / 25.5
>>> mag = abs(fftshift(A))
>>> freq = linspace(-0.5,0.5,len(A))
>>> response = 20*log10(mag)
>>> response = clip(response,-100,100)
>>> plt.plot(freq, response)
+ [<matplotlib.lines.Line2D object at 0x...>]
>>> plt.title("Frequency response of Kaiser window")
+ <matplotlib.text.Text object at 0x...>
>>> plt.ylabel("Magnitude [dB]")
+ <matplotlib.text.Text object at 0x...>
>>> plt.xlabel("Normalized frequency [cycles per sample]")
+ <matplotlib.text.Text object at 0x...>
>>> plt.axis('tight')
+ (-0.5, 0.5, -100.0, ...)
>>> plt.show()
"""
@@ -2674,9 +2729,13 @@ def sinc(x):
>>> import matplotlib.pyplot as plt
>>> plt.plot(x, np.sinc(x))
+ [<matplotlib.lines.Line2D object at 0x...>]
>>> plt.title("Sinc Function")
+ <matplotlib.text.Text object at 0x...>
>>> plt.ylabel("Amplitude")
+ <matplotlib.text.Text object at 0x...>
>>> plt.xlabel("X")
+ <matplotlib.text.Text object at 0x...>
>>> plt.show()
It works in 2-D as well:
@@ -2684,6 +2743,7 @@ def sinc(x):
>>> x = np.arange(-200., 201.)/50.
>>> xx = np.outer(x, x)
>>> plt.imshow(np.sinc(xx))
+ <matplotlib.image.AxesImage object at 0x...>
"""
y = pi* where(x == 0, 1.0e-20, x)
@@ -3277,7 +3337,7 @@ def append(arr, values, axis=None):
>>> np.append([[1, 2, 3], [4, 5, 6]], [7, 8, 9], axis=0)
Traceback (most recent call last):
...
- ValueError: arrays must have same number of dimension
+ ValueError: arrays must have same number of dimensions
"""
arr = asanyarray(arr)
diff --git a/numpy/lib/index_tricks.py b/numpy/lib/index_tricks.py
index 450ac4df8..8bbdeae1e 100644
--- a/numpy/lib/index_tricks.py
+++ b/numpy/lib/index_tricks.py
@@ -462,6 +462,7 @@ class RClass(AxisConcatenator):
String integers specify the axis to concatenate along or the minimum
number of dimensions to force entries into.
+ >>> a = np.array([[0, 1, 2], [3, 4, 5]])
>>> np.r_['-1', a, a] # concatenate along last axis
array([[0, 1, 2, 0, 1, 2],
[3, 4, 5, 3, 4, 5]])
@@ -757,8 +758,8 @@ def fill_diagonal(a, val):
Examples
--------
- >>> a = zeros((3, 3), int)
- >>> fill_diagonal(a, 5)
+ >>> a = np.zeros((3, 3), int)
+ >>> np.fill_diagonal(a, 5)
>>> a
array([[5, 0, 0],
[0, 5, 0],
@@ -766,8 +767,8 @@ def fill_diagonal(a, val):
The same function can operate on a 4-D array:
- >>> a = zeros((3, 3, 3, 3), int)
- >>> fill_diagonal(a, 4)
+ >>> a = np.zeros((3, 3, 3, 3), int)
+ >>> np.fill_diagonal(a, 4)
We only show a few blocks for clarity:
diff --git a/numpy/lib/io.py b/numpy/lib/io.py
index 7d2ac1c0e..66d695b00 100644
--- a/numpy/lib/io.py
+++ b/numpy/lib/io.py
@@ -734,9 +734,10 @@ def savetxt(fname, X, fmt='%.18e', delimiter=' '):
Examples
--------
- >>> savetxt('test.out', x, delimiter=',') # X is an array
- >>> savetxt('test.out', (x,y,z)) # x,y,z equal sized 1D arrays
- >>> savetxt('test.out', x, fmt='%1.4e') # use exponential notation
+ >>> x = y = z = np.arange(0.0,5.0,1.0)
+ >>> np.savetxt('test.out', x, delimiter=',') # X is an array
+ >>> np.savetxt('test.out', (x,y,z)) # x,y,z equal sized 1D arrays
+ >>> np.savetxt('test.out', x, fmt='%1.4e') # use exponential notation
"""
@@ -833,7 +834,7 @@ def fromregex(file, regexp, dtype):
>>> regexp = r"(\\d+)\\s+(...)" # match [digits, whitespace, anything]
>>> output = np.fromregex('test.dat', regexp,
- [('num', np.int64), ('key', 'S3')])
+ ... [('num', np.int64), ('key', 'S3')])
>>> output
array([(1312L, 'foo'), (1534L, 'bar'), (444L, 'qux')],
dtype=[('num', '<i8'), ('key', '|S3')])
@@ -974,7 +975,7 @@ def genfromtxt(fname, dtype=float, comments='#', delimiter=None,
>>> s = StringIO("1,1.3,abcde")
>>> data = np.genfromtxt(s, dtype=[('myint','i8'),('myfloat','f8'),
- ('mystring','S5')], delimiter=",")
+ ... ('mystring','S5')], delimiter=",")
>>> data
array((1, 1.3, 'abcde'),
dtype=[('myint', '<i8'), ('myfloat', '<f8'), ('mystring', '|S5')])
@@ -983,7 +984,7 @@ def genfromtxt(fname, dtype=float, comments='#', delimiter=None,
>>> s.seek(0) # needed for StringIO example only
>>> data = np.genfromtxt(s, dtype=None,
- names = ['myint','myfloat','mystring'], delimiter=",")
+ ... names = ['myint','myfloat','mystring'], delimiter=",")
>>> data
array((1, 1.3, 'abcde'),
dtype=[('myint', '<i8'), ('myfloat', '<f8'), ('mystring', '|S5')])
@@ -992,7 +993,7 @@ def genfromtxt(fname, dtype=float, comments='#', delimiter=None,
>>> s.seek(0)
>>> data = np.genfromtxt(s, dtype="i8,f8,S5",
- names=['myint','myfloat','mystring'], delimiter=",")
+ ... names=['myint','myfloat','mystring'], delimiter=",")
>>> data
array((1, 1.3, 'abcde'),
dtype=[('myint', '<i8'), ('myfloat', '<f8'), ('mystring', '|S5')])
@@ -1001,7 +1002,7 @@ def genfromtxt(fname, dtype=float, comments='#', delimiter=None,
>>> s = StringIO("11.3abcde")
>>> data = np.genfromtxt(s, dtype=None, names=['intvar','fltvar','strvar'],
- delimiter=[1,3,5])
+ ... delimiter=[1,3,5])
>>> data
array((1, 1.3, 'abcde'),
dtype=[('intvar', '<i8'), ('fltvar', '<f8'), ('strvar', '|S5')])
diff --git a/numpy/lib/polynomial.py b/numpy/lib/polynomial.py
index cf6cd65be..e953f71ba 100644
--- a/numpy/lib/polynomial.py
+++ b/numpy/lib/polynomial.py
@@ -92,11 +92,17 @@ def poly(seq_of_zeros):
Given a sequence of a polynomial's zeros:
>>> np.poly((0, 0, 0)) # Multiple root example
- array([1, 0, 0, 0]) # i.e., z**3 + 0*z**2 + 0*z + 0
+ array([1, 0, 0, 0])
+
+ The line above represents z**3 + 0*z**2 + 0*z + 0.
+
>>> np.poly((-1./2, 0, 1./2))
- array([ 1. , 0. , -0.25, 0. ]) # z**3 - z/4
+ array([ 1. , 0. , -0.25, 0. ])
+
+ The line above represents z**3 - z/4
+
>>> np.poly((np.random.random(1.)[0], 0, np.random.random(1.)[0]))
- array([ 1. , -0.77086955, 0.08618131, 0. ])
+ array([ 1. , -0.77086955, 0.08618131, 0. ]) #random
Given a square array object:
@@ -264,6 +270,7 @@ def polyint(p, m=1, k=None):
>>> p = np.poly1d([1,1,1])
>>> P = np.polyint(p)
+ >>> P
poly1d([ 0.33333333, 0.5 , 1. , 0. ])
>>> np.polyder(P) == p
True
@@ -279,7 +286,7 @@ def polyint(p, m=1, k=None):
0.0
>>> P = np.polyint(p, 3, k=[6,5,3])
>>> P
- poly1d([ 0.01666667, 0.04166667, 0.16666667, 3., 5., 3. ])
+ poly1d([ 0.01666667, 0.04166667, 0.16666667, 3. , 5. , 3. ])
Note that 3 = 6 / 2!, and that the constants are given in the order of
integrations. Constant of the highest-order polynomial term comes first:
@@ -484,6 +491,7 @@ def polyfit(x, y, deg, rcond=None, full=False):
>>> x = np.array([0.0, 1.0, 2.0, 3.0, 4.0, 5.0])
>>> y = np.array([0.0, 0.8, 0.9, 0.1, -0.8, -1.0])
>>> z = np.polyfit(x, y, 3)
+ >>> z
array([ 0.08703704, -0.81349206, 1.69312169, -0.03968254])
It is convenient to use `poly1d` objects for dealing with polynomials:
@@ -512,7 +520,9 @@ def polyfit(x, y, deg, rcond=None, full=False):
>>> import matplotlib.pyplot as plt
>>> xp = np.linspace(-2, 6, 100)
>>> plt.plot(x, y, '.', xp, p(xp), '-', xp, p30(xp), '--')
+ [<matplotlib.lines.Line2D object at 0x...>, <matplotlib.lines.Line2D object at 0x...>, <matplotlib.lines.Line2D object at 0x...>]
>>> plt.ylim(-2,2)
+ (-2, 2)
>>> plt.show()
"""
@@ -836,7 +846,7 @@ def polydiv(u, v):
>>> x = np.array([3.0, 5.0, 2.0])
>>> y = np.array([2.0, 1.0])
>>> np.polydiv(x, y)
- >>> (array([ 1.5 , 1.75]), array([ 0.25]))
+ (array([ 1.5 , 1.75]), array([ 0.25]))
"""
truepoly = (isinstance(u, poly1d) or isinstance(u, poly1d))
@@ -930,7 +940,9 @@ class poly1d(object):
>>> p.r
array([-1.+1.41421356j, -1.-1.41421356j])
>>> p(p.r)
- array([ -4.44089210e-16+0.j, -4.44089210e-16+0.j]) # i.e., (0, 0)
+ array([ -4.44089210e-16+0.j, -4.44089210e-16+0.j])
+
+ These numbers in the previous line represent (0, 0) to machine precision
Show the coefficients:
@@ -955,7 +967,7 @@ class poly1d(object):
poly1d([ 1, 4, 10, 12, 9])
>>> (p**3 + 4) / p
- (poly1d([ 1., 4., 10., 12., 9.]), poly1d([4]))
+ (poly1d([ 1., 4., 10., 12., 9.]), poly1d([ 4.]))
``asarray(p)`` gives the coefficient array, so polynomials can be
used in all functions that accept arrays:
diff --git a/numpy/lib/recfunctions.py b/numpy/lib/recfunctions.py
index b3eecdc0e..3f99a1ba8 100644
--- a/numpy/lib/recfunctions.py
+++ b/numpy/lib/recfunctions.py
@@ -50,10 +50,12 @@ def recursive_fill_fields(input, output):
Examples
--------
+ >>> from numpy.lib import recfunctions as rfn
>>> a = np.array([(1, 10.), (2, 20.)], dtype=[('A', int), ('B', float)])
>>> b = np.zeros((3,), dtype=a.dtype)
- >>> recursive_fill_fields(a, b)
- np.array([(1, 10.), (2, 20.), (0, 0.)], dtype=[('A', int), ('B', float)])
+ >>> rfn.recursive_fill_fields(a, b)
+ array([(1, 10.0), (2, 20.0), (0, 0.0)],
+ dtype=[('A', '<i4'), ('B', '<f8')])
"""
newdtype = output.dtype
@@ -81,12 +83,13 @@ def get_names(adtype):
Examples
--------
- >>> get_names(np.empty((1,), dtype=int)) is None
+ >>> from numpy.lib import recfunctions as rfn
+ >>> rfn.get_names(np.empty((1,), dtype=int)) is None
True
- >>> get_names(np.empty((1,), dtype=[('A',int), ('B', float)]))
+ >>> rfn.get_names(np.empty((1,), dtype=[('A',int), ('B', float)]))
('A', 'B')
>>> adtype = np.dtype([('a', int), ('b', [('ba', int), ('bb', int)])])
- >>> get_names(adtype)
+ >>> rfn.get_names(adtype)
('a', ('b', ('ba', 'bb')))
"""
listnames = []
@@ -112,12 +115,13 @@ def get_names_flat(adtype):
Examples
--------
- >>> get_names_flat(np.empty((1,), dtype=int)) is None
+ >>> from numpy.lib import recfunctions as rfn
+ >>> rfn.get_names_flat(np.empty((1,), dtype=int)) is None
True
- >>> get_names_flat(np.empty((1,), dtype=[('A',int), ('B', float)]))
+ >>> rfn.get_names_flat(np.empty((1,), dtype=[('A',int), ('B', float)]))
('A', 'B')
>>> adtype = np.dtype([('a', int), ('b', [('ba', int), ('bb', int)])])
- >>> get_names_flat(adtype)
+ >>> rfn.get_names_flat(adtype)
('a', 'b', 'ba', 'bb')
"""
listnames = []
@@ -136,8 +140,9 @@ def flatten_descr(ndtype):
Examples
--------
+ >>> from numpy.lib import recfunctions as rfn
>>> ndtype = np.dtype([('a', '<i4'), ('b', [('ba', '<f8'), ('bb', '<i4')])])
- >>> flatten_descr(ndtype)
+ >>> rfn.flatten_descr(ndtype)
(('a', dtype('int32')), ('ba', dtype('float64')), ('bb', dtype('int32')))
"""
@@ -198,12 +203,13 @@ def get_fieldstructure(adtype, lastname=None, parents=None,):
Examples
--------
- >>> ndtype = np.dtype([('A', int),
+ >>> from numpy.lib import recfunctions as rfn
+ >>> ndtype = np.dtype([('A', int),
... ('B', [('BA', int),
... ('BB', [('BBA', int), ('BBB', int)])])])
- >>> get_fieldstructure(ndtype)
- {'A': [], 'B': [], 'BA': ['B'], 'BB': ['B'],
- 'BBA': ['B', 'BB'], 'BBB': ['B', 'BB']}
+ >>> rfn.get_fieldstructure(ndtype)
+ ... # XXX: possible regression, order of BBA and BBB is swapped
+ {'A': [], 'B': [], 'BA': ['B'], 'BB': ['B'], 'BBA': ['B', 'BB'], 'BBB': ['B', 'BB']}
"""
if parents is None:
@@ -340,20 +346,33 @@ def merge_arrays(seqarrays,
Examples
--------
- >>> merge_arrays((np.array([1, 2]), np.array([10., 20., 30.])))
+ >>> from numpy.lib import recfunctions as rfn
+ >>> rfn.merge_arrays((np.array([1, 2]), np.array([10., 20., 30.])))
masked_array(data = [(1, 10.0) (2, 20.0) (--, 30.0)],
- mask = [(False, False) (False, False) (True, False)],
- fill_value=(999999, 1e+20)
- dtype=[('f0', '<i4'), ('f1', '<f8')])
- >>> merge_arrays((np.array([1, 2]), np.array([10., 20., 30.])),
+ mask = [(False, False) (False, False) (True, False)],
+ fill_value = (999999, 1e+20),
+ dtype = [('f0', '<i4'), ('f1', '<f8')])
+
+ >>> rfn.merge_arrays((np.array([1, 2]), np.array([10., 20., 30.])),
... usemask=False)
- array(data = [(1, 10.0) (2, 20.0) (-1, 30.0)],
+ array([(1, 10.0), (2, 20.0), (-1, 30.0)],
dtype=[('f0', '<i4'), ('f1', '<f8')])
- >>> merge_arrays((np.array([1, 2]).view([('a', int)]),
- np.array([10., 20., 30.])),
- usemask=False, asrecarray=True)
- rec.array(data = [(1, 10.0) (2, 20.0) (-1, 30.0)],
- dtype=[('a', int), ('f1', '<f8')])
+ >>> rfn.merge_arrays((np.array([1, 2]).view([('a', int)]),
+ ... np.array([10., 20., 30.])),
+ ... usemask=False, asrecarray=True)
+ rec.array([(1, 10.0), (2, 20.0), (-1, 30.0)],
+ dtype=[('a', '<i4'), ('f1', '<f8')])
+
+ Notes
+ -----
+ * Without a mask, the missing value will be filled with something,
+ * depending on what its corresponding type:
+ -1 for integers
+ -1.0 for floating point numbers
+ '-' for characters
+ '-1' for strings
+ True for boolean values
+ * XXX: I just obtained these values empirically
"""
if (len(seqarrays) == 1):
seqarrays = seqarrays[0]
@@ -436,15 +455,16 @@ def drop_fields(base, drop_names, usemask=True, asrecarray=False):
Examples
--------
+ >>> from numpy.lib import recfunctions as rfn
>>> a = np.array([(1, (2, 3.0)), (4, (5, 6.0))],
- dtype=[('a', int), ('b', [('ba', float), ('bb', int)])])
- >>> drop_fields(a, 'a')
+ ... dtype=[('a', int), ('b', [('ba', float), ('bb', int)])])
+ >>> rfn.drop_fields(a, 'a')
array([((2.0, 3),), ((5.0, 6),)],
dtype=[('b', [('ba', '<f8'), ('bb', '<i4')])])
- >>> drop_fields(a, 'ba')
+ >>> rfn.drop_fields(a, 'ba')
array([(1, (3,)), (4, (6,))],
dtype=[('a', '<i4'), ('b', [('bb', '<i4')])])
- >>> drop_fields(a, ['ba', 'bb'])
+ >>> rfn.drop_fields(a, ['ba', 'bb'])
array([(1,), (4,)],
dtype=[('a', '<i4')])
"""
@@ -500,12 +520,12 @@ def rename_fields(base, namemapper):
Examples
--------
+ >>> from numpy.lib import recfunctions as rfn
>>> a = np.array([(1, (2, [3.0, 30.])), (4, (5, [6.0, 60.]))],
- dtype=[('a', int),
- ('b', [('ba', float), ('bb', (float, 2))])])
- >>> rename_fields(a, {'a':'A', 'bb':'BB'})
- array([(1, (2.0, 3)), (4, (5.0, 6))],
- dtype=[('A', '<i4'), ('b', [('ba', '<f8'), ('BB', '<i4')])])
+ ... dtype=[('a', int),('b', [('ba', float), ('bb', (float, 2))])])
+ >>> rfn.rename_fields(a, {'a':'A', 'bb':'BB'})
+ array([(1, (2.0, [3.0, 30.0])), (4, (5.0, [6.0, 60.0]))],
+ dtype=[('A', '<i4'), ('b', [('ba', '<f8'), ('BB', '<f8', 2)])])
"""
def _recursive_rename_fields(ndtype, namemapper):
@@ -650,19 +670,21 @@ def stack_arrays(arrays, defaults=None, usemask=True, asrecarray=False,
Examples
--------
+ >>> from numpy.lib import recfunctions as rfn
>>> x = np.array([1, 2,])
- >>> stack_arrays(x) is x
+ >>> rfn.stack_arrays(x) is x
True
>>> z = np.array([('A', 1), ('B', 2)], dtype=[('A', '|S3'), ('B', float)])
>>> zz = np.array([('a', 10., 100.), ('b', 20., 200.), ('c', 30., 300.)],
- dtype=[('A', '|S3'), ('B', float), ('C', float)])
- >>> test = stack_arrays((z,zz))
- >>> masked_array(data = [('A', 1.0, --) ('B', 2.0, --) ('a', 10.0, 100.0)
- ... ('b', 20.0, 200.0) ('c', 30.0, 300.0)],
- ... mask = [(False, False, True) (False, False, True) (False, False, False)
- ... (False, False, False) (False, False, False)],
- ... fill_value=('N/A', 1e+20, 1e+20)
- ... dtype=[('A', '|S3'), ('B', '<f8'), ('C', '<f8')])
+ ... dtype=[('A', '|S3'), ('B', float), ('C', float)])
+ >>> test = rfn.stack_arrays((z,zz))
+ >>> test
+ masked_array(data = [('A', 1.0, --) ('B', 2.0, --) ('a', 10.0, 100.0) ('b', 20.0, 200.0)
+ ('c', 30.0, 300.0)],
+ mask = [(False, False, True) (False, False, True) (False, False, False)
+ (False, False, False) (False, False, False)],
+ fill_value = ('N/A', 1e+20, 1e+20),
+ dtype = [('A', '|S3'), ('B', '<f8'), ('C', '<f8')])
"""
if isinstance(arrays, ndarray):
@@ -735,10 +757,12 @@ def find_duplicates(a, key=None, ignoremask=True, return_index=False):
Examples
--------
+ >>> from numpy.lib import recfunctions as rfn
>>> ndtype = [('a', int)]
- >>> a = ma.array([1, 1, 1, 2, 2, 3, 3],
+ >>> a = np.ma.array([1, 1, 1, 2, 2, 3, 3],
... mask=[0, 0, 1, 0, 0, 0, 1]).view(ndtype)
- >>> find_duplicates(a, ignoremask=True, return_index=True)
+ >>> rfn.find_duplicates(a, ignoremask=True, return_index=True)
+ ... # XXX: judging by the output, the ignoremask flag has no effect
"""
a = np.asanyarray(a).ravel()
# Get a dictionary of fields
diff --git a/numpy/lib/shape_base.py b/numpy/lib/shape_base.py
index f21786805..69fe98314 100644
--- a/numpy/lib/shape_base.py
+++ b/numpy/lib/shape_base.py
@@ -46,9 +46,9 @@ def apply_along_axis(func1d,axis,arr,*args):
... return (a[0] + a[-1]) * 0.5
>>> b = np.array([[1,2,3], [4,5,6], [7,8,9]])
>>> np.apply_along_axis(my_func, 0, b)
- array([4., 5., 6.])
+ array([ 4., 5., 6.])
>>> np.apply_along_axis(my_func, 1, b)
- array([2., 5., 8.])
+ array([ 2., 5., 8.])
For a function that doesn't return a scalar, the number of dimensions in
`outarr` is the same as `arr`.
@@ -729,7 +729,7 @@ def kron(a,b):
>>> J1 = (0,) + J # extend to ndim=4
>>> S1 = (1,) + b.shape
>>> K = tuple(np.array(I) * np.array(S1) + np.array(J1))
- >>> C[K] == A[I]*B[J]
+ >>> c[K] == a[I]*b[J]
True
"""
diff --git a/numpy/lib/tests/test_format.py b/numpy/lib/tests/test_format.py
index 35558400f..ec035ca28 100644
--- a/numpy/lib/tests/test_format.py
+++ b/numpy/lib/tests/test_format.py
@@ -108,7 +108,7 @@ Test the header writing.
>>> for arr in basic_arrays + record_arrays:
... f = StringIO()
- ... format.write_array_header_1_0(f, arr)
+ ... format.write_array_header_1_0(f, arr) # XXX: arr is not a dict, items gets called on it
... print repr(f.getvalue())
...
"F\x00{'descr': '|u1', 'fortran_order': False, 'shape': (0,)} \n"
diff --git a/numpy/lib/twodim_base.py b/numpy/lib/twodim_base.py
index 0d53bbb68..40a5aec33 100644
--- a/numpy/lib/twodim_base.py
+++ b/numpy/lib/twodim_base.py
@@ -52,7 +52,7 @@ def fliplr(m):
[ 3., 0., 0.]])
>>> A = np.random.randn(2,3,5)
- >>> np.all(numpy.fliplr(A)==A[:,::-1,...])
+ >>> np.all(np.fliplr(A)==A[:,::-1,...])
True
"""
@@ -582,7 +582,7 @@ def histogram2d(x,y, bins=10, range=None, normed=False, weights=None):
>>> x, y = np.random.randn(2, 100)
>>> H, xedges, yedges = np.histogram2d(x, y, bins=(5, 8))
>>> H.shape, xedges.shape, yedges.shape
- ((5,8), (6,), (9,))
+ ((5, 8), (6,), (9,))
We can now use the Matplotlib to visualize this 2-dimensional histogram:
diff --git a/numpy/lib/type_check.py b/numpy/lib/type_check.py
index f11672609..c0a5760a4 100644
--- a/numpy/lib/type_check.py
+++ b/numpy/lib/type_check.py
@@ -260,7 +260,7 @@ def iscomplexobj(x):
False
>>> np.iscomplexobj(1+0j)
True
- np.iscomplexobj([3, 1+0j, True])
+ >>> np.iscomplexobj([3, 1+0j, True])
True
"""
@@ -345,6 +345,7 @@ def nan_to_num(x):
Examples
--------
+ >>> np.set_printoptions(precision=8)
>>> x = np.array([np.inf, -np.inf, np.nan, -128, 128])
>>> np.nan_to_num(x)
array([ 1.79769313e+308, -1.79769313e+308, 0.00000000e+000,
diff --git a/numpy/lib/ufunclike.py b/numpy/lib/ufunclike.py
index 5e89b0930..bb8ee7808 100644
--- a/numpy/lib/ufunclike.py
+++ b/numpy/lib/ufunclike.py
@@ -99,9 +99,9 @@ def isposinf(x, y=None):
>>> x = np.array([-np.inf, 0., np.inf])
>>> y = np.array([2, 2, 2])
>>> np.isposinf(x, y)
- array([1, 0, 0])
+ array([0, 0, 1])
>>> y
- array([1, 0, 0])
+ array([0, 0, 1])
"""
if y is None:
diff --git a/numpy/lib/utils.py b/numpy/lib/utils.py
index 43d6ede1f..3e73c2a0f 100644
--- a/numpy/lib/utils.py
+++ b/numpy/lib/utils.py
@@ -344,7 +344,7 @@ def who(vardict=None):
>>> d = {'x': np.arange(2.0), 'y': np.arange(3.0), 'txt': 'Some str',
... 'idx':5}
- >>> np.whos(d)
+ >>> np.who(d)
Name Shape Bytes Type
===========================================================
y 3 24 float64
@@ -666,7 +666,7 @@ def source(object, output=sys.stdout):
Examples
--------
- >>> np.source(np.interp)
+ >>> np.source(np.interp) #doctest: +SKIP
In file: /usr/lib/python2.6/dist-packages/numpy/lib/function_base.py
def interp(x, xp, fp, left=None, right=None):
\"\"\".... (full docstring printed)\"\"\"
@@ -677,7 +677,7 @@ def source(object, output=sys.stdout):
The source code is only returned for objects written in Python.
- >>> np.source(np.array)
+ >>> np.source(np.array) #doctest: +SKIP
Not available for this object.
"""
@@ -737,6 +737,8 @@ def lookfor(what, module=None, import_modules=True, regenerate=False,
------------------------------------------
numpy.binary_repr
Return the binary representation of the input number as a string.
+ numpy.core.setup_common.long_double_representation
+ Given a binary dump as given by GNU od -b, look for long double
numpy.base_repr
Return a string representation of a number in the given base system.
...
@@ -1066,7 +1068,7 @@ def safe_eval(source):
...
SyntaxError: invalid syntax
- >>> safe_eval('open("/home/user/.ssh/id_dsa").read()')
+ >>> np.safe_eval('open("/home/user/.ssh/id_dsa").read()')
Traceback (most recent call last):
...
SyntaxError: Unsupported source construct: compiler.ast.CallFunc