Skip to content

Commit 6748c6d

Browse files
committed
revert some of the changes to the test (didnt notice numpy.testing was already used)
1 parent eae2b04 commit 6748c6d

File tree

1 file changed

+46
-43
lines changed

1 file changed

+46
-43
lines changed

nibabel/tests/test_affines.py

Lines changed: 46 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@
99
from ..affines import (AffineError, apply_affine, append_diag, to_matvec,
1010
from_matvec, dot_reduce, voxel_sizes, obliquity)
1111

12+
1213
import pytest
13-
import numpy.testing as npt
14+
from numpy.testing import assert_array_equal, assert_almost_equal, \
15+
assert_array_almost_equal
1416

1517

1618
def validated_apply_affine(T, xyz):
@@ -30,27 +32,28 @@ def test_apply_affine():
3032
rng = np.random.RandomState(20110903)
3133
aff = np.diag([2, 3, 4, 1])
3234
pts = rng.uniform(size=(4, 3))
33-
npt.assert_equal(apply_affine(aff, pts), pts * [[2, 3, 4]])
35+
assert_array_equal(apply_affine(aff, pts),
36+
pts * [[2, 3, 4]])
3437
aff[:3, 3] = [10, 11, 12]
35-
npt.assert_equal(apply_affine(aff, pts),
36-
pts * [[2, 3, 4]] + [[10, 11, 12]])
38+
assert_array_equal(apply_affine(aff, pts),
39+
pts * [[2, 3, 4]] + [[10, 11, 12]])
3740
aff[:3, :] = rng.normal(size=(3, 4))
3841
exp_res = np.concatenate((pts.T, np.ones((1, 4))), axis=0)
3942
exp_res = np.dot(aff, exp_res)[:3, :].T
40-
npt.assert_equal(apply_affine(aff, pts), exp_res)
43+
assert_array_equal(apply_affine(aff, pts), exp_res)
4144
# Check we get the same result as the previous implementation
42-
npt.assert_almost_equal(validated_apply_affine(aff, pts), apply_affine(aff, pts))
45+
assert_almost_equal(validated_apply_affine(aff, pts), apply_affine(aff, pts))
4346
# Check that lists work for inputs
44-
npt.assert_equal(apply_affine(aff.tolist(), pts.tolist()), exp_res)
47+
assert_array_equal(apply_affine(aff.tolist(), pts.tolist()), exp_res)
4548
# Check that it's the same as a banal implementation in the simple case
4649
aff = np.array([[0, 2, 0, 10], [3, 0, 0, 11], [0, 0, 4, 12], [0, 0, 0, 1]])
4750
pts = np.array([[1, 2, 3], [2, 3, 4], [4, 5, 6], [6, 7, 8]])
4851
exp_res = (np.dot(aff[:3, :3], pts.T) + aff[:3, 3:4]).T
49-
npt.assert_equal(apply_affine(aff, pts), exp_res)
52+
assert_array_equal(apply_affine(aff, pts), exp_res)
5053
# That points can be reshaped and you'll get the same shape output
5154
pts = pts.reshape((2, 2, 3))
5255
exp_res = exp_res.reshape((2, 2, 3))
53-
npt.assert_equal(apply_affine(aff, pts), exp_res)
56+
assert_array_equal(apply_affine(aff, pts), exp_res)
5457
# That ND also works
5558
for N in range(2, 6):
5659
aff = np.eye(N)
@@ -64,7 +67,7 @@ def test_apply_affine():
6467
exp_pts = np.dot(aff, new_pts)
6568
exp_pts = np.rollaxis(exp_pts[:-1, :], 0, 2)
6669
exp_res = exp_pts.reshape((2, 3, nd))
67-
npt.assert_almost_equal(res, exp_res)
70+
assert_array_almost_equal(res, exp_res)
6871

6972

7073
def test_matrix_vector():
@@ -75,39 +78,39 @@ def test_matrix_vector():
7578
newmat, newvec = to_matvec(xform)
7679
mat = xform[:-1, :-1]
7780
vec = xform[:-1, -1]
78-
npt.assert_equal(newmat, mat)
79-
npt.assert_equal(newvec, vec)
80-
npt.assert_equal(newvec.shape, (M - 1,))
81-
npt.assert_equal(from_matvec(mat, vec), xform)
81+
assert_array_equal(newmat, mat)
82+
assert_array_equal(newvec, vec)
83+
assert newvec.shape == (M - 1,)
84+
assert_array_equal(from_matvec(mat, vec), xform)
8285
# Check default translation works
8386
xform_not = xform[:]
8487
xform_not[:-1, :] = 0
85-
npt.assert_equal(from_matvec(mat), xform)
86-
npt.assert_equal(from_matvec(mat, None), xform)
88+
assert_array_equal(from_matvec(mat), xform)
89+
assert_array_equal(from_matvec(mat, None), xform)
8790
# Check array-like works
8891
newmat, newvec = to_matvec(xform.tolist())
89-
npt.assert_equal(newmat, mat)
90-
npt.assert_equal(newvec, vec)
91-
npt.assert_equal(from_matvec(mat.tolist(), vec.tolist()), xform)
92+
assert_array_equal(newmat, mat)
93+
assert_array_equal(newvec, vec)
94+
assert_array_equal(from_matvec(mat.tolist(), vec.tolist()), xform)
9295

9396

9497
def test_append_diag():
9598
# Routine for appending diagonal elements
96-
npt.assert_equal(append_diag(np.diag([2, 3, 1]), [1]),
99+
assert_array_equal(append_diag(np.diag([2, 3, 1]), [1]),
97100
np.diag([2, 3, 1, 1]))
98-
npt.assert_equal(append_diag(np.diag([2, 3, 1]), [1, 1]),
101+
assert_array_equal(append_diag(np.diag([2, 3, 1]), [1, 1]),
99102
np.diag([2, 3, 1, 1, 1]))
100103
aff = np.array([[2, 0, 0],
101104
[0, 3, 0],
102105
[0, 0, 1],
103106
[0, 0, 1]])
104-
npt.assert_equal(append_diag(aff, [5], [9]),
107+
assert_array_equal(append_diag(aff, [5], [9]),
105108
[[2, 0, 0, 0],
106109
[0, 3, 0, 0],
107110
[0, 0, 0, 1],
108111
[0, 0, 5, 9],
109112
[0, 0, 0, 1]])
110-
npt.assert_equal(append_diag(aff, [5, 6], [9, 10]),
113+
assert_array_equal(append_diag(aff, [5, 6], [9, 10]),
111114
[[2, 0, 0, 0, 0],
112115
[0, 3, 0, 0, 0],
113116
[0, 0, 0, 0, 1],
@@ -117,7 +120,7 @@ def test_append_diag():
117120
aff = np.array([[2, 0, 0, 0],
118121
[0, 3, 0, 0],
119122
[0, 0, 0, 1]])
120-
npt.assert_equal(append_diag(aff, [5], [9]),
123+
assert_array_equal(append_diag(aff, [5], [9]),
121124
[[2, 0, 0, 0, 0],
122125
[0, 3, 0, 0, 0],
123126
[0, 0, 0, 5, 9],
@@ -133,24 +136,24 @@ def test_dot_reduce():
133136
with pytest.raises(TypeError):
134137
dot_reduce()
135138
# Anything at all on its own, passes through
136-
npt.assert_equal(dot_reduce(1), 1)
137-
npt.assert_equal(dot_reduce(None), None)
138-
npt.assert_equal(dot_reduce([1, 2, 3]), [1, 2, 3])
139+
assert dot_reduce(1) == 1
140+
assert dot_reduce(None) is None
141+
assert dot_reduce([1, 2, 3]) == [1, 2, 3]
139142
# Two or more -> dot product
140143
vec = [1, 2, 3]
141144
mat = np.arange(4, 13).reshape((3, 3))
142-
npt.assert_equal(dot_reduce(vec, mat), np.dot(vec, mat))
143-
npt.assert_equal(dot_reduce(mat, vec), np.dot(mat, vec))
145+
assert_array_equal(dot_reduce(vec, mat), np.dot(vec, mat))
146+
assert_array_equal(dot_reduce(mat, vec), np.dot(mat, vec))
144147
mat2 = np.arange(13, 22).reshape((3, 3))
145-
npt.assert_equal(dot_reduce(mat2, vec, mat),
146-
np.dot(mat2, np.dot(vec, mat)))
147-
npt.assert_equal(dot_reduce(mat, vec, mat2, ),
148-
np.dot(mat, np.dot(vec, mat2)))
148+
assert_array_equal(dot_reduce(mat2, vec, mat),
149+
np.dot(mat2, np.dot(vec, mat)))
150+
assert_array_equal(dot_reduce(mat, vec, mat2, ),
151+
np.dot(mat, np.dot(vec, mat2)))
149152

150153

151154
def test_voxel_sizes():
152155
affine = np.diag([2, 3, 4, 1])
153-
npt.assert_almost_equal(voxel_sizes(affine), [2, 3, 4])
156+
assert_almost_equal(voxel_sizes(affine), [2, 3, 4])
154157
# Some example rotations
155158
rotations = []
156159
for x_rot, y_rot, z_rot in product((0, 0.4), (0, 0.6), (0, 0.8)):
@@ -159,24 +162,24 @@ def test_voxel_sizes():
159162
for n in range(2, 10):
160163
vox_sizes = np.arange(n) + 4.1
161164
aff = np.diag(list(vox_sizes) + [1])
162-
npt.assert_almost_equal(voxel_sizes(aff), vox_sizes)
165+
assert_almost_equal(voxel_sizes(aff), vox_sizes)
163166
# Translations make no difference
164167
aff[:-1, -1] = np.arange(n) + 10
165-
npt.assert_almost_equal(voxel_sizes(aff), vox_sizes)
168+
assert_almost_equal(voxel_sizes(aff), vox_sizes)
166169
# Does not have to be square
167170
new_row = np.vstack((np.zeros(n + 1), aff))
168-
npt.assert_almost_equal(voxel_sizes(new_row), vox_sizes)
171+
assert_almost_equal(voxel_sizes(new_row), vox_sizes)
169172
new_col = np.c_[np.zeros(n + 1), aff]
170-
npt.assert_almost_equal(voxel_sizes(new_col),
171-
[0] + list(vox_sizes))
173+
assert_almost_equal(voxel_sizes(new_col),
174+
[0] + list(vox_sizes))
172175
if n < 3:
173176
continue
174177
# Rotations do not change the voxel size
175178
for rotation in rotations:
176179
rot_affine = np.eye(n + 1)
177180
rot_affine[:3, :3] = rotation
178181
full_aff = rot_affine.dot(aff)
179-
npt.assert_almost_equal(voxel_sizes(full_aff), vox_sizes)
182+
assert_almost_equal(voxel_sizes(full_aff), vox_sizes)
180183

181184

182185
def test_obliquity():
@@ -186,6 +189,6 @@ def test_obliquity():
186189
aligned[:-1, -1] = [-10, -10, -7]
187190
R = from_matvec(euler2mat(x=0.09, y=0.001, z=0.001), [0.0, 0.0, 0.0])
188191
oblique = R.dot(aligned)
189-
npt.assert_almost_equal(obliquity(aligned), [0.0, 0.0, 0.0])
190-
npt.assert_almost_equal(obliquity(oblique) * 180 / pi,
191-
[0.0810285, 5.1569949, 5.1569376])
192+
assert_almost_equal(obliquity(aligned), [0.0, 0.0, 0.0])
193+
assert_almost_equal(obliquity(oblique) * 180 / pi,
194+
[0.0810285, 5.1569949, 5.1569376])

0 commit comments

Comments
 (0)