@@ -30,34 +30,96 @@ def _as_fname(img):
30
30
31
31
32
32
def test_concat ():
33
- shape = (1 ,2 ,5 )
34
- data0 = np .arange (10 ).reshape (shape )
33
+ # Smoke test: concat empty list.
34
+ assert_raises (ValueError , concat_images , [])
35
+
36
+ # Build combinations of 3D, 4D w/size[3] == 1, and 4D w/size[3] == 3
37
+ all_shapes_5D = ((1 , 4 , 5 , 3 , 3 ),
38
+ (7 , 3 , 1 , 4 , 5 ),
39
+ (0 , 2 , 1 , 4 , 5 ))
40
+
35
41
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 )
42
+ for dim in range (2 , 6 ):
43
+ all_shapes_ND = tuple ((shape [:dim ] for shape in all_shapes_5D ))
44
+ all_shapes_N1D_unary = tuple ((shape + (1 ,) for shape in all_shapes_ND ))
45
+ all_shapes = all_shapes_ND + all_shapes_N1D_unary
46
+
47
+ # Loop over all possible combinations of images, in first and
48
+ # second position.
49
+ for data0_shape in all_shapes :
50
+ data0_numel = np .asarray (data0_shape ).prod ()
51
+ data0 = np .arange (data0_numel ).reshape (data0_shape )
52
+ img0_mem = Nifti1Image (data0 , affine )
53
+
54
+ for data1_shape in all_shapes :
55
+ data1_numel = np .asarray (data1_shape ).prod ()
56
+ data1 = np .arange (data1_numel ).reshape (data1_shape )
57
+ img1_mem = Nifti1Image (data1 , affine )
58
+ img2_mem = Nifti1Image (data1 , affine + 1 ) # bad affine
59
+
60
+ # Loop over every possible axis, including None (explicit and implied)
61
+ for axis in (list (range (- (dim - 2 ), (dim - 1 ))) + [None , '__default__' ]):
62
+
63
+ # Allow testing default vs. passing explicit param
64
+ if axis == '__default__' :
65
+ np_concat_kwargs = dict (axis = - 1 )
66
+ concat_imgs_kwargs = dict ()
67
+ axis = None # Convert downstream
68
+ elif axis is None :
69
+ np_concat_kwargs = dict (axis = - 1 )
70
+ concat_imgs_kwargs = dict (axis = axis )
71
+ else :
72
+ np_concat_kwargs = dict (axis = axis )
73
+ concat_imgs_kwargs = dict (axis = axis )
74
+
75
+ # Create expected output
76
+ try :
77
+ # Error will be thrown if the np.concatenate fails.
78
+ # However, when axis=None, the concatenate is possible
79
+ # but our efficient logic (where all images are
80
+ # 3D and the same size) fails, so we also
81
+ # have to expect errors for those.
82
+ if axis is None : # 3D from here and below
83
+ all_data = np .concatenate ([data0 [..., np .newaxis ],
84
+ data1 [..., np .newaxis ]],
85
+ ** np_concat_kwargs )
86
+ else : # both 3D, appending on final axis
87
+ all_data = np .concatenate ([data0 , data1 ],
88
+ ** np_concat_kwargs )
89
+ expect_error = False
90
+ except ValueError :
91
+ # Shapes are not combinable
92
+ expect_error = True
93
+
94
+ # Check filenames and in-memory images work
95
+ with InTemporaryDirectory ():
96
+ # Try mem-based, file-based, and mixed
97
+ imgs = [img0_mem , img1_mem , img2_mem ]
98
+ img_files = [_as_fname (img ) for img in imgs ]
99
+ imgs_mixed = [imgs [0 ], img_files [1 ], imgs [2 ]]
100
+ for img0 , img1 , img2 in (imgs , img_files , imgs_mixed ):
101
+ try :
102
+ all_imgs = concat_images ([img0 , img1 ],
103
+ ** concat_imgs_kwargs )
104
+ except ValueError as ve :
105
+ assert_true (expect_error , str (ve ))
106
+ else :
107
+ assert_false (expect_error , "Expected a concatenation error, but got none." )
108
+ assert_array_equal (all_imgs .get_data (), all_data )
109
+ assert_array_equal (all_imgs .affine , affine )
110
+
111
+ # check that not-matching affines raise error
112
+ assert_raises (ValueError , concat_images , [img0 , img2 ], ** concat_imgs_kwargs )
113
+
114
+ # except if check_affines is False
115
+ try :
116
+ all_imgs = concat_images ([img0 , img1 ], ** concat_imgs_kwargs )
117
+ except ValueError as ve :
118
+ assert_true (expect_error , str (ve ))
119
+ else :
120
+ assert_false (expect_error , "Expected a concatenation error, but got none." )
121
+ assert_array_equal (all_imgs .get_data (), all_data )
122
+ assert_array_equal (all_imgs .affine , affine )
61
123
62
124
63
125
def test_closest_canonical ():
0 commit comments