21
21
from .dwiparams import B2q , nearest_pos_semi_def , q2bg
22
22
from ..openers import ImageOpener
23
23
from ..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
25
25
26
26
27
27
class WrapperError (Exception ):
@@ -461,10 +461,19 @@ def __init__(self, dcm_data):
461
461
Wrapper .__init__ (self , dcm_data )
462
462
self .dcm_data = dcm_data
463
463
self .frames = dcm_data .get ('PerFrameFunctionalGroupsSequence' )
464
+ self ._nframes = self .get ('NumberOfFrames' )
464
465
try :
465
466
self .frames [0 ]
466
467
except TypeError :
467
468
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 )
468
477
try :
469
478
self .shared = dcm_data .get ('SharedFunctionalGroupsSequence' )[0 ]
470
479
except TypeError :
@@ -503,8 +512,7 @@ def image_shape(self):
503
512
if None in (rows , cols ):
504
513
raise WrapperError ("Rows and/or Columns are empty." )
505
514
# Check number of frames
506
- n_frames = self .get ('NumberOfFrames' )
507
- assert len (self .frames ) == n_frames
515
+ assert len (self .frames ) == self ._nframes
508
516
frame_indices = np .array (
509
517
[frame .FrameContentSequence [0 ].DimensionIndexValues
510
518
for frame in self .frames ])
@@ -528,12 +536,15 @@ def image_shape(self):
528
536
# Store frame indices
529
537
self ._frame_indices = frame_indices
530
538
if n_dim < 4 : # 3D volume
531
- return rows , cols , n_frames
539
+ return rows , cols , self . _nframes
532
540
# More than 3 dimensions
533
541
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 )
534
545
shape = (rows , cols ) + tuple (ns_unique )
535
546
n_vols = np .prod (shape [3 :])
536
- if n_frames != n_vols * shape [2 ]:
547
+ if self . _nframes != n_vols * shape [2 ]:
537
548
raise WrapperError ("Calculated shape does not match number of "
538
549
"frames." )
539
550
return tuple (shape )
0 commit comments