Skip to content

Commit c49dff2

Browse files
moloneyeffigies
authored andcommitted
BF: Fix for 'split' (concatenated?) multiframe DICOM
Can't just use number of frame indices to determine shape of data, as the actual frames could still be split into different files. Also can't assume a multiframe file is more than a single slice.
1 parent 5d10c5b commit c49dff2

File tree

1 file changed

+16
-18
lines changed

1 file changed

+16
-18
lines changed

nibabel/nicom/dicomwrappers.py

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -554,23 +554,20 @@ def image_shape(self):
554554
raise WrapperError('Missing information, cannot remove indices with confidence.')
555555
derived_dim_idx = dim_seq.index(derived_tag)
556556
frame_indices = np.delete(frame_indices, derived_dim_idx, axis=1)
557-
# account for the 2 additional dimensions (row and column) not included
558-
# in the indices
559-
n_dim = frame_indices.shape[1] + 2
560557
# Store frame indices
561558
self._frame_indices = frame_indices
562-
if n_dim < 4: # 3D volume
563-
return rows, cols, n_frames
564-
# More than 3 dimensions
559+
# Determine size of any extra-spatial dimensions
565560
ns_unique = [len(np.unique(row)) for row in self._frame_indices.T]
566-
shape = (rows, cols) + tuple(ns_unique)
567-
n_vols = np.prod(shape[3:])
568-
n_frames_calc = n_vols * shape[2]
569-
if n_frames != n_frames_calc:
570-
raise WrapperError(
571-
f'Calculated # of frames ({n_frames_calc}={n_vols}*{shape[2]}) '
572-
f'of shape {shape} does not match NumberOfFrames {n_frames}.'
573-
)
561+
shape = (rows, cols) + tuple(x for i, x in enumerate(ns_unique) if i == 0 or x != 1)
562+
n_dim = len(shape)
563+
if n_dim > 3:
564+
n_vols = np.prod(shape[3:])
565+
n_frames_calc = n_vols * shape[2]
566+
if n_frames != n_frames_calc:
567+
raise WrapperError(
568+
f'Calculated # of frames ({n_frames_calc}={n_vols}*{shape[2]}) '
569+
f'of shape {shape} does not match NumberOfFrames {n_frames}.'
570+
)
574571
return tuple(shape)
575572

576573
@cached_property
@@ -640,10 +637,11 @@ def get_data(self):
640637
raise WrapperError('No valid information for image shape')
641638
data = self.get_pixel_array()
642639
# Roll frames axis to last
643-
data = data.transpose((1, 2, 0))
644-
# Sort frames with first index changing fastest, last slowest
645-
sorted_indices = np.lexsort(self._frame_indices.T)
646-
data = data[..., sorted_indices]
640+
if len(data.shape) > 2:
641+
data = data.transpose((1, 2, 0))
642+
# Sort frames with first index changing fastest, last slowest
643+
sorted_indices = np.lexsort(self._frame_indices.T)
644+
data = data[..., sorted_indices]
647645
data = data.reshape(shape, order='F')
648646
return self._scale_data(data)
649647

0 commit comments

Comments
 (0)