Skip to content

Commit c836dce

Browse files
author
Ben Cipollini
committed
Fix broadcasting bug (img1.shape == (1,2,3), img2.shape=(1,2,1), axis=None would broadcast rather than error)
1 parent 84640b3 commit c836dce

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

nibabel/funcs.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,17 +122,23 @@ def concat_images(images, check_affines=True, axis=None):
122122
if i == 0: # first image, initialize data from loaded image
123123
affine = img.affine
124124
header = img.header
125+
shape = img.shape
125126
klass = img.__class__
126127

127128
if axis is None: # collect images in output array for efficiency
128-
out_shape = (n_imgs, ) + img.shape
129+
out_shape = (n_imgs, ) + shape
129130
out_data = np.empty(out_shape)
130131
else: # collect images in list for use with np.concatenate
131132
out_data = [None] * n_imgs
132133

133134
elif check_affines and not np.all(img.affine == affine):
134135
raise ValueError('Affines do not match')
135136

137+
elif axis is None and not np.array_equal(shape, img.shape):
138+
# shape mismatch; numpy broadcasting can hide these.
139+
raise ValueError("Image %d (shape=%s) does not match first image "
140+
" shape (%s)." % (i, shape, img.shape))
141+
136142
out_data[i] = img.get_data()
137143

138144
del img

0 commit comments

Comments
 (0)