Skip to content

Commit c39caac

Browse files
author
Ben Cipollini
committed
Add greater coverage of different shapes.
1 parent 2626884 commit c39caac

File tree

1 file changed

+59
-58
lines changed

1 file changed

+59
-58
lines changed

nibabel/tests/test_funcs.py

Lines changed: 59 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -30,70 +30,71 @@ def _as_fname(img):
3030

3131

3232
def test_concat():
33-
shape = (1,2,5)
34-
data0 = np.arange(10).reshape(shape)
35-
affine = np.eye(4)
36-
img0_mem = Nifti1Image(data0, affine)
37-
data1 = data0 - 10
38-
img1_mem = Nifti1Image(data1, affine)
39-
img2_mem = Nifti1Image(data1, affine+1)
40-
img3_mem = Nifti1Image(data1.T, affine)
41-
all_data = np.concatenate(
42-
[data0[:,:,:,np.newaxis],data1[:,:,:,np.newaxis]],3)
43-
# Check filenames and in-memory images work
44-
with InTemporaryDirectory():
45-
imgs = [img0_mem, img1_mem, img2_mem, img3_mem]
46-
img_files = [_as_fname(img) for img in imgs]
47-
for img0, img1, img2, img3 in (imgs, img_files):
48-
all_imgs = concat_images([img0, img1])
49-
assert_array_equal(all_imgs.get_data(), all_data)
50-
assert_array_equal(all_imgs.affine, affine)
51-
# check that not-matching affines raise error
52-
assert_raises(ValueError, concat_images, [img0, img2])
53-
assert_raises(ValueError, concat_images, [img0, img3])
54-
# except if check_affines is False
55-
all_imgs = concat_images([img0, img1])
56-
assert_array_equal(all_imgs.get_data(), all_data)
57-
assert_array_equal(all_imgs.affine, affine)
58-
# Delete images as prophylaxis for windows access errors
59-
for img in imgs:
60-
del(img)
33+
for shape in ((1,2,5), (7,3,1), (13,11,11), (0,1,1)):
34+
numel = np.asarray(shape).prod()
35+
data0 = np.arange(numel).reshape(shape)
36+
affine = np.eye(4)
37+
img0_mem = Nifti1Image(data0, affine)
38+
data1 = data0 - 10
39+
img1_mem = Nifti1Image(data1, affine)
40+
img2_mem = Nifti1Image(data1, affine+1)
41+
img3_mem = Nifti1Image(data1.T, affine)
42+
all_data = np.concatenate(
43+
[data0[:,:,:,np.newaxis],data1[:,:,:,np.newaxis]],3)
44+
# Check filenames and in-memory images work
45+
with InTemporaryDirectory():
46+
imgs = [img0_mem, img1_mem, img2_mem, img3_mem]
47+
img_files = [_as_fname(img) for img in imgs]
48+
for img0, img1, img2, img3 in (imgs, img_files):
49+
all_imgs = concat_images([img0, img1])
50+
assert_array_equal(all_imgs.get_data(), all_data)
51+
assert_array_equal(all_imgs.affine, affine)
52+
# check that not-matching affines raise error
53+
assert_raises(ValueError, concat_images, [img0, img2])
54+
assert_raises(ValueError, concat_images, [img0, img3])
55+
# except if check_affines is False
56+
all_imgs = concat_images([img0, img1])
57+
assert_array_equal(all_imgs.get_data(), all_data)
58+
assert_array_equal(all_imgs.affine, affine)
59+
# Delete images as prophylaxis for windows access errors
60+
for img in imgs:
61+
del(img)
6162

62-
# Test axis parameter and trailing unary dimension
63-
shape_4D = np.asarray(shape + (1,))
64-
data0 = np.arange(10).reshape(shape_4D)
65-
affine = np.eye(4)
66-
img0_mem = Nifti1Image(data0, affine)
67-
img1_mem = Nifti1Image(data0 - 10, affine)
63+
# Test axis parameter and trailing unary dimension
64+
shape_4D = np.asarray(shape + (1,))
65+
data0 = np.arange(numel).reshape(shape_4D)
66+
affine = np.eye(4)
67+
img0_mem = Nifti1Image(data0, affine)
68+
img1_mem = Nifti1Image(data0 - 10, affine)
6869

69-
# 4d, same shape, append on axis 3
70-
concat_img1 = concat_images([img0_mem, img1_mem], axis=3)
71-
expected_shape1 = shape_4D.copy()
72-
expected_shape1[-1] *= 2
73-
assert_array_equal(concat_img1.shape, expected_shape1)
70+
# 4d, same shape, append on axis 3
71+
concat_img1 = concat_images([img0_mem, img1_mem], axis=3)
72+
expected_shape1 = shape_4D.copy()
73+
expected_shape1[-1] *= 2
74+
assert_array_equal(concat_img1.shape, expected_shape1)
7475

75-
# 4d, same shape, append on axis 0
76-
concat_img2 = concat_images([img0_mem, img1_mem], axis=0)
77-
expected_shape2 = shape_4D.copy()
78-
expected_shape2[0] *= 2
79-
assert_array_equal(concat_img2.shape, expected_shape2)
76+
# 4d, same shape, append on axis 0
77+
concat_img2 = concat_images([img0_mem, img1_mem], axis=0)
78+
expected_shape2 = shape_4D.copy()
79+
expected_shape2[0] *= 2
80+
assert_array_equal(concat_img2.shape, expected_shape2)
8081

81-
# 4d, same shape, append on axis -1
82-
concat_img3 = concat_images([img0_mem, img1_mem], axis=-1)
83-
expected_shape3 = shape_4D.copy()
84-
expected_shape3[-1] *= 2
85-
assert_array_equal(concat_img3.shape, expected_shape3)
82+
# 4d, same shape, append on axis -1
83+
concat_img3 = concat_images([img0_mem, img1_mem], axis=-1)
84+
expected_shape3 = shape_4D.copy()
85+
expected_shape3[-1] *= 2
86+
assert_array_equal(concat_img3.shape, expected_shape3)
8687

87-
# 4d, different shape, append on axis that's different
88-
print('%s %s' % (str(concat_img3.shape), str(img1_mem.shape)))
89-
concat_img4 = concat_images([concat_img3, img1_mem], axis=-1)
90-
expected_shape4 = shape_4D.copy()
91-
expected_shape4[-1] *= 3
92-
assert_array_equal(concat_img4.shape, expected_shape4)
88+
# 4d, different shape, append on axis that's different
89+
print('%s %s' % (str(concat_img3.shape), str(img1_mem.shape)))
90+
concat_img4 = concat_images([concat_img3, img1_mem], axis=-1)
91+
expected_shape4 = shape_4D.copy()
92+
expected_shape4[-1] *= 3
93+
assert_array_equal(concat_img4.shape, expected_shape4)
9394

94-
# 4d, different shape, append on axis that's not different...
95-
# Doesn't work!
96-
assert_raises(ValueError, concat_images, [concat_img3, img1_mem], axis=1)
95+
# 4d, different shape, append on axis that's not different...
96+
# Doesn't work!
97+
assert_raises(ValueError, concat_images, [concat_img3, img1_mem], axis=1)
9798

9899

99100
def test_closest_canonical():

0 commit comments

Comments
 (0)