@@ -461,23 +461,10 @@ 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' )
465464 try :
466465 self .frames [0 ]
467466 except TypeError :
468467 raise WrapperError ("PerFrameFunctionalGroupsSequence is empty." )
469- try :
470- # DWI image where derived isotropic, ADC or trace volume
471- # was appended to the series
472- if self .frames [0 ].get ([0x18 , 0x9117 ], None ):
473- self .frames = Sequence (
474- frame for frame in self .frames if
475- frame .get ([0x18 , 0x9117 ])[0 ].get ([0x18 , 0x9075 ]).value
476- != 'ISOTROPIC'
477- )
478- self ._nframes = len (self .frames )
479- except AttributeError :
480- pass
481468 try :
482469 self .shared = dcm_data .get ('SharedFunctionalGroupsSequence' )[0 ]
483470 except TypeError :
@@ -515,8 +502,23 @@ def image_shape(self):
515502 rows , cols = self .get ('Rows' ), self .get ('Columns' )
516503 if None in (rows , cols ):
517504 raise WrapperError ("Rows and/or Columns are empty." )
505+
518506 # Check number of frames
519- assert len (self .frames ) == self ._nframes
507+ first_frame = self .frames [0 ]
508+ n_frames = self .get ('NumberOfFrames' )
509+ # some Philips may have derived images appended
510+ has_derived = False
511+ if hasattr (first_frame , 'get' ) and first_frame .get ([0x18 , 0x9117 ]):
512+ # DWI image may include derived isotropic, ADC or trace volume
513+ # 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
521+ assert len (self .frames ) == n_frames
520522 frame_indices = np .array (
521523 [frame .FrameContentSequence [0 ].DimensionIndexValues
522524 for frame in self .frames ])
@@ -540,15 +542,15 @@ def image_shape(self):
540542 # Store frame indices
541543 self ._frame_indices = frame_indices
542544 if n_dim < 4 : # 3D volume
543- return rows , cols , self . _nframes
545+ return rows , cols , n_frames
544546 # More than 3 dimensions
545547 ns_unique = [len (np .unique (row )) for row in self ._frame_indices .T ]
546- if len (ns_unique ) == 3 :
548+ if len (ns_unique ) == 3 and has_derived :
547549 # derived volume is included
548550 ns_unique .pop (1 )
549551 shape = (rows , cols ) + tuple (ns_unique )
550552 n_vols = np .prod (shape [3 :])
551- if self ._nframes != n_vols * shape [2 ]:
553+ if n_frames == self .get ( 'NumberOfFrames' ) and n_frames != n_vols * shape [2 ]:
552554 raise WrapperError ("Calculated shape does not match number of "
553555 "frames." )
554556 return tuple (shape )
0 commit comments