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