Skip to content

Commit 84640b3

Browse files
author
Ben Cipollini
committed
Remove 3D/4D special-case code.
1 parent f8ad078 commit 84640b3

File tree

2 files changed

+5
-29
lines changed

2 files changed

+5
-29
lines changed

nibabel/funcs.py

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -125,31 +125,21 @@ def concat_images(images, check_affines=True, axis=None):
125125
klass = img.__class__
126126

127127
if axis is None: # collect images in output array for efficiency
128-
out_shape = (n_imgs, ) + img.shape[:3]
128+
out_shape = (n_imgs, ) + img.shape
129129
out_data = np.empty(out_shape)
130130
else: # collect images in list for use with np.concatenate
131131
out_data = [None] * n_imgs
132132

133133
elif check_affines and not np.all(img.affine == affine):
134134
raise ValueError('Affines do not match')
135135

136-
# Special case for 4D image with size[3] == 1; reshape to work!
137-
if axis is None and img.get_data().ndim == 4 and img.get_data().shape[3] == 1:
138-
out_data[i] = np.reshape(img.get_data(), img.get_data().shape[:-1])
139-
else:
140-
out_data[i] = img.get_data()
136+
out_data[i] = img.get_data()
141137

142138
del img
143139

144140
if axis is None:
145141
out_data = np.rollaxis(out_data, 0, out_data.ndim)
146142
else:
147-
# Massage the output, to allow combining 3D and 4D images.
148-
is_3D = [len(d.shape) == 3 for d in out_data]
149-
is_4D = [len(d.shape) == 4 for d in out_data]
150-
if np.any(is_3D) and np.any(is_4D): # Convert all to 4D
151-
out_data = [data if is_4D[di] else np.reshape(data, data.shape + (1,))
152-
for di, data in enumerate(out_data)]
153143
out_data = np.concatenate(out_data, axis=axis)
154144

155145
return klass(out_data, affine, header)

nibabel/tests/test_funcs.py

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -76,23 +76,9 @@ def test_concat():
7676
# but our efficient logic (where all images are
7777
# 3D and the same size) fails, so we also
7878
# have to expect errors for those.
79-
expect_error = False
80-
if data0.ndim == 3 and data1.ndim == 4:
81-
expect_error = axis is None and data1.shape[3] != 1
82-
all_data = np.concatenate([data0[..., np.newaxis], data1],
83-
**np_concat_kwargs)
84-
elif data0.ndim == 4 and data1.ndim == 3:
85-
expect_error = axis is None and data0.shape[3] != 1
86-
all_data = np.concatenate([data0, data1[..., np.newaxis]],
87-
**np_concat_kwargs)
88-
elif data0.ndim == 4 and data1.ndim == 4:
89-
expect_error = axis is None and (data0.shape[3] != 1 or
90-
data1.shape[3] != 1)
91-
all_data = np.concatenate([data0, data1],
92-
**np_concat_kwargs)
93-
elif axis is None: # 3D from here and below
94-
all_data = np.concatenate(
95-
[data0[..., np.newaxis], data1[..., np.newaxis]], 3)
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)
9682
else: # both 3D, appending on final axis
9783
all_data = np.concatenate([data0, data1],
9884
**np_concat_kwargs)

0 commit comments

Comments
 (0)