Skip to content

BF: Fix for 'split' (concatenated?) multiframe DICOM #1328

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 16 additions & 18 deletions nibabel/nicom/dicomwrappers.py
Original file line number Diff line number Diff line change
Expand Up @@ -554,23 +554,20 @@
raise WrapperError('Missing information, cannot remove indices with confidence.')
derived_dim_idx = dim_seq.index(derived_tag)
frame_indices = np.delete(frame_indices, derived_dim_idx, axis=1)
# account for the 2 additional dimensions (row and column) not included
# in the indices
n_dim = frame_indices.shape[1] + 2
# Store frame indices
self._frame_indices = frame_indices
if n_dim < 4: # 3D volume
return rows, cols, n_frames
# More than 3 dimensions
# Determine size of any extra-spatial dimensions
ns_unique = [len(np.unique(row)) for row in self._frame_indices.T]
shape = (rows, cols) + tuple(ns_unique)
n_vols = np.prod(shape[3:])
n_frames_calc = n_vols * shape[2]
if n_frames != n_frames_calc:
raise WrapperError(
f'Calculated # of frames ({n_frames_calc}={n_vols}*{shape[2]}) '
f'of shape {shape} does not match NumberOfFrames {n_frames}.'
)
shape = (rows, cols) + tuple(x for i, x in enumerate(ns_unique) if i == 0 or x != 1)
n_dim = len(shape)
if n_dim > 3:
n_vols = np.prod(shape[3:])
n_frames_calc = n_vols * shape[2]
if n_frames != n_frames_calc:
raise WrapperError(

Check warning on line 567 in nibabel/nicom/dicomwrappers.py

View check run for this annotation

Codecov / codecov/patch

nibabel/nicom/dicomwrappers.py#L567

Added line #L567 was not covered by tests
f'Calculated # of frames ({n_frames_calc}={n_vols}*{shape[2]}) '
f'of shape {shape} does not match NumberOfFrames {n_frames}.'
)
return tuple(shape)

@one_time
Expand Down Expand Up @@ -640,10 +637,11 @@
raise WrapperError('No valid information for image shape')
data = self.get_pixel_array()
# Roll frames axis to last
data = data.transpose((1, 2, 0))
# Sort frames with first index changing fastest, last slowest
sorted_indices = np.lexsort(self._frame_indices.T)
data = data[..., sorted_indices]
if len(data.shape) > 2:
data = data.transpose((1, 2, 0))
# Sort frames with first index changing fastest, last slowest
sorted_indices = np.lexsort(self._frame_indices.T)
data = data[..., sorted_indices]
data = data.reshape(shape, order='F')
return self._scale_data(data)

Expand Down
Loading