1414from __future__ import division
1515
1616import operator
17+ import warnings
1718
1819import numpy as np
1920
2021from . import csareader as csar
2122from .dwiparams import B2q , nearest_pos_semi_def , q2bg
2223from ..openers import ImageOpener
2324from ..onetime import setattr_on_read as one_time
24- from ..pydicom_compat import tag_for_keyword
25+ from ..pydicom_compat import tag_for_keyword , Sequence
2526
2627
2728class WrapperError (Exception ):
@@ -502,8 +503,32 @@ def image_shape(self):
502503 rows , cols = self .get ('Rows' ), self .get ('Columns' )
503504 if None in (rows , cols ):
504505 raise WrapperError ("Rows and/or Columns are empty." )
506+
505507 # Check number of frames
508+ first_frame = self .frames [0 ]
506509 n_frames = self .get ('NumberOfFrames' )
510+ # some Philips may have derived images appended
511+ has_derived = False
512+ if hasattr (first_frame , 'get' ) and first_frame .get ([0x18 , 0x9117 ]):
513+ # DWI image may include derived isotropic, ADC or trace volume
514+ try :
515+ self .frames = Sequence (
516+ frame for frame in self .frames if
517+ frame .MRDiffusionSequence [0 ].DiffusionDirectionality
518+ != 'ISOTROPIC'
519+ )
520+ except IndexError :
521+ # Sequence tag is found but missing items!
522+ raise WrapperError ("Diffusion file missing information" )
523+ except AttributeError :
524+ # DiffusionDirectionality tag is not required
525+ pass
526+ else :
527+ if n_frames != len (self .frames ):
528+ warnings .warn ("Derived images found and removed" )
529+ n_frames = len (self .frames )
530+ has_derived = True
531+
507532 assert len (self .frames ) == n_frames
508533 frame_indices = np .array (
509534 [frame .FrameContentSequence [0 ].DimensionIndexValues
@@ -522,6 +547,15 @@ def image_shape(self):
522547 if stackid_tag in dim_seq :
523548 stackid_dim_idx = dim_seq .index (stackid_tag )
524549 frame_indices = np .delete (frame_indices , stackid_dim_idx , axis = 1 )
550+ dim_seq .pop (stackid_dim_idx )
551+ if has_derived :
552+ # derived volume is included
553+ derived_tag = tag_for_keyword ("DiffusionBValue" )
554+ if derived_tag not in dim_seq :
555+ raise WrapperError ("Missing information, cannot remove indices "
556+ "with confidence." )
557+ derived_dim_idx = dim_seq .index (derived_tag )
558+ frame_indices = np .delete (frame_indices , derived_dim_idx , axis = 1 )
525559 # account for the 2 additional dimensions (row and column) not included
526560 # in the indices
527561 n_dim = frame_indices .shape [1 ] + 2
0 commit comments