Skip to content

Commit 7b8a1b8

Browse files
committed
moving test_affines to pytest
1 parent 785e9e2 commit 7b8a1b8

File tree

1 file changed

+48
-49
lines changed

1 file changed

+48
-49
lines changed

nibabel/tests/test_affines.py

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

12-
13-
from nose.tools import assert_equal, assert_raises
14-
from numpy.testing import assert_array_equal, assert_almost_equal, \
15-
assert_array_almost_equal
12+
import pytest
13+
import numpy.testing as npt
1614

1715

1816
def validated_apply_affine(T, xyz):
@@ -32,28 +30,27 @@ def test_apply_affine():
3230
rng = np.random.RandomState(20110903)
3331
aff = np.diag([2, 3, 4, 1])
3432
pts = rng.uniform(size=(4, 3))
35-
assert_array_equal(apply_affine(aff, pts),
36-
pts * [[2, 3, 4]])
33+
npt.assert_equal(apply_affine(aff, pts), pts * [[2, 3, 4]])
3734
aff[:3, 3] = [10, 11, 12]
38-
assert_array_equal(apply_affine(aff, pts),
39-
pts * [[2, 3, 4]] + [[10, 11, 12]])
35+
npt.assert_equal(apply_affine(aff, pts),
36+
pts * [[2, 3, 4]] + [[10, 11, 12]])
4037
aff[:3, :] = rng.normal(size=(3, 4))
4138
exp_res = np.concatenate((pts.T, np.ones((1, 4))), axis=0)
4239
exp_res = np.dot(aff, exp_res)[:3, :].T
43-
assert_array_equal(apply_affine(aff, pts), exp_res)
40+
npt.assert_equal(apply_affine(aff, pts), exp_res)
4441
# Check we get the same result as the previous implementation
45-
assert_almost_equal(validated_apply_affine(aff, pts), apply_affine(aff, pts))
42+
npt.assert_almost_equal(validated_apply_affine(aff, pts), apply_affine(aff, pts))
4643
# Check that lists work for inputs
47-
assert_array_equal(apply_affine(aff.tolist(), pts.tolist()), exp_res)
44+
npt.assert_equal(apply_affine(aff.tolist(), pts.tolist()), exp_res)
4845
# Check that it's the same as a banal implementation in the simple case
4946
aff = np.array([[0, 2, 0, 10], [3, 0, 0, 11], [0, 0, 4, 12], [0, 0, 0, 1]])
5047
pts = np.array([[1, 2, 3], [2, 3, 4], [4, 5, 6], [6, 7, 8]])
5148
exp_res = (np.dot(aff[:3, :3], pts.T) + aff[:3, 3:4]).T
52-
assert_array_equal(apply_affine(aff, pts), exp_res)
49+
npt.assert_equal(apply_affine(aff, pts), exp_res)
5350
# That points can be reshaped and you'll get the same shape output
5451
pts = pts.reshape((2, 2, 3))
5552
exp_res = exp_res.reshape((2, 2, 3))
56-
assert_array_equal(apply_affine(aff, pts), exp_res)
53+
npt.assert_equal(apply_affine(aff, pts), exp_res)
5754
# That ND also works
5855
for N in range(2, 6):
5956
aff = np.eye(N)
@@ -67,7 +64,7 @@ def test_apply_affine():
6764
exp_pts = np.dot(aff, new_pts)
6865
exp_pts = np.rollaxis(exp_pts[:-1, :], 0, 2)
6966
exp_res = exp_pts.reshape((2, 3, nd))
70-
assert_array_almost_equal(res, exp_res)
67+
npt.assert_almost_equal(res, exp_res)
7168

7269

7370
def test_matrix_vector():
@@ -78,39 +75,39 @@ def test_matrix_vector():
7875
newmat, newvec = to_matvec(xform)
7976
mat = xform[:-1, :-1]
8077
vec = xform[:-1, -1]
81-
assert_array_equal(newmat, mat)
82-
assert_array_equal(newvec, vec)
83-
assert_equal(newvec.shape, (M - 1,))
84-
assert_array_equal(from_matvec(mat, vec), xform)
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)
8582
# Check default translation works
8683
xform_not = xform[:]
8784
xform_not[:-1, :] = 0
88-
assert_array_equal(from_matvec(mat), xform)
89-
assert_array_equal(from_matvec(mat, None), xform)
85+
npt.assert_equal(from_matvec(mat), xform)
86+
npt.assert_equal(from_matvec(mat, None), xform)
9087
# Check array-like works
9188
newmat, newvec = to_matvec(xform.tolist())
92-
assert_array_equal(newmat, mat)
93-
assert_array_equal(newvec, vec)
94-
assert_array_equal(from_matvec(mat.tolist(), vec.tolist()), xform)
89+
npt.assert_equal(newmat, mat)
90+
npt.assert_equal(newvec, vec)
91+
npt.assert_equal(from_matvec(mat.tolist(), vec.tolist()), xform)
9592

9693

