2121from .dwiparams import B2q , nearest_pos_semi_def , q2bg
2222from ..openers import ImageOpener
2323from ..onetime import setattr_on_read as one_time
24- from ..pydicom_compat import tag_for_keyword
24+ from ..pydicom_compat import tag_for_keyword , Sequence
2525
2626
2727class WrapperError (Exception ):
@@ -461,10 +461,19 @@ def __init__(self, dcm_data):
461461 Wrapper .__init__ (self , dcm_data )
462462 self .dcm_data = dcm_data
463463 self .frames = dcm_data .get ('PerFrameFunctionalGroupsSequence' )
464+ self ._nframes = self .get ('NumberOfFrames' )
464465 try :
465466 self .frames [0 ]
466467 except TypeError :
467468 raise WrapperError ("PerFrameFunctionalGroupsSequence is empty." )
469+ # DWI image where derived isotropic, ADC or trace volume was appended to the series
470+ if self .frames [0 ].get ([0x18 , 0x9117 ]):
471+ self .frames = Sequence (
472+ frame for frame in self .frames if
473+ frame .get ([0x18 , 0x9117 ])[0 ].get ([0x18 , 0x9075 ]).value
474+ != 'ISOTROPIC'
475+ )
476+ self ._nframes = len (self .frames )
468477 try :
469478 self .shared = dcm_data .get ('SharedFunctionalGroupsSequence' )[0 ]
470479 except TypeError :
@@ -503,8 +512,7 @@ def image_shape(self):
503512 if None in (rows , cols ):
504513 raise WrapperError ("Rows and/or Columns are empty." )
505514 # Check number of frames
506- n_frames = self .get ('NumberOfFrames' )
507- assert len (self .frames ) == n_frames
515+ assert len (self .frames ) == self ._nframes
508516 frame_indices = np .array (
509517 [frame .FrameContentSequence [0 ].DimensionIndexValues
510518 for frame in self .frames ])
@@ -528,12 +536,15 @@ def image_shape(self):
528536 # Store frame indices
529537 self ._frame_indices = frame_indices
530538 if n_dim < 4 : # 3D volume
531- return rows , cols , n_frames
539+ return rows , cols , self . _nframes
532540 # More than 3 dimensions
533541 ns_unique = [len (np .unique (row )) for row in self ._frame_indices .T ]
542+ if len (ns_unique ) == 3 :
543+ # derived volume is included
544+ ns_unique .pop (1 )
534545 shape = (rows , cols ) + tuple (ns_unique )
535546 n_vols = np .prod (shape [3 :])
536- if n_frames != n_vols * shape [2 ]:
547+ if self . _nframes != n_vols * shape [2 ]:
537548 raise WrapperError ("Calculated shape does not match number of "
538549 "frames." )
539550 return tuple (shape )
0 commit comments