Skip to content

Commit 21af99d

Browse files
mgxdeffigies
authored andcommitted
rf: better error handling, reflect change on frame_indices
1 parent b8f32bf commit 21af99d

File tree

1 file changed

+18
-10
lines changed

1 file changed

+18
-10
lines changed

nibabel/nicom/dicomwrappers.py

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from __future__ import division
1515

1616
import operator
17+
import warnings
1718

1819
import numpy as np
1920

@@ -511,13 +512,20 @@ def image_shape(self):
511512
if hasattr(first_frame, 'get') and first_frame.get([0x18, 0x9117]):
512513
# DWI image may include derived isotropic, ADC or trace volume
513514
# check and remove
514-
self.frames = Sequence(
515-
frame for frame in self.frames if
516-
frame.get([0x18, 0x9117])[0].get([0x18, 0x9075]).value
517-
!= 'ISOTROPIC'
518-
)
519-
n_frames = len(self.frames)
520-
has_derived = True
515+
try:
516+
self.frames = Sequence(
517+
frame for frame in self.frames if
518+
frame.MRDiffusionSequence[0].DiffusionDirectionality
519+
!= 'ISOTROPIC'
520+
)
521+
n_frames = len(self.frames)
522+
has_derived = True
523+
except IndexError:
524+
# Sequence tag is found but missing items!
525+
raise WrapperError("Diffusion file missing information")
526+
except AttributeError:
527+
# DiffusionDirectionality tag is not required
528+
pass
521529
assert len(self.frames) == n_frames
522530
frame_indices = np.array(
523531
[frame.FrameContentSequence[0].DimensionIndexValues
@@ -536,6 +544,9 @@ def image_shape(self):
536544
if stackid_tag in dim_seq:
537545
stackid_dim_idx = dim_seq.index(stackid_tag)
538546
frame_indices = np.delete(frame_indices, stackid_dim_idx, axis=1)
547+
if has_derived:
548+
# derived volume is included
549+
frame_indices = np.delete(frame_indices, 1, axis=1)
539550
# account for the 2 additional dimensions (row and column) not included
540551
# in the indices
541552
n_dim = frame_indices.shape[1] + 2
@@ -545,9 +556,6 @@ def image_shape(self):
545556
return rows, cols, n_frames
546557
# More than 3 dimensions
547558
ns_unique = [len(np.unique(row)) for row in self._frame_indices.T]
548-
if len(ns_unique) == 3 and has_derived:
549-
# derived volume is included
550-
ns_unique.pop(1)
551559
shape = (rows, cols) + tuple(ns_unique)
552560
n_vols = np.prod(shape[3:])
553561
if n_frames != n_vols * shape[2]:

0 commit comments

Comments
 (0)