9794
def test_append_diag():
9895
# Routine for appending diagonal elements
99-
assert_array_equal(append_diag(np.diag([2, 3, 1]), [1]),
96+
npt.assert_equal(append_diag(np.diag([2, 3, 1]), [1]),
10097
np.diag([2, 3, 1, 1]))
101-
assert_array_equal(append_diag(np.diag([2, 3, 1]), [1, 1]),
98+
npt.assert_equal(append_diag(np.diag([2, 3, 1]), [1, 1]),
10299
np.diag([2, 3, 1, 1, 1]))
103100
aff = np.array([[2, 0, 0],
104101
[0, 3, 0],
105102
[0, 0, 1],
106103
[0, 0, 1]])
107-
assert_array_equal(append_diag(aff, [5], [9]),
104+
npt.assert_equal(append_diag(aff, [5], [9]),
108105
[[2, 0, 0, 0],
109106
[0, 3, 0, 0],
110107
[0, 0, 0, 1],
111108
[0, 0, 5, 9],
112109
[0, 0, 0, 1]])
113-
assert_array_equal(append_diag(aff, [5, 6], [9, 10]),
110+
npt.assert_equal(append_diag(aff, [5, 6], [9, 10]),
114111
[[2, 0, 0, 0, 0],
115112
[0, 3, 0, 0, 0],
116113
[0, 0, 0, 0, 1],
@@ -120,38 +117,40 @@ def test_append_diag():
120117
aff = np.array([[2, 0, 0, 0],
121118
[0, 3, 0, 0],
122119
[0, 0, 0, 1]])
123-
assert_array_equal(append_diag(aff, [5], [9]),
120+
npt.assert_equal(append_diag(aff, [5], [9]),
124121
[[2, 0, 0, 0, 0],
125122
[0, 3, 0, 0, 0],
126123
[0, 0, 0, 5, 9],
127124
[0, 0, 0, 0, 1]])
128125
# Length of starts has to match length of steps
129-
assert_raises(AffineError, append_diag, aff, [5, 6], [9])
126+
with pytest.raises(AffineError):
127+
append_diag(aff, [5, 6], [9])
130128

131129

132130
def test_dot_reduce():
133131
# Chaining numpy dot
134132
# Error for no arguments
135-
assert_raises(TypeError, dot_reduce)
133+
with pytest.raises(TypeError):
134+
dot_reduce()
136135
# Anything at all on its own, passes through
137-
assert_equal(dot_reduce(1), 1)
138-
assert_equal(dot_reduce(None), None)
139-
assert_equal(dot_reduce([1, 2, 3]), [1, 2, 3])
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])
140139
# Two or more -> dot product
141140
vec = [1, 2, 3]
142141
mat = np.arange(4, 13).reshape((3, 3))
143-
assert_array_equal(dot_reduce(vec, mat), np.dot(vec, mat))
144-
assert_array_equal(dot_reduce(mat, vec), np.dot(mat, vec))
142+
npt.assert_equal(dot_reduce(vec, mat), np.dot(vec, mat))
143+
npt.assert_equal(dot_reduce(mat, vec), np.dot(mat, vec))
145144
mat2 = np.arange(13, 22).reshape((3, 3))
146-
assert_array_equal(dot_reduce(mat2, vec, mat),
147-
np.dot(mat2, np.dot(vec, mat)))
148-
assert_array_equal(dot_reduce(mat, vec, mat2, ),
149-
np.dot(mat, np.dot(vec, mat2)))
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)))
150149

151150

152151
def test_voxel_sizes():
153152
affine = np.diag([2, 3, 4, 1])
154-
assert_almost_equal(voxel_sizes(affine), [2, 3, 4])
153+
npt.assert_almost_equal(voxel_sizes(affine), [2, 3, 4])
155154
# Some example rotations
156155
rotations = []
157156
for x_rot, y_rot, z_rot in product((0, 0.4), (0, 0.6), (0, 0.8)):
@@ -160,24 +159,24 @@ def test_voxel_sizes():
160159
for n in range(2, 10):
161160
vox_sizes = np.arange(n) + 4.1
162161
aff = np.diag(list(vox_sizes) + [1])
163-
assert_almost_equal(voxel_sizes(aff), vox_sizes)
162+
npt.assert_almost_equal(voxel_sizes(aff), vox_sizes)
164163
# Translations make no difference
165164
aff[:-1, -1] = np.arange(n) + 10
166-
assert_almost_equal(voxel_sizes(aff), vox_sizes)
165+
npt.assert_almost_equal(voxel_sizes(aff), vox_sizes)
167166
# Does not have to be square
168167
new_row = np.vstack((np.zeros(n + 1), aff))
169-
assert_almost_equal(voxel_sizes(new_row), vox_sizes)
168+
npt.assert_almost_equal(voxel_sizes(new_row), vox_sizes)
170169
new_col = np.c_[np.zeros(n + 1), aff]
171-
assert_almost_equal(voxel_sizes(new_col),
172-
[0] + list(vox_sizes))
170+
npt.assert_almost_equal(voxel_sizes(new_col),
171+
[0] + list(vox_sizes))
173172
if n < 3:
174173
continue
175174
# Rotations do not change the voxel size
176175
for rotation in rotations:
177176
rot_affine = np.eye(n + 1)
178177
rot_affine[:3, :3] = rotation
179178
full_aff = rot_affine.dot(aff)
180-
assert_almost_equal(voxel_sizes(full_aff), vox_sizes)
179+
npt.assert_almost_equal(voxel_sizes(full_aff), vox_sizes)
181180

182181

183182
def test_obliquity():
@@ -187,6 +186,6 @@ def test_obliquity():
187186
aligned[:-1, -1] = [-10, -10, -7]
188187
R = from_matvec(euler2mat(x=0.09, y=0.001, z=0.001), [0.0, 0.0, 0.0])
189188
oblique = R.dot(aligned)
190-
assert_almost_equal(obliquity(aligned), [0.0, 0.0, 0.0])
191-
assert_almost_equal(obliquity(oblique) * 180 / pi,
192-
[0.0810285, 5.1569949, 5.1569376])
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])

0 commit comments

Comments
 (0)