Revision 27 1.10/trunk/binaries/mac/raster/gdal/GDAL.framework/Versions/1.7/Python/site-packages/numpy/lib/tests/test_index_tricks.py
test_index_tricks.py | ||
---|---|---|
1 | 1 |
from numpy.testing import * |
2 |
from numpy import array, ones, r_, mgrid, unravel_index |
|
2 |
import numpy as np |
|
3 |
from numpy import ( array, ones, r_, mgrid, unravel_index, zeros, where, |
|
4 |
ndenumerate, fill_diagonal, diag_indices, |
|
5 |
diag_indices_from ) |
|
3 | 6 |
|
4 | 7 |
class TestUnravelIndex(TestCase): |
5 | 8 |
def test_basic(self): |
... | ... | |
21 | 24 |
assert_almost_equal(b[-1],b[0]+19*0.1,11) |
22 | 25 |
assert_almost_equal(a[1]-a[0],2.0/9.0,11) |
23 | 26 |
|
27 |
def test_linspace_equivalence(self): |
|
28 |
y,st = np.linspace(2,10,retstep=1) |
|
29 |
assert_almost_equal(st,8/49.0) |
|
30 |
assert_array_almost_equal(y,mgrid[2:10:50j],13) |
|
31 |
|
|
24 | 32 |
def test_nd(self): |
25 | 33 |
c = mgrid[-1:1:10j,-2:2:10j] |
26 | 34 |
d = mgrid[-1:1:0.1,-2:2:0.2] |
... | ... | |
62 | 70 |
assert_array_equal(d[5:,:],c) |
63 | 71 |
|
64 | 72 |
|
73 |
class TestNdenumerate(TestCase): |
|
74 |
def test_basic(self): |
|
75 |
a = array([[1,2], [3,4]]) |
|
76 |
assert_equal(list(ndenumerate(a)), |
|
77 |
[((0,0), 1), ((0,1), 2), ((1,0), 3), ((1,1), 4)]) |
|
78 |
|
|
79 |
|
|
80 |
def test_fill_diagonal(): |
|
81 |
a = zeros((3, 3),int) |
|
82 |
fill_diagonal(a, 5) |
|
83 |
yield (assert_array_equal, a, |
|
84 |
array([[5, 0, 0], |
|
85 |
[0, 5, 0], |
|
86 |
[0, 0, 5]])) |
|
87 |
|
|
88 |
# The same function can operate on a 4-d array: |
|
89 |
a = zeros((3, 3, 3, 3), int) |
|
90 |
fill_diagonal(a, 4) |
|
91 |
i = array([0, 1, 2]) |
|
92 |
yield (assert_equal, where(a != 0), (i, i, i, i)) |
|
93 |
|
|
94 |
|
|
95 |
def test_diag_indices(): |
|
96 |
di = diag_indices(4) |
|
97 |
a = array([[1, 2, 3, 4], |
|
98 |
[5, 6, 7, 8], |
|
99 |
[9, 10, 11, 12], |
|
100 |
[13, 14, 15, 16]]) |
|
101 |
a[di] = 100 |
|
102 |
yield (assert_array_equal, a, |
|
103 |
array([[100, 2, 3, 4], |
|
104 |
[ 5, 100, 7, 8], |
|
105 |
[ 9, 10, 100, 12], |
|
106 |
[ 13, 14, 15, 100]])) |
|
107 |
|
|
108 |
# Now, we create indices to manipulate a 3-d array: |
|
109 |
d3 = diag_indices(2, 3) |
|
110 |
|
|
111 |
# And use it to set the diagonal of a zeros array to 1: |
|
112 |
a = zeros((2, 2, 2),int) |
|
113 |
a[d3] = 1 |
|
114 |
yield (assert_array_equal, a, |
|
115 |
array([[[1, 0], |
|
116 |
[0, 0]], |
|
117 |
|
|
118 |
[[0, 0], |
|
119 |
[0, 1]]]) ) |
|
120 |
|
|
121 |
def test_diag_indices_from(): |
|
122 |
x = np.random.random((4, 4)) |
|
123 |
r, c = diag_indices_from(x) |
|
124 |
assert_array_equal(r, np.arange(4)) |
|
125 |
assert_array_equal(c, np.arange(4)) |
|
126 |
|
|
127 |
|
|
65 | 128 |
if __name__ == "__main__": |
66 | 129 |
run_module_suite() |
Also available in: Unified diff