Skip to content

Commit cf71eac

Browse files
committed
BF: Remove assumption that StackID is always the first dimension of the frame indices for multiframe DICOM
1 parent cf99743 commit cf71eac

File tree

1 file changed

+22
-6
lines changed

1 file changed

+22
-6
lines changed

nibabel/nicom/dicomwrappers.py

100644100755
Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -473,13 +473,29 @@ def image_shape(self):
473473
frame_indices = np.array(
474474
[frame.FrameContentSequence[0].DimensionIndexValues
475475
for frame in self.frames])
476-
n_dim = frame_indices.shape[1] + 1
477-
# Check there is only one multiframe stack index
478-
if np.any(np.diff(frame_indices[:, 0])):
479-
raise WrapperError("File contains more than one StackID. Cannot "
480-
"handle multi-stack files")
476+
477+
# Check that there is only one multiframe stack index
478+
stack_ids = set(frame.FrameContentSequence[0].get('StackID')
479+
for frame in self.frames)
480+
if len(stack_ids) > 1:
481+
raise WrapperError("File contains more than one StackID. "
482+
"Cannot handle multi-stack files")
483+
484+
# Determine if one of the Dimension indices refers to the stack id
485+
dim_seq = [(dim.DimensionIndexPointer, dim.FunctionalGroupPointer)
486+
for dim in self.get('DimensionIndexSequence')]
487+
try:
488+
stack_dim_idx = dim_seq.index(((0x14, 0x2369), (0x14, 0x2397)))
489+
except ValueError:
490+
stack_dim_idx = None
491+
492+
# remove superfluous stack id index
493+
if stack_dim_idx is not None:
494+
frame_indices = np.delete(frame_indices, stack_dim_idx, axis=1)
495+
496+
n_dim = frame_indices.shape[1] + 2
481497
# Store frame indices
482-
self._frame_indices = frame_indices[:, 1:]
498+
self._frame_indices = frame_indices
483499
if n_dim < 4: # 3D volume
484500
return rows, cols, n_frames
485501
# More than 3 dimensions

0 commit comments

Comments
 (0